链表一
1.链表和数组区别及实现
#include<stdio.h> struct Test { int data; struct Test *next; }; int main() { int i; int arr[] = {1,2,3}; for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++){ printf("%d ",arr[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 link output:\n"); printf("%d %d %d\n",t1.data,t2.data,t3.data); return 0; }
2.链表的静态添加和动态遍历
#include<stdio.h> struct Test { int data; struct Test *next; }; void printLink(struct Test *head) { while(1){ if(head !=0){ printf("%d ",head->data); head = head->next; }else{ putchar('\n'); break; } } } int main() { int i; int arr[] = {1,2,3}; for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++){ printf("%d ",arr[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 link output:\n"); printf("%d %d %d\n",t1.data,t2.data,t3.data); printLink(&t1); return 0; }
3.统计链表节点个数及链表查找
#include<stdio.h> struct Test { int data; struct Test *next; }; void printLink(struct Test *head) { while(1){ if(head !=0){ printf("%d ",head->data); head = head->next; }else{ putchar('\n'); break; } } } int nodeSumLink(struct Test *head) { struct Test *point; point = head; int nodeSum = 0; while(point != NULL){ nodeSum++; point = point->next; } return nodeSum; } int searchLink(struct Test *head,int data) { int num = 0; while(head != NULL){ num++; if(head->data == data){ return num; }else{ return -1; } } } int main() { int i; int arr[] = {1,2,3}; for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++){ printf("%d ",arr[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 link output:\n"); printf("%d %d %d\n",t1.data,t2.data,t3.data); printLink(&t1); printf("link node sum:%d\n",nodeSumLink(&t1)); printf("link node search:%d\n",searchLink(&t1,1)); return 0; }
4.链表从节点后方插入新节点
int insertFromBehind(struct Test *head,int data,struct Test *new) { struct Test *p = head; while(p != NULL){ if(p->data == data){ new->next = p->next; p->next = new; return 1; } p = p->next; } return 0; }
5.链表从节点前方插入新节点
int insertFromFort(struct Test *head,int data,struct Test *new) { struct Test *p = head; int temp; while(p != NULL){ if(p->data == data){ new->next = p->next; p->next = new; temp = new->data; new->data = p->data; p->data = temp; return 1; } p = p->next; } return 0; }
6.链表的结点删除
struct Test* deletNode(struct Test *head,int data) { struct Test *p = head; if(p->data == data){ head =head->next; return head; } while(p->next != NULL){ if(p->next->data == data){ p->next = p->next->next; } p = p->next; } return head; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!