链表定义、链表的插入、链表的删除、链表的查找
生活随笔
收集整理的這篇文章主要介紹了
链表定义、链表的插入、链表的删除、链表的查找
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈表的定義
鏈表是一種常見的重要的數據結構。它是動態地進行存儲分配的一種結構。它可以根據需要開辟內存單元。鏈表有一個“頭指針”變量,以head表示,它存放一個地址。該地址指向一個元素。鏈表中每一個元素稱為“結點”,每個結點都應包括兩個部分:一為用戶需要用的實際數據,二為下一個結點的地址。因此,head指向第一個元素:第一個元素又指向第二個元素;……,直到最后一個元素,該元素不再指向其它元素,它稱為“表尾”,它的地址部分放一個“NULL”(表示“空地址”),鏈表到此結束。
結構體形式
鏈表的插入
(1)頭插法
(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)在指定節點后插
head:鏈表頭節點
insert_data :被后插節點的值
new:新節點
鏈表固定節點的刪除
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;} 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的链表定义、链表的插入、链表的删除、链表的查找的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux+显卡驱动+七彩虹,9800g
- 下一篇: IIS介绍