求教:单向循环链表
为什么单向循环链表输出值不对啊~无法实现循环输出?
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *pNext;
}NODE, *PNODE;
PNODE Create_List();
void show_list(PNODE);
int main(void)
{
PNODE pHead = Create_List();
show_list(pHead);
return 0;
}
PNODE Create_List()
{
int i;
PNODE pHead = (PNODE)malloc(sizeof(NODE));
PNODE p = pHead;
if (NULL == p)
{
printf("内存分配失败");
exit(-1);
}
p->pNext = NULL;
for (i=0; i<5; i++)
{
printf("第%d个元素是:", i+1);
scanf("%d", &p->data);
PNODE q = (PNODE)malloc(sizeof(NODE));
if (NULL == q)
{
printf("内存分配失败");
exit(-1);
}
q->pNext = NULL;
p->pNext = q;
p = q;
}
p->pNext = pHead;
return pHead;
}
void show_list(PNODE pHead)
{
int i;
PNODE p = pHead;
for (i=0; i<6; i++)
{
printf("%3d", p->data);
p = p->pNext;
}
}
[解决办法]
单步调试
[解决办法]
你新建的是单链表,不是单向循环链表!当然不能循环输出了,把上面建立链表的for改成i<6或者输出链表的for改成i<5.