#include<stdio.h>typedefstructlink{int data;structlink*next;}LINK;//函数声明voidprintLink(LINK *head);//打印链表intgetNodeNum(LINK *head);//统计链表节点总个数voidfindNode(LINK *head,int num);//查找目标节点intmain(){//静态创建链表
LINK t1 ={1,NULL};
LINK t2 ={2,NULL};
LINK t3 ={3,NULL};
LINK *head =&t1;
t1.next =&t2;
t2.next =&t3;//打印链表printLink(head);//统计链表个数int ret =getNodeNum(head);printf("链表中节点的个数为%d\n",ret);//查找节点findNode(head,3);return0;}//打印链表voidprintLink(LINK *head){while(head !=NULL){printf("%d ",head->data);
head = head->next;}putchar('\n');}//统计链表节点总个数intgetNodeNum(LINK *head){int count =0;//计算节点个数while(head !=NULL){
count++;
head = head->next;}return count;}//查找目标节点voidfindNode(LINK *head,int num){while(head !=NULL){if(head->data == num){printf("你要找的小%d找到了\n",head->data);}
head = head->next;}}
练习2:静态创建链表实现增删改查功能
#include<stdio.h>typedefstructlink{int data;structlink*next;}LINK;//函数声明voidprintLink(LINK *head);//打印链表intgetNodeNum(LINK *head);//统计节点个数
LINK*addBehindNode(LINK *head,LINK* new,int data);//从节点后添加新节点
LINK*addAheadNode(LINK *head,LINK* new,int data);//从节点前添加新节点
LINK*deleteNode(LINK*head,int delete_data);//删除节点
LINK*modifyNode(LINK *head,int data ,int alterdata);//修改节点
LINK*findNode(LINK *head);//查找节点intmain(){//静态创建链表
LINK t1 ={1,NULL};
LINK t2 ={2,NULL};
LINK t3 ={3,NULL};
LINK new ={4,NULL};
LINK new2 ={0,NULL};
LINK *head =&t1;
t1.next =&t2;
t2.next =&t3;//打印链表puts("初始链表:");printLink(head);//增加节点puts("在节点之后增新加节点的链表:");
head =addBehindNode(head,&new,3);printLink(head);puts("在节点之前增加新节点的链表:");
head =addAheadNode(head,&new2,1);printLink(head);//删除节点puts("删除指定节点后的链表:");
head =deleteNode(head,0);printLink(head);//修改节点puts("修改指定节点后的链表:");
head =modifyNode(head,1,666);printLink(head);//查找节点findNode(head);return0;}//打印链表voidprintLink(LINK *head){
LINK* p = head;while(p !=NULL){printf("%d ",p->data);
p = p->next;}putchar('\n');}//统计节点个数intgetNodeNum(LINK *head){int count =0;//计算节点个数while(head !=NULL){
count++;
head = head->next;}return count;}//从节点后添加新节点
LINK *addBehindNode(LINK *head,LINK* new,int data){
LINK *phead = head;while(phead !=NULL){if(phead->data == data){
new->next = phead->next;先连接新节点的后面
phead->next = new;//再连新节点的前面return head;}
phead = phead->next;}}//从节点前添加新节点
LINK*addAheadNode(LINK*head,LINK*new,int data){
LINK*phead = head;if( head->data ==data ){
new->next = head;//先连接新节点的后面
head = new;//再连新节点的前面return head;}while(phead->next !=NULL){if(data == phead->next->data){
new->next = phead->next;//先连新节点的后面
phead->next = new;//再连新节点的前面return head;}
phead = phead->next;//让链表遍历起来}}//删除节点
LINK *deleteNode(LINK* head,int delete_data){
LINK* p = head;if( head->data == delete_data){
head = head->next;//删除首节点,头指针需要指向下一个节点return head;}while( p->next !=NULL){if(p->next->data == delete_data)//遍历找到目标节点{
p->next = p->next->next;return head;}
p = p->next;}}//修改节点
LINK*modifyNode(LINK *head,int data ,int alterdata){
LINK *phead = head;if(head->data == data){
head->data = alterdata;return head;}while(phead->next !=NULL){if(phead->next->data == data){
phead->next->data = alterdata;return head;}
phead = phead->next;}}
LINK*findNode(LINK *head){printf("输入你要查找的节点:");int num=0;scanf("%d",&num);
LINK* phead = head;while(phead !=NULL){if(phead->data == num){printf("你要找的小%d找到了\n",phead->data);return head;}
phead = phead->next;}printf("没有你要找的节点\n");}
动态创建链表
头插法&尾插法
#include<stdio.h>#include<stdlib.h>typedefstructlink{int data;structlink*next;}LINK;voidprintLink(LINK *head){
LINK* p = head;while(p !=NULL){printf("%d ",p->data);
p = p->next;}putchar('\n');}//头插法
LINK *insertFromHead(LINK *head,LINK* new){if(head ==NULL){
head = new;return head;}else{
new->next = head;
head = new;return head;}}//尾插法
LINK *insertFromTail(LINK *head,LINK* new){
LINK *phead = head;if(head ==NULL){
head = new;return head;}while( phead->next !=NULL){
phead = phead->next;//遍历链表找到尾巴!!!}
phead->next = new;//保证new节点是尾节点!!return head;}//头插法动态创建链表
LINK *create_Link(LINK *head,int flag){
LINK* new;while(1){
new =(LINK *)malloc(sizeof(LINK));printf("请输入新节点,退出请输入888\n");scanf("%d",&new->data);if(new->data ==888){free(new);
new =NULL;return head;}if(flag ==1){
head =insertFromHead(head,new);}elseif(flag ==2){
head =insertFromTail(head,new);}}}intmain(){
LINK *head =NULL;//动态创建链表,第二个参数1为头插法,2尾插法
head =create_Link(head,2);//打印链表printf("创建链表如下:");printLink(head);return0;}