找了很久都找不到错误的原因!
#include<stdio.h>#include<stdlib.h>
typedef struct LinkList
{
int data;
struct LinkList *next;
}node;
int main()
{
node *pHead=NULL;
char a;
printf("1、查看链表\n2、创建链表(尾插法)\n3、链表长度\n4、中间结点值\n0、退出\n请选择你的操作:");
while(a!='0')
{
scanf("%c",&a);
switch(a)
{
case '1':locate(pHead);
printf("\n");
break;
case '2':printf("输入数据:");
printf("\n");
createLinkList(&pHead);
locate(pHead);
printf("\n");
break;
case '3':printf("链表长度为:%d\n",lenght(pHead));
break;
case '4':printf("中间值为:%d\n",getmidnode(pHead));
break;
case '0':exit(0);
}
}
}
void locate(node *pnode)
{
node *temp;
temp = pnode;
if((pnode)==NULL)
exit(0);
printf("*************链表中的元素*************************\n");
do
{
printf("%4d",temp->data);
}while((temp = temp->next) !=pnode);
printf("\n");
}
void createLinkList(node **pnode)
{
node *target;
int elem;
node *temp;
while(1)
{
temp=(node *)malloc(sizeof(struct LinkList));
scanf("%d",&elem);
fflush(stdin);
temp->data=elem;
if(elem==0)
return;
if((*pnode)==NULL)
{
*pnode = (node*)malloc(sizeof(struct LinkList));
if(!(*pnode))
exit(0);
(*pnode)->data=temp->data;
target=*pnode;
}
else
{
target->next=temp;
target->data=temp->data;
target->next=NULL;
}
}
}
int lenght(node *pnode)
{
node *target;
int j;
j=1;
for(target=pnode;target->next!=NULL;j++)
target=target->next;
if(pnode=NULL)
return 0;
else
return j;
}
int getmidnode(node *pnode)
{
int j;
node *man;
node *kuai;
if(pnode==NULL)
return 0;
for(man=kuai=pnode;kuai->next->next!=NULL;kuai=kuai->next->next,man=man->next)
{
j=man->data;
}
return j;
}
求指教,谢谢!!
能给个错误提示?
页:
[1]