資料結構——單鍵表操作集
單連結串列由多個結點構成,每個結點有資料域和指標域。指標域指向其後繼的結點。最後一個結點的指標指向NULL。
頭結點的資料域存放長度。
標頭檔案head.h
#ifndef _HEAD_H
#define _HEAD_H
typedef struct node* List;
typedef int ElementType;
typedef struct node {
ElementType data;
List next;
}Node;
/*建立連結串列, 表長為length*/
List CreateList(int length);
/*插入資料*/
void InsertNode(List head, int n);
/*刪除資料*/
void DeleteNode(List head, int n);
/*修改資料*/
void ModifyNode(List head, int n);
/*查詢資料*/
List FindData(const Node* head, ElementType n);
/*釋放記憶體*/
void FreeSpace(List head);
/*顯示*/
void Display(const Node* head);
/*清除緩衝區*/
void clean(void);
/*判斷是空*/
int IsEmpty(const Node* head);
/*輸入函式,加了輸入限制保證不越界*/
int Input(const Node* l);
#endif
主函式main.c
#include<stdio.h>
#include"operation.c"
#include"head.h"
int main(void)
{
int location = 0;
printf("建立一個初始連結串列,你想有幾個結點:\n");
int n;
scanf("%d", &n);
clean();
List head = CreateList(n);
Display(head);
puts("*****************************************");
printf("插入操作\n");
location = Input(head);
InsertNode(head, location - 1);
Display(head);
puts("*****************************************");
printf("刪除操作\n");
location = Input(head);
DeleteNode(head, location - 1);
Display(head);
puts("*****************************************");
puts("*****************************************");
printf("修改操作\n");
location = Input(head);
ModifyNode(head, location - 1);
Display(head);
puts("*****************************************");
puts("*****************************************");
printf("查詢操作\n");
ElementType find;
printf("輸入你要查詢的值:");
scanf("%d", &find);
clean();
List p = FindData(head, find);
printf("%d\n", p == NULL ? printf("沒有這個資料\n") : p->data);
puts("*****************************************");
puts("*****************************************");
Display(head);
puts("*****************************************");
FreeSpace(head);
return 0;
}
操作集operation.c
不考慮分配不到記憶體的情況。
#include"head.h"
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
List CreateList(int length)
{
srand((unsigned int)time(0)); //種子設定為系統時間
List head = (List)malloc(sizeof(Node)); //頭結點,準確來說head是指向頭結點的頭指標,但是這裡頭結點也是malloc的了。如果有必要可以在主函式裡Node head一個頭結點。
head->data = 0;
head->next = NULL;
List record = head;
while(length--)
{
List p = (List)malloc(sizeof(Node));
p->data = rand() % 10; //生成不大於10的隨機種子
p->next = NULL;
record->next = p;
record = p;
head->data++;
}
return head;
}
int Input(const Node* l)
{
printf("選擇位置:");
int location;
while(scanf("%d", &location))
{
if (location > 0 && location <= l->data)
break;
printf("選擇位置, 不小於1, 不大於%d:", l->data);
clean();
}
clean();
return location;
}
void InsertNode(List head, int n)
{
while(n--)
{
head = head->next;
}
List p = (List)malloc(sizeof(Node));
printf("插入的值:\n");
scanf("%d", &p->data);
clean();
p->next = head->next;
head->next = p;
head->data++;
}
void DeleteNode(List head, int n)
{
List h = head;
List front = head;
head = head->next; //head指向第一個有效值的位置
while(n--)
{
front = head;
head = head->next;
}
front->next = head->next;
free(head);
h->data--;
}
void ModifyNode(List head, int n)
{
List l = head->next;
while(n--)
{
l = l ->next;
}
printf("要修改的值:\n");
scanf("%d", &l->data);
}
List FindData(const Node* head, ElementType n)
{
List l = head->next;
while(l)
{
if (l->data == n)
return l;
l = l->next;
}
printf("沒有這個資料\n");
return NULL;
}
void FreeSpace(List head)
{
List p = head->next;
List q = head;
while(p)
{
q = p->next;
free(p);
p = q;
}
free(head);
}
void Display(const Node* head)
{
int length = head->data;
if (IsEmpty(head))
{
printf("Is Empty");
exit(EXIT_FAILURE);
}
List l = head->next;
/*
for (int i = 1; l; i++)
{
printf("%2d", l->data);
l = l->next;
if (i % 10 == 0)
{
printf("\n");
i = 0;
}
}
*/
for (int i = 1; i <= length; i++)
{
printf("%2d", l->data);
if (i != length) //表示到達最後一個
printf("->");
l = l->next;
}
printf("\n");
return ;
}
void clean(void)
{
while(getchar() != '\n')
continue;
}
int IsEmpty(const Node* head)
{
return head->next == NULL;
}
相關文章
- 資料庫-單表結構-建表語句資料庫
- 【資料結構】線性表-單連結串列資料結構
- 資料結構線性表的鏈式儲存結構(單連結串列)的表示及其基本操作資料結構
- 資料結構:線性表(Python實現基本操作)資料結構Python
- EntityFramework Core筆記:表結構及資料基本操作(2)Framework筆記
- 資料結構-並查集資料結構並查集
- 資料結構之連結串列操作資料結構
- 資料結構之連結串列篇(單連結串列的常見操作)資料結構
- 資料結構c語言實現順序表基本操作資料結構C語言
- 資料結構與演算法-線性表-單連結串列資料結構演算法
- 超給力,一鍵生成資料庫文件-資料庫表結構逆向工程資料庫
- 資料結構之並查集資料結構並查集
- 資料結構——線性表資料結構
- 資料結構——雜湊表資料結構
- 資料結構 | 線性表資料結構
- 資料結構-線性表資料結構
- 資料結構—線性表資料結構
- [資料結構] - 線性表資料結構
- 資料結構簡單題資料結構
- 資料結構之單連結串列資料結構
- 資料結構04——單連結串列資料結構
- 資料結構 - 線性表 - 順序表資料結構
- 資料結構:速通並查集資料結構並查集
- 【資料結構】帶權並查集資料結構並查集
- C語言資料結構:單向迴圈連結串列的增刪操作C語言資料結構
- 【資料結構】用C語言實現單連結串列及其常見操作資料結構C語言
- 資料結構(一)--- 跳躍表資料結構
- 資料結構進階:ST表資料結構
- 資料結構之「雜湊表」資料結構
- 資料結構 - 雜湊表,初探資料結構
- Redis資料結構—跳躍表Redis資料結構
- 資料結構-線性表、連結串列資料結構
- 南郵資料結構實驗1.1:順序表的相關操作資料結構
- 資料結構練習題(順序表和單連結串列)C++資料結構C++
- 特殊資料結構:單調棧資料結構
- 碎片資料收集利器-結構化動態表單設計思路
- 考研資料結構-線性表-順序表資料結構
- C++資料結構連結串列的基本操作C++資料結構