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

1.创建了一个头节点

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

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

以下代码在 vs2010 测试通过:

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>

#define FALSE 0
#define TRUE 1

typedef 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值