什么是链表?
链表其实就是一种数据结构,所谓的数据结构就是数据存放的思想。
数组、链表优缺点:
增加一个元素或者删除一个元素都很难,因为地址是连续的,删除一个元素可能会挪动多个元素,不灵活。但是对于链表来说就很轻松了,链表的每一个节点都是一个结构体,可以通过指针指向的方式将链表串起来,很灵活。
链表和数组的引入:
#include <stdio.h>
#include <stdlib.h>
//只要是通过指针的凡是访问结构体中的数据都要用->
struct Text//用链表的形式存储数据
{int data;struct Text* next;
};
int main()
{int i;int array[]={1,2,3,4};//用数组的方式实现数据的存储,访问到数组的首地址就可以输出整个数组//因为数组的地址是连续的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};//下面几行代码是将上面定义的几个结构体串起来,形成链表t1.next=&t2;//指针是存放别人的地址t1里的指针变量指向t2的地址t2.next=&t3;//这样就将三个结构体联系了起来printf("use t1 printf four number:\n");printf("%d %d %d\n",t1.data,t1.next->data,t1.next->next->data);//t1.next就是指向t2的一个指针(我理解的就是相当于struct Text * p)//然后通过->这种方式去访问结构体中的数据,然后t1.next->next就是相当于//指向t3的一个指针,也要通过->这种方式去访问里面的数据system("pause");return 0;
}
链表的动态输出:
#include <stdio.h>
#include <stdlib.h>
//只要是通过指针的凡是访问结构体中的数据都要用->
struct Text//用链表的形式存储数据
{int data;struct Text* next;
};void printLink(struct Text *head)//链表的动态输出函数,struct Text *head是链表的头
{struct Text*Point;Point=head;while(Point!=NULL){printf("%d ",Point->data);Point=Point->next;//head是第一个节点的指针,输出完第一个链表的内容后,将链表的头部往后移一个,变为指向第二个节点的指针//head->next是指向下一个链表的头}putchar('\n');}
int main()
{int i;int array[]={1,2,3,4};//用数组的方式实现数据的存储,访问到数组的首地址就可以输出整个数组//因为数组的地址是连续的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};//下面几行代码是将上面定义的几个结构体串起来,形成链表t1.next=&t2;//指针是存放别人的地址t1里的指针变量指向t2的地址t2.next=&t3;//这样就将三个结构体联系了起来printLink(&t1);//将第一个链表的地址传入动态输出函数system("pause");return 0;
}
链表的查找和节点个数的计算:
#include <stdio.h>
#include <stdlib.h>
//只要是通过指针的凡是访问结构体中的数据都要用->
struct Text//用链表的形式存储数据
{int data;struct Text* next;
};void printLink(struct Text *head)//链表的动态输出函数,struct Text *head是链表的头
{struct Text*Point;Point=head;while(Point!=NULL){printf("%d ",Point->data);Point=Point->next;//head是第一个节点的指针,输出完第一个链表的内容后,将链表的头部往后移一个,变为指向第二个节点的指针//head->next是指向下一个链表的头}putchar('\n');}
void countLink(struct Text* head)//链表计算节点个数函数
{int cont=0;struct Text* Point;Point=head;while(head!=NULL){cont++;head=head->next;}printf("链表节点数为:%d\n",cont);}
void serchLink(struct Text*head,int data)
{while(head!=NULL){if(head->data==data){printf("存在此节点\n");return;}head=head->next;}printf("无此节点\n");
}
int main()
{int i;int input;int array[]={1,2,3,4};//用数组的方式实现数据的存储,访问到数组的首地址就可以输出整个数组//因为数组的地址是连续的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};//下面几行代码是将上面定义的几个结构体串起来,形成链表t1.next=&t2;//指针是存放别人的地址t1里的指针变量指向t2的地址t2.next=&t3;//这样就将三个结构体联系了起来printLink(&t1);countLink(&t1);printf("请输入要查找的节点:\n");scanf("%d",&input);serchLink(&t1,input);system("pause");return 0;
}
从指定节点后插入节点:
#include <stdio.h>
#include <stdlib.h>
//只要是通过指针的凡是访问结构体中的数据都要用->
struct Text//用链表的形式存储数据
{int data;struct Text* next;
};void printLink(struct Text *head)//链表的动态输出函数,struct Text *head是链表的头
{struct Text*Point;Point=head;while(Point!=NULL){printf("%d ",Point->data);Point=Point->next;//head是第一个节点的指针,输出完第一个链表的内容后,将链表的头部往后移一个,变为指向第二个节点的指针//head->next是指向下一个链表的头}putchar('\n');}
void countLink(struct Text* head)//链表计算节点个数函数
{int cont=0;struct Text* Point;Point=head;while(head!=NULL){cont++;head=head->next;}printf("链表节点数为:%d\n",cont);}
void serchLink(struct Text*head,int data)
{while(head!=NULL){if(head->data==data){printf("存在此节点\n");return;}head=head->next;}printf("无此节点\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是插入位置后一个节点的指针,就是head->next指向插入位置的后一个节点Point->next=newLink;//修改插入位置前一个节点内的指针指向,指向新节点的地址,将链表串起来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};//下面几行代码是将上面定义的几个结构体串起来,形成链表t1.next=&t2;//指针是存放别人的地址t1里的指针变量指向t2的地址t2.next=&t3;//这样就将三个结构体联系了起来printLink(&t1);countLink(&t1);printf("请输入要查找的节点:\n");scanf("%d",&input);serchLink(&t1,input);printf("请输入在哪个节点后插入;\n");scanf("%d",&link);InsertFromBhind(&t1,&t4,link);system("pause");return 0;
}
在指定节点前插入节点:
#include <stdio.h>
#include <stdlib.h>
//只要是通过指针的凡是访问结构体中的数据都要用->
struct Text//用链表的形式存储数据
{int data;struct Text* next;
};void printLink(struct Text *head)//链表的动态输出函数,struct Text *head是链表的头
{struct Text*Point;Point=head;while(Point!=NULL){printf("%d ",Point->data);Point=Point->next;//head是第一个节点的指针,输出完第一个链表的内容后,将链表的头部往后移一个,变为指向第二个节点的指针//head->next是指向下一个链表的头}putchar('\n');}
void countLink(struct Text* head)//链表计算节点个数函数
{int cont=0;struct Text* Point;Point=head;while(head!=NULL){cont++;head=head->next;}printf("链表节点数为:%d\n",cont);}
void serchLink(struct Text*head,int data)
{while(head!=NULL){if(head->data==data){printf("存在此节点\n");return;}head=head->next;}printf("无此节点\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){//因为是在指定节点前插入节点//所以要通过指定节点的前一个节点插入//Point就是指向,指定节点前一个节点的指针//Point->next是指向,指定的插入节点的指针,也就是在这个节点前插入节点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};//下面几行代码是将上面定义的几个结构体串起来,形成链表t1.next=&t2;//指针是存放别人的地址t1里的指针变量指向t2的地址t2.next=&t3;//这样就将三个结构体联系了起来printLink(&t1);countLink(&t1);printf("请输入要查找的节点:\n");scanf("%d",&input);serchLink(&t1,input);printf("请输入在哪个节点前插入;\n");scanf("%d",&link);InsertFromBefore(&t1,&t4,link);system("pause");return 0;
}
静态链表的删除和修改:
#include <stdio.h>
#include <stdlib.h>
//只要是通过指针的凡是访问结构体中的数据都要用->
struct Text//用链表的形式存储数据
{int data;struct Text* next;
};void printLink(struct Text *head)//链表的动态输出函数,struct Text *head是链表的头
{struct Text*Point;Point=head;while(Point!=NULL){printf("%d ",Point->data);Point=Point->next;//head是第一个节点的指针,输出完第一个链表的内容后,将链表的头部往后移一个,变为指向第二个节点的指针//head->next是指向下一个链表的头}putchar('\n');}
void countLink(struct Text* head)//链表计算节点个数函数
{int cont=0;struct Text* Point;Point=head;while(head!=NULL){cont++;head=head->next;}printf("链表节点数为:%d\n",cont);}
void serchLink(struct Text*head,int data)//查找函数
{while(head!=NULL){if(head->data==data){printf("存在此节点\n");return;}head=head->next;}printf("无此节点\n");
}void changeLink(struct Text*head,int data,int data2)//改函数
{struct Text*Point=head;while(Point!=NULL){if(Point->data==data){printf("此节点存在可改\n");Point->data=data2;printLink(head);return;}Point=Point->next;}printf("无此节点\n");
}void delLink(struct Text*head,int data)
{struct Text*Point=head;if(head->data==data){head=head->next;//将第二个节点指定为数组头,那么第一个节点就需要释放掉避免造成内存泄漏//free(Point); //将这个地址释放掉,可以通过Point指针将之前的head的内存空间释放掉//free可以将指针指向的内存空间释放掉,但是释放掉之后最好将指针指向NULL//因为free只是把指针所指的内存给释放掉,但并没有把指针本身干掉。//free只能释放malloc开辟的内存空间printLink(head);return;}while(Point->next!=NULL){//head->next是指向要删除的那个节点的指针if(Point->next->data==data){Point->next=Point->next->next;//如果是动态创建,就要新定义一个指针//通过新定义的指针把(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};//下面几行代码是将上面定义的几个结构体串起来,形成链表t1.next=&t2;//指针是存放别人的地址t1里的指针变量指向t2的地址t2.next=&t3;//这样就将三个结构体联系了起来printLink(&t1);countLink(&t1);printf("请输入要查找的节点:\n");scanf("%d",&input);serchLink(&t1,input);printf("请输入要删除哪个节点;\n");scanf("%d",&link);delLink(&t1,link);printf("修改哪个节点;\n");scanf("%d",&link);changeLink(&t1,link,100);//这些所有的函数并不修改main函数中的节点system("pause");return 0;
}
链表的动态创建之头插法(每插进来一个节点就将它当做头结点,就像栈一样)
#include <stdio.h>
#include <stdlib.h>struct Text
{int data;struct Text*next;
};
void printLink(struct Text *head)//链表的动态输出函数,struct Text *head是链表的头
{struct Text*Point;Point=head;while(Point!=NULL){printf("%d ",Point->data);Point=Point->next;//head是第一个节点的指针,输出完第一个链表的内容后,将链表的头部往后移一个,变为指向第二个节点的指针//head->next是指向下一个链表的头 }putchar('\n');}
struct Text *creatFromHead(struct Text*head,struct Text*newnode)//节点头插法插入函数
//节点创建函数和插入节点函数分开写,有利于后面节点的插入
{if(head==NULL){head=newnode;}else{newnode->next=head;head=newnode;}return head;
}
struct Text*creatLink(struct Text*head)//链表节点创建函数
{struct Text*newnode=NULL;while(1){//不断创建新的节点,直到输入的数字为0newnode=(struct Text*)malloc(sizeof(struct Text));newnode->next=NULL;printf("请输入data:\n");scanf("%d",&(newnode->data));if(newnode->data==0){free(newnode);//新创建的节点里的data等于0;所以不把他插入链表中//将它指向的内存空间释放掉printf("链表创建结束\n");return head;}head=creatFromHead(head,newnode);}
}
int main()
{struct Text *head=NULL;printLink(creatLink(head));system("pause");return 0;
}
尾插法插入节点:
#include <stdio.h>
#include <stdlib.h>struct Text
{int data;struct Text*next;
};
void printLink(struct Text *head)//链表的动态输出函数,struct Text *head是链表的头
{struct Text*Point;Point=head;while(Point!=NULL){printf("%d ",Point->data);Point=Point->next;//head是第一个节点的指针,输出完第一个链表的内容后,将链表的头部往后移一个,变为指向第二个节点的指针//head->next是指向下一个链表的头 }putchar('\n');}
struct Text *insertFromBehind(struct Text*head,struct Text*newnode)//节点尾插法插入函数
{struct Text* p=NULL;p=head;if(p==NULL){//如果没有这句话,如果P为空指针,继续往下执行p->next对空指针进行操作,会出现段错误head=newnode;return head;}while(p->next!=NULL){p=p->next;}p->next=newnode;return head;
}
struct Text*creatLink(struct Text*head)//链表节点创建函数
{struct Text*newnode=NULL;while(1){//不断创建新的节点,直到输入的数字为0newnode=(struct Text*)malloc(sizeof(struct Text));newnode->next=NULL;printf("请输入data:\n");scanf("%d",&(newnode->data));if(newnode->data==0){free(newnode);//新创建的节点里的data等于0;所以不把他插入链表中//将它指向的内存空间释放掉printf("链表创建结束\n");return head;}head=insertFromBehind(head,newnode);}
}
int main()
{struct Text *head=NULL;printLink(creatLink(head));system("pause");return 0;
}