该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
OK!@心眼观物
# include
# include
# include
typedef struct Node
{
int date;//数据域
struct Node * PNext;//指针域
}Node,*PNode;//Node == struct Node;PNode == struct Node * ;
//函数声明如下
PNode bliud_list();
void traver_list(PNode);
int main(void)
{
PNode PHead = bliud_list();
traver_list(PHead);
return 0;
}
PNode bliud_list()
{
int i, val, len;
PNode PHead = (PNode)malloc(sizeof(Node));//分配头结点
PNode PEnd = PHead;//尾节点
PEnd->PNext = NULL;
if (NULL == PHead)
{
printf("内存分配失败!程序终止");
exit(-1);
}
printf("请输入需要存储的数据数量:\nlen = ");
scanf_s("%d",&len,1);
for (i = 0; i < len; ++i)
{
PNode PNew = (PNode)malloc(sizeof(Node));
printf("请输入第%d个元素的数值:",i+1);
scanf_s("%d",&val);
if (NULL == PNew)
{
printf("内存分配失败!程序终止!");
exit(-1);
}
PNew->date = val;
PHead->PNext = PNew;
PNew->PNext = NULL;
PEnd = PNew;
}
return PHead;
}
void traver_list(PNode PHead)
{
/*
PNode p;
for (p = PHead->PNext; NULL != p; p = p->PNext)
printf("%3d",p->date);
printf("\n");
*/
PNode P = PHead->PNext;
while (NULL != P)
{
printf("%3d",P->date);
P = P->PNext;
}
printf("\n");
}
全部代码如上!
不胜感谢!