单向链表的操作
2011-09-29 11:10
357 查看
#include "stdio.h"
#include "malloc.h"
struct Node* current = NULL; //声明的一个全局变量表示的是链表的尾部
struct Node
{
int value;
struct Node* next; //下一个节点
};
void addNode(struct Node *node) //添加一个节点
{
current->next = node;
current = current->next;
}
void readNode(struct Node *first) //遍历链表
{
struct Node* temp = first;
while(temp != NULL)
{
printf("%d\n", temp->value);
temp = temp->next;
}
}
int main()
{
struct Node* first = (struct Node*)malloc(sizeof(struct Node));
first->value = 1;
first->next = NULL;
current = first;
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->value = 2;
node->next = NULL;
addNode(node);
node = (struct Node*)malloc(sizeof(struct Node));
node->value = 3;
node->next = NULL;
addNode(node);
node = (struct Node*)malloc(sizeof(struct Node));
node->value = 4;
node->next = NULL;
addNode(node);
readNode(first);
getchar();
return 0;
}
#include "malloc.h"
struct Node* current = NULL; //声明的一个全局变量表示的是链表的尾部
struct Node
{
int value;
struct Node* next; //下一个节点
};
void addNode(struct Node *node) //添加一个节点
{
current->next = node;
current = current->next;
}
void readNode(struct Node *first) //遍历链表
{
struct Node* temp = first;
while(temp != NULL)
{
printf("%d\n", temp->value);
temp = temp->next;
}
}
int main()
{
struct Node* first = (struct Node*)malloc(sizeof(struct Node));
first->value = 1;
first->next = NULL;
current = first;
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->value = 2;
node->next = NULL;
addNode(node);
node = (struct Node*)malloc(sizeof(struct Node));
node->value = 3;
node->next = NULL;
addNode(node);
node = (struct Node*)malloc(sizeof(struct Node));
node->value = 4;
node->next = NULL;
addNode(node);
readNode(first);
getchar();
return 0;
}
相关文章推荐
- java 单向链表的操作
- 面试题11 删除单向链表中的一个元素 但是只提供这个元素的操作权
- 数据结构-单向链表相关操作算法
- 使用C语言对单向链表的操作
- 数据结构学习之单向链表的基本操作(非递归实现)
- 单向链表操作
- 单向链表的有关操作(链式存储结构)
- C语言实现单向链表的常见操作
- 单向链表的基本操作
- C语言 单向链表的创建、遍历、排序、删除操作
- C语言单向链表的操作(持续更新中)
- 数据结构之链表与数组(二) -单向链表上的简单操作问题
- 数据结构--单向链表操作(C++版)
- 单向链表操作中的二级指针的运用
- 单向链表操作总结
- 单向链表操作详解(一)
- 基本单向链表的操作
- 单向链表操作详解(一)
- 我的单向链表的初始化、显示、反转操作
- 单向链表的基本操作及逆序实现