To get into software development, you must be familiar with data structures in order to store and process the data. Stack and queue both are linear data structures that you will use in many challenging situations. But, to understand when to use which data structure, you must first understand the variance between both these data structures

## What Is Stack?

Stack in data structure in computer science is similar to stack as a method of arranging items in the real world.

Stack is a linear data structure similar to arrays and linked lists, restricting the random access of elements. In arrays or linked lists, you can access the items via traversal or random indexing, but the stack data structure doesn't allow it either. A stack can be best understood by seeing it as a container of pieces that can only be stacked on top of each other and removed from that same direction only.

#### Full Stack Java Developer Course

In Partnership with HIRIST and HackerEarth Stack as a data structure in computer science is similar to stack as a method of arranging items in the real world. A stack can be represented as a set of books piled on top of each other. These books can only be placed on each other from the top end. This scenario illustrates sequential book access, which is equivalent to the stack data structure in computer science. The image given below represents the stack as a real-life example: Moving forward in the topic of Stacks and Queues, you will understand the Representation of Stacks.

## Representation of Stacks

The stack data structure follows Last In First Out or First In Last Out principles to execute its operations. In simpler words, the stack data structure removes the last inserted element at first from the topmost position and the first inserted element at last. That is why this data structure only requires one pointer to remember the element at the top position ## Basic Operations on Stacks

Both Stacks and Queues can perform some of the fundamental operations, like storing and manipulating the data elements. Now, you will understand the functions of the stack.

You can perform some basic operations on the stacks, they are:

### push(arg):

The components are added at the top of the stack during this process. You must give an element to the Push() method that you wish to place into a stack.

Push operation involves the following steps:

Step 1: Checks if the stack is complete or not.

Step 2: If the stack is complete, it will exit and produce an overflow condition.

Step 3: If the stack is not complete, it increments the top by one and points to the next space.

Step 4: Adds data element to that space

Step 5: Returns as a success #### Algorithm of the Push Operation:

 begin :stack, data_element  If the stack is full    return null end if top <- top + 1 stack[top] = data_element end

#### Example of a Push Operation:

 void push(int data_element){ if(!isfull()) { top = top + 1; stack[top] = data_element; } else{ printf(“ Stack is already full”); } }

#### New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & More ### Pop ():

In this operation, an element at the top of the stack gets removed. This pop() method does not require any parameters.

The pop operation also includes some steps, which are as follows:

Step 1: Checks if the stack is empty or not.

Step 2: If the stack is already empty, it exits and produces an underflow condition.

Step 3: When the stack is not empty, it then accesses the topmost data element.

Step 4: Decreases the value of the top by one.

Step 5: Returns as a success. #### Algorithm of a Pop Operation:

 begin pop: Stack if the stack is empty   return null end if data_element <- stack[top] top <-top - 1 return data_ element end

#### Example of Pop Operation:

 int pop(int data_element){ if(isempty()) { data_element = stack[top]; top = top - 1; return data_elemnt; } else { printf(“ stack is already empty”) } }

### Peek ():

It involves retrieving the topmost data element from the stack without removing it from the stack's data elements.

#### Algorithm of a Peek Operation:

 begin  return stack[top] end

#### Example of a Peek Operation:

 int peek() {  return stack[top]; }

### isfull():

This operation is used to check whether or not the stack is complete.

#### Algorithm of an isfull() Operation:

Max_Size is the maximum size of the stack.

 begin If top equals to Max_Size   return true else    return false end if end

#### Example of an isfull() Operation:

 bool isfull(){  if( top == Max_Size)  return true; else  return false; }

### isempty():

The isEmpty method is used to check whether the stack is empty or not.

#### Algorithm of an isempty() Operation:

 Begin  If the top is less than -1  Return true Else  Return false End if end

#### Example of an isempty() Operation:

 bool isempty(){ if ( top ==  -1)  return true; else  return false; }

## Stacks Implementation

Stacks and Queues might look similar, but there is a significant difference in the implementation. Now, you will learn about stack data structure implementation. The implementation of the stacks is done in two ways:

• Stack implementation using an array.  The Stacks and Queues are applied for various computational purposes. This tutorial will now discuss the applications of stacks:

• The stacks are used for expression evaluation
• The stacks are used for parentheses checking expressions
• Stacks are used to convert expressions from one form to another
• The stacks can be used as backtracking
• Tower of Hanoi is the best use of stacks
• Memory management and function are the application of stacks
• The stacks are also used for string reversal

#### Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTME ## What Is the Queue?

A queue is a linear data structure like a stack having some restrictions on insertion and deletion. In a queue, insertion is performed at one end, and the removal is performed at another or opposite end.

A real-life illustration of a movie ticket counter can help you understand the queue data structure. In a movie ticket counter, the customer who arrives first is always served first. And the consumer who arrives last will undoubtedly be served last. Both the ends of these queues are open and can execute different operations. The rear end of the food court line inserts a customer, while the front end removes the customer after providing him with the service he desires. Next, in the stacks and queues data structure tutorial, you will learn about the representation of queue data structure.

## Representation of Queues

The queue in the data structure resembles all these properties of the real-world queue. It follows the First in First Out principle for manipulating the data elements. According to this principle, the element which gets inserted in a list at first gets removed at first. The illustration given below explains the FIFO nature of a queue: Also, from the above image, it’s vividly clear that the queue data structure will require two pointers for performing insertion and deletion at two distinct ends. The primary operations in the queue are insertion and deletion, which are referred to as Enqueue() and Dequeue(), respectively.

## Basic Operations

As you saw before, you can perform data manipulation operations both on stacks and queues. Here, you will learn the operations to be performed on the queue. Now, try to understand some fundamental operations of queue data structure:

### Peek() Operation:

In the peek operation, you try to get the data element at the front without removing it from the queue.

#### The Algorithm of the "Peek" Operation Is as Follows:

 begin to peek return queue[ front ] end

#### The Example of a Peek Operation Is as Follows:

 int peek(){ return  queue[front]; }

### isfull() Operation:

This operation is performed to check whether or not the queue is full.

#### The Algorithm of the isfull Operation Is as Follows:

Here MAXSIZE determines the maximum size of the queue.

Begin isfull

 Begin isfull if rear equals MAXSIZE    return true else    return false end if end

#### Example of an isfull Operation

 bool isfull() {  If (rear== MAXSIZE-1)  return true;  else  return false; }

### isempty() Operation:

The Isempty operation is performed to check whether the queue is empty or not.

#### The Algorithm of the isempty Operation Is as Follows:

 Begin isempty If the front is less than min or the front is greater than the rear  return true else  return false end if end

#### The Example of an isempty Operation

 bool isempty() {  if ( front< 0 || front > rear)  return true; else  return false; }

### Enqueue Operation:

Enqueue refers to inserting or adding some data element to the queue. You always insert an element with the help of a rear pointer.

Enqueue operation includes two pointers, REAR, and FRONT. Following are some steps for the same:

Step 1: Verify whether the queue is full or not.

Step 2: If the queue is full already, exit and produce an overflow condition.

Step 3: If the stack is not complete, increment the rear and point to space.

Step 4: Add the data element where the rear is pointing.

Step 5: Success. #### Algorithm of Enqueue Operation:

 Begin enqueue(data_element)  If the queue is full  Overflow end if  rear <- rear +1  queue[rear] = data_element  return true end

#### Example of Enqueue Operation:

 int enqueue( int data_element) if (isfull())  return 0; rear= rear + 1; queue[rear] = data_element; return 1;

### Dequeue Operation:

Dequeue refers to delete or remove data elements from the queue, and you use a front pointer to do this operation.

Dequeue operation includes the following steps:

Step 1: Check if the queue is empty or not.

Step 2: If the queue is the empty exit and if it does produce an underflow condition.

Step 3: Queue is not empty, then access the data where the front is pointing and remove that data element from the queue.

Step 4: Increment front to point to the next existing data element

Step 5: Return success. #### Algorithm of the Dequeue Operation:

 Begin if the queue is empty  Return underflow End if data_element = queue[front] front <- front +1 return true end

#### Example of a Dequeue Operation:

 int dequeue() {  if ( isempty() )   return 0; int data_element = queue[front]; front = front + 1;  return data_element;  }

In the next section of the stacks and queues tutorial, you will learn about the implementation of a queue.

#### Stand Out From Your Peers this Appraisal Season

Start Learning With Our FREE Courses ## Queues Implementation

Implementation of the queues can be done in two ways:

• Implementation of a queue using an array • Implementation of queue using a linked-list So far in this Stacks and Queues tutorial, you learned the implementation of queues. Now you will look into the Types of Queues.

## Types of Queues

There are three types of queue data structures:

1. Circular Queue
2. Dequeue (Double Ended Queue)
3. Priority Queue

### Circular Queue

• The end node of the circular queue is connected to the first node
• A circular queue is also called a Ring Buffer ### Dequeue (Double Ended Queue)

In the double-ended queue, you can perform insertion and deletion on both ends that are rear as well as the front. ### Priority Queue

• Every node has some predefined priority in the priority queue
• The node having the least priority will be the first which is removed from the queue
• Insertion in the priority queue is done as the arrival of node. In the next section of the Stacks and Queues tutorial, you will learn the application of queues in real-time.

## Application of Queues

• CPU scheduling and disk scheduling, where one resource is shared among various consumers
• IO Buffers, files, and Pipes IO, where data is transferred asynchronously between two processes
• Semaphores in the operating system
• FCFS (First Come First Serve) scheduling
• Spooling in printers
• Queue in routers and switches in networking

Moving forward, you will look at the differences between stack and queue in tabular format.

## Differences Between Stack and Queue

### Queue

Operational

principle

Follows Last In First Out or First In Last Out principle

Follows First In First Out or Last In Last Out principle

Structure

Insertion and deletion both take place from one end called as top

Insertion occurs at the rear end, whereas deletion happens at the front end

Number of pointers required

It contains only one pointer known as top, which stores the reference of the topmost element

It contains two pointers named front (holds the address of the first element in a list) and rear (holds the address of last queue element)

Primary

operations

push(x) and pop() are two primary queue operations

Enqueue() and Dequeue() are two primary data manipulation operations

Condition to check empty state

If top == -1, the stack is considered as empty

If front == -1 && rear == -1, the queue is considered as empty

Condition to check full state

If top == MaxSize - 1, the stack is considered as full

If rear == Maxsize - 1, the queue is considered as full

Implementation

Easy implementation

Little complex implementation

Problem

Solving

This data structure is used to solve recursive problems.

This data structure is used to solve problems that require sequential processing.

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

### Next Steps

In this difference between stack and queue tutorial, you explored the dissimilarities between stack and queue based on different parameters. You learned the basics of both stack and queue data structure. In the end, you saw a few key differences between stack and heap memory in tabular format.

If you're looking for more in-depth training that goes beyond data structures and covers the foundations of interactive application development, Simplilearn's Software Development Courses will prove ideal for you. The courses in this catalog can help you improve your odds of becoming a software developer by assisting you in mastering the art of software development. So, go ahead and start exploring! Simplilearn