#include
typedef struct LNode//注意这里用typedef定义数据类型
{ int data;
struct LNode *next;//注意这里有struct关键字
}LNode,*LinkList;
LinkList creat(LinkList list)
{ //尾插法建立单链表
char flag;
LinkList p,r;
list=malloc(sizeof(LNode));
r=list;
while(1)
{ printf("请输入数据:");
p=malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=NULL;
r->next=p;
r=p;
printf("是否继续输入(Y,是 N否)");
flag=getchar();//接受回车键
flag=getchar();
if(flag=='N')
break;
}
return list;
}
LinkList reverse(LinkList list)
{ LinkList p,q,r;
if(list==NULL||list->next==NULL)
return list;
else
{ p=list->next;
list->next=NULL;
r=list;
while(p!=NULL)
{ q=p;
p=p->next;
q->next=r->next;
r->next=q;
}
}
return list;
}
void output(LinkList list)
{ LinkList p=list;
while(p->next!=NULL)
{ printf("%d",p->next->data);
p=p->next;
}
}
void main()
{ LinkList list=NULL;
list=creat(list);
printf("原始数据是:\n");
output(list);
list=reverse(list);
printf("\n转置后的数据是:\n");
output(list);
printf("\n");
}
阅读(1325) | 评论(0) | 转发(0) |