数据结构 链表

这篇文章详细介绍了使用C语言实现链表的基本操作,包括头插、尾插、检查链表是否为空以及删除节点的方法。作者通过示例展示了如何插入数据、处理边界条件和维护链表结构。
摘要由CSDN通过智能技术生成

整理代码

 52 {                                                            |  1 #include "link.h"                                                |  1 #ifndef ww
 53     if(h==0)                                                 |  2 int main(int argc, const char *argv[])                           |  2 #define ww
 54     {                                                        |  3 {                                                                |  3 #include <stdio.h>
 55         printf("入参为空检查\n");                            |  4     p h=create_link();                                           |  4 #include <string.h>
 56         return;                                              |  5     //create_link();                                             |  5 #include <stdlib.h>
 57     }                                                        |  6     //create_node(10);                                           |  6 
 58     if(empty_link(h))                                        |  7     insert_head(h,11);                                           |  7 typedef struct node
 59     {                                                        |  8     insert_head(h,12);                                           |  8 {
 60         printf("链表为空无法输出\n");                        |  9     insert_head(h,13);                                           |  9     int data;
 61     }                                                        | 10     insert_head(h,11);                                           | 10     struct node* next;
 62     p p1=h;//跳过头结点                                      | 11     del_head(h);                                                 | 11 }link,*p;
 63                                                              | 12     insert_end(h,80);                                            | 12 
 64     while(p1!=0)                                             | 13     show(h);                                                     | 13 //创建头节点
 65     {                                                        | 14                                                                  | 14 p  create_link();
 66         printf("%d->",p1->data);                             | 15      return 0;                                                   | 15 //创建节点
 67         p1=p1->next;                                         | 16 }                                                                | 16 p create_node(int n);
 68     }                                                        | 17                                                                  | 17 //头插
 69     putchar(10);                                             |~                                                                    | 18 void insert_head(p h,int n);
 70 }                                                            |~                                                                    | 19 //判空
 71 //头删                                                       |~                                                                    | 20 int empty_link(p h);
 72 void del_head(p h)                                           |~                                                                    | 21 //输出
 73 {                                                            |~                                                                    | 22 void show(p h);
 74     if(h==0)                                                 |~                                                                    | 23 //头删
 75     {                                                        |~                                                                    | 24 void del_head(p h);
 76         printf("入参为空\n");                                |~                                                                    | 25 //尾插
 77         return;                                              |~                                                                    | 26 void insert_end(p h,int n);
 78     }                                                        |~                                                                    | 27 //尾删
 79     if(empty_link(h))                                        |~                                                                    | 28 void del_end(p h);
 80     {                                                        |~                                                                    | 29 //按位置插入
 81         printf("链表为空无需删除\n");                        |~                                                                    | 30 void insert_pos(p h,int n,int pos);                                                                                                                                                                                                                                                                                                                                                                                                         
 82         return;                                              |~                                                                    | 31 
 83     }                                                        |~                                                                    | 32 #endif
 84     p del=h->next;                                           |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
 85     h->next=h->next->next;                                   |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
 86     free(del);                                               |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
 87     h->data--;                                               |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
 88 }                                                            |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
 89 //尾插                                                       |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
 90 void insert_end(p h,int n)                                  |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
 91 {                                                            |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
 92     if(h==0)                                                 |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
 93     {                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
 94         printf("入参为空\n");                                |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
 95         return;                                              |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
 96     }                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
 97     p p1=h;//定义结构体指针指向头                            |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
 98     while(p1->next!=0)//从头开始循直到找到结尾               |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
 99     {                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
100         p1=p1->next;//每循环一次找到下一个节点的指针域       |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
101     }                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
102     //定义指针接收新节点                                     |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
103     p new=create_node(n);                                    |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
104     //新节点指针域储存原尾节点指针域                         |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
105     new->next=p1->next;//null                                |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
106     //原尾节点指针域指向新节点                               |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
107     p1->next=new;                                            |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
108     h->data++;                                               |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
109 }                                                            |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
110                                                              |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
111 //尾删                                                       |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
112 void del_end(p h)                                            |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
113 {                                                            |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
114     if(h==0)                                                 |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
115     {                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
116         printf("入参为空\n");                                |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
117         return;                                              |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
118     }                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
119     if(empty_link(h))                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
120     {                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
121         printf("表为空\n");                                  |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
122         return;                                              |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
123     }                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
124     //从头开始找                                             |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
125     p p1=h;                                                  |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
126     //找到倒数第二个节点                                     |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
127     while(p1->next->next!=0)                                 |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
128     {                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
129         p1=p1->next;                                         |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
130     }                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
131     //保存要删除节点                                         |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
132     p del=p1->next;                                          |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
133     //将倒数第二个节点指空                                   |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
134     p1->next=0;                                              |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
135     free(del);                                               |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
136     h->data--;                                               |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
137 }                                                            |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
138 //按位置插入                                                 |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
139 void insert_pos(p h,int n,int pos)                           |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
140 {                                                            |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
141     if(h==0)                                                 |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
142     {                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
143         printf("入参为空\n");                                |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
144         return;                                              |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
145     }                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
146     //位置合理性判断                                         |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
147     //5个元素 插第6个位置相当于尾插                          |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
148     if(pos<=0||pos>h->data+1)                                |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
149     {                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
150         printf("位置不合理\n");                              |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
151         return;                                              |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
152     }                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
153     //找pos-1的位置                                          |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
154     p p1=h;//从头结点开始                                    |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
155     for(int i=0;i<pos-1;i++)                                 |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
156     {                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
157         p1=p1->next;                                         |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
158     }                                                        |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
159     //申请新节点                                             |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
160     p new=create_node(n);                                    |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
161     new->next=p1->next;                                      |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
162     p1->next=new;                                            |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
163     h->data++;                                               |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
164                                                              |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
165                                                              |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
166 }                                                            |~                                                                    |~                                                                                                                                                                                                                                                                                                                                                                                                                                               
167               

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值