Saturday, August 20, 2011

Stack implimentation using linked list

//Stack implimentation using linked list
#include<stdio.h>
#include<conio.h>
struct node
{
       int data;
       struct node *next;
};
main()
{
     struct node *tos;
     int i,c,n,j;
     void push(struct node **top,int item);
     extern int isfull();
     extern int isempty(struct node *top);
     int pop(struct node **top);
     tos=NULL;
     printf("Enter number of items to be pushed:");
     scanf("%d",&n);
     for(j=0;j<n;j++)
     {
      printf("Enter the item to be pushed:");
      scanf("%d",&i);
      push(&tos,i);
     }
     for(j=0;j<n;j++)
     {
     i=pop(&tos);
     }
     getch();
}
void push(struct node **top,int item)
{
     int t=0;
     struct node *temp=NULL;
     t=isfull();
     if(t==0)
     {
             printf("stack is already full.It is not possible to PUSH the given item");
             return;
     }
     temp=(struct node *)malloc(sizeof(struct node));
     temp->data=item;
     temp->next=*top;
     *top=temp;
}
int isfull()
{
    struct node *temp=NULL;
    temp=(struct node *)malloc(sizeof(struct node));
    if(temp==NULL)
     return 0;
    free(temp);
    return 1;
}
int pop(struct node **top)
{
    int i,c;
    struct node *t;
     c=isempty(*top);
     if(c==0)
     {
      printf("stack is empty.It is not possible to POP the item");  
      return 0;
     }
     i=(*top)->data;
     t=*top;

    *top=(*top)->next;
         free(top);
    printf("\npoped item is %d",i);
    return i;
}
int isempty(struct node *top)
{
    if(top==NULL)
     return 0;
    return 1;
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More