各位好:下面是个循环单链表不正确的程序,谁能告诉我怎么改成循环链表,最好把代码告诉我,谢谢。
#include<stdio.h>
#include<stdlib.h>
typedef struct CLNode{
int data;
struct CLNode *next;
}CLnode,*CLinkList;
CLinkList CreateList(CLinkList L,int value);
void Output(CLinkList L);
int main(void)
{
CLinkList L=NULL;
int i,n,value;
printf("Please input n: ");
scanf("%d",&n);
for(i=1;i<=n;i++) //n代表结点数量
{
scanf("%d",&value);
L=CreateList(L,value); //创建程序
Output(L); //输出连表
}
return 0;
}
CLinkList CreateList(CLinkList L,int value)
{
CLinkList newPtr,previousPtr,currentPtr;
newPtr=(CLinkList)malloc(sizeof(CLnode));
if(newPtr)
{
newPtr->data=value;
newPtr->next=NULL;
previousPtr=NULL;
currentPtr=L;
while(currentPtr && currentPtr->data < value)
{
previousPtr=currentPtr;
currentPtr=currentPtr->next;
if(currentPtr==NULL) //这是我个人的想法,不对
previousPtr->next=L;
}
if(previousPtr==NULL)
{
newPtr->next=L;
L=newPtr;
}
else
{
previousPtr->next=newPtr;
newPtr->next=currentPtr;
}
}
else
printf("Dynamic malloc memory failure.\n");
return L;
}
void Output(CLinkList L)
{
CLinkList currentPtr;
if(L)
{
currentPtr=L;
while(currentPtr!=L)
{
printf("%d->",currentPtr->data);
currentPtr=currentPtr->next;
}
printf("NULL\n\n");
}
}