×

Stack vs Queue: Data Structure

 Difference Between Stack and Queue

What is Stack?

The LIFO principle applies on insertion and deletion operations of the stack which means last inserted element to the stack will remove first. The insertion and deletion operations take place from the one side of the stack known as top. In stack, we can insert the elements which have same data types or it can contain homogeneous elements only.

Stack vs Queue: Data Structure

There are some operations perform which are given below:

  • push(data) adds a new data item into the stack. We give data item as parameter and it returns nothing.
  • pop() removes the data item from the stack, returns the data item and it will be modified.
  • top() function returns top element present in the stack.
  • is_Empty() function checks whether the stack is empty or not and it returns a boolean value.
  • is_Full() function checks whether the stack is full or not and it returns a boolean value.
  • size() returns the number of items in the stack and return an integer.

Queue: -7 8 /cwb,7e 

Stack vs Queue: Data Structure

A queue is linear data structure which works on first in first out principle. We can say a queue is homogeneous collection of data items where new data items are added at one end of queue called rear and the deletion of the data item is done from other end called front of the queue.

There are some operations perform which are given below:

  • enqueue(data) inserts the data item into the queue.
  • dequeue() deletes the data item from the queue called dequeue operation.
  • is_Empty() function checks whether the queue is empty or not and it returns a boolean value.
  • is_Full() function checks whether the queue is full or not and it returns a boolean value.
  • size() returns the number of items in the queue and return an integer.

 Difference Between Stack and Queue

ParametersStackQueue
DefinitionA stack is a kind of linear data structure. We can say it is container that has only one end is opened and it follows a principle which is called Last in First out (LIFO).A queue is linear data structure which works on first in first out (FIFO) concept. We can say a queue is homogeneous collection of data items
Insertion and DeletionIn stack, there is only one end opened so insertion and deletion operations are done from one end called top of the stackIn queue, we can insert the data item from the end of the queue and deletion can be done from the front of the queue.
Full conditionFor checking whether the stack is full or not. If (top== max-1)For checking whether the queue is full or not. If (rear==max-1)
Empty conditionFor checking whether the stack is empty or not. If (top==-1)
For checking whether the queue is empty or not. If (front== -1 or front = rear+1)
TypesStack doesn’t have any kind of typesQueue has three other types like circular queue, priority queue and double ended queue.
VisualizationA stack is visualized as a vertical collection.A queue is visualized as a horizontal collection.

Related Topics

Does Overloading Work with Inheritance

This is a question that occasionally comes to many programmers. Who are curious to know more now has a complete explanation and a solution through this tutorial! Inheritance: The functions of...

3 minutes read.

What is a Sparse Matrix in Data Structure?

Definition A matrix in which a few non-zero elements are present is called a Sparse matrix. In a Sparse matrix, almost all the matrices are filled with zero (0). A matrix...

5 minutes read.

What is the Use of Segment Trees in Data Structure?

Segment trees Segment trees are also called statistical trees in computer science. They are a type of tree data structure. Segment trees are used to store information regarding segments and intervals....

6 minutes read.

Flatten Binary Tree to a linked list

Implementation In this section, we will see the implementation of the binary Tree and its conversion into linked lists. let us proceed: - // Writing a C++ program that will convert a...

4 minutes read.

Quick Sort

Quicksort is a sorting algorithm that uses a divide-and-conquer strategy. A pivot element is used to divide an array into subarrays (element selected from the array).  The pivot element should be...

4 minutes read.

Given a Binary Tree Print the Shortest Path

Implementation // Writing a program in C++ to find the shortest between the nodes i and j.  #include <bits/stdc++.h> using namespace std; // the given function will print the path between nodes i and...

7 minutes read.

What is Skewed Binary Tree

To understand the skewed binary tree, we must first understand the concept of a binary tree. A binary is generally the one in which every single node has two further...

3 minutes read.

Time Complexity of Selection Sort in Data Structure

What is Time Complexity? The term “Time complexity” can be defined as the number of times executions made of a particular sequence of instructions and not the total amount of time...

3 minutes read.

Diameter of a Binary Tree

Implementation We will now witness the implementation of the diameter of a binary tree. // Creating a recursive and challenging C program that will help us determine the diameter of a binary...

4 minutes read.

Equal Sum

Find an element in array such that the sum of left array is equal to the sum of right array You have been given an array of numbers. You have to...

4 minutes read.

Construction of B tree in Data Structure

A B-tree is a type of balanced tree data structure that is commonly used in file systems and databases to improve the efficiency of search, insert, and delete operations. The structure...

4 minutes read.

Recursion - Factorial and Fibonacci

In this article, we will learn how to find the factorial of a number and the Fibonacci series up to n using the recursion method. What is recursion? Defining anything in terms...

7 minutes read.

Operations on 1D-Arrays

One Dimensional Array Operations Basic Methods The fundamental operations enabled by an array are listed below. Traverse prints each element of the array one by one.Insert a new element at the specified index.Delete...

8 minutes read.

Adding one to the number represented an array of digits

You have given one array, which consists of values which represent the different digits of a number. You have to add 1 to this number and store the result in...

3 minutes read.

About Data Structures

What exactly are data structures? A data structure is a type of storage that is used to organise and store data. It is a method of organising data on a computer...

5 minutes read.

Binary tree deletion

This article will discuss the deletion operation's implementation in the binary tree. The deletion operation helps us eliminate an element from the tree. Implementation #include <bits/stdc++.h> using namespace std; /* A binary tree node...

4 minutes read.

Red Black Tree vs AVL Tree: Data Structure

Difference Between Red Black Tree vs AVL Tree Red Black Tree: A red-black tree is referred as self-balancing binary search tree. In red-black, each node stores an extra bit that determines...

4 minutes read.

Serialize and Deserialize a Binary Tree

Implementation // Writing a C++ program to check the serialization and deserialization of binary tree.   #include <iosstream> /* A binary tree node contains a key and a pointer to the left and right...

4 minutes read.

Asymptotic Notation

Asymptotic notation is expressions that are used to represent the complexity of algorithms. The complexity of the algorithm is analyzed from two perspectives:  Time complexitySpace complexity Time complexity The time complexity of an algorithm is the...

3 minutes read.

Given a Binary Tree Swap Nodes at K Height

Implementation // Writing a C++ program that will help us exchange the nodes.  #include<bits/stdc++.h> using namespace std; // Creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a function that will help...

8 minutes read.