資料結構 鍊錶

2021-07-09 11:24:12 字數 2737 閱讀 1285

一般的建立線性鍊錶有兩種:

1.正序法:需要三個指標,head作為頭指標,pre作為前乙個指標,cur作為當前指標用來建立空間

2.倒序法,利用指標的插入,只需要兩個指標,不斷的往頭指標後插入新空間,不過插入的越早,離頭指標越遠,也就越後面輸出

1.線性鍊錶的建立及查詢刪除

#include#include#include#define max 45

struct node

;struct node *head = null;

void insert(struct node *current)//插在第i個後面

struct node *s =(struct node*)malloc(sizeof(node));

printf("input the title of insert book\n");

gets_s(s->title);

puts("enter your rating <0-10>:");

scanf("%d", &s->rating);

s->next = current->next;

current->next = s;

}void del(struct node *current)//結點刪除函式,結點是從0開始的,所以輸入1刪除的是第二個

struct node*q;

q = current->next;

current->next =q->next;

free(q);

}void outcome(struct node *current)//列印鍊錶

}void clean(struct node *current)//有問題,編譯可以通過但最後會出錯

}int main()

outcome(current);

insert(current);

outcome(current);

del(current);

outcome(current);

//clean(current);

return 0;

}

二:順序表的合併

#include#includestruct node

;void create(struct node *l, int n)//反序建立鍊錶,後輸入的鍊錶排在前面

}void outcome(const node*l)

printf("\n");

}void mergelist(struct node *la, struct node *lb, struct node *lc)

else

}pc->next = pa ? pa : pb;

}int main()

1.靜態鍊錶的建立和輸入輸出

#include#include#define maxn 3

struct node

;void locate(struct node s, int e)//查詢函式

printf("the location is %d\n", i);

}void creat(struct node s)//建立函式

}void output(struct node s)//輸出函式

}int main()

2.以(a-b)∪(b-a)為例的靜態鍊錶的運用

#include#define maxn 1000

struct node

;void init(struct node space) //備用空間的建立

int mall(struct node space) //分配乙個結點

void clean(struct node space,int k)//將刪除的結點**到備用空間

int main()

space[r].cur = 0; //尾結點的游標為0

printf("enter the data of b\n");

int num,p,k;

for (j = 1; j <= b; j++)

if (p == r) //已經搜尋到最後說明不存在一樣的

else //元素已經在表中

}printf("the following is the result:\n");

i = space[s].cur;

while(i)

return 0;

}

1.插入倒序法建立雙向鍊錶以及刪除操作

#include#include#includetypedef struct node

type;

type *l = (type*)malloc(sizeof(type));

void outcome(type *p) //輸出函式

printf("\n");

}void del(type *p) //刪除函式

p->prior->next = p->next;

p->next->prior = p->prior;

free(p);

}int main()

outcome(p);

del(p);

outcome(p);

return 0;

}

資料結構 鍊錶

鍊錶 what 就是一張鏈式儲存的表,是一種資料結構,是基礎,所以還是不要想有什麼用。具體呢?在c中就用結構體實現物件描述,然後通過函式來實現各個基本操作 c 則用類來表述,c中的結構體就可以看成c 中的類,然後通過類封裝各個操作步驟。這些操作實現後就需要 來測試,號稱demo,就是main函式裡面...

資料結構 鍊錶

鍊錶中的資料是以節點來表示的,每個結點的構成 元素 資料元素的映象 指標 指示後繼元素儲存位置 元素就是儲存資料的儲存單元,指標就是連線每個結點的位址資料。鍊錶的結點結構 data next data域 存放結點值的資料域 next域 存放結點的直接後繼的位址 位置 的指標域 鏈域 以 結點的序列 ...

資料結構 鍊錶

ifndef link h define link h include include includeusing namespace std templatestruct linknode 鍊錶結點類定義 linknode const t item,linknode ptr null 初始化 tem...