Saturday, November 5, 2011

Reverse link list using stack


#include <conio.h>
#include<stdio.h>
#define MAX 9
int top=-1;
int stack[10];
void push(int a)
{
if(top==MAX)
{
printf("over flow.......");
 }
 else
{
 top=top+1;
 stack [top]=a;
}
}
int pop()
{
 int n;
 if(top==-1)
 {
 printf("under flow.....\n");
 }
 else
 {
 n=stack[top];
 top=top-1;
 }
return n;

}

struct node
{
int info;
struct node *next;
};
typedef struct node NODE;
NODE * makenode(int item)
{
NODE *ptr;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->next=NULL;
ptr->info=item;
return ptr;
}
void add_beg(NODE **s,int item)
{
NODE *p;
p=(NODE *)makenode(item);
p->next=*s;
*s=p;
}

void display(NODE *s)
{
NODE *ptr;
ptr=s;
while(ptr!=NULL)
{
printf("Address %u \tValue %u \tNext %u\n",ptr,ptr->info,ptr->next);
ptr=ptr->next;
}
}

int del_beg(NODE **s)
{
int r=0;
if(*s==NULL)
{
printf("Under flow");
}
else
{
r=(*s)->info;
*s=(*s)->next;
}
return(r);
}

void main()
{
NODE *s;
int i,item;
s=NULL;

for(i=1;i<=10;i++)
{
printf("Enter your new number:");
scanf("%d",&item);
add_beg(&s,item);
}
display(s);
for(i=1;i<=10;i++)
{
push(del_beg(&s));
}
for(i=1;i<=10;i++)
{
add_beg(&s,pop());
}
display(s);
}

0 comments:

Post a Comment

You are most welcome for making comments here. I will consider your view and will reply you soon.