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

Tuesday, November 3, 2020

Stack Using STL : CODE | C++


// stack using STL


#include<stack>

#include<iostream>


using namespace std;

int main(){

stack<int>s;

// push

s.push(2);

s.push(4);

s.push(5);

s.push(7);


// 2 4 5 7


//pop

s.pop();  // removed 7


//top

cout<<" top = "<<s.top();  // 5 


// is empty

if(s.empty())cout<<"\nyes";

else cout<<"\nNO";


// size

cout<<" \nsize = "<<s.size();  // 3

}






Sunday, November 1, 2020

Code of Stack in C Using Array

// stack using array


// library


#include<stdio.h>

#include<stdlib.h>


// make structure   maxsize , top , *items


struct stack{

int maxSize;

int top;

int *items;

};



// initialization

struct stack* newStack(int capacity){

    struct stack *p =  (struct stack *)malloc(sizeof(struct stack));


    p->maxSize  = capacity;

    p->top = -1;

    p->items = (int *)malloc(sizeof(int) * capacity);


    return p;

}



// operations for our stack


// top

int top(struct stack* p ){

    return p->items[p->top];

}


// isEmpty

int isEmpty(struct stack* p ){

    return p->top == -1;

}


// isFull


int isFull(struct stack *p){

    return p->top + 1 == p->maxSize;

}

// size


int size(struct stack * p){

    return p->top + 1;

}


// push

void push(struct stack* p , int element){    //        12 3  4 4 3

    if(isFull(p)){

        printf("Overflow!\n");

    }else{

        p->items[++p->top] = element;

    }

}


// pop


int pop(struct stack * p ){

    if(isEmpty(p)){

        printf("Underflow!");

    }else{

        return p->items[p->top--];

    }


}




int main(){

struct stack * s = newStack(5);


push(s,1);

push(s,5);

push(s,3);

push(s,6);


//   1    5     3   6


printf("%d\n",top(s));


printf("%d",size(s));



return 0;

}







Friday, October 30, 2020

CREATING STACK IN PYTHON

CREATING STACK USING PYTHON



OPERATIONS:

1- PUSH

2- POP

OTHER OPERATIONS:

1- IS EMPTY

2- SIZE

3- TOP



CODE : 

We can implement stack using :

1- list

2- collections.deque

3- queue.LifoQueue


1 - IMPLEMENTING STACK USING LIST

CODE:

stack = []  // list is created to be used as stack
stack.append('a') // append() push the element in stack which is actually a list
stack.append('b')
stack.append('c')
print(stack.pop()) // pop() is used to pop / delete element on top of list
print(stack.pop())
print(stack.pop())

print(stack)


2- IMPLEMENTING STACK USING collections.deque


from collections import deque  // importing the library
stack = deque() // initializing a variable

stack.append('a') // append function to insert element
stack.append('b')
stack.append('c')

print(stack.pop())  // pop() function to remove the top element
print(stack.pop())
print(stack.pop())

print(stack)

3- IMPLEMENTING STACK USING queue module

Functions available in this module are :

1- get() - remove and return the element from queue
2- put(element) - put element in the queue
3- maxsize() - maximum elements allowed
4- empty() - check whether stack is empty or not
5- full() - check whether stack is full or not if maxsize is declared
6- qsize() - get number of items in queue

CODE:

from queue import LifoQueue
stack = LifoQueue(maxsize = 5)
// initializing stack with maxsize = 5
print(stack.qsize()) // show the number of elements in the stack
stack.put('a')  // put() is used to push elements in the stack
stack.put('b')
stack.put('c')
print("Full: ", stack.full())  // is the stack full or does stack contain maxsize elements
print(stack.get()) // return and remove the top element of the stack
print(stack.get())
print(stack.get())
print("Is Empty : ", stack.empty()) // test is the stack empty or not






CREATING STACK USING STL

 STACK IN STL

Standard library provides us stack container with number of built in features.


STACK TEMPLATE :

template <class T, class Container = deque<T> > class stack;


MEMBER FUNCTIONS :

All these operations can be performed in 0(1)

empty  :  test whether the container is empty
size    : return the size of the container
top   : return the top element
push  : insert element
emplace  : construct and insert element
pop  : remove top element
swap  : swap contents


CODE ;

#include <iostream>
#include <stack>    // for using stack container
using namespace std;

int main()
{
int sum = 0;
stack<int> mystack; // declaring stack
mystack.push(21);   // inserting element
mystack.push(32);
mystack.push(23);
mystack.push(63);
mystack.push(212);

while (!mystack.empty()) {   // checking the empty condition
sum = sum + mystack.top();   // checking the top element
mystack.pop();               // performing pop operation

cout << sum;
return 0;
}






 

IMPLEMENTING STACK USING LINKEDLIST

 IMPLEMENTING STACK USING LINKEDLIST


This provide us ability to define our own data structure and point it to another same data structure using pointers which can be done dynamically .

Here , Since we are using Linked list we will use malloc and free ( in C ) to allocate memory and then deallocate it when needed.


Structure or Linked list Node




Header Files



OPERATIONS


NOTE : Since  , performing operations on head is easy and efficient so  we will choose head node as top node and will perform all operations on head node 


1- PUSH





2- IS EMPTY



    

3- TOP



4- POP














IMPLEMENT STACK USING ARRAY

IMPLEMENTATION  USING ARRAY

DATA STRUCTURE

We will use structure to create stack that will have three things
1- Max Size : contains the maximum size stack can have or its capacity
2- Top : contains the index of the top element
3- items array or pointer to array : contains the elements of stack








Header Files :





operations to perform :

1- Initialization of stack




2- Getting size of stack




3- Check the status i.e. is the stack empty or not




4- Check the status i.e. is the stack full or not




5- Adding element to stack using Push operation




6- Removing the top of the stack using Pop operation





7- Getting the top of the stack











STACK : INTRODUCTION | OPERATIONS | REAL LIFE EXAMPLE | IMPLEMENTATION

 What Is Stack

. Linear Data Structure

TWO OPERATION : 

1 - PUSH : Entering the data on the top or at the end






2- POP : Remove the top element or latest element





So, Stack performs LIFO operation where LIFO stands for Last In First Out.

Nowadays every language has some more operations i.e. Peek, Is empty, Is full , size 

3 - PEEK : It help us to check the top entry without deleting it

4- IS EMPTY : this checks whether the stack is empty or not and provides boolean value (true / false)

5- IS FULL : this checks whether the stack is full or not in non dynamic data structure. If you are using standard library then it resize dynamically .

6- SIZE : this gives the size of the stack




Whenever you get stack , think it to be a data structure where you have to preform insertions and deletion on the same end what ever data structure you are using.

Real Life Example :

1- Plates stacked over one another
2- Stacked books

So , the book on top is the first book to be taken . Although it was the last book to place . Similarly
in marriages the top most dinner plate is the first plate to be taken and used.


APPLICATION OF STACK

1- Undo - Redo features 

2- Infix to Prefix or Postfix

3- Balancing the symbols

4- Used in Algorithms 
    a) Rat in maze
    b) N Queen Problem
    c) Tower of Hanoi
    d) Stock span problem
    e) Topological Sorting
    f) Strongly Connected Components

5- Syntax Parsing

6- Browser back button

7- Useful for performing operations like Backtracking in Programming


IMPLEMENTATION

You can use any data structure to perform this operation :

1- Array
2- Linkedlist

If you use standard library , you can use the data structure provided such as stack (we will see the implementation soon ) which is dynamic and provided certain operations.