服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C语言实现单链表实现方法

C语言实现单链表实现方法

2021-05-27 14:24卡哇伊的柠檬 C/C++

这篇文章主要介绍了C语言实现单链表实现方法的相关资料,链表分为单向链表、双向链表、循环链表,需要的朋友可以参考下

C语言实现单链表实现方法

链表和我们之前实现过的顺序表一样,都是简单的数据结构,链表分为单向链表、双向链表、循环链表。而单向链表又分为两种实现方法,一种为带头节点的单链表,一种为不带头节点的单链表。我们来具体看看不带头节点的单链表的实现

单链表:它是一种链式存储的线性表,用一组地址任意的存储单元存放线性表的数据元素,称存储单元为一个节点。

C语言实现单链表实现方法

今天我们来实现一些单链表的简单接口

先看看单链表的结构: (为了通用性,我们将类型重命名为DataType)

?
1
2
3
4
5
6
7
8
typedef int DataType;
 
//链表
typedef struct Node
{
  DataType *data;
  struct Node *next;
}Node, *pNode, *pList;

接下来看看我们要实现的接口:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
void InitLinkList(pList *pplist);//初始化链表
pNode BuyNode(DataType d);//创建链表节点
void PushBack(pList *pplist, DataType d);//尾插
void PopBack(pList *pplist);//尾删
void PushFront(pList *pplist, DataType d);//头插
void PopFront(pList *pplist);//头删
void PrintList(pList plist);//打印链表
pNode Find(pList plist, DataType d);//查找指定元素
void Remove(pList *pplist, DataType d);//删除指定的一个元素
void RemoveAll(pList *pplist, DataType d);//删除指定的所有元素
void Insert(pList *pplist, pNode pos, DataType d);//指定位置的后面插入
void Erase(pList *pplist, pNode pos);//指定位置删除
void DestroyList(pList *pplist);//销毁链表

来看看每个接口的具体实现:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
pNode BuyNode(DataType d)
{
  pNode newNode = (pNode)malloc(sizeof(Node));
  if (newNode == NULL)
  {
    perror("malloc");
    exit(EXIT_FAILURE);
  }
  newNode->data = d;
  newNode->next = NULL;
  return newNode;
}
void InitLinkList(pList *pplist)
{
  assert(pplist);
  *pplist = NULL;
}
void PushBack(pList *pplist, DataType d)
{
  assert(pplist);
  pNode newNode = BuyNode(d);
  pNode cur = *pplist;
  //链表没有节点
  if (*pplist == NULL)
  {
    *pplist = newNode;
    return;
  }
  //链表有节点
  while (cur->next != NULL)
  {
    cur = cur->next;
  }
  cur->next = newNode;
}
void PopBack(pList *pplist)
{
  pNode cur = *pplist;
  pNode prev = NULL;
  assert(pplist);
  //链表没有节点
  if (*pplist == NULL)
  {
    return;
  }
  //链表有一个节点
  if (cur->next == NULL)
  {
    free(*pplist);
    *pplist = NULL;
    return;
  }
  //链表有两个及两个以上节点
  while (cur->next != NULL)
  {
    prev = cur;//prev中保存的是cur之前的那个节点
    cur = cur->next;
  }
  prev->next = NULL;
  free(cur);
}
void PushFront(pList *pplist, DataType d)
{
  pNode newNode = BuyNode(d);
  //pNode cur = *pplist;
  assert(pplist);
  ////链表没有节点
  //if (*pplist == NULL)
  //{
  // *pplist = newNode;
  //}
  ////链表有节点
  newNode->next = *pplist;
  *pplist = newNode;
 
}
void PopFront(pList *pplist)
{
  pNode cur = *pplist;
  assert(pplist);
  //链表为空
  if (*pplist == NULL)
  {
    return;
  }
  *pplist = cur->next;
  free(cur);
  cur = NULL;
}
void PrintList(pList plist)
{
  pNode cur = plist;
  while (cur)
  {
    printf("%d-->", cur->data);
    cur = cur->next;
  }
  printf("NULL\n");
}
pNode Find(pList plist, DataType d)
{
  pNode cur = plist;
  while (cur)
  {
    if (cur->data == d)
    {
      return cur;
    }
    cur = cur->next;
  }
  return NULL;
}
void Remove(pList *pplist, DataType d)
{
  pNode cur = *pplist;
  pNode prev = NULL;
  assert(pplist);
  if (cur == NULL)
  {
    return;
  }
  while (cur)
  {
    if (cur->data == d)
    {
      pNode del = cur;
      if (cur == *pplist)
      {
        *pplist = cur->next;
      }
      prev->next = cur->next;
      free(del);
      del = NULL;
      return;
    }
    else
    {
      prev = cur;
      cur = cur->next;
    }
  }
}
void RemoveAll(pList *pplist, DataType d)
{
  pNode cur = *pplist;
  pNode prev = NULL;
  assert(pplist);
  if (*pplist == NULL)
  {
    return;
  }
  while (cur)
  {
    if (cur->data == d)
    {
      pNode del = cur;
      if (cur == *pplist)
      {
        *pplist = cur->next;
      }
      else
      {
        prev->next = cur->next;
      }
      cur = cur->next;
      free(del);
      del = NULL;
    }
    else
    {
      prev = cur;
      cur = cur->next;
    }
  }
 
}
//在pos后面插入一个元素
void Insert(pList *pplist, pNode pos, DataType d)
{
  pNode newNode = BuyNode(d);
  assert(pplist);
  assert(pos);
  if (*pplist == NULL)
  {
    PushFront(pplist, d);
    return;
  }
  newNode->next = pos->next;
  pos->next = newNode;
}
void Erase(pList *pplist, pNode pos)
{
  assert(pplist);
  assert(pos);
  //要删除的是尾节点
  if (pos->next == NULL)
  {
    PopBack(pplist);
  }
  //删除的是非尾节点
  else
  {
    pNode del = pos->next;
    pos->data = pos->next->data;
    pos->next = pos->next->next;
    free(del);
    del = NULL;
  }
}
void DestroyList(pList *pplist)
{
  assert(pplist);
  pNode cur = *pplist;
  while (cur)
  {
    pNode del = cur;
    cur = cur->next;
    printf("del:%d\n", del->data);
    free(del);
    del = NULL;
  }
}

由于这些接口都较为简单,所以不进行具体的测试展示,读者可以自行测试

以上就是C语言实现单链表实现方法,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/qq_34021920/article/details/76594389

延伸 · 阅读

精彩推荐
  • C/C++OpenCV实现拼接图像的简单方法

    OpenCV实现拼接图像的简单方法

    这篇文章主要为大家详细介绍了OpenCV实现拼接图像的简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    iteye_183805102021-07-29
  • C/C++关于C语言中E-R图的详解

    关于C语言中E-R图的详解

    今天小编就为大家分享一篇关于关于C语言中E-R图的详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看...

    Struggler095962021-07-12
  • C/C++C语言实现双人五子棋游戏

    C语言实现双人五子棋游戏

    这篇文章主要为大家详细介绍了C语言实现双人五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    两片空白7312021-11-12
  • C/C++C语言main函数的三种形式实例详解

    C语言main函数的三种形式实例详解

    这篇文章主要介绍了 C语言main函数的三种形式实例详解的相关资料,需要的朋友可以参考下...

    ieearth6912021-05-16
  • C/C++使用C++制作简单的web服务器(续)

    使用C++制作简单的web服务器(续)

    本文承接上文《使用C++制作简单的web服务器》,把web服务器做的功能稍微强大些,主要增加的功能是从文件中读取网页并返回给客户端,而不是把网页代码...

    C++教程网5492021-02-22
  • C/C++深入C++拷贝构造函数的总结详解

    深入C++拷贝构造函数的总结详解

    本篇文章是对C++中拷贝构造函数进行了总结与介绍。需要的朋友参考下...

    C++教程网5182020-11-30
  • C/C++c/c++内存分配大小实例讲解

    c/c++内存分配大小实例讲解

    在本篇文章里小编给大家整理了一篇关于c/c++内存分配大小实例讲解内容,有需要的朋友们可以跟着学习参考下。...

    jihite5172022-02-22
  • C/C++c/c++实现获取域名的IP地址

    c/c++实现获取域名的IP地址

    本文给大家汇总介绍了使用c/c++实现获取域名的IP地址的几种方法以及这些方法的核心函数gethostbyname的详细用法,非常的实用,有需要的小伙伴可以参考下...

    C++教程网10262021-03-16