program-
#include< stdio.h>
#include< conio.h>
#include< alloc.h>
struct stack
{
int data;
struct stack *back;
};
struct stack *top;
struct stack *top2;
void create()
{
char ch;
struct stack *ptr,*cpt;
printf("Enter Number\n");
cpt=(struct node*)malloc(sizeof(struct stack));
scanf("%d",&cpt->data);
cpt->back=NULL;
do
{
printf("Enter Number\n");
ptr=(struct stack*)malloc(sizeof(struct stack));
scanf("%d",&ptr->data);
ptr->back=cpt;
cpt=ptr;
printf("Continue(y/n)?\n");
ch=getch();
}while(ch=='y');
top=cpt;
}
void traverse(struct stack *ptr)
{
printf("Stack is :\n");
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->back;
}
}
void reverse()
{
struct stack *ptr=top,*cpt,*temp;
temp=(struct stack*)malloc(sizeof(struct stack));
temp->data=ptr->data;
temp->back=NULL;
ptr=ptr->back;
while(ptr!=NULL)
{
cpt=(struct stack*)malloc(sizeof(struct stack));
cpt->data=ptr->data;
cpt->back=temp;
temp=cpt;
ptr=ptr->back;
}
top2=temp;
}
void main()
{
clrscr();
create();
traverse(top);
reverse();
printf("After reverse\n");
traverse(top2);
getch();
}
Enter Number
3
Enter Number
4
Continue(y/n)?
Enter Number
7
Continue(y/n)?
Enter Number
1
Continue(y/n)?
Enter Number
6
Continue(y/n)?
Stack is :
6 1 7 4 3
After reverse
Stack is :
3 4 7 1 6