Connect to

facebook google Email you tube you tube you tube
Theme images by MichaelJay. Powered by Blogger.

Projects

23.7.20

Implementation of Stacks Using Linked List in C++ and C

As we have discussed about Static Stacks in one of our previous article where we also talked about the definition of stacks ie Stacks are kind of non primitive data structure where NON Primitive means derived from basic data types.

In the article we will be studying about Dynamic Stacks. Now when the word Dynamic comes it means we will be talking about implementation of stacks using Linked List.

For a better understanding of our video on Dynamic Stacks you should know about the traversal of linked list which is available here.

Before Starting  I would like to make a few points clear. Which are :

  1. What is a Linked List?
  2. Difference between Linked List and Array.
  3. Differences between Static Stacks and Dynamic Stacks. 
1. Linked List :
A linked list is a set data collection (each called a NODE) each containing the address of its successor or predecessor or both depending on the type of the linked list.
In the above picture we see a Singular linear linked list where each node contains the address of the successive node.
For More information about Basic concept anf Traversal of Linked List click here.

2. We will be going through the major difference only in this article which is that in case of an array we have random access memory moreover the size of the array has to be mentioned before which makes it static and also the wastage of memory is also a disadvantage of arrays. On the contrary in case of linked list the memory allocation is Dynamic and so we don't have to mention the size and also no wastage of memory. This concept would be more clear in our article of linked list.

3. Difference between Static Stacks and Dynamic Stacks.
In case of Static Stacks we have to mention the size first and so it makes it a constraint the no of elements is fixed.
Whereas in case of dynamic stacks the constraint of size is not a problem as the size is not fixed and so we can push(insert) as many elements into the stack as we want.

Now Coming to our topic



We have the source code in C++

// Dynamic Stacks

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>


struct node
 { int adno;
   char name[20];
   float mks;
   node *link;
   };
node *top;

void push()
 { node *ptr;
   ptr=new node;
   if(ptr==NULL)
    { cout<<"Overflow";
      exit(0);
      }
   cout<<"\nEnter ADNO ";cin>>ptr->adno;
   cout<<"\nEnter Name ";gets(ptr->name);
   cout<<"\nEnter Marks ";cin>>ptr->mks;
   ptr->link=NULL;
   if(top==NULL)
    top=ptr;
   else
    {ptr->link=top;
     top=ptr;}
     }


void pop()
 { if(top==NULL)
    {cout<<"Under Flow";
     exit(0);
      }
   node *ptr;
   ptr=top;
   top=top->link;
   ptr->link=NULL;
   cout<<"Deleted\n";
   cout<<"\nAdmission Number "<<ptr->adno;
   cout<<"\nNAme "<<ptr->name;
   cout<<"\nMarks "<<ptr->mks;
   delete ptr;
   }

void display()
 { if(top==NULL)
    {cout<<"No Elements to Display";
     getch();
     exit(0);
     }
    node *ptr;
    ptr=top;
    while(ptr!=NULL)
     {  cout<<"\nAdmission Number "<<ptr->adno;
cout<<"\nNAme "<<ptr->name;
cout<<"\nMarks "<<ptr->mks;
cout<<'\n';
ptr=ptr->link;
}
  }


void main()
 {
   char ch;
   do {clrscr();
       cout<<"\nPush an Element     1";
       cout<<"\nPop an Element      2";
       cout<<"\nDisplay Stack       3";
       cout<<"\nExit                4";
       int n;
       cin>>n;
       switch(n)
{ case 1:{ clrscr();
  push();}break;

 case 2:{ clrscr();
  pop();}break;
 case 3:{ clrscr();
  display();}break;
 case 4:exit(0);

 }cout<<"Want to Continue? ";cin>>ch;
   }while(ch=='Y' || ch=='y');
  getch();
  }

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,


Source Code in C


#include<conio.h>
#include<stdio.h>
#include<process.h>

struct node
 { int adno;
   struct node *link;
   };
#define NODE struct node

NODE *top;

void push()
 { NODE *ptr=(NODE*)malloc(sizeof(NODE));
   if(ptr==NULL)
    { printf("Overflow");
      getch();
      exit(0);
      }
   printf("\nEnter ADNO ");
    scanf("%d",&ptr->adno);
   ptr->link=NULL;
   if(top==NULL)
    top=ptr;
   else
    {ptr->link=top;
     top=ptr;}
     }


void pop()
 { NODE *ptr=(NODE*)malloc(sizeof(NODE));

  if(top==NULL)
    { printf("Under Flow");
     exit(0);
      }

    ptr=top;
    top=top->link;
    ptr->link=NULL;
   printf("\nDeleted\n");
   printf("\nAdmission Number %d ",ptr->adno);
   free(ptr);
   }

void display()
 { NODE *ptr=(NODE*)malloc(sizeof(NODE));
   if(top==NULL)
    { printf("\nNo Elements to Display");
      getch();
      exit(0);
    }
    ptr=top;
    while(ptr!=NULL)
     {  printf("\nAdmission Number %d",ptr->adno);

ptr=ptr->link;
     }
  }


void main()
 {
   int ch,n;
   do {clrscr();
       printf("\nPush an Element     1");
       printf("\nPop an Element      2");
       printf("\nDisplay Stack       3");
       printf("\nExit                4\n");
       scanf("%d",&n);
       switch(n)
 { case 1:{ clrscr();
   push();
 }break;

   case 2:{ clrscr();
   pop();
 }break;
   case 3:{ clrscr();
   display();
 }break;
   case 4:exit(0);

   }printf("\nWant to Continue?(yes=1) ");
     scanf("%d",&ch);
   }while(ch==1);
  getch();
  }


No comments:
Write comments

Interested in our works and services?
Get more of our update !