Showing posts with label Stack. Show all posts
Showing posts with label Stack. Show all posts

Stack Implementation Using Template - C++

#include<iostream>
using namespace std;
template<class t>
class stack
{
public:
    t *a;
    int top,i,size;
    stack(int siz)
    {
        a=new t[siz];
        size=siz;
        top=-1;
    }
                       
    void push(t ele)
    {
        if(top!=size-1){a[++top]=ele;}
    }
               
    t pop()     
    {
          return(a[top--]);
    }
                       
    void print()
    {
        for(i=0;i<top;i++)
            cout<<" "<<a[i];
                               
        cout<<"\n";
    }
               
};
                   
int main()
{   
    int i,ele;
    float el;
    char e;
    cout<<"\n\nINTEGER STACK:\n";
    stack<int> st1(10);
    for(i=1;i<=5;i++)
    {
        cout<<"\n\tpushed item:"<<i;
        st1.push(i);
    }
    cout<<"\n\n";
    for(i=0;i<5;i++)
    {
        ele=st1.pop(); 
        cout<<"\t\npoped Item: "<<ele;
    }
    cout<<"\n\nFLOAT STACK:\n";
    stack<float> st2(10);
    for(i=1;i<=5;i++)
    {
        el=(.05*i);
        cout<<"\n\tpushed item:"<<el;
        st2.push(el);
    }
    cout<<"\n\n";
    for(i=0;i<5;i++)
    {
        el=st2.pop(); 
        cout<<"\t\npoped Item: "<<el;
    }
    cout<<"\n\nCHARACTER STACK:\n";
    stack<char> st3(10);
    for(i=0;i<5;i++)
    {
        e=65+i;
        cout<<"\n\tpushed item:"<<e;
        st3.push(65+i);
    }
    cout<<"\n\n";
    for(i=0;i<5;i++)
    {
        e=st3.pop(); 
        cout<<"\t\npoped Item: "<<e;
    }
    cout<<"\n";
    return(1);
}

Output:
nn@ubuntu:~$ g++ ct1.cpp
nn@ubuntu:~$ ./a.out


INTEGER STACK:

    pushed item:1
    pushed item:2
    pushed item:3
    pushed item:4
    pushed item:5

   
poped Item: 5   
poped Item: 4   
poped Item: 3   
poped Item: 2   
poped Item: 1

FLOAT STACK:

    pushed item:0.05
    pushed item:0.1
    pushed item:0.15
    pushed item:0.2
    pushed item:0.25

   
poped Item: 0.25   
poped Item: 0.2   
poped Item: 0.15   
poped Item: 0.1   
poped Item: 0.05

CHARACTER STACK:

    pushed item:A
    pushed item:B
    pushed item:C
    pushed item:D
    pushed item:E

   
poped Item: E   
poped Item: D   
poped Item: C   
poped Item: B   
poped Item: A
nn@ubuntu:~$
 


Stack Using Linkedlist - Datastructure - JAVA

Program:
import java.io.*;
class Node
{
    public int data;
    public Node next;
    public Node(int x)
    {
        data=x;
    }
    public void display()
    {
        System.out.print(data+" ");
    }
}
class stacker
{
    public Node first;
    public stacker()
    {
        first=null;
    }
    public boolean isEmpty()
    {
        return(first==null);
    }
    public void push(int value)
    {
        Node newnode=new Node(value);
        newnode.next=first;
        first=newnode;
    }
    public int pop()
    {
        if(isEmpty())
         {
            System.out.println("\n Stack Empty...");
            return 0;
         }
        else
         {
            int r1=first.data;
            first=first.next;
            return r1;
         }
       
    }
    public void display()
    {
        Node current=first;
        while(current!=null)
         {
            System.out.print(" "+current.data);
            current=current.next;
         }
        System.out.println("");
    }
}
class stack_list
{
public static void main(String arg[]) throws IOException
{
    stacker a1=new stacker();
    DataInputStream x=new DataInputStream(System.in);
    System.out.print("\n\t\t<<STACK USING LINKEDLIST>>\n\nStack ready...\n\nEnter number of elements to push: ");
    int n1=Integer.parseInt(x.readLine());
    System.out.println("\nEnter elements: ");
    for(int i=0;i<n1;i++)
     {
        a1.push(Integer.parseInt(x.readLine()));
     }
    System.out.println("\nDo you want to see the stack items?\n\t1.Yes 2.No");
    switch(Integer.parseInt(x.readLine()))
    {
        case 1:
            System.out.println("\nThe current stack items:");
            a1.display();
            break;
        case 2:
        default:
            break;
    }
    System.out.println("\nEnter the number of elements to pop: ");
    int n2=Integer.parseInt(x.readLine());
    for(int i=0;i<n2;i++)
     {
        System.out.println(a1.pop()+" is poped from stack... ");
     }
    System.out.println("\nDo you want to see the stack items?\n\t1.Yes 2.No");
    switch(Integer.parseInt(x.readLine()))
    {
        case 1:
            System.out.println("\nThe current stack items:");
            a1.display();
            break;
        case 2:
        default:
            break;
    }
}
}
   
Output:
nn@linuxmint ~ $ javac stack_list.java
Note: stack_list.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java stack_list

        <<STACK USING LINKEDLIST>>

Stack ready...

Enter number of elements to push: 3

Enter elements: 
1
2
3

Do you want to see the stack items?
    1.Yes 2.No
1

The current stack items:
 3 2 1

Enter the number of elements to pop: 
1
3 is poped from stack... 

Do you want to see the stack items?
    1.Yes 2.No
1

The current stack content:
 2 1
nn@linuxmint ~ $

Stack Using Array - Datastructure - JAVA

Program:
import java.io.*;
class stackarray
{   
    private int maxsize;
    private int st[];
    public int top;
    public stackarray(int size)
    {
        maxsize=size;
        st=new int[maxsize];
        top=-1;
    }

    public boolean isempty()
    {
        return(top==-1);
    }
    public boolean isfull()
    {
        return(top==(maxsize-1));
    }
    public void push(int data)
    {
        if(isfull())
            System.out.println("Error:stack overflow");
        else
            {
                st[++top]=data;
                System.out.println("\tpushing to stack.... "+data);
            }

    }
    public int pop()
    {
        if(isempty())
        {
            System.out.print("\nError:empty");
            return 0;
        }
        else
        {
            return st[top--];
        }
    }
    public void display()
    {
        System.out.println("\n--STACK ITEMS-- ");
        for(int i=0;i<=top;i++)       
            System.out.print(st[i]+" ");
    }
}
class stack_array
{
public static void main(String arg[])throws IOException
{
    System.out.print("\n\n\t\t<<<STACK USING ARRAY>>>\nEnter the size of array:");
    DataInputStream x= new DataInputStream(System.in);
    stackarray ar1=new stackarray(Integer.parseInt(x.readLine()));
    int proceed=1;
    System.out.print("Enter the number of elements to push:");
    int p=Integer.parseInt(x.readLine());   
    System.out.println("Enter the elements:");
    for(int k=0;k<p;k++)
        ar1.push(Integer.parseInt(x.readLine()));
    ar1.display();
    System.out.print("\nEnter the number of elements to pop:");
    int q=Integer.parseInt(x.readLine());   
    for(int k=0;k<q;k++)
        System.out.println("\nPoping.... "+ar1.pop());
   
    do{
        ar1.display();
        System.out.println("\n---MENU---\n1.Push an element to stack.\n2.Pop an element from stack\n3.Check Full\n4.Check Empty\n5.Display Stack Items \n");
        int s1=Integer.parseInt(x.readLine());   
        switch(s1)
        {
            case 1:
                System.out.print("---PUSH AN ELEMENT---\nEnter the element:");
                ar1.push(Integer.parseInt(x.readLine()));
                break;
            case 2:
                System.out.print("---POP AN ELEMENT---\nPoping.... "+ar1.pop());
                break;
            case 3:
                if(ar1.isfull())
                    System.out.println("Stack is full.");
                else
                    System.out.println("Stack is not full.");
                break;
            case 4:
                if(ar1.isempty())
                    System.out.println("Stack is empty.");
                else
                    System.out.println("Stack is not empty.");
            case 5:
                    ar1.display();
            default:
                break;
        }
        System.out.println("\nPress 1 to continue...");
        proceed=Integer.parseInt(x.readLine());   
    }while(proceed==1);


}   
}

Output:
nn@linuxmint ~ $ javac stack_array.java
Note: stack_array.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java stack_array


        <<<STACK USING ARRAY>>>
Enter the size of array:3
Enter the number of elements to push:3
Enter the elements:
1
    pushing to stack.... 1
2
    pushing to stack.... 2
3
    pushing to stack.... 3

--STACK ITEMS--
1 2 3
Enter the number of elements to pop:1

Poping.... 3

--STACK ITEMS--
1 2
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

1
---PUSH AN ELEMENT---
Enter the element:4
    pushing to stack.... 4

Press 1 to continue...
1

--STACK ITEMS--
1 2 4
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

3
Stack is full.

Press 1 to continue...
1

--STACK ITEMS--
1 2 4
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

2
---POP AN ELEMENT---
Poping.... 4
Press 1 to continue...
1

--STACK ITEMS--
1 2
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

4
Stack is not empty.

--STACK ITEMS--
1 2
Press 1 to continue...
1

--STACK ITEMS--
1 2
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

5

--STACK ITEMS--
1 2
Press 1 to continue...
2
nn@linuxmint ~ $
Related Posts Plugin for WordPress, Blogger...