Search This Blog

Showing posts with label stack. Show all posts
Showing posts with label stack. Show all posts

Tuesday, 2 February 2016

Stack - Queue Implementation using class C++

//implement stack-queue such that
//insert() - push item in stack
//pop() - pop elements from stack and insert into queue
//display() - display the queue

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

using namespace std;

class SQ
{
    int rear,front,top,stack[5],queue[5];
    public:
    int full();
    int empty();
    void set();
    void insert(int);
    void pop();
    void display();
};
int SQ::full()
{
    if(rear==4 || top==4)
    return 1;
    else
    return 0;
}
int SQ::empty()
{
    if(top==-1)
    return 1;
    else
    return 0;
}
void SQ::set()
{
    rear=-1;
    front=0;
    top=-1;
}
void SQ::insert(int i)
{
    stack[++top]=i;
}
void SQ::display()
{
       /*    cout<<"\nStack is:"<<endl;

    for(int i=top;i>=0;i--)
    {
        cout<<"\n";
        cout<<stack[i];                                          // if you want to see the stack
    }
    cout<<"\n";
    */
    if(rear==-1)
    {
        cout<<"\nPop Element from stack to insert in queue"<<endl;
    }
    else{
    cout<<"\nQueue is:"<<endl;
    for(int j=front;j<=rear;j++)
    {

        cout<<queue[j]<<" ";
    }
    cout<<"\n";
    }

}
void SQ::pop()
{
    /*if(top==-1)
    {
        cout<<"\nDone"<<endl;
    } */
    if(rear==-1){
    queue[++rear]=stack[top];
    top--;
    }else
    {
        queue[++rear]=stack[top];
        top--;
    }

}
int main()
{
    SQ sq;
    sq.set();
    int ch,val;
    do
    {
        cout<<"\nEnter your choice:\n1.insert into stack\n2.pop from stack and push into queue\n3.display\n4.enter 0 to exit"<<endl;
        cin>>ch;

        switch(ch)
        {
            case 1:if(sq.full())
            {
                cout<<"\nFull!"<<endl;
            }
            else
            {cout<<"\nEnter ele:";
            cin>>val;
            sq.insert(val);
            }
            break;

            case 2:if(sq.empty())
            {
                cout<<"\nEmpty"<<endl;
            }else
            {
                sq.pop();
            }
            break;

            case 3:sq.display();
            break;


            default:cout<<"\nEnter Proper choice"<<endl;
        }
    }while(ch!=0);
    return 0;
}

Stack Implementation using C++

// Do not forget to take a look at that both stack-queue class implementation ! label is stackqueue


#include<iostream>

using namespace std;

class stack
{
    int top;
    int a[5];
  
    public:
    void set()
    {
        top=-1;
    }
    int full()
    {
        if(top==4)
            return 1;
        else
            return 0;
    }
  
    int empty()
    {
        if(top==-1)
            return 1;
        else
            return 0;
    }
  
    void push(int i)
    {
        a[++top]=i;
    }
    void peep()
    {
        cout<<"\nTop element is :"<<a[top];
    }
  
    void display()
    {
        int i;
        for(i=top;i>=0;i--)
        {
            cout<<"\n";
            cout<<a[i];
        }
        cout<<"\n";
    }
    void pop()
    {
        top--;
    }
  
};

int main()
{
    stack s;
    int ch,i;
    s.set();
    do{
  
    cout<<"\n1.push\n2.peep\n3.pop\n4.display\n5.enter 0 for exit:";
    cin>>ch;
    switch(ch)
    {
        case 1:if(s.full()){
                cout<<"\nFull";
            }else
            {
        cout<<"\nEnter element:";
        cin>>i;
        s.push(i);
        }
        break;
  
        case 2:s.peep();
        break;
      
        case 3:if(s.empty())
        {
            cout<<"\nstack is empty";
        }else
        s.pop();
        break;
      
        case 4:s.display();
        break;
      
        default:cout<<"\nProper choice plz!";
    }
    }while(ch!=0);
  
    return 0;
}

Wednesday, 13 January 2016

Stack Using Liked List

/* Stack Using Linked List*/

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

typedef struct stack
{

int data;
struct stack *next;

}node;
node *top=NULL;

node *getnode()
{

int val;
node *temp;
printf("Enter val");
scanf("%d",&val);
temp=(node *)malloc(sizeof(node));
temp->data=val;
temp->next=NULL;
return temp;

}
void push()
{

node *neww;
neww=getnode();
neww->next=top;
top=neww;

}
void pop()
{

if(top==NULL)
{
      printf("\nStack is Empty!\n");
}
else
{
     node *t;
     t=top;
     printf("\n%d is deleted\n",top->data);
     top=top->next;
     t->next=NULL;
     free(t);
}

}
void peep()
{

     printf("\nTop element is %d",top->data);
}

void display()
{

if(top==NULL)
{
     printf("\nEmpty!\n");
}
else
{
     node *t;
     t=top;
     while(t)
     {
          printf("\n%d",t->data);
          t=t->next;
     }
     printf("\n");
}

}
void main()
{

int ch;
clrscr();
while(1)
{
printf("\n1.push\n2.pop\n3.peep\n4.display\n5.exit\nYour choice:");
scanf("%d",&ch);

switch(ch)
{
case 1:push();
break;

case 2:pop();
break;

case 3:peep();
break;

case 4:display();
break;

case 5:exit(0);
break;

default:printf("\nProper choice plz!\n");
}
}

}

Stack Using Array

/* Stack using array*/

    #include<stdio.h>
    #define size 5

    struct stack
    {

    int top,S[size];

    }st;

    int stfull()
    {

    if(st.top==size-1)
    {
         return 1;
    }
    else
    {
         return  0;
    }

    }

    int stempty()
    {

    if(st.top==-1)
    {
         return 1;
    }
    else
    {
         return 0;
    }

    }

    void push(int value)
    {

    st.top++;
    st.S[st.top]=value;

    }

    void pop()
    {

    int r;
    r=st.S[st.top];
    st.top--;

    }

    void peep()
    {

    printf("\nTop element is %d\n",st.S[st.top]);

    }

    void display()
    {

    int i;
    printf("\n");
    for(i=st.top;i>=0;i--)
    {
         printf("%d\n",st.S[i]);
    }

    }

    void main()
    {

    int choice,data;
    st.top=-1;

    while(1)
    {
    printf("\n1.Push\n2.Pop\n3.Peep\n4.Display\n5.Exit\nYour choice:");
    scanf("\n%d",&choice);

    switch(choice)
    {
    case 1:
    if(stfull())
    {
         printf("\nStack is Full\n");
    }
    else
    {
         printf("\nEnter value:");
         scanf("%d",&data);
         push(data);
    }
    break;

    case 2:
    if(stempty())
    {
         printf("\nStack is Empty\n");
    }
    else
    {
         pop();
    }
    break;

    case 3:peep();
           break;

    case 4:if(stempty())
    {
         printf("\nAdd at least one element\n");
    }
    else
    {
         display();
    }
    break;

    case 5:exit(0);
           break;

    default:printf("\nEnter proper choice\n");
    }
    }

    }