数据结构 -- 单链表创建之传地址创建新的链表

来源:互联网 发布:四海认证淘宝渔具钓箱 编辑:程序博客网 时间:2024/05/21 01:08

1.创建了一个头节点

2.通过传地址 来修改 增加原来空链表的值

3.通过尾插法来创建链表

以下代码在 vs2010 测试通过:

#include "stdafx.h"#include <stdlib.h>#include <stdio.h>#define FALSE 0#define TRUE 1typedef struct NODE{struct NODE *plink;int value;}Node; int create_link(Node **list,int length){int input, i ;Node *new_node;Node *current;current = *list;for(i = 0 ; i< length ;i++){printf("请输入第 %d 个数字.\n",i+1);scanf("%d",&input);new_node = (Node *)malloc(sizeof(Node));if(new_node == NULL){return FALSE;}new_node->value = input;current->plink = new_node;current = new_node;}new_node->plink = NULL;return TRUE;}int main(void){int sign,length;Node *list;Node *ptmp;printf("请输入创建的链表的数量:\n");scanf("%d",&length);if(length <=0 ){return FALSE;}//头节点list = (Node *)malloc(sizeof(Node));list->value = length;if(list == NULL){return FALSE;}sign = create_link(&list,length);if(sign == 1){printf("创建的链表是:\n");ptmp = list->plink;while(ptmp != NULL){printf("%2d",ptmp->value);ptmp = ptmp->plink;}system("pause");return TRUE;}else{return FALSE;}}



0 0
原创粉丝点击