链表基本应用
链表创建
链表插入
链表删除
链表修改
链表查询
链表反转
完整代码
截图

链表创建
| typedef struct Node |
| { |
| int data; |
| struct Node *next; |
| |
| }Node; |
| |
| Node *CreateNode() |
| { |
| Node *head=malloc(sizeof(Node)); |
| head->next=NULL; |
| head->data=0; |
| |
| return head; |
| } |
| } |
链表插入
| Node* AddNode(Node* head, int data) |
| { |
| Node* p = malloc(sizeof(Node)); |
| p->data = data; |
| p->next = head->next; |
| head->next = p; |
| return head; |
| } |
链表删除
| void Delete_List(Node **head) |
| { |
| Node *p; |
| while(*head!=NULL) |
| { |
| p=*head; |
| *head=(*head)->next; |
| free(p); |
| p=NULL; |
| } |
| } |
链表修改
| Node *Revise_List(Node *head,int i,int j) |
| { |
| Node *p=head; |
| if(head->next==NULL) |
| { |
| printf("链表为空\n"); |
| return NULL; |
| } |
| while(head){ |
| if(head->data==i) |
| { |
| head->data=j; |
| } |
| } |
| head=p; |
| return head; |
| } |
链表查询
| int ReadNode(Node *head,int data) |
| { |
| while(head) |
| { |
| if(head->data==data) |
| return 0; |
| head=head->next; |
| } |
| return 1; |
| } |
链表反转
| Node *RollbackNode(Node *head) |
| { |
| Node* swap = NULL; |
| Node* phead = NULL; |
| Node* head1 = head; |
| head = head->next; |
| while (head) |
| { |
| phead = swap; |
| swap = head; |
| head = head->next; |
| swap->next = phead; |
| } |
| swap->next = phead; |
| phead = swap; |
| |
| head1->next = phead; |
| phead = head1; |
| return phead; |
| } |
完整代码
| #include <stdio.h> |
| #include <stdlib.h> |
| typedef struct Node |
| { |
| int data; |
| struct Node *next; |
| |
| }Node; |
| |
| Node *CreateNode() |
| { |
| Node *head=malloc(sizeof(Node)); |
| head->next=NULL; |
| head->data=0; |
| |
| return head; |
| } |
| |
| Node* AddNode(Node* head, int data) |
| { |
| Node* p = malloc(sizeof(Node)); |
| p->data = data; |
| p->next = head->next; |
| head->next = p; |
| return head; |
| } |
| |
| void Delete_List(Node **head) |
| { |
| Node *p; |
| while(*head!=NULL) |
| { |
| p=*head; |
| *head=(*head)->next; |
| free(p); |
| p=NULL; |
| } |
| } |
| Node *Revise_List(Node *head,int i,int j) |
| { |
| Node *p=head; |
| if(head->next==NULL) |
| { |
| printf("链表为空\n"); |
| return NULL; |
| } |
| while(head){ |
| if(head->data==i) |
| { |
| head->data=j; |
| } |
| } |
| head=p; |
| return head; |
| } |
| |
| int ReadNode(Node *head,int data) |
| { |
| while(head) |
| { |
| if(head->data==data) |
| return 0; |
| head=head->next; |
| } |
| return 1; |
| } |
| |
| Node *RollbackNode(Node *head) |
| { |
| Node* swap = NULL; |
| Node* phead = NULL; |
| Node* head1 = head; |
| head = head->next; |
| while (head) |
| { |
| phead = swap; |
| swap = head; |
| head = head->next; |
| swap->next = phead; |
| } |
| swap->next = phead; |
| phead = swap; |
| |
| head1->next = phead; |
| phead = head1; |
| return phead; |
| } |
| |
| void showNode(Node* head) |
| { |
| head = head->next; |
| while (head) |
| { |
| printf("%d ", head->data); |
| head = head->next; |
| } |
| printf("\n"); |
| } |
| |
| int main() |
| { |
| Node *head=CreateNode(); |
| head=AddNode(head, 1); |
| head=AddNode(head, 2); |
| head=AddNode(head, 3); |
| head=AddNode(head, 4); |
| |
| showNode(head); |
| head=RollbackNode(head); |
| |
| showNode(head); |
| return 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!