adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C and C++ to implement stack Using Structure and Class.


Program in C and C++ to implement stack Using Structure and Class.


C PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
  struct stack
   {
      int data;
      struct stack *next;
   };
    typedef struct stack stack;
    stack *top=NULL;
    void push(int d)
     {
       stack *temp;
       temp=(stack*)malloc(sizeof(stack));
       if(temp==NULL)
        {
         printf("\nStact overflow");
         return;
            }  
         temp->data=d;
         temp->next=top;
         top=temp;
  }
    void pop()
      {
       stack *temp;
       if(top==NULL)
         { 
           printf(" Stack is empty.");
           return;
   }
  temp=top;
  printf("  %d",temp->data);
  top=temp->next;
  free(temp); 
   }
    void show()
     {
       stack *temp;
       if(top==NULL)
         { 
           printf(" Stack is empty.");
           return;
   }
  temp=top;
  while(temp!=NULL)
   {
     printf(" %d",temp->data);
     temp=temp->next;
   }
  }
    int main()
    {
     
     push(7);
 push(9);
     push(79);
     push(17);
 push(93);
     push(49);
     printf("Elements of stack ");
     show();
     printf("\nDelete data is ");
     pop();
     return 0;
 }
C++ PROGRAM
#include<iostream>
#include<conio.h>
#include<stdlib.h>
 using namespace std;
  class stack
   {
     private:
      class Node{
        public:
         int data;
         Node *next;
   };
  Node *top=NULL;
    int isEmpty()
     {
       return top==NULL?0:1;
     }
   public:
     stack()
       {
        top=NULL;
        
    }
   ~stack(){ delete top;}
    void push(int d)
         { 
           Node *temp,*newNode;
           newNode=new Node[sizeof(Node)];
           newNode->data=d;
           newNode->next=NULL;
            if(top==NULL)
           top=newNode;
         else{
          newNode->next=top;
          top=newNode;    
      }
         }
         
     int pop()
        {
           int d;
           Node *temp;
           if(!isEmpty())
              cout<<"\nStack is underflow.";
           else{
            temp=top;
            d=temp->data;
            top=temp->next;
            free(temp);
    }
    return d;
     }
   void show()
      {
         Node *temp;
         if(!isEmpty())
              cout<<"\nStack is underflow.";
         else{
       temp=top;
       while(temp!=NULL)
        {
         cout<<"  "<<temp->data;
         temp=temp->next;
     }
      }
   } 
  int Length()
   {
    Node *temp;
    int count=0;
   temp=top;
   while(temp!=NULL)
    {
         count++;
      temp=temp->next;
     }
   return count;
   }
   
   };
   int main()
    {
     int i,n,ch;
     stack s1;
     s1.push(56);
     s1.push(33);
     s1.push(12);
     s1.push(25);
     cout<<"Elements of stack ";
     s1.show();
     cout<<"\nTotal number of stack elements = "<<s1.Length()<<"\n";
     cout<<"\nDeleted data "<<s1.pop()<<"\n";
     cout<<"Elements of stack ";
     s1.show();
     return 0;
   }
Output :-





Post a Comment

0 Comments