您的位置:首页 > 其它

单向链表的操作

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: