#include
#include
struct Node * create_list(void);
void traverse_list(struct Node *);
struct Node
{
int data;
struct Node * pNext;
};
int main(void)
{
struct Node * pHead = NULL;
pHead = create_list();
traverse_list(pHead);
return 0;
}
struct Node * create_list(void)
{
int val;
int i;
int len;
struct Node * pHead = (struct Node *)malloc(sizeof(struct Node));
if (pHead == NULL)
{
printf("分配失败,程序中止!\n");
exit(-1);
}
struct Node *pTail = pHead;
pTail->pNext = NULL;
printf("请输入您要生成的链表的节点的个数:len = ");
scanf ("%d", &len);
for (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("分配失败,退出程序!\n");
exit(-1);
}
pNew->data = val;
pTail->pNext = pNew;
pNew->pNext = NULL;
pTail = pNew;
}
return pHead;
}
void traverse_list(struct Node *pHead)
{
struct Node * p = pHead->pNext;
while ( p != NULL)
{
printf("%d\n", p->data);
p = p->pNext;
}
}
vc++你新建的什么类型的程序?是控制台么?不要删除默认的代码中的include stdafx.h
把你的代码贴在下面。
如果还不行,贴出错误
将main函数放代码最后吧