c++ stack

STL FUNCTION TO IMPLEMENT STACK:-


Stack in C++ Standard Template Library (STL)



Stacks are  LIFO(Last In First Out) type of working, In stack new element added at end and remove element is also from end.
The functions for stack operations:-
empty() – Returns whether the stack is empty
size() – Returns the size of the stack
top() – Returns a top most element of the stack
push(g) – Adds the element ‘g’ at the top of the stack
pop() – Deletes the top element of the stack

STACK USING LINKED LIST:-

#include <cstdlib>
#include<iostream>
using namespace std;

class node
{
 public:
    int data;
    node* next;
};

class StackusingList
{
public:
StackusingList(int max)
{
    top = NULL;
    maxnum = max;
    count=0;
}

void push(int element)
{
    if(count == maxnum)
            cout<<"stack is full";
    else
    {
        node *newTop = new node;
        if(top == NULL)
        {       
            newTop->data = element;
            newTop->next = NULL;
            top = newTop;
            count++;
        }
        else
        {
            newTop->data = element;
            newTop->next = top;
            top = newTop;
            count++;
        }
    }
}

void pop()
{
    if(top == NULL)
            cout<< "nothing to pop";
    else
    {
        node * old = top;
        top = top->next;
        count--;
        delete(old);
    }
}
void print()
{
    node *temp;
    temp = top;
    while(temp!=NULL)
    {
        cout<<temp->data<<",";
        temp=temp->next;
    }
}
private:
    node *top;
    int count; //head
    int maxnum;
    int stackData;      
};

int main(int argc, char** argv) {   
    StackusingList *sl = new StackusingList(5);
    sl->push(1);
    sl->push(2);
    sl->push(3);
    sl->push(4);
    sl->push(5);
    sl->push(6);

    sl->pop();
    cout<<"new stack\n";
    sl->print();

    return 0;
}


No comments:

Post a Comment