24 bytes

  • HOME
  • Algorithms
    blue
  • Bubble Sort
  • Bucket Sort
  • Insertion Sort
  • Selection Sort
  • Merge Sort
  • Quick Sort
  • Heap Sort
  • Counting Sort
  • Stack
  • Queue
  • Double Linked List
  • Binary Search Tree
  • Towers OF Hanoi

  • Java Programs
    green bullet
  • Download RMI Calculator
  • Download file
  • Clock applet
  • File Upload
  • JDOM Parser
  • Client server
  • Udp Client server
  • Sudoku Solver

  • System Programming
  • Newtwork Sniffer
  • Good Links
    green bullet
  • Free Source Code
  • Top Coder
  • Code Project
  • Learn Today
  • Concept
  • Intresting Programs
    green bullet
  • Prime Number
  • GCD Euclids
  • Permutations


  • Google
     
    Web 24bytes.com


    // Stack program in c++
    #include <stdio.h>
    #include <conio.h>
    #include <iostream.h>
    #include <stdlib.h>
    
    class element   
    {
    public:
    int value;
    element* next;
    };//class element
    
    class stack 
    {
    public:
    int size;
    element* current;
    
    stack()
    {
      size=0;
      current=NULL;
    }//default constructor
    
    bool push(int,element*);
    bool pop();
    bool isEmpty();
    int getStackSize();
    void printStackSize();
    void printStackElements(element*);
    void printStackMenu();
    };
    
    bool stack::push(int ele,element* temp)
    {
    	temp=new element;
    	if(current==NULL)
    	{
    		temp->next=NULL;
    	}
    	else
    	{
    		temp->next=current;
    	}
    	temp->value=ele;
    	current=temp;
    	printf("%d inserted\n\n",ele);
    	size++;
        return false;
    }
    
    bool stack::pop()
    {
    	if(isEmpty())
    	{
    	cout<<"\nStack is Empty\n";      
    	return false;
    	}
    	else
    	{
    	cout<<"\n Element To POP :"<<current->value;
    	cout<<"\n Before POP";
    	printStackElements(current);
    	current=current->next;
    	cout<<"\n After POP";
    	printStackElements(current);
    	size=size--;
    	}
    	return true;
    }
    
    bool stack::isEmpty()
    {
    	if(getStackSize()==0)
    	return true;
    	
    	return false;
    }
    
    int stack::getStackSize()
    {
    return size;
    }//returns size of the stack
    
    void stack::printStackSize()
    {
    cout<<"\nThe Size of the Stack:"<<size<<"\n";
    }//print the stack size
    
    void stack::printStackElements(element* base)
    {
         	element* curr2;
    		curr2= base;
    		cout<<"\n-----\n";
    		cout<<"STACK\n";
    		cout<<"-----\n";
    		while(curr2!=NULL)
    		{
    			cout<<" |"<<curr2->value<<"|\n";
    			curr2=curr2->next;
    		}
    }// print the stack
    
    void stack::printStackMenu()
    {
    cout<<"Welcome to Stack \n";
    cout<<"1.Push an element\n";
    cout<<"2.Pop an element\n";
    cout<<"3.Display Stack\n";
    cout<<"4.Size Of Stack\n";
    cout<<"5.Exit\n";
    }
    
    void main()
    {
    stack st;
    char Option=0;
    int val;
    	while(1)
    	{
    		st.printStackMenu();
    		cin>>Option;
    			switch(Option)
    			{
    			case '1':
    					cout<<"Enter a Number \n";
    					cin>>val;
    					st.push(val,st.current);
    					break;
    			case '2':
    					st.pop();
    					break;
    
    			case '3':
    					st.printStackElements(st.current);
    					break;
    
    			case '4':
    					st.printStackSize();
    				
    					break;
    			case '5':
    					exit(0);
    					break;
    			}
    	}
    }
    
    
    
    . original template by Aran Down.