#include
#include
#include
typedef struct Node
{
int data;
struct Node* pNext;
}*PNode,Node;
PNode creat_list(void);
void show_list(PNode pHead);
int main(void)
{
PNode pHead=NULL;
pHead=creat_list();
show_list(pHead);
return 0;
}
PNode creat_list(void)
{
int i;
int len;//链表长度
int val;//链表里的值
PNode pHead=(PNode)malloc(sizeof(Node));
if(pHead==NULL)
{
printf("No");
exit(-1);
}
PNode pTail=pHead;
pTail->pNext=NULL;
printf("请输入您需要生成的链表节点的个数:len=");
scanf("%d",&len);
for(i=0; iprintf("请输入第%d 个节点的值:",i+1);
scanf("%d",&val);
PNode pNew=(PNode)malloc(sizeof(Node));
if(pNew==NULL)
{
printf("No");
exit(-1);
}
pNew->data=val;
pTail->pNext=pNew;
pNew->pNext==NULL;
pTail=pNew;//尾插法
}
return pHead;
}
void show_list(PNode pHead)
{
PNode p=pHead->pNext;
while(NULL!=p)
{
printf("%d ",p->data);
p=p->pNext;
}
printf("\n");
return 0;
}
改动处见注释,供参考:
#include <stdio.h>
#include <stdlib.h>
//#include
typedef struct Node
{
int data;
struct Node* pNext;
}*PNode,Node;
PNode creat_list(void);
void show_list(PNode pHead);
int main(void)
{
PNode pHead=NULL;
pHead=creat_list();
show_list(pHead);
return 0;
}
PNode creat_list(void)
{
int i;
int len;//链表长度
int val;//链表里的值
PNode pHead=(PNode)malloc(sizeof(Node));
if(pHead==NULL)
{
printf("No");
exit(-1);
}
PNode pTail=pHead;
pTail->pNext=NULL;
printf("请输入您需要生成的链表节点的个数:len=");
scanf("%d",&len);
for(i=0; i < len; i++){
printf("请输入第%d 个节点的值:",i+1);
scanf("%d",&val);
PNode pNew=(PNode)malloc(sizeof(Node));
if(pNew==NULL)
{
printf("No");
exit(-1);
}
pNew->data=val;
pTail->pNext=pNew;
pNew->pNext=NULL; //pNew->pNext==NULL; 修改
pTail=pNew;//尾插法
}
return pHead;
}
void show_list(PNode pHead)
{
PNode p=pHead->pNext;
while(NULL!=p)
{
printf("%d ",p->data);
p=p->pNext;
}
printf("\n");
return; //return 0; 修改
}