Git Product home page Git Product logo

stack-queue's Introduction

Stack-Queue

Stack

It is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out) .Initially We Declare a Top Variable and store -1 inside it. It is the starting index of our Stack.

image

In Stack Data Structure, there are mainly 5 Operations:

  1. Push
  2. Pop
  3. Peek
  4. isEmpty
  5. isFull

Basic Syntax of Stack is :

struct Stack
{
    int *arr;
    int top;
    int size;
};

Push

It means adding a data inside stack. In this case, we will increament top 1st. Then we store that value in that top index.

Pop

It means removing the last data added inside stack. In this case, we will store the value of the stack top. Then we decreamented the top bt -1 and return the value at previous index.

image


Peek

To retrive the top of the Stack without removing it.

isEmpty

To Check if the stack is empty or not i.e value of top == -1 ?

isFull

To Check if the stack is full or not i.e value of stack == stack size?

IN C++ STL

image




Queue

A queue is defined as a linear data structure that is open at both ends and the operations are performed in First In First Out (FIFO) order.

We define a queue to be a list in which all additions to the list are made at one end, and all deletions from the list are made at the other end. The element which is first pushed into the order, the operation is first performed on that.


Queue


FIFO Principle of Queue:

--> A Queue is like a line waiting to purchase tickets, where the first person in line is the first person served. (i.e. First come first serve). Position of the entry in a queue ready to be served, that is, the first entry that will be removed from the queue, is called the front of the queue(sometimes, head of the queue), similarly, the position of the last entry in the queue, that is, the one most recently added, is called the rear (or the tail) of the queue. See the below figure.

Queue


Characteristics of Queue:

Queue can handle multiple data. We can access both ends. They are fast and flexible.

Queue Representation:

Like stacks, Queues can also be represented in an array: In this representation, the Queue is implemented using the array. Variables used in this case are

Queue: the name of the array storing queue elements. Front: the index where the first element is stored in the array representing the queue. Rear: the index where the last element is stored in an array representing the queue.


In queue, insertion and deletion happen at the opposite ends, so implementation is not as simple as stack. To implement a queue using array, create an array arr of size n and take two variables front and rear both of which will be initialized to 0 which means the queue is currently empty. Element rear is the index upto which the elements are stored in the array and front is the index of the first element of the array. Now, some of the implementation of queue operations are as follows:

Enqueue

Addition of an element to the queue. Adding an element will be performed after checking whether the queue is full or not. If rear < n which indicates that the array is not full then store the element at arr[rear] and increment rear by 1 but if rear == n then it is said to be an Overflow condition as the array is full.

Dequeue

Removal of an element from the queue. An element can only be deleted when there is at least an element to delete i.e. rear > 0. Now, element at arr[front] can be deleted but all the remaining elements have to shifted to the left by one position in order for the dequeue operation to delete the second element from the left on another dequeue operation.

Front

Get the front element from the queue i.e. arr[front] if queue is not empty.

Display

Print all element of the queue. If the queue is non-empty, traverse and print all the elements from index front to rear.

stack-queue's People

Contributors

soumajyoti02 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.