嵌入式C语言基础链表
生活随笔
收集整理的這篇文章主要介紹了
嵌入式C语言基础链表
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
什么是鏈表?
鏈表其實就是一種數(shù)據(jù)結(jié)構(gòu),所謂的數(shù)據(jù)結(jié)構(gòu)就是數(shù)據(jù)存放的思想。
數(shù)組、鏈表優(yōu)缺點:
增加一個元素或者刪除一個元素都很難,因為地址是連續(xù)的,刪除一個元素可能會挪動多個元素,不靈活。但是對于鏈表來說就很輕松了,鏈表的每一個節(jié)點都是一個結(jié)構(gòu)體,可以通過指針指向的方式將鏈表串起來,很靈活。
鏈表和數(shù)組的引入:
#include <stdio.h> #include <stdlib.h> //只要是通過指針的凡是訪問結(jié)構(gòu)體中的數(shù)據(jù)都要用-> struct Text//用鏈表的形式存儲數(shù)據(jù) {int data;struct Text* next; }; int main() {int i;int array[]={1,2,3,4};//用數(shù)組的方式實現(xiàn)數(shù)據(jù)的存儲,訪問到數(shù)組的首地址就可以輸出整個數(shù)組//因為數(shù)組的地址是連續(xù)的for(i=0;i<sizeof(array)/sizeof(array[0]);i++){printf("%d",array[i]);}putchar('\n');struct Text t1={1,NULL};struct Text t2={2,NULL};struct Text t3={3,NULL};//下面幾行代碼是將上面定義的幾個結(jié)構(gòu)體串起來,形成鏈表t1.next=&t2;//指針是存放別人的地址t1里的指針變量指向t2的地址t2.next=&t3;//這樣就將三個結(jié)構(gòu)體聯(lián)系了起來printf("use t1 printf four number:\n");printf("%d %d %d\n",t1.data,t1.next->data,t1.next->next->data);//t1.next就是指向t2的一個指針(我理解的就是相當(dāng)于struct Text * p)//然后通過->這種方式去訪問結(jié)構(gòu)體中的數(shù)據(jù),然后t1.next->next就是相當(dāng)于//指向t3的一個指針,也要通過->這種方式去訪問里面的數(shù)據(jù)system("pause");return 0; }鏈表的動態(tài)輸出:
#include <stdio.h> #include <stdlib.h> //只要是通過指針的凡是訪問結(jié)構(gòu)體中的數(shù)據(jù)都要用-> struct Text//用鏈表的形式存儲數(shù)據(jù) {int data;struct Text* next; };void printLink(struct Text *head)//鏈表的動態(tài)輸出函數(shù),struct Text *head是鏈表的頭 {struct Text*Point;Point=head;while(Point!=NULL){printf("%d ",Point->data);Point=Point->next;//head是第一個節(jié)點的指針,輸出完第一個鏈表的內(nèi)容后,將鏈表的頭部往后移一個,變?yōu)橹赶虻诙€節(jié)點的指針//head->next是指向下一個鏈表的頭}putchar('\n');} int main() {int i;int array[]={1,2,3,4};//用數(shù)組的方式實現(xiàn)數(shù)據(jù)的存儲,訪問到數(shù)組的首地址就可以輸出整個數(shù)組//因為數(shù)組的地址是連續(xù)的for(i=0;i<sizeof(array)/sizeof(array[0]);i++){printf("%d",array[i]);}putchar('\n');struct Text t1={1,NULL};struct Text t2={2,NULL};struct Text t3={3,NULL};//下面幾行代碼是將上面定義的幾個結(jié)構(gòu)體串起來,形成鏈表t1.next=&t2;//指針是存放別人的地址t1里的指針變量指向t2的地址t2.next=&t3;//這樣就將三個結(jié)構(gòu)體聯(lián)系了起來printLink(&t1);//將第一個鏈表的地址傳入動態(tài)輸出函數(shù)system("pause");return 0; }鏈表的查找和節(jié)點個數(shù)的計算:
#include <stdio.h> #include <stdlib.h> //只要是通過指針的凡是訪問結(jié)構(gòu)體中的數(shù)據(jù)都要用-> struct Text//用鏈表的形式存儲數(shù)據(jù) {int data;struct Text* next; };void printLink(struct Text *head)//鏈表的動態(tài)輸出函數(shù),struct Text *head是鏈表的頭 {struct Text*Point;Point=head;while(Point!=NULL){printf("%d ",Point->data);Point=Point->next;//head是第一個節(jié)點的指針,輸出完第一個鏈表的內(nèi)容后,將鏈表的頭部往后移一個,變?yōu)橹赶虻诙€節(jié)點的指針//head->next是指向下一個鏈表的頭}putchar('\n');} void countLink(struct Text* head)//鏈表計算節(jié)點個數(shù)函數(shù) {int cont=0;struct Text* Point;Point=head;while(head!=NULL){cont++;head=head->next;}printf("鏈表節(jié)點數(shù)為:%d\n",cont);} void serchLink(struct Text*head,int data) {while(head!=NULL){if(head->data==data){printf("存在此節(jié)點\n");return;}head=head->next;}printf("無此節(jié)點\n"); } int main() {int i;int input;int array[]={1,2,3,4};//用數(shù)組的方式實現(xiàn)數(shù)據(jù)的存儲,訪問到數(shù)組的首地址就可以輸出整個數(shù)組//因為數(shù)組的地址是連續(xù)的for(i=0;i<sizeof(array)/sizeof(array[0]);i++){printf("%d",array[i]);}putchar('\n');struct Text t1={1,NULL};struct Text t2={2,NULL};struct Text t3={3,NULL};//下面幾行代碼是將上面定義的幾個結(jié)構(gòu)體串起來,形成鏈表t1.next=&t2;//指針是存放別人的地址t1里的指針變量指向t2的地址t2.next=&t3;//這樣就將三個結(jié)構(gòu)體聯(lián)系了起來printLink(&t1);countLink(&t1);printf("請輸入要查找的節(jié)點:\n");scanf("%d",&input);serchLink(&t1,input);system("pause");return 0; }從指定節(jié)點后插入節(jié)點:
#include <stdio.h> #include <stdlib.h> //只要是通過指針的凡是訪問結(jié)構(gòu)體中的數(shù)據(jù)都要用-> struct Text//用鏈表的形式存儲數(shù)據(jù) {int data;struct Text* next; };void printLink(struct Text *head)//鏈表的動態(tài)輸出函數(shù),struct Text *head是鏈表的頭 {struct Text*Point;Point=head;while(Point!=NULL){printf("%d ",Point->data);Point=Point->next;//head是第一個節(jié)點的指針,輸出完第一個鏈表的內(nèi)容后,將鏈表的頭部往后移一個,變?yōu)橹赶虻诙€節(jié)點的指針//head->next是指向下一個鏈表的頭}putchar('\n');} void countLink(struct Text* head)//鏈表計算節(jié)點個數(shù)函數(shù) {int cont=0;struct Text* Point;Point=head;while(head!=NULL){cont++;head=head->next;}printf("鏈表節(jié)點數(shù)為:%d\n",cont);} void serchLink(struct Text*head,int data) {while(head!=NULL){if(head->data==data){printf("存在此節(jié)點\n");return;}head=head->next;}printf("無此節(jié)點\n"); }void InsertFromBhind(struct Text*head,struct Text*newLink,int data) {struct Text* Point;Point=head;while(Point!=NULL){if(Point->data==data){newLink->next=Point->next;//head->next是插入位置后一個節(jié)點的指針,就是head->next指向插入位置的后一個節(jié)點Point->next=newLink;//修改插入位置前一個節(jié)點內(nèi)的指針指向,指向新節(jié)點的地址,將鏈表串起來printLink(head);return;}Point=Point->next;}printf("無此節(jié)點\n"); } int main() {int i;int input;int link;struct Text t1={1,NULL};struct Text t2={2,NULL};struct Text t3={3,NULL};struct Text t4={4,NULL};//下面幾行代碼是將上面定義的幾個結(jié)構(gòu)體串起來,形成鏈表t1.next=&t2;//指針是存放別人的地址t1里的指針變量指向t2的地址t2.next=&t3;//這樣就將三個結(jié)構(gòu)體聯(lián)系了起來printLink(&t1);countLink(&t1);printf("請輸入要查找的節(jié)點:\n");scanf("%d",&input);serchLink(&t1,input);printf("請輸入在哪個節(jié)點后插入;\n");scanf("%d",&link);InsertFromBhind(&t1,&t4,link);system("pause");return 0; }在指定節(jié)點前插入節(jié)點:
#include <stdio.h> #include <stdlib.h> //只要是通過指針的凡是訪問結(jié)構(gòu)體中的數(shù)據(jù)都要用-> struct Text//用鏈表的形式存儲數(shù)據(jù) {int data;struct Text* next; };void printLink(struct Text *head)//鏈表的動態(tài)輸出函數(shù),struct Text *head是鏈表的頭 {struct Text*Point;Point=head;while(Point!=NULL){printf("%d ",Point->data);Point=Point->next;//head是第一個節(jié)點的指針,輸出完第一個鏈表的內(nèi)容后,將鏈表的頭部往后移一個,變?yōu)橹赶虻诙€節(jié)點的指針//head->next是指向下一個鏈表的頭}putchar('\n');} void countLink(struct Text* head)//鏈表計算節(jié)點個數(shù)函數(shù) {int cont=0;struct Text* Point;Point=head;while(head!=NULL){cont++;head=head->next;}printf("鏈表節(jié)點數(shù)為:%d\n",cont);} void serchLink(struct Text*head,int data) {while(head!=NULL){if(head->data==data){printf("存在此節(jié)點\n");return;}head=head->next;}printf("無此節(jié)點\n"); } void InsertFromBefore(struct Text*head,struct Text*newlink,int data) {struct Text*Point;Point=head;if(head!=NULL && head->data==data){newlink->next=head;printLink(newlink);return;}while(Point->next!=NULL){//因為是在指定節(jié)點前插入節(jié)點//所以要通過指定節(jié)點的前一個節(jié)點插入//Point就是指向,指定節(jié)點前一個節(jié)點的指針//Point->next是指向,指定的插入節(jié)點的指針,也就是在這個節(jié)點前插入節(jié)點if(Point->next->data==data){newlink->next=Point->next;Point->next=newlink;printLink(head);return;}Point=Point->next;} }int main() {int i;int input;int link;struct Text t1={1,NULL};struct Text t2={2,NULL};struct Text t3={3,NULL};struct Text t4={4,NULL};//下面幾行代碼是將上面定義的幾個結(jié)構(gòu)體串起來,形成鏈表t1.next=&t2;//指針是存放別人的地址t1里的指針變量指向t2的地址t2.next=&t3;//這樣就將三個結(jié)構(gòu)體聯(lián)系了起來printLink(&t1);countLink(&t1);printf("請輸入要查找的節(jié)點:\n");scanf("%d",&input);serchLink(&t1,input);printf("請輸入在哪個節(jié)點前插入;\n");scanf("%d",&link);InsertFromBefore(&t1,&t4,link);system("pause");return 0; }靜態(tài)鏈表的刪除和修改:
#include <stdio.h> #include <stdlib.h> //只要是通過指針的凡是訪問結(jié)構(gòu)體中的數(shù)據(jù)都要用-> struct Text//用鏈表的形式存儲數(shù)據(jù) {int data;struct Text* next; };void printLink(struct Text *head)//鏈表的動態(tài)輸出函數(shù),struct Text *head是鏈表的頭 {struct Text*Point;Point=head;while(Point!=NULL){printf("%d ",Point->data);Point=Point->next;//head是第一個節(jié)點的指針,輸出完第一個鏈表的內(nèi)容后,將鏈表的頭部往后移一個,變?yōu)橹赶虻诙€節(jié)點的指針//head->next是指向下一個鏈表的頭}putchar('\n');} void countLink(struct Text* head)//鏈表計算節(jié)點個數(shù)函數(shù) {int cont=0;struct Text* Point;Point=head;while(head!=NULL){cont++;head=head->next;}printf("鏈表節(jié)點數(shù)為:%d\n",cont);} void serchLink(struct Text*head,int data)//查找函數(shù) {while(head!=NULL){if(head->data==data){printf("存在此節(jié)點\n");return;}head=head->next;}printf("無此節(jié)點\n"); }void changeLink(struct Text*head,int data,int data2)//改函數(shù) {struct Text*Point=head;while(Point!=NULL){if(Point->data==data){printf("此節(jié)點存在可改\n");Point->data=data2;printLink(head);return;}Point=Point->next;}printf("無此節(jié)點\n"); }void delLink(struct Text*head,int data) {struct Text*Point=head;if(head->data==data){head=head->next;//將第二個節(jié)點指定為數(shù)組頭,那么第一個節(jié)點就需要釋放掉避免造成內(nèi)存泄漏//free(Point); //將這個地址釋放掉,可以通過Point指針將之前的head的內(nèi)存空間釋放掉//free可以將指針指向的內(nèi)存空間釋放掉,但是釋放掉之后最好將指針指向NULL//因為free只是把指針?biāo)傅膬?nèi)存給釋放掉,但并沒有把指針本身干掉。//free只能釋放malloc開辟的內(nèi)存空間printLink(head);return;}while(Point->next!=NULL){//head->next是指向要刪除的那個節(jié)點的指針if(Point->next->data==data){Point->next=Point->next->next;//如果是動態(tài)創(chuàng)建,就要新定義一個指針//通過新定義的指針把(Ponit->next)free掉printLink(head);return;}Point=Point->next;}printf("沒有找到\n"); } int main() {int i;int input;int link;struct Text t1={1,NULL};struct Text t2={2,NULL};struct Text t3={3,NULL};struct Text t4={4,NULL};//下面幾行代碼是將上面定義的幾個結(jié)構(gòu)體串起來,形成鏈表t1.next=&t2;//指針是存放別人的地址t1里的指針變量指向t2的地址t2.next=&t3;//這樣就將三個結(jié)構(gòu)體聯(lián)系了起來printLink(&t1);countLink(&t1);printf("請輸入要查找的節(jié)點:\n");scanf("%d",&input);serchLink(&t1,input);printf("請輸入要刪除哪個節(jié)點;\n");scanf("%d",&link);delLink(&t1,link);printf("修改哪個節(jié)點;\n");scanf("%d",&link);changeLink(&t1,link,100);//這些所有的函數(shù)并不修改main函數(shù)中的節(jié)點system("pause");return 0; }鏈表的動態(tài)創(chuàng)建之頭插法(每插進(jìn)來一個節(jié)點就將它當(dāng)做頭結(jié)點,就像棧一樣)
#include <stdio.h> #include <stdlib.h>struct Text {int data;struct Text*next; }; void printLink(struct Text *head)//鏈表的動態(tài)輸出函數(shù),struct Text *head是鏈表的頭 {struct Text*Point;Point=head;while(Point!=NULL){printf("%d ",Point->data);Point=Point->next;//head是第一個節(jié)點的指針,輸出完第一個鏈表的內(nèi)容后,將鏈表的頭部往后移一個,變?yōu)橹赶虻诙€節(jié)點的指針//head->next是指向下一個鏈表的頭 }putchar('\n');} struct Text *creatFromHead(struct Text*head,struct Text*newnode)//節(jié)點頭插法插入函數(shù) //節(jié)點創(chuàng)建函數(shù)和插入節(jié)點函數(shù)分開寫,有利于后面節(jié)點的插入 {if(head==NULL){head=newnode;}else{newnode->next=head;head=newnode;}return head; } struct Text*creatLink(struct Text*head)//鏈表節(jié)點創(chuàng)建函數(shù) {struct Text*newnode=NULL;while(1){//不斷創(chuàng)建新的節(jié)點,直到輸入的數(shù)字為0newnode=(struct Text*)malloc(sizeof(struct Text));newnode->next=NULL;printf("請輸入data:\n");scanf("%d",&(newnode->data));if(newnode->data==0){free(newnode);//新創(chuàng)建的節(jié)點里的data等于0;所以不把他插入鏈表中//將它指向的內(nèi)存空間釋放掉printf("鏈表創(chuàng)建結(jié)束\n");return head;}head=creatFromHead(head,newnode);} } int main() {struct Text *head=NULL;printLink(creatLink(head));system("pause");return 0; }尾插法插入節(jié)點:
#include <stdio.h> #include <stdlib.h>struct Text {int data;struct Text*next; }; void printLink(struct Text *head)//鏈表的動態(tài)輸出函數(shù),struct Text *head是鏈表的頭 {struct Text*Point;Point=head;while(Point!=NULL){printf("%d ",Point->data);Point=Point->next;//head是第一個節(jié)點的指針,輸出完第一個鏈表的內(nèi)容后,將鏈表的頭部往后移一個,變?yōu)橹赶虻诙€節(jié)點的指針//head->next是指向下一個鏈表的頭 }putchar('\n');} struct Text *insertFromBehind(struct Text*head,struct Text*newnode)//節(jié)點尾插法插入函數(shù) {struct Text* p=NULL;p=head;if(p==NULL){//如果沒有這句話,如果P為空指針,繼續(xù)往下執(zhí)行p->next對空指針進(jìn)行操作,會出現(xiàn)段錯誤head=newnode;return head;}while(p->next!=NULL){p=p->next;}p->next=newnode;return head; } struct Text*creatLink(struct Text*head)//鏈表節(jié)點創(chuàng)建函數(shù) {struct Text*newnode=NULL;while(1){//不斷創(chuàng)建新的節(jié)點,直到輸入的數(shù)字為0newnode=(struct Text*)malloc(sizeof(struct Text));newnode->next=NULL;printf("請輸入data:\n");scanf("%d",&(newnode->data));if(newnode->data==0){free(newnode);//新創(chuàng)建的節(jié)點里的data等于0;所以不把他插入鏈表中//將它指向的內(nèi)存空間釋放掉printf("鏈表創(chuàng)建結(jié)束\n");return head;}head=insertFromBehind(head,newnode);} } int main() {struct Text *head=NULL;printLink(creatLink(head));system("pause");return 0; }總結(jié)
以上是生活随笔為你收集整理的嵌入式C语言基础链表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python获取列表控件_PyQt学习随
- 下一篇: java学习(130):treemap类