链表
链表概述
链表是一种数据结构,是一种数据存放的思想,是一个集合。
跟数组类似,但在一些数据处理增、删、改、查上,内存管理上会有优势
数组的地址是连续的,链表的地址不一定是连续的
链表和数组的区别
#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
int main()
{
int i;
int array[] = {1,2,3};
for(i=0;i<sizeof(array)/sizeof(array[0]);i++){
printf("%d ",array[i]);
}
putchar('\n');
struct Test t1 = {1,NULL};
struct Test t2 = {2,NULL};
struct Test t3 = {3,NULL};
t1.next = &t2;
t2.next = &t3;
printf("use t1 to print three nums\n");
printf("%d %d %d\n",t1.data,t1.next->data,t1.next->next->data);
system("pause");
return 0;
}
执行结果:
1 2 3
use t1 to print three nums
1 2 3
链表的静态添加和动态历遍
链表静态添加一个数
#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
int main()
{
struct Test t1 = {1,NULL};
struct Test t2 = {2,NULL};
struct Test t3 = {3,NULL};
struct Test t4 = {4,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
printf("use t1 to print three nums\n");
printf("%d %d %d %d\n",t1.data,t1.next->data,t1.next->next->data,t1.next->next->next->data);
system("pause");
return 0;
}
执行结果:
use t1 to print three nums
1 2 3 4
历遍链表
#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
void printLink(struct Test *head)
{
struct Test *p = head;
while(p){
printf("%d ",p->data);
p = p->next;
}
putchar('\n');
}
int main()
{
struct Test t1 = {1,NULL};
struct Test t2 = {2,NULL};
struct Test t3 = {3,NULL};
struct Test t4 = {4,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
printf("use t1 to print three nums\n");
printLink(&t1);
system("pause");
return 0;
}
执行结果:
use t1 to print three nums
1 2 3 4
历遍优化方法:
void printLink(struct Test *head)
{
struct Test *p = head;
while(p){
printf("%d ",p->data);
p = p->next;
}
putchar('\n');
}
void printLink(struct Test *head)
{
struct Test *point;
point = head;
while(1){
if(point != NULL){
printf("%d ",point->data);
point = point->next;
}else{
putchar('\n');
break;
}
}
}
链表中的节点
这是链表的头结点:
struct Test t1 = {1,NULL};
第二个结点是:
struct Test t2 = {2,NULL};
t1.next = &t2;
历遍链表的结点
void printLink(struct Test *head) //接受传过来的链表头结点地址
{
struct Test *p = head; // 把头结点的地址给到p
while(p){ //判断结构体p结点的下一个是否还有内容
printf("%d ",p->data); //通过指针的方式访问当前结点的内容
p = p->next; //把下一个结点的地址给到p
}
putchar('\n');
}
统计链表个数及链表查找
链表个数统计
int getLinkTotalNum(struct Test *head)
{
int cnt = 0;
while(head != NULL){
cnt++;
head = head->next;
}
return cnt;
}
#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
int getLinkTotalNum(struct Test *head)
{
int cnt = 0;
while(head != NULL){
cnt++;
head = head->next;
}
return cnt;
}
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};
struct Test t6 = {6,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
t5.next = &t6;
int ret = getLinkTotalNum(&t1);
printf("链表个数:%d\n",ret);
system("pause");
return 0;
}
链表查找
int searchLink(struct Test *head,int data)
{
while(head != NULL){
if(head->data == data){
return 1;
}
head = head->next;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
int searchLink(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};
struct Test t6 = {6,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
t5.next = &t6;
int ret = 0;
ret = searchLink(&t1,5);
if(ret == 0){
printf("no 5\n");
}else{
printf("have 5\n");
}
system("pause");
return 0;
}
从指定位置插入新节点
从结点后方插入新结点
int insertFromBehind(struct Test *head,int data,struct Test *news)
{
struct Test *p = head;
while(p){
if(p->data == data){
news->next = p->next;
p->next = news;
return 1;
}
p = p->next;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
void printLink(struct Test *head)
{
struct Test *p = head;
while(p->next != NULL){
printf("%d ",p->data);
p = p->next;
}
putchar('\n');
}
int insertFromBehind(struct Test *head,int data,struct Test *news)
{
struct Test *p = head;
while(p){
if(p->data == data){
news->next = p->next;
p->next = news;
return 1;
}
p = p->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};
struct Test t6 = {6,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
t5.next = &t6;
printLink(&t1);
struct Test news = {100,NULL};
int ret = insertFromBehind(&t1,5,&news);
if(ret == 0){
printf("插入失败\n");
}else{
printf("插入成功\n");
printLink(&t1);
}
system("pause");
return 0;
}
从结点前方插入新结点
struct Test *insertFromfor(struct Test *head,int data,struct Test *news)
{
struct Test *p = head;
if(p->data == data){
news->next = head;
return news;
}
while(p->next != NULL){
if(p->next->data == data){
news->next = p->next;
p->next = news;
return head;
}
p = p->next;
}
return head;
}
#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
void printLink(struct Test *head)
{
struct Test *p = head;
while(p){
printf("%d ",p->data);
p = p->next;
}
putchar('\n');
}
struct Test *insertFromfor(struct Test *head,int data,struct Test *news)
{
struct Test *p = head;
if(p->data == data){
news->next = head;
return news;
}
while(p->next != NULL){
if(p->next->data == data){
news->next = p->next;
p->next = news;
return head;
}
p = p->next;
}
return head;
}
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};
struct Test t6 = {6,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
t5.next = &t6;
struct Test *head = NULL;
head = &t1;
printLink(head);
struct Test news2 = {101,NULL};
head = insertFromfor(head,3,&news2);
printLink(head);
system("pause");
return 0;
}
链表删除指定节点
struct Test *deletNode(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){
struct Test *tmp = p->next;
p->next = tmp->next;
free(tmp);
return head;
}
p = p->next;
}
return head;
}
#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
void printLink(struct Test *head)
{
struct Test *p = head;
while(p){
printf("%d ",p->data);
p = p->next;
}
putchar('\n');
}
struct Test *deletNode(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){
struct Test *tmp = p->next;
p->next = tmp->next;
free(tmp);
return head;
}
p = p->next;
}
return head;
}
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};
struct Test t6 = {6,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
t5.next = &t6;
struct Test *head = NULL;
head = &t1;
printLink(head);
head = deletNode(head,4);
printLink(head);
system("pause");
return 0;
}
链表创建头插法
struct Test* insertFromHead(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("out\n");
free(new);
return head;
}else if(head == NULL){
head = new;
}else{
new->next = head;
head = new;
}
}
return head;
}
#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
void printLink(struct Test *head)
{
struct Test *p = head;
while(p){
printf("%d ",p->data);
p = p->next;
}
putchar('\n');
}
struct Test* insertFromHead(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("out\n");
free(new);
return head;
}else if(head == NULL){
head = new;
}else{
new->next = head;
head = new;
}
}
return head;
}
int main()
{
struct Test *head = NULL;
head = insertFromHead(head);
printLink(head);
return 0;
}
连接链表
struct Test *insertFromHead(struct Test *head,struct Test *new)
{
if(head == NULL){
head = new;
}else{
new->next = head;
head = new;
}
return head;
}
创建链表
struct Test *createlLink(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("out\n");
free(new);
return head;
}
head = insertFromHead(head,new);
}
}
#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
void printLink(struct Test *head)
{
struct Test *p = head;
while(p){
printf("%d ",p->data);
p = p->next;
}
putchar('\n');
}
struct Test *insertFromHead(struct Test *head,struct Test *new)
{
if(head == NULL){
head = new;
}else{
new->next = head;
head = new;
}
return head;
}
struct Test *createlLink(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("out\n");
free(new);
return head;
}
head = insertFromHead(head,new);
}
}
int main()
{
struct Test *head = NULL;
head = createlLink(head);
printLink(head);
struct Test t1 = {100,NULL};
head = insertFromHead(head,&t1);
printLink(head);
return 0;
}
链表创建尾插法
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;
}
#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
void printLink(struct Test *head)
{
struct Test *p = head;
while(p){
printf("%d ",p->data);
p = p->next;
}
putchar('\n');
}
struct Test *insertFromHead(struct Test *head,struct Test *new)
{
if(head == NULL){
head = new;
}else{
new->next = head;
head = new;
}
return head;
}
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;
}
struct Test *createlLink(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("out\n");
free(new);
return head;
}
head = insertFromHead(head,new);
}
}
int main()
{
struct Test *head = NULL;
head = createlLink(head);
printLink(head);
struct Test t1 = {100,NULL};
head = insertFromHead(head,&t1);
printLink(head);
struct Test t2 = {300,NULL};
head = insertBehind(head,&t2);
printLink(head);
return 0;
}
链表反转
struct Test *reverseList(struct Test *head)
{
struct Test *curr = head;
struct Test *prev = NULL;
struct Test *next = NULL;
while(curr){
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
void printLink(struct Test *head)
{
struct Test *p = head;
while(p){
printf("%d ",p->data);
p = p->next;
}
putchar('\n');
}
struct Test *insertFromHead(struct Test *head,struct Test *new)
{
if(head == NULL){
head = new;
}else{
new->next = head;
head = new;
}
return head;
}
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;
}
struct Test *createlLink(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("out\n");
free(new);
return head;
}
head = insertFromHead(head,new);
}
}
int main()
{
struct Test *head = NULL;
head = createlLink(head);
printLink(head);
struct Test t1 = {100,NULL};
head = insertFromHead(head,&t1);
printLink(head);
struct Test t2 = {300,NULL};
head = insertBehind(head,&t2);
printLink(head);
head = reverseList(head);
printLink(head);
return 0;
}