线性表 输出链表中的最大值和最小值 ?代码错在哪里
# include
# include
typedef struct node
{
int data;
struct node *next;
}Node,*pnode;
pnode creatlist()
{
int i,k,j=0;
pnode Head =(pnode)malloc(sizeof(Node));
Head->next= NULL;
scanf("%d",&i);
while(j
{
pnode pnew = (pnode)malloc(sizeof(Node));
scanf("%d ",&k);
pnew->data=k;
pnew->next=Head->next;
Head->next = pnew;
Head = pnew;
j++;
}
return Head;
}
void traverlist(pnode l)
{
pnode p1= (pnode)malloc(sizeof(Node));
pnode p2= (pnode)malloc(sizeof(Node));
pnode p3= (pnode)malloc(sizeof(Node));
pnode p4= (pnode)malloc(sizeof(Node));
int t;
p1= l->next;
p2=l->next->next;
while(p2!=NULL)
{
if(p1->data>p2->data)
{
t=p1->data;
p1->data=p2->data;
p2->data=t;
}
p1=p1->next;
p2=p2->next;
}
printf("%d ",p1->next->data);
while(p4!=NULL)
{
if(p3->datadata)
{
int t = p3->data;
p3->data= p4->data;
p4->data= t;
}
p3=p3->next;
p4=p4->next;
}
printf("%d",p3->next->data);
}
void main()
{
pnode l=creatlist();
traverlist(l);
}
分享到:
------解决方案--------------------
# include
# include
typedef struct node
{
int data;
struct node *next;
}Node,*pnode;
pnode creatlist()
{
int i,k,j=0;
pnode Head =(pnode)malloc(sizeof(Node));
Head->next= NULL;
printf("Input element number:");
scanf("%d",&i);
while(j
{
printf("Input %dth element:",j);
pnode pnew = (pnode)malloc(sizeof(Node));
scanf("%d",&k);
pnew->data=k;
pnew->next=Head->next;
Head->next = pnew;
//Head = pnew;
j++;
}
return Head;
}
void traverlist(pnode l)
{
pnode p1;//= (pnode)malloc(sizeof(Node));
pnode p2;//= (pnode)malloc(sizeof(Node));
int t;
if(l->next==NULL)
return;
p1 = l->next;
t = p1->data;
while(p1->next != NULL)
{
if( t > p1->next->data )
{
t = p1->next->data;
}
p1=p1->next;
}
printf("Min:%d\n",t);
p2 = l->next;
t = p2->data;
while(p2->next != NULL)
{
if( t next->data)
{
t = p2->next->data;