如何在C语言中实现链表、栈和队列等数据结构?
在C语言中实现链表、栈和队列等数据结构需要定义相关的数据结构和操作。下面我会给出每种数据结构的简单实现示例。
- 链表(LinkedList)
链表是由一系列节点组成,每个节点包含数据部分和指向下一个节点的指针。
c复制代码
#include <stdio.h> |
|
#include <stdlib.h> |
|
// 定义链表节点 |
|
typedef struct Node { |
|
int data; |
|
struct Node* next; |
|
} Node; |
|
// 创建新节点 |
|
Node* createNode(int data) { |
|
Node* newNode = (Node*)malloc(sizeof(Node)); |
|
if (!newNode) { |
|
printf("Memory error\n"); |
|
return NULL; |
|
} |
|
newNode->data = data; |
|
newNode->next = NULL; |
|
return newNode; |
|
} |
|
// 在链表末尾添加节点 |
|
void appendNode(Node** head, int data) { |
|
Node* newNode = createNode(data); |
|
if (!*head) { |
|

最低0.47元/天 解锁文章
1410

被折叠的 条评论
为什么被折叠?



