#include<stdio.h>structTest{int data;structTest*next;};voidprintNode(structTest*head){structTest*p = head;while(p !=NULL){printf("%d \n",p->data);
P = P->next;}}intmain(){structTest t1 ={1,NULL};structTest t2 ={2,NULL};structTest t3 ={3,NULL};structTest t4 ={4,NULL};// 创建一个静态链表,将整个结构体的地址存放在 next 中
t1.next =&t2;
t2.next =&t3;
t3.next =&t4;// 遍历链表printNode(&t1);return0;}
统计节点个数
#include<stdio.h>structTest{int data;structTest*next;};intgetNumNode(structTest*head){int num =0;structTest*p = head;while(p !=NULL){
num++;
p = p->next;}return num;}intmain(){structTest t1 ={1,NULL};structTest t2 ={2,NULL};structTest t3 ={3,NULL};structTest t4 ={4,NULL};
t1.next =&t2;
t2.next =&t3;
t3.next =&t4;// 统计链表节点个数int n =getNumNode(&t1);printf("number = %d\n",n);return0;}
添加节点——尾插与头插
#include<stdio.h>structTest{int data;structTest*next;};structTest*addNode1(structTest*head,structTest*new,int data){structTest*p = head;if(head->data == data){
new->next = head;return new;}while(p->next !=NULL){if(p->data == data){
new->next= p->next;
p->next = new;break;}
p = p->next;}return head;}structTest*addNode2(structTest*head,structTest*new,int data){structTest*p = head;if(head->data == data){
new->next = head;return new;}while(p->next !=NULL){if(p->next->data == data){
new->next = p->next;
p->next = new;break;}
p = p->next;}return head;}voidprintNode(structTest*head){int num =0;structTest*p = head;while(p !=NULL){printf("%d ",p->data);
p = p->next;}}intmain(){structTest t1 ={1,NULL};structTest t2 ={2,NULL};structTest t3 ={3,NULL};structTest t4 ={4,NULL};
t1.next =&t2;
t2.next =&t3;
t3.next =&t4;structTest new ={66,NULL};structTest*head =&t1;// head = addNode1(head,&new,3); // 尾插法
head =addNode2(head,&new,3);// 头插法printNode(head);return0;}
删除节点
#include<stdio.h>structTest{int data;structTest*next;};structTest*deleteNode(structTest*head,int data){structTest*p = head;if(head->data == data){
head = head->next;return head;}while(p !=NULL){if(p->data == data){
p->next = p->next->next;break;}
p = p->next;}return head;}voidprintNode(structTest*head){int num =0;structTest*p = head;while(p !=NULL){printf("%d ",p->data);
p = p->next;}}intmain(){structTest t1 ={1,NULL};structTest t2 ={2,NULL};structTest t3 ={3,NULL};structTest t4 ={4,NULL};
t1.next =&t2;
t2.next =&t3;
t3.next =&t4;structTest*head =&t1;
head =deleteNode(head,2);printNode(head);return0;}
更换节点
#include<stdio.h>structTest{int data;structTest*next;};structTest*changeNode(structTest*head,structTest*new,int data){structTest*p = head;if(head->data == data){
new->next = head->next;return new;}while(p->next !=NULL){if(p->next->data == data){
new->next = p->next->next;
p->next = new;break;}
p = p->next;}return head;}voidprintNode(structTest*head){int num =0;structTest*p = head;while(p !=NULL){printf("%d ",p->data);
p = p->next;}}intmain(){structTest t1 ={1,NULL};structTest t2 ={2,NULL};structTest t3 ={3,NULL};structTest t4 ={4,NULL};
t1.next =&t2;
t2.next =&t3;
t3.next =&t4;structTest*head =&t1;structTest new ={88,NULL};
head =changeNode(head,&new,2);printNode(head);return0;}
动态链表
尾插法
#include<stdio.h>#include<stdlib.h>structTest{int data;structTest*next;};voidprintNode(structTest*head){structTest*p = head;while(p !=NULL){printf("%d ",p->data);
p = p->next;}}structTest*addNode(structTest*head,structTest*new){structTest*p = head;if(head ==NULL)return new;while(p->next !=NULL)
p = p->next;
p->next = new;return head;}structTest*creatNode(structTest*head){int i;structTest*p = head;structTest*new;for(i =0; i <5;++i){
new =(structTest*)malloc(sizeof(structTest));printf("数据%d:",i+1);scanf("%d",&(new->data));
head =addNode(head,new);}}intmain(){structTest*head =NULL;
head =creatNode(head);printNode(head);return0;}
头插法
#include<stdio.h>#include<stdlib.h>structTest{int data;structTest*next;};voidprintNode(structTest*head){structTest*p = head;while(p !=NULL){printf("%d ",p->data);
p = p->next;}}structTest*addNode(structTest*head,structTest*new){structTest*p = head;if(head ==NULL){
head = new;}else{
new->next = head;
head = new;}return head;}structTest*creatNode(structTest*head){int i;structTest*p = head;structTest*new;for(i =0; i <5;++i){
new =(structTest*)malloc(sizeof(structTest));printf("数据%d:",i+1);scanf("%d",&(new->data));
head =addNode(head,new);}}intmain(){structTest*head =NULL;
head =creatNode(head);printNode(head);return0;}