C Program of reversing linked list using stack

This Program Shows the Demonstration of reversing linked list using stack.


 
#include< stdio.h>
#include< stdlib.h>
struct tag
{
int a;
struct tag *next;
};
int i=0;
struct tag *node,*p,*p1;
void create(struct tag *n)
{
char ch;
node=n;
printf("\n Do You Want to create(y/n)");
ch=getche();
while(ch!='n')
{
node->next=(struct tag*)malloc(sizeof(struct tag));
node=node->next;
printf("\n Value");
scanf("%d",&node->a);
printf("\nany more(y/n)");
ch=getche();
}
node->next=NULL;
}
void display(struct tag*n)
{
node=n;
if(node==NULL)
i=0;
else
i=1;
while(node)
{
printf("%4d",node->a);
node=node->next;
}
}
struct tag *push(struct tag *p1,struct tag *p2)
{
p2->next=p1;
return(p2);
}
struct tag *delF(struct tag *n)
{
node=n->next;
n->next=node->next;
return(node);
}
struct tag *pop(struct tag *n)
{
node=n->next;
free(n);
return node;
}
void main()
{
struct tag *n,*link,*stack,*p;
clrscr();
link=NULL;
stack=NULL;
create(link);
printf("\nList is as follows\n");
display(link->next);
do
{
p=delF(link);
stack=push(stack,p);
}while(link->next!=NULL);
printf("\nList is as follows (after converting it into a stack.)");
display(link->next);
if(i==0)
printf("\nNo element in the list.");
printf("\nStack is\n");
display(stack);
n=link;
while(stack!=NULL)
{
n->next=stack;
n=n->next;
stack=pop(stack);
}
printf("\nStack is as follows (after converting it into a linked list.)");
display(stack);
if(i==0)
printf("\nNo element in the stack.");
printf("\nFinal list\n");
display(link->next);
getch();
}

Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!