/*************************************
--程序描述: 实现链表逆转
--修改日期: 2009.5.29
--修改人: 吴强
--修改原因: 从前写的程序不够规范
--输入要求:
**************************************/
#include
#include
#include
typedef struct list
{
char date;
struct list *next;
}node;
typedef struct stack
{
node *N;
struct stack *next;
}Stack;
node *InitList()
{
char c;
node *head;
node *r;
node *s;
head=(node *)malloc(sizeof(node));
head->next=NULL;
r=head;
c=getchar();
while (c!=' ')
{
s=(node *)malloc( sizeof(node) );
s->date= c;
s->next= NULL;
r->next= s;
r= s;
c=getchar();
}
return head;
}
node *NZlist(Stack *stack)
{
node *h;
node *r;
node *q;
extern Stack *Pop(Stack *stack);
Stack *s;
if ( StackEmpty(stack) )
{
printf("stack kong");
exit(0);
}
s=Pop(stack);
h=s->N;
stack=s->next;
r=h;
while( !StackEmpty(stack) )
{
s=Pop(stack);
q=s->N;
stack=s->next;
r->next=q;
r=q;
}
r->next=NULL;
return h;
}
Stack *Push(Stack *stack,node *z)
{
Stack *p;
p=(Stack *)malloc( sizeof(Stack) );
p->N=z;
p->next=stack;
stack=p;
return stack;
}
Stack *Pop(Stack *stack)
{
Stack *p;
p=stack;
stack=stack->next;
return p;
}
int StackEmpty(Stack *stack)
{
if ( stack==NULL )
{
return 1;
}
else
{
return 0;
}
}
main()
{
node *head;
node *q;
Stack *stack;
void putlist(node *head);
head=InitList();
stack=NULL;
q=head->next;
putlist(head);
while(q!=NULL)
{
stack=Push(stack,q);
q=q->next;
}
head->next=NZlist(stack);
putlist(head);
}
void putlist(node *head)
{
node *r;
r=head->next;
while ( r!=NULL )
{
printf("%c",r->date);
r=r->next;
}
printf("/n");
}
逆转链表(栈)
最新推荐文章于 2020-03-30 23:23:12 发布
2815

被折叠的 条评论
为什么被折叠?



