链表 --- 117-127节-----老版本过度

79 阅读6分钟

链表是什么

链表是一种动态数据结构,由多个节点组成,每个节点包含了数据和指向下一个节点的指针,相邻节点之间通过指针连接起来,形成了一个链式结构。链表的优点是可以高效地插入和删除元素,但它的缺点是无法随机访问元素,必须从头节点开始遍历到目标节点才能访问,因此访问时间较长。链表常见的类型有单向链表、双向链表和循环链表。链表常用于实现队列、栈等数据结构,以及一些常见算法的实现,比如归并排序、快速排序等。

链表的静态添加个动态遍历

struct Test
{
        int i;
        struct Test *next;
};
void printLink(struct Test *head)
{
        struct Test *pa;
        pa = head;
        while(1){

                if(pa != NULL){
                        printf("%d \n",pa->i);
                        pa = pa->next;
                }else{
                        break;
                }
        }
}
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};
        struct Test t7 = {7,NULL};
        struct Test t8 = {8,NULL};
        struct Test t9 = {9,NULL};

        t1.next = &t2;
        t2.next = &t3;
        t3.next = &t4;
        t4.next = &t5;
        t5.next = &t6;
        t6.next = &t7;
        t7.next = &t8;
        t8.next = &t9;

        printLink(&t1);
}
~

链表的查找与统计

#include <stdio.h>
struct Test
{
	int i;
	struct Test *next;
};
void printLink(struct Test *head)
{
	struct Test *pa;
	pa = head;
	while(1){
		
		if(pa != NULL){
			printf("%d ",pa->i);
			pa = pa->next;
		}else{
			break;
		}
	}
	puts("\n");
}
int chaXunJieDian(struct Test *head)
{
	int cnt;
	while(head != NULL){
		cnt++;
		head = head->next;
	}
	return cnt;
}
int chaXun(struct Test *head,int data)
{
	int cnt = 0;
	int data1;
	while(head != NULL){
		cnt++;
		if(cnt == data){
			data1 = head->i;	
			return data1;
		}
		head = head->next;
	}
	
	
}
int chaXunDingWei(struct Test *head,int data)
{
	
		
	while(head != NULL){
		if(head->i == data){
			head = head->next;	
			return 1;
		}
		head = head->next;
	}
	return 0;
}
int main()
{
	int i;
	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};
	struct Test t7 = {7,NULL};
	struct Test t8 = {8,NULL};
	struct Test t9 = {9,NULL};

	t1.next = &t2;
	t2.next = &t3;
	t3.next = &t4;
	t4.next = &t5;
	t5.next = &t6;
	t6.next = &t7;
	t7.next = &t8;
	t8.next = &t9;

	printLink(&t1);
       int  cnt = chaXunJieDian(&t1);
       printf("it is number%d\n",cnt);	
       puts("请输入你想查询的数\n");
       scanf("%d",&i);
      int c = chaXun(&t1,i);
       printf("你所查询的位置的数是%d\n",c);	
      int ret = chaXunDingWei(&t1,i);
      if(ret == 1){
      	printf("有你要查询的数,他是:%d\n",i);
      }else{
      	printf("没有你要找的数、\n");
      }
}

从指定节点后插入新节点

#include <stdio.h>
struct Test
{
	int i;
	struct Test *next;
};
void printLink(struct Test *head)
{
	struct Test *pa;
	pa = head;
	while(1){
		
		if(pa != NULL){
			printf("%d ",pa->i);
			pa = pa->next;
		}else{
			break;
		}
	}
	puts("\n");
}
int chaRu(struct Test *head,int data,struct Test *new)
{
	struct Test *p = head;

	while(p != NULL){
		if(p->i == data){
			new->next = p->next;
			p->next = new;
				return 1;
		}
		p = p->next;
	}
return 0;	
}
int main()
{
	int i;
	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};
	struct Test t7 = {7,NULL};

	t1.next = &t2;
	t2.next = &t3;
	t3.next = &t4;
	t4.next = &t5;
	t5.next = &t6;
	t6.next = &t7;

	struct Test New = {767,NULL};
	
	printLink(&t1);
	chaRu(&t1,4,&New);
	printLink(&t1);

}

从指定节点前加入新节点(抱含添加到链表头)

#include <stdio.h>
struct Test
{
	int i;
	struct Test *next;
};
void printLink(struct Test *head)
{
	struct Test *pa;
	pa = head;
	while(1){
		
		if(pa != NULL){
			printf("%d ",pa->i);
			pa = pa->next;
		}else{
			break;
		}
	}
	puts("\n");
}
struct Test* chaRuQianDuan(struct Test *head,int data,struct Test *new)
{
	struct Test *p = head;
		while(p->i == data){
			new->next = p;
			return new;
		}
	
	while(p->next != NULL){
		if(p->next->i == data){
			new->next = p->next;
			p->next = new;
		return head;
		}
		p = p->next;
	}
	
}
int chaRu(struct Test *head,int data,struct Test *new)
{
	struct Test *p = head;

	while(p != NULL){
		if(p->i == data){
			new->next = p->next;
			p->next = new;
				return 1;
		}
		p = p->next;
	}
return 0;	
}
int main()
{
	int i;
	struct Test *head;
	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};
	struct Test t7 = {7,NULL};

	t1.next = &t2;
	t2.next = &t3;
	t3.next = &t4;
	t4.next = &t5;
	t5.next = &t6;
	t6.next = &t7;

	head = &t1;
	struct Test New = {767,NULL};
	struct Test New2 = {9426,NULL};
	struct Test New3 = {326,NULL};
	
	printLink(head);
	chaRu(head,4,&New);
	printLink(head);
	head = chaRuQianDuan(head,1,&New2);//链表头添加
	printLink(head);
	chaRuQianDuan(head,767,&New3);//指定节点前添加
	printLink(head);


}

删除指定节点

#include <stdio.h>
struct Test
{
	int i;
	struct Test *next;
};
void printLink(struct Test *head)
{
	struct Test *pa;
	pa = head;
	while(1){
		
		if(pa != NULL){
			printf("%d ",pa->i);
			pa = pa->next;
		}else{
			break;
		}
	}
	puts("\n");
}
struct Test* chaRuQianDuan(struct Test *head,int data,struct Test *new)
{
	struct Test *p = head;
		while(p->i == data){
			new->next = p;
			return new;
		}
	
	while(p->next != NULL){
		if(p->next->i == data){
			new->next = p->next;
			p->next = new;
		return head;
		}
		p = p->next;
	}
	
}
int chaRu(struct Test *head,int data,struct Test *new)
{
	struct Test *p = head;

	while(p != NULL){
		if(p->i == data){
			new->next = p->next;
			p->next = new;
				return 1;
		}
		p = p->next;
	}
return 0;	
}
struct Test* shangChuJieDian(struct Test *head, int data)
{
	struct Test *p = head;
	while(p->next != NULL){
		if(p->next->i == data){
			p->next = p->next->next;
return head;
		}
		p = p->next;
	}
}
int main()
{
	int i;
	struct Test *head;
	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};
	struct Test t7 = {7,NULL};

	t1.next = &t2;
	t2.next = &t3;
	t3.next = &t4;
	t4.next = &t5;
	t5.next = &t6;
	t6.next = &t7;

	head = &t1;
	struct Test New = {767,NULL};
	struct Test New2 = {9426,NULL};
	struct Test New3 = {326,NULL};
	
	printLink(head);
	chaRu(head,4,&New);
	printLink(head);
	head = chaRuQianDuan(head,1,&New2);
	printLink(head);
	chaRuQianDuan(head,767,&New3);
	printLink(head);
	shangChuJieDian(head,6);//删除指定节点
	printLink(head);
}

链表动态创建之头插法

#include <stdio.h>
#include<stdlib.h>
struct Test
{
 int i;
 struct Test *next;
};
void printLink(struct Test *head)
{
 struct Test *pa;
 pa = head;
 while(1){

 	if(pa != NULL){
 		printf("%d ",pa->i);
 		pa = pa->next;
 	}else{
 		break;
 	}
 }
 puts("\n");
}
struct Test* chaRuQianDuan(struct Test *head,int data,struct Test *new)
{
 struct Test *p = head;
 while(p->i == data){
 	new->next = p;
 	return new;
 }

 while(p->next != NULL){
 	if(p->next->i == data){
 		new->next = p->next;
 		p->next = new;
 		return head;
 	}
 	p = p->next;
 }

}
int chaRu(struct Test *head,int data,struct Test *new)
{
 struct Test *p = head;

 while(p != NULL){
 	if(p->i == data){
 		new->next = p->next;
 		p->next = new;
 		return 1;
 	}
 	p = p->next;
 }
 return 0;	
}
struct Test* shangChuJieDian(struct Test *head, int data)
{
 struct Test *p = head;
 while(p->next != NULL){
 	if(p->next->i == data){
 		p->next = p->next->next;
 		return head;
 	}
 	p = p->next;
 }
}
struct Test* touChaFa(struct Test* head)
{
 struct Test* new;
 new = (struct Test *)malloc(sizeof(struct Test));
 puts("亲输入");
 scanf("%d",&(new->i));
 if(head == NULL){
 	head = new;

 }else{
 	new->next = head;
 	head = new;
return head;
 }
}

int main()
{
 int i = 4;
 struct Test* head = NULL;
 while(i--){
 	head = touChaFa(head);	
 }
 printLink(head);
}

链表动态创建值尾插法

#include<stdlib.h>
struct Test
{
	int i;
	struct Test *next;
};
void printLink(struct Test *head)
{
	struct Test *pa;
	pa = head;
	while(1){

		if(pa != NULL){
			printf("%d ",pa->i);
			pa = pa->next;
		}else{
			break;
		}
	}
	puts("\n");
}
struct Test* chaRuQianDuan(struct Test *head,int data,struct Test *new)
{
	struct Test *p = head;
	while(p->i == data){
		new->next = p;
		return new;
	}

	while(p->next != NULL){
		if(p->next->i == data){
			new->next = p->next;
			p->next = new;
			return head;
		}
		p = p->next;
	}

}
int chaRu(struct Test *head,int data,struct Test *new)
{
	struct Test *p = head;

	while(p != NULL){
		if(p->i == data){
			new->next = p->next;
			p->next = new;
			return 1;
		}
		p = p->next;
	}
	return 0;	
}
struct Test* shangChuJieDian(struct Test *head, int data)
{
	struct Test *p = head;
	while(p->next != NULL){
		if(p->next->i == data){
			p->next = p->next->next;
			return head;
		}
		p = p->next;
	}
}
struct Test* touChaFa(struct Test* head)
{
	struct Test* new;
	new = (struct Test *)malloc(sizeof(struct Test));
	puts("亲输入");
	scanf("%d",&(new->i));
	if(head == NULL){
		head = new;
		return head;
	}else{
		new->next = head;
		head = new;
		return head;
	}
}
struct Test* weiChaFa(struct Test* head,struct Test* new)
{
	struct Test *p = head;
	if(p == NULL){
		p =new;
		return head;
	}
	while(p->next != NULL){
		p = p->next;
	}
		p->next = new->next;
	return head;
}
struct Test* weiChaFaLink(struct Test* head)
{
	struct Test *new;
	new = (struct Test *)malloc(sizeof(struct Test));
	puts("亲输入");
	scanf("%d",&(new->i));
	head = weiChaFa(head,new);
	return head;

}
int main()
{
	int i = 4;
	struct Test* head = NULL;
	while(i--){
		head = touChaFa(head);	
	}
	printLink(head);
}

总结

难啊 折腾了我好几个晚上 做完 弄到陵城十二点还没有搞定 就一个小小的错误 以后看的时候要记得