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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

链表定义、链表的插入、链表的删除、链表的查找

發布時間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 链表定义、链表的插入、链表的删除、链表的查找 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈表的定義
鏈表是一種常見的重要的數據結構。它是動態地進行存儲分配的一種結構。它可以根據需要開辟內存單元。鏈表有一個“頭指針”變量,以head表示,它存放一個地址。該地址指向一個元素。鏈表中每一個元素稱為“結點”,每個結點都應包括兩個部分:一為用戶需要用的實際數據,二為下一個結點的地址。因此,head指向第一個元素:第一個元素又指向第二個元素;……,直到最后一個元素,該元素不再指向其它元素,它稱為“表尾”,它的地址部分放一個“NULL”(表示“空地址”),鏈表到此結束。
結構體形式

struct test {int data;struct test *next; };

鏈表的插入
(1)頭插法

struct Test *insertfromhead(struct Test *head) {struct Test *new = NULL;while(1){new =( struct Test *)malloc(sizeof(struct Test));printf("please input new node(0 qiut)!\n");scanf("%d",&new->data);if(new->data == 0){printf("quit\n");return head; }else if(head == NULL){head = new;}else{new->next = head;head=new;}}return head; }

(2)尾插法

struct Test *insertfromtail(struct Test *head) {struct Test *new = NULL;struct Test *p = head;while(1){new = (struct Test *)malloc(sizeof(struct Test));printf("please input new node(0 quit)!\n");scanf("%d",&new->data);if(new->data == 0){printf("quit\n");return head; }if(p == NULL){p = new;head = p; }else if{while(p->next != NULL){p = p->next; }p->next = new;}}return head;}

(3)在指定節點前插

struct Test *insertfrombefore(struct Test *head,int insert_data,struct Test *new) {struct Test *p = head;if(p->data == insert_data){new->next = head;return new;}//遍歷while(p->next != NULL){if(p->next->data == insert_data){new->next = p->next;p->next=new;return head;}p = p->next;}printf("no this data %d\n",insert_data);return head;}

head:鏈表頭節點
insert_data :被前插節點的值
new:新節點
(4)在指定節點后插

struct Test *insertfrombehind(struct Test *head,int insert_data,struct Test *new) {struct Test *p = head;while(p != NULL){if(p->data == insert_data){new->next = p->next;p->next = new;return head;}p = p->next;}printf("no this data %d\n",insert_data);return head;}

head:鏈表頭節點
insert_data :被后插節點的值
new:新節點
鏈表固定節點的刪除

struct test *delelink(struct test*head,int data) {struct test*p=head;if(p->data==data){head=head->next;// free(p);一般只有malloc開辟的空間才能被freereturn head;}while(p->next!=NULL){if(p->next->data==data){p->next=p->next->next;return head;}p=p->next;}return head; }

data:要刪除節點的data值

鏈表的查找

int searchlink(struct test* head,int data){while(head!=NULL){if(head->data==data){return 1;}head=head->next;} }

鏈表節點的計算

int getlinknumbr(struct test* head) {int cnt=0;while(head!=NULL){cnt++;head=head->next;}return cnt; }

鏈表的打印

void printLink(struct test *head) {struct test *point;point=head;while(point!=NULL){printf("%d ",point->data);point=point->next;}putchar('\n'); }

鏈表空間的釋放

void FreeSpace(struct Text *head){struct Text *p;while(head!=NULL){p=head->next;free(head);head=p;} 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的链表定义、链表的插入、链表的删除、链表的查找的全部內容,希望文章能夠幫你解決所遇到的問題。

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