Stack using linked list

 


Stack Using linked list





OURSHOPKEEPER













#include <iostream>
using namespace std;

void pop();
void push();
void display();

#define SIZE 10

int stack[SIZE];

struct Node
{
   int data;
   struct Node *next;
} *top = NULL;

void push()
{
   int value;
   cout << "Enter the element you want to insert: " << endl;
   cin >> value;
   struct Node *newNode;
   newNode = (struct Node *)malloc(sizeof(struct Node));
   newNode->data = value;
   if (top == NULL)
      newNode->next = NULL;
   else
      newNode->next = top;
   top = newNode;
   cout << "\nInsertion is Successful\n"
        << endl;
}
void pop()
{
   if (top == NULL)
      cout << "\nUnderflow. Stack is empty\n";
   else
   {
      struct Node *temp = top;
      cout << "\nDeleted element is: " << temp->data << endl;
      top = temp->next;
      free(temp);
   }
}

void display()
{
   if (top == NULL)
   {
      cout << "Queue is empty" << endl;
   }
   else
   {
      struct Node *temp = top;
      while (temp != NULL)
      {

         cout << temp->data << " ";
         temp = temp->next;
      }
   }
}

int main()
{
   int choice;
   while (1)
   {
      cout << "\n1.push\n2.pop\n3.display\n4.exit" << endl;
      cin >> choice;
      switch (choice)
      {
      case 1:
         push();
         break;
      case 2:
         pop();
         break;
      case 3:
         display();
         break;
      case 4:
         exit(0);
         break;
      default:
         cout << "invalid" << endl;
      }
   }
   return 0;
}






    


Click on the above button to download the code.



Post a Comment

If you have furthur QUERIES please let us know

Previous Post Next Post