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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

双向链表 c

發(fā)布時(shí)間:2025/7/25 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 双向链表 c 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
雙向鏈表的插入、刪除、查找。要求關(guān)聯(lián)一起。插入、刪除、查找用注釋標(biāo)出來。短小,簡(jiǎn)單易懂。

#include <stdio.h>
#include <string.h>
typedef struct person person;
struct person {
char name[10];
int number;
person *next;
person *last;
};
person *p, *q, *start = NULL, *end;
void insert(int num, char nam[10])/* 插入函數(shù),按姓名的首字母順序插入鏈表中的。 */
{
person *q;
q=(person*) malloc(sizeof(struct person));
if (start == NULL) /* 判斷start是否為空 若空則新建 */
{
start = q;
end = q;
p = q;
start->last = NULL;
end->next = NULL;
} else {

if (strcmp(nam, start->name) <= 0)/* 插入鏈表頭 */
{
start->last = q;
q->next = start;
q->last = NULL;
start = q;
} else if (strcmp(nam, end->name) > 0) /* 插入表尾部 */
{
end->next = q;
q->last = end;
end = q;
q->next = NULL;
} else if (strcmp(nam, end->name) < 0 && strcmp(nam, start->name) > 0)/* 插入鏈表中 */
{
for (p = start; strcmp(nam, p->name) > 0; p = p->next)
;
q->next = p;
p->last = q;
for (p = start; p->next != q->next; p = p->next)
;
p->next = q;
q->last = p;
}

}
strcpy(q->name, nam);
q->number = num;

}
void find(int num) /* 按編號(hào)查找 */
{
if (start == NULL) {
printf("無記錄\n");
} else {
for (p = start; p != NULL && p->number != num; p = p->next)
;
if (p == NULL) {
printf("不存在的編號(hào)!\n");

} else if (p->number == num) {
printf("您查找的編號(hào)是:%d\n", num);
printf("該生的姓名為:%s", p->name);
}

}
}
void del(int num) /* 按編號(hào)刪除 */
{
for (p = start; p->number != num; p = p->next)
;
if (p->number == num) {
if (p->next == NULL) {
if (p->last == NULL) {
start = NULL;

} else {
(p->last)->next = NULL;
}
free( p );
} else if (p->last == NULL) {
(p->next)->last = NULL;
start = p->next;
free( p );
} else if (p->last != NULL && p->next != NULL) {
(p->last)->next = p->next;
(p->next)->last = p->last;
free( p );
}
}

else {
printf("不存在的編號(hào)!\n");
}

}
void print_list() {
printf("學(xué)號(hào)\t姓名\n");
for (p = start;; p = p->next) {
printf("%d\t%s\n", p->number, p->name);
if (p->next == NULL)
break;
}
}
void insert_node_test() {
int i, num;
char nam[10];
printf("輸入三位學(xué)生信息(學(xué)號(hào),姓名):");
/*
for (i = 0; i < 3; i++) {
scanf("%d%s", &num, nam);
insert(num, nam);
}
*/
num = 1;
memset(nam,0,sizeof(nam));
strcpy(nam,"abc");
insert(num, nam);
num = 3;
memset(nam,0,sizeof(nam));
strcpy(nam,"world");
insert(num, nam);
num = 2;
memset(nam,0,sizeof(nam));
strcpy(nam,"hello");
insert(num, nam);
print_list();

printf("輸入要?jiǎng)h除的學(xué)生的學(xué)號(hào):");
scanf("%d", &num);
del(num);
print_list();
printf("輸入要查找的學(xué)生的學(xué)號(hào):");
scanf("%d", &num);
find(num);
/* print_list(); */
}

轉(zhuǎn)載于:https://www.cnblogs.com/qunxuan/p/4224378.html

總結(jié)

以上是生活随笔為你收集整理的双向链表 c的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。