一、什么是链表
链表是物理存储单元上非连续的、非顺序的存储结构,数据元素的逻辑顺序是通过链表的指针地址实现,有一系列结点(地址)组成,结点可动态的生成。相对于线性表顺序结构,操作复杂。
二、链表的作用
实现数据元素的存储按一定顺序储存,允许在任意位置插入和删除结点,包括单向结点,双向结点,循环接点。
三、静态创建链表(包含计算链接节点总数、查找链表节点、遍历链表信息)
#include <stdio.h>
struct Test
{
int data;
struct Test *next;//定义结构体指针,指向结构体里面的元素
};
void printLink(struct Test *head)
{
struct Test *point;//定义临时结构体指针
point = head;//指向链表的头
while(point != NULL){
printf("%d ",point->data);
point = point->next;
}//遍历链表
putchar('\n');
}
int getLinkTotalNum(struct Test *head)
{
int cnt = 0;//定义临时变量记录链表节点数
while(head != NULL){
cnt++;
head = head->next;
}//遍历链表
return cnt;//返回节点数
}
int findLink(struct Test *head,int data)
{
while(head != NULL){
if(head->data == data){
return 1;
}
head = head->next;
}
return 0;
}
int main()
{
//初始化结构体
struct Test t1 = {1,NULL};
struct Test t2 = {2,NULL};
struct Test t3 = {3,NULL};
struct Test t4 = {4,NULL};
struct Test t5 = {5,NULL};
//链接节点
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
printf("use t1 to printf three nums:\n");
//printf("%d %d %d\n",t1.data,t1.next->data,t1.next->next->data);
printLink(&t1);//打印链表数据
int ret = getLinkTotalNum(&t1);//调用查找链表节点函数
printf("total num = %d\n",ret);
ret = findLink(&t1,3);//查找链表是否存在节点3
//返回值判断
if(ret == 0){
printf("no 3\n");
}else{
printf("have 3\n");
}
return 0;
}
四、链表的插入
从指定节点后方插入新节点(节点后插法)
实现步骤分为两步:1.1 找到要插入的节点位置
2.1 把新节点链接要插入节点位置的下一个,再把插入节点位置的下一个链接新节点
//函数包含三个形参(链表头,插入节点位置,新节点)
int insertFromBehind(struct Test *head,int data,struct Test *new)
{
struct Test *p=head;//定义临时结构体指针指向链表头
//遍历链表
while(p !=NULL){
if(p->data==data){ //判断插入新节点位置
new->next=p->next;//把新节点的尾链接要插入节点的下一节点
p->next=new;//把插入节点的下一个链接新节点
return 1;
}
p=p->next;
}
return 0;
}
2、从指定节点前方插入新节点(头插法)
实现步骤分为两种:1.1 直接在链表头插入
2.1 在链表中间节点插入
2.1.1 找到节点的下一个为插入位置的目标节点,把新节点接入目标节点的下一个,再把目标节点的下一个链接新节点
struct Test* insertFromForward(struct Test *head,int data,struct Test *new)
{
struct Test *p=head;//定义临时结构体指针指向链表头
//直接在链表头插入
if(p->data==data){
new->next=head;//把新节点的下一个直接链接链表头
return new;//链表头返回新节点
}
//在链表中间节点插入
while(p->next != NULL){
if(p->next->data==data){//找到节点的下一个为插入位置的目标节点
new->next=p->next;//把新节点接入目标节点的下一个
p->next=new;//再把目标节点的下一个链接新节点
printf("insert success\n");
return head;//返回链表头
}
p=p->next;
}
printf("insert no\n");//没找到插入位置,插入失败
return head;//返回链表头
}
五、链表的删除
从指定节点后方插入新节点(节点后插法)
实现步骤分为两种:1.1 直接在链表头删除
2.1 在链表中间节点删除
2.1.1 找到节点的下一个为删除位置的目标节点,再把目标节点的下一个链接目标节点的后第二个
struct Test* delectNode(struct Test *head,int data)
{
struct Test *p=head;
//直接在链表头删除
if(p->data == data){
head=head->next;//把链表头链接到链表头的下一个
free(p);//释放删除节点
return head;
}
//在链表中间节点删除
while(p->next != NULL){
if(p->next->data ==data){//找到节点的下一个为删除位置的目标节点
p->next=p->next->next;//再把目标节点的下一个链接目标节点的后第二个
return head;
}
p=p->next;
}
return head;
}
六、修改非链表头节点内容
实现步骤分为两步:1.1 找到要修改的节点位置
2.1 把节点位置对应的数据修改成新数据
int reviseLink(struct Test *head,int data,int newdata)
{
while(head != NULL){
if(head->data == data){
head->data = newdata
return 1;
}
head = head->next;
}
return 0;
}
七、动态创建链表
1、头插法创建链表
struct Test* creatLink(struct Test *head)
{
struct Test *new;//定义临时结构体指针
while(1){
new=(struct Test*)malloc(sizeof(struct Test));//开辟动态空间内存
printf("input your new node data\n");//提醒用户新插入节点位置
scanf("%d",&(new->data));//输入数据放在临时结构体指针的数据里
if(new->data==0){
printf("0 quit\n");
free(new);
return head;
}//防止插入失败,释放节点
head= insertFromHead(head,new);//
}
}
struct Test* insertFromHead(struct Test *head,struct Test *new)
{
if(head==NULL){
head=new;
}else{
new->next=head;
head=new;
}
return head;
}
2、尾插法创建链表
struct Test* creatLink2(struct Test *head)
{
struct Test *new;
while(1){
new=(struct Test*)malloc(sizeof(struct Test));
printf("input your new node data\n");
scanf("%d",&(new->data));
if(new->data==0){
printf("0 quit\n");
free(new);
return head;
}
head= insertBehind(head,new);
}
}
struct Test* insertBehind(struct Test *head,struct Test *new)
{
struct Test *p=head;
if(p == NULL){
head=new;
return head;
}
while(p->next != NULL){
p=p->next;
}
p->next=new;
return head;
}
3、尾插法
struct ListNode* ReverseList(struct ListNode* pHead ) {
if(pHead == NULL) return NULL;
if(pHead -> next == NULL) return pHead;
struct ListNode* p = NULL;
struct ListNode* tmp = NULL;
p = pHead -> next;
pHead -> next = NULL;
while(p -> next != NULL){
tmp = p -> next;
p -> next = pHead;
pHead = p;
p = tmp;
}
p->next = pHead;
pHead = p;
return pHead;
}