一、什么是链表
链表是数据结构之一,其中的数据呈线性排列。在链表中,数据的添加和删除都较为方便,就是访问比较耗费时间。
二、静态创建链表
#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);
if(ret == 0){
printf("no 3\n");
}else{
printf("have 3\n");
}
return 0;
}
三、链表的查找
1、计算链表的个数
int getLinkTotalNum(struct Test *head)
{
int cnt = 0;
while(head != NULL){
cnt++;
head = head->next;
}
return cnt;
}
int ret = getLinkTotalNum(&t1);
printf("total num = %d\n",ret);
2、链表的查找
int findLink(struct Test *head,int data)
{
while(head != NULL){
if(head->data == data){
return 1;
}
head = head->next;
}
return 0;
}
ret = findLink(&t1,3);
if(ret == 0){
printf("no 3\n");
}else{
printf("have 3\n");
}
四、链表的插入
1、从指定节点后方插入新节点
struct Test* insertFromBehand(struct Test *head,int data,struct Test *new)//从指定节点后方插入新节点
{
struct Test *point = head;
while(point != NULL){
if(point->data == data){
new->next = point->next;
point->next = new;
printf("insert ok!\n");
return head;
}
point = point->next;
}
printf("insert fail!\n");
return head;
}
2、从指定节点前方插入新节点
需判断该指定节点是否为链表头
struct Test* insertFromFront(struct Test *head,int data,struct Test *new) //从指定节点前方插入新节点
{
struct Test *point = head;
if(point->data == data){ //判断是否为链表头
new->next = head;
printf("insert ok!\n");
return new;
}
while(point->next != NULL){
if(point->next->data == data){
new->next = point->next;
point->next = new;
printf("insert ok!\n");
return head;
}
point = point->next;
}
printf("insert fail!\n");
return head;
}
main函数
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;
struct Test *head = NULL;
head = &t1;
struct Test new1 = {11,NULL}; //从指定节点后方插入新节点
puts("after insert behand:");
head = insertFromBehand(head,3,&new1);
printLink(head);
struct Test new2 = {22,NULL}; //在链表头前插入新节点
puts("after insert front:");
head = insertFromFront(head,1,&new2);
printLink(head);
struct Test new3 = {33,NULL}; //从指定节点前方插入新节点
puts("after insert front:");
head = insertFromFront(head,3,&new3);
printLink(head);
return 0;
}
运行结果
五、链表的删除
struct Test* deletNode(struct Test *head,int data)
{
struct Test *point = head;
if(point->data == data){ //判断是否删除头节点
head = head->next;
//free(point);
printf("delet ok!\n");
return head;
}
while(point->next != NULL){
if(point->next->data == data){
point->next = point->next->next;
printf("delet ok!\n");
return head;
}
point = point->next;
}
printf("delet fail!\n");
return head;
}
main函数
六、链表的修改
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* createLink(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){ //输入0退出
printf("0 quit\n");
free(new);
return head;
}
head = insertFromHead(head,new);
}
}
2、头插法
struct Test* insertHead(struct Test *head,struct Test *new)
{
// if(head == NULL){
// head = new;
// }else{
new->next = head;
head = new;
//}
return head;
}
3、尾插法
struct Test* insertBehind(struct Test *head,struct Test *new)
{
struct Test *point = head;
if(point == NULL){
head = new;
return head;
}
while(point->next != NULL){
point = point->next;
}
point->next = new;
return head;
}
main函数
int main()
{
struct Test *head = NULL;
head = createLink(head);
printLink(head);
struct Test t1 = {1000,NULL};
head = insertHead(head,&t1);
printLink(head);
struct Test t2 = {2000,NULL};
head = insertBehind(head,&t2);
printLink(head);
return 0;
}
八、反转链表
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;
}