日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言链表代码大全,C语言实现链表

發布時間:2023/12/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言链表代码大全,C语言实现链表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/*

* main.c

*? 鏈表

*? Created on: Oct 29, 2010

*????? Author: jenson

*/

#include

#include

struct chain {

int value;

struct chain *next;

};

struct chain *create();

struct chain * insert(struct chain *head, int a, int b);

struct chain * delete(struct chain *head, int a);

void display(struct chain * head);

int main() {

struct chain *q, *head;

int a, b;

q = create();

head = q;

display(q);

printf("\n請輸入的表元素數據位于哪個數據之前:");

scanf("%d", &a);

printf("\n請輸入所要插入的數據:");

scanf("%d", &b);

q = insert(head, a, b);

display(q);

printf("\n請輸入所要刪除的數據:");

scanf("%d", &b);

head = q;

q = delete(head, b);

display(q);

free(q);

return 0;

}

struct chain * create() {

struct chain * head, *tail, *p;

int x;

head = tail = NULL;

int i = 0;

for (i = 0; i < 4; i++) {

printf("insert data %d:", i);

scanf("%d", &x);

p = (struct chain *) malloc(sizeof(struct chain));

if (p == NULL) {

perror("malloc");

exit(1);

}

p->value = x;

p->next = NULL;

if (head == NULL) {

head = tail = p;

} else {

tail = tail->next = p;

}

}

return head;

}

struct chain * insert(struct chain *head, int a, int b) {

struct chain *p, *q, *s;

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

if (s == NULL) {

perror("insert create");

exit(1);

}

s->value = b;

if (head == NULL) {

head = s;

s->next = head;

}

if (head->value == a) {

s->next = head;

head = s;

} else {

p = head;

while ((p->value != a) && (p->next != NULL)) {

q = p;

p = p->next;

}

if (p->value == a) {

q->next = s;

s->next = p;

} else {//插入節點s作為表尾

p->next = s;

s->next = NULL;

}

}

return head;

}

struct chain * delete(struct chain *head, int a) {

struct chain *p, *q;

if (head == NULL) {

perror("空鏈表,退出\n");

exit(1);

} else if (head->value == a) {//第一個節點為a節點

p = head;

head = p->next;

} else {

p = head;

while (p->value != a && p->next != NULL) {

q = p;

p = p->next;

}

if (p->value != a) {

printf("沒有找到節點%d\n", a);

} else {

q->next = p->next;

free(p);

}

}

return head;

}

void display(struct chain * head) {

if (head != NULL) {

struct chain * p;

p = head;

while (p != NULL) {

printf("%d\t", p->value);

p = p->next;

}

}

}

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的c语言链表代码大全,C语言实现链表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。