Skip to content

Basic Theory of Stack and Queue in Data Structure and Algorithms along with many standard level interview problems picked from several SDE SHEETs. Algorithms and Code with explanation both are available for each problem.

Notifications You must be signed in to change notification settings

soumajyoti02/Stack-Queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Basic Theory of Stack and Queue in Data Structure and Algorithms along with many standard level interview problems picked from several SDE SHEETs. Algorithms and Code with explanation both are available for each problem.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages