IMPLEMENTATION OF STACK ADT USING LINKED LIST

SPURCE CODE:

“Lstack.h” File:

#include<stdio.h>
#include<conio.h>
struct node
{
   int data;
   struct node *next;
};
typedef struct node *ptrToNode;
typedef ptrToNode STACK;
typedef ptrToNode POSITION;

STACK Create(void)
{
   STACK S;
   S=(struct node*)malloc(sizeof(struct node));
   if(S==NULL)
      printf ("not created");
   else
   {
      S->next=NULL;
      printf("stack is created successfully");
   }
return S;
}
void Push(int x, STACK S)
{
   ptrToNode temp;
   temp=(struct node*)malloc(sizeof(struct node));
   if(temp==NULL)
      printf("fatal error");
   else
   {
      temp->data=x;
      temp->next=S->next;
      S->next=temp;
   }
}
void Pop(STACK S)
{
   ptrToNode first;
   if(Isempty(S))
      printf("no element to pop");
   else
   {
      first=S->next;
      S->next=S->next->next;


      free(first);
   }
}
void MakeEmpty(STACK S)
{
   if(S==NULL)
   printf("stack is not created");
   else
   {
      while(!Isempty(S))
      Pop(S);
   }
}
STACK Dispose(STACK S)
{
   MakeEmpty(S);
   free(S);
   S=NULL;
   return S;
}
POSITION Top(int x, STACK S)
{
   if(S->next->data==x)
   return S->next;
   else
   return NULL;
}
int Isempty(STACK S)
{
   return S->next==NULL;
}
void Display (STACK S)
{
   S=S->next;
   while(S!=NULL)
   {
      printf("\n%d",S->data);
      S=S->next;
   }
}


“Lstack.c” File:

#include<stdio.h>
#include<conio.h>
#include"Lstack.h"
void main()
{
STACK S=NULL;
int n,i,c;
clrscr();

printf("\n\n1.createStack\n2.push\n3.pop\n4.make empty\n5.dispose\n6.display\n7.Top\n8.exit\n");
x:
{
printf("\n\nEnter the option:\t");
scanf("%d",&n);
switch(n)
{
case 1:
    if(S==NULL)
       S=Create();
    else
       printf("\n\nstack is already created");
    break;
case 2:
    if(S==NULL)
       printf("\n\nstack is not created");
    else
    {
       printf("enter the element:\t");
       scanf("%d",&i);
       Push(i,S);
    }
    break;
case 3:
    if (S==NULL)
       printf ("\n\nstack is not yet created");
    else
       Pop(S);
    break;
case 4:
    if(S==NULL)
       printf("\n\nstack is not yet created");
    else
       MakeEmpty(S);
       printf("\n the stack is empty");
    break;
case 5:
    if (S==NULL)
       printf("\n\nnot created");
    else
       S=Dispose(S);
       printf("\n\n The stack is Deleted");
    break;
case 6:
    if(S==NULL)
       printf("not created");
    else
       printf("\n the elements present in stack are:\n");
       Display(S);
    break;
case 7:
    if (S==NULL)
       printf ("\nStack is not created");
    else
    {
       printf("\nEnter to check the top element:\t");
       scanf("%d",&c);
       if(Top(c,S)==NULL)
       printf("\n NO");
       else
       printf("\n YES Top Element is :\t%d",c);
    }
    break;
case 8:
    exit(0);
default:
    printf("\n\n........wrong entry.........");
}
goto x;
}
}


OUTPUT:

1.CreateStack
2.Push
3.Pop
4.MakeEmpty
5.Dispose
6.Display
7.Top
8.Exit
Enter the option:    1
Stack is created Successfully

Enter the option:    2
Enter the element:    100

Enter the option:    2
Enter the element:    200

Enter the option:    2
Enter the element:    300

Enter the option:    6
The Elements present in the stack are:
300
200
100

Enter the option:    7
Enter to check the top element:    300
Yes Top element is 300

Enter the option:    3
Enter the option:    6
The Elements present in the stack are:
200
100

Enter the option:    7
Enter to check the top element:    300
No

Enter the option:    4
 The stack is empty

Enter the option:    5
The stack is deleted

Enter the option:    8
Previous
Next Post »

If you still didn't find what you're looking for, you can search this website below: