// 链表Demo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <malloc.h>
#include <stdlib.h>
/*
定义结构体
*/
struct Node
{
int number;//数据域
struct Node *pNext;//指针域
};
//声明函数
struct Node * NodecreatList(void);
void PutNodeList(struct Node *);
int main(void)
{
struct Node *pHead;
pHead=NodecreatList();//创建头结点单表链
PutNodeList(pHead);
return 0;
}
struct Node *NodecreatList()
{
int len;
int val;
struct Node *pHead;
pHead=(struct Node*)malloc(sizeof(struct Node));
if (NULL==pHead)
{
printf("分配失败!程序终止.....");
exit(-1);
}
struct Node * pTail=pHead;
pTail->pNext=NULL;
printf("请输入链表长度:\n");
scanf("%d",&len);
for (int i=0;i<len;i++)
{
printf("请输入第%d个数:",i+1);
scanf("%d",&val);
struct Node *pNew=(struct Node *)malloc(sizeof(struct Node));
if (NULL==pNew)
{
printf("分配失败!程序终止.....");
exit(-1);
}
pNew->number=val;
pTail->pNext=pNew;
pNew->pNext=NULL;
pTail=pNew;
}
return pHead;
}
void PutNodeList(struct Node *pHead)
{
struct Node *p=pHead->pNext;
while(p!=NULL)
{
printf("%d\n",p->number);
p=p->pNext;
}
printf("\n");
return;
}