Previous slide Next slide Toggle fullscreen Open presenter view
CE205 Data Structures Week-3
CE205 Data Structures
Week-3
Download PDF ,DOCX , SLIDE , PPTX
RTEU CE205 Week-3
CE205 Data Structures Week-3
Outline-1
Stack ADT
Stack Using Array
Stack Using Linked List
Expressions
Infix
Postfix
Prefix
Infix to Postfix Conversion
Postfix Expression Evaluation
RTEU CE205 Week-3
CE205 Data Structures Week-3
Outline-2
Queue ADT
First Come First Serve, FCFS, FIFO
Queue Data structure Using Array
Queue Using Linked List
Circular Queue Data structure
Double Ended Queue Data structure
Multilevel Queue (MLQ)
Hanoi Tower
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack ADT
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Data Structure
In this tutorial, you will learn about the stack data structure and its implementation in Python, Java and C/C++.
A stack is a linear data structure that follows the principle of Last In First Out (LIFO) . This means the last element inserted inside the stack is removed first.
You can think of the stack data structure as the pile of plates on top of another.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Data Structure
Stack representation similar to a pile of plate
Here, you can:
Put a new plate on top
Remove the top plate
And, if you want the plate at the bottom,
You must first remove all the plates on top.
This is exactly how the stack data structure works.
RTEU CE205 Week-3
CE205 Data Structures Week-3
LIFO Principle of Stack
In programming terms, putting an item on top of the stack is called push and removing an item is called pop .
In the above image, although item 3 was kept last, it was removed first. This is exactly how the LIFO (Last In First Out) Principle works.
We can implement a stack in any programming language like C, C++, Java, Python or C#, but the specification is pretty much the same.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Basic Operations of Stack
Some basic operations allow us to perform different actions on a stack.
Push : Add an element to the top of a stack
Pop : Remove an element from the top of a stack
IsEmpty : Check if the stack is empty
IsFull : Check if the stack is full
Peek : Get the value of the top element without removing it
RTEU CE205 Week-3
CE205 Data Structures Week-3
Working of Stack Data Structure
The operations work as follows:
A pointer called TOP is used to keep track of the top element in the stack.
When initializing the stack, we set its value to -1 so that we can check if the stack is empty by comparing TOP == -1
.
On pushing an element, we increase the value of TOP and place the new element in the position pointed to by TOP.
On popping an element, we return the element pointed to by TOP and reduce its value.
Before pushing, we check if the stack is already full
Before popping, we check if the stack is already empty
RTEU CE205 Week-3
CE205 Data Structures Week-3
Working of Stack Data Structure
RTEU CE205 Week-3
CE205 Data Structures Week-3
What is a Stack?
Stack is a linear data structure in which the insertion and deletion operations are performed at only one end.
In a stack, adding and removing of elements are performed at a single position which is known as "top ".
That means, a new element is added at top of the stack and an element is removed from the top of the stack.
In stack, the insertion and deletion operations are performed based on LIFO (Last In First Out) principle.
RTEU CE205 Week-3
CE205 Data Structures Week-3
What is a Stack?
In a stack, the insertion operation is performed using a function called "push" and deletion operation is performed using a function called "pop" .
RTEU CE205 Week-3
CE205 Data Structures Week-3
What is a Stack?
In the figure, PUSH and POP operations are performed at a top position in the stack.
That means, both the insertion and deletion operations are performed at one end (i.e., at Top)
A stack data structure can be defined as follows
Stack is a linear data structure in which the operations are performed based on LIFO principle.
Stack can also be defined as
"A Collection of similar data items in which both insertion and deletion operations are performed based on LIFO principle".
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Example
If we want to create a stack by inserting 10,45,12,16,35 and 50.
Then 10 becomes the bottom-most element and 50 is the topmost element.
The last inserted element 50 is at Top of the stack as shown in the image below.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Operations on a Stack
The following operations are performed on the stack
Push (To insert an element on to the stack)
Pop (To delete an element from the stack)
Display (To display elements of the stack)
RTEU CE205 Week-3
CE205 Data Structures Week-3
Implementation of a Stack
Stack data structure can be implemented in two ways.
Using Array
Using Linked List
When a stack is implemented using an array, that stack can organize an only limited number of elements.
When a stack is implemented using a linked list, that stack can organize an unlimited number of elements.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Using Array
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Using Array
A stack data structure can be implemented using a one-dimensional array.
But stack implemented using array stores only a fixed number of data values. This implementation is very simple.
Just define a one dimensional array of specific size and insert or delete the values into that array by using LIFO principle with the help of a variable called 'top' .
Initially, the top is set to -1.
Whenever we want to insert a value into the stack, increment the top value by one and then insert.
Whenever we want to delete a value from the stack, then delete the top value and decrement the top value by one.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Operations using Array
A stack can be implemented using array as follows
Before implementing actual operations, first follow the below steps to create an empty stack.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Operations using Array
Step 1 - Include all the header files which are used in the program and define a constant 'SIZE' with specific value.
Step 2 - Declare all the functions used in stack implementation.
Step 3 - Create a one dimensional array with fixed size (int stack[SIZE] )
Step 4 - Define a integer variable 'top' and initialize with '-1' . (int top = -1 )
Step 5 - In main method, display menu with list of operations and make suitable function calls to perform operation selected by the user on the stack.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Operations using Array
push(value) - Inserting value into the stack
In a stack, push() is a function used to insert an element into the stack.
In a stack, the new element is always inserted at top position.
Push function takes one integer value as parameter and inserts that value into the stack. We can use the following steps to push an element on to the stack
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Operations using Array
Step 1 - Check whether stack is FULL . (top == SIZE-1 )
Step 2 - If it is FULL , then display "Stack is FULL!!! Insertion is not possible!!!" and terminate the function.
Step 3 - If it is NOT FULL , then increment top value by one (top++ ) and set stack[top] to value (stack[top] = value ).
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Operations using Array
pop() - Delete a value from the Stack
In a stack, pop() is a function used to delete an element from the stack.
In a stack, the element is always deleted from top position.
Pop function does not take any value as parameter.
We can use the following steps to pop an element from the stack
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Operations using Array
Step 1 - Check whether stack is EMPTY . (top == -1 )
Step 2 - If it is EMPTY , then display "Stack is EMPTY!!! Deletion is not possible!!!" and terminate the function.
Step 3 - If it is NOT EMPTY , then delete stack[top] and decrement top value by one (top-- ).
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Operations using Array
display() - Displays the elements of a Stack
We can use the following steps to display the elements of a stack
Step 1 - Check whether stack is EMPTY . (top == -1 )
Step 2 - If it is EMPTY , then display "Stack is EMPTY!!!" and terminate the function.
Step 3 - If it is NOT EMPTY , then define a variable 'i ' and initialize with top. Display stack[i] value and decrement i value by one (i-- ).
Step 3 - Repeat above step until i value becomes '0'.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in C
#include <stdio.h>
#include <conio.h>
#define SIZE 10
void push (int ) ;
void pop () ;
void display () ;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in C
...
int stack [SIZE], top = -1 ;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in C
...
void main ()
{
int value, choice;
clrscr();
while (1 ){
printf ("\n\n***** MENU *****\n" );
printf ("1. Push\n2. Pop\n3. Display\n4. Exit" );
printf ("\nEnter your choice: " );
scanf ("%d" ,&choice);
switch (choice){
case 1 : printf ("Enter the value to be insert: " );
scanf ("%d" ,&value);
push(value);
break ;
case 2 : pop();
break ;
case 3 : display();
break ;
case 4 : exit (0 );
default : printf ("\nWrong selection!!! Try again!!!" );
}
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in C
...
void push (int value) {
if (top == SIZE-1 )
printf ("\nStack is Full!!! Insertion is not possible!!!" );
else {
top++;
stack [top] = value;
printf ("\nInsertion success!!!" );
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in C
...
void pop () {
if (top == -1 )
printf ("\nStack is Empty!!! Deletion is not possible!!!" );
else {
printf ("\nDeleted : %d" , stack [top]);
top--;
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in C
...
void display () {
if (top == -1 )
printf ("\nStack is Empty!!!" );
else {
int i;
printf ("\nStack elements are:\n" );
for (i=top; i>=0 ; i--)
printf ("%d\n" ,stack [i]);
}
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Output
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in Java
class Stack {
private int arr[];
private int top;
private int capacity;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in Java
...
Stack(int size) {
arr = new int [size];
capacity = size;
top = -1 ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in Java
...
public void push (int x) {
if (isFull()) {
System.out.println("OverFlow\nProgram Terminated\n" );
System.exit(1 );
}
System.out.println("Inserting " + x);
arr[++top] = x;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in Java
...
public int pop () {
if (isEmpty()) {
System.out.println("STACK EMPTY" );
System.exit(1 );
}
return arr[top--];
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in Java
...
public int size () {
return top + 1 ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in Java
...
public Boolean isEmpty () {
return top == -1 ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in Java
...
public Boolean isFull () {
return top == capacity - 1 ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in Java
...
public void printStack () {
for (int i = 0 ; i <= top; i++) {
System.out.println(arr[i]);
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in Java
...
public static void main (String[] args) {
Stack stack = new Stack (5 );
stack.push(1 );
stack.push(2 );
stack.push(3 );
stack.push(4 );
stack.pop();
System.out.println("\nAfter popping out" );
stack.printStack();
}
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in C
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int count = 0 ;
struct stack {
int items[MAX];
int top;
};
typedef struct stack st ;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in C
...
void createEmptyStack (st *s) {
s->top = -1 ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in C
...
int isfull (st *s) {
if (s->top == MAX - 1 )
return 1 ;
else
return 0 ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in C
...
int isempty (st *s) {
if (s->top == -1 )
return 1 ;
else
return 0 ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in C
...
void push (st *s, int newitem) {
if (isfull(s)) {
printf ("STACK FULL" );
} else {
s->top++;
s->items[s->top] = newitem;
}
count++;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in C
...
void pop (st *s) {
if (isempty(s)) {
printf ("\n STACK EMPTY \n" );
} else {
printf ("Item popped= %d" , s->items[s->top]);
s->top--;
}
count--;
printf ("\n" );
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in C
...
void printStack (st *s) {
printf ("Stack: " );
for (int i = 0 ; i < count; i++) {
printf ("%d " , s->items[i]);
}
printf ("\n" );
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in C
...
int main () {
int ch;
st *s = (st *)malloc (sizeof (st));
createEmptyStack(s);
push(s, 1 );
push(s, 2 );
push(s, 3 );
push(s, 4 );
printStack(s);
pop(s);
printf ("\nAfter popping out\n" );
printStack(s);
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in CPP
#include <stdlib.h>
#include <iostream>
using namespace std;
#define MAX 10
int size = 0 ;
struct stack {
int items[MAX];
int top;
};
typedef struct stack st;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in CPP
void createEmptyStack (st *s) {
s->top = -1 ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in CPP
int isfull (st *s) {
if (s->top == MAX - 1 )
return 1 ;
else
return 0 ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in CPP
int isempty (st *s) {
if (s->top == -1 )
return 1 ;
else
return 0 ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in CPP
void push (st *s, int newitem) {
if (isfull (s)) {
cout << "STACK FULL" ;
} else {
s->top++;
s->items[s->top] = newitem;
}
size++;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in CPP
void pop (st *s) {
if (isempty (s)) {
cout << "\n STACK EMPTY \n" ;
} else {
cout << "Item popped= " << s->items[s->top];
s->top--;
}
size--;
cout << endl;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in CPP
void printStack (st *s) {
printf ("Stack: " );
for (int i = 0 ; i < size; i++) {
cout << s->items[i] << " " ;
}
cout << endl;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Array Implementation in CPP
int main () {
int ch;
st *s = (st *)malloc (sizeof (st));
createEmptyStack (s);
push (s, 1 );
push (s, 2 );
push (s, 3 );
push (s, 4 );
printStack (s);
pop (s);
cout << "\nAfter popping out\n" ;
printStack (s);
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Time Complexity
For the array-based implementation of a stack, the push and pop operations take constant time, i.e. O(1)
.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Applications of Stack Data Structure
Although stack is a simple data structure to implement, it is very powerful. The most common uses of a stack are:
To reverse a word - Put all the letters in a stack and pop them out. Because of the LIFO order of stack, you will get the letters in reverse order.
In compilers - Compilers use the stack to calculate the value of expressions like 2 + 4 / 5 * (7 - 9)
by converting the expression to prefix or postfix form.
In browsers - The back button in a browser saves all the URLs you have visited previously in a stack. Each time you visit a new page, it is added on top of the stack. When you press the back button, the current URL is removed from the stack, and the previous URL is accessed.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Using Linked List
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Using Linked List
The major problem with the stack implemented using an array is, it works only for a fixed number of data values.
That means the amount of data must be specified at the beginning of the implementation itself.
Stack implemented using an array is not suitable, when we don't know the size of data which we are going to use.
A stack data structure can be implemented by using a linked list data structure. The stack implemented using linked list can work for an unlimited number of values.
That means, stack implemented using linked list works for the variable size of data. So, there is no need to fix the size at the beginning of the implementation. The Stack implemented using linked list can organize as many data values as we want.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Using Linked List
In linked list implementation of a stack, every new element is inserted as 'top ' element. That means every newly inserted element is pointed by 'top '. Whenever we want to remove an element from the stack, simply remove the node which is pointed by 'top ' by moving 'top ' to its previous node in the list. The next field of the first element must be always NULL .
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Using Linked List Example
In the above example, the last inserted node is 99 and the first inserted node is 25. The order of elements inserted is 25, 32,50 and 99.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Operations using Linked List
To implement a stack using a linked list, we need to set the following things before implementing actual operations.
Step 1 - Include all the header files which are used in the program. And declare all the user defined functions .
Step 2 - Define a 'Node ' structure with two members data and next .
Step 3 - Define a Node pointer 'top ' and set it to NULL .
Step 4 - Implement the main method by displaying Menu with list of operations and make suitable function calls in the main method.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Operations using Linked List
push(value) - Inserting an element into the Stack
We can use the following steps to insert a new node into the stack...
Step 1 - Create a newNode with given value.
Step 2 - Check whether stack is Empty (top == NULL )
Step 3 - If it is Empty , then set newNode → next = NULL .
Step 4 - If it is Not Empty , then set newNode → next = top .
Step 5 - Finally, set top = newNode .
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Operations using Linked List
pop() - Deleting an Element from a Stack
We can use the following steps to delete a node from the stack...
Step 1 - Check whether stack is Empty (top == NULL ).
Step 2 - If it is Empty , then display "Stack is Empty!!! Deletion is not possible!!!" and terminate the function
Step 3 - If it is Not Empty , then define a Node pointer 'temp ' and set it to 'top '.
Step 4 - Then set 'top = top → next '.
Step 5 - Finally, delete 'temp '. (free(temp) ).
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack Operations using Linked List
display() - Displaying stack of elements
We can use the following steps to display the elements (nodes) of a stack...
Step 1 - Check whether stack is Empty (top == NULL ).
Step 2 - If it is Empty , then display 'Stack is Empty!!!' and terminate the function.
Step 3 - If it is Not Empty , then define a Node pointer 'temp' and initialize with top .
Step 4 - Display 'temp → data --->' and move it to the next node. Repeat the same until temp reaches to the first node in the stack. (temp → next != NULL ).
Step 5 - Finally! Display 'temp → data ---> NULL '.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack using Linked List in C
#include <stdio.h>
#include <conio.h>
struct Node
{
int data;
struct Node *next ;
}*top = NULL ;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack using Linked List in C
...
void push (int ) ;
void pop () ;
void display () ;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack using Linked List in C
...
void main ()
{
int choice, value;
clrscr();
printf ("\n:: Stack using Linked List ::\n" );
while (1 ){
printf ("\n****** MENU ******\n" );
printf ("1. Push\n2. Pop\n3. Display\n4. Exit\n" );
printf ("Enter your choice: " );
scanf ("%d" ,&choice);
switch (choice){
case 1 : printf ("Enter the value to be insert: " );
scanf ("%d" , &value);
push(value);
break ;
case 2 : pop(); break ;
case 3 : display(); break ;
case 4 : exit (0 );
default : printf ("\nWrong selection!!! Please try again!!!\n" );
}
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack using Linked List in C
...
void push (int value)
{
struct Node *newNode ;
newNode = (struct Node*)malloc (sizeof (struct Node));
newNode->data = value;
if (top == NULL )
newNode->next = NULL ;
else
newNode->next = top;
top = newNode;
printf ("\nInsertion is Success!!!\n" );
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack using Linked List in C
...
void pop ()
{
if (top == NULL )
printf ("\nStack is Empty!!!\n" );
else {
struct Node *temp = top;
printf ("\nDeleted element: %d" , temp->data);
top = temp->next;
free (temp);
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Stack using Linked List in C
...
void display ()
{
if (top == NULL )
printf ("\nStack is Empty!!!\n" );
else {
struct Node *temp = top;
while (temp->next != NULL ){
printf ("%d--->" ,temp->data);
temp = temp -> next;
}
printf ("%d--->NULL" ,temp->data);
}
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Output
RTEU CE205 Week-3
CE205 Data Structures Week-3
Output
RTEU CE205 Week-3
CE205 Data Structures Week-3
Expressions
RTEU CE205 Week-3
CE205 Data Structures Week-3
What is an Expression?
In any programming language, if we want to perform any calculation or to frame a condition etc.,
we use a set of symbols to perform the task.
These set of symbols makes an expression.
RTEU CE205 Week-3
CE205 Data Structures Week-3
What is an Expression?
An expression is a collection of operators and operands that represents a specific value.
In above definition, operator is a symbol which performs a particular task like arithmetic operation or logical operation or conditional operation etc.,
Operands are the values on which the operators can perform the task. Here operand can be a direct value or variable or address of memory location.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Expression Types
Based on the operator position, expressions are divided into THREE types. They are as follows...
Infix Expression
Postfix Expression
Prefix Expression
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix Expression
In infix expression, operator is used in between the operands.
The general structure of an Infix expression is as follows.
Operand1 Operator Operand2
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix Expression Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression
In postfix expression, operator is used after operands.
We can say that "Operator follows the Operands ".
The general structure of Postfix expression is as follows.
Operand1 Operand2 Operator
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Prefix Expression
In prefix expression, operator is used before operands.
We can say that "Operands follows the Operator ".
The general structure of Prefix expression is as follows.
Operator Operand1 Operand2
RTEU CE205 Week-3
CE205 Data Structures Week-3
Prefix Expression Example
Every expression can be represented using all the above three different types of expressions.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Expression Conversions
And we can convert an expression from one form to another form like
Infix to Postfix ,
Infix to Prefix ,
Prefix to Postfix and vice versa.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion
Any expression can be represented using three types of expressions (Infix, Postfix, and Prefix).
We can also convert one type of expression to another type of expression like
Infix to Postfix,
Infix to Prefix,
Postfix to Prefix and vice versa.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion
To convert any Infix expression into Postfix or Prefix expression we can use the following procedure.
Find all the operators in the given Infix Expression.
Find the order of operators evaluated according to their Operator precedence.
Convert each operator into required type of expression (Postfix or Prefix) in the same order.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Example
Consider the following Infix Expression to be converted into Postfix Expression.
D = A + B * C
Step 1 - The Operators in the given Infix Expression : = , + , *
Step 2 - The Order of Operators according to their preference : * , + , =
Step 3 - Now, convert the first operator *
-> D = A + B C *
Step 4 - Convert the next operator +
-> D = A BC* +
Step 5 - Convert the next operator =
-> D ABC*+ =
Finally, given Infix Expression is converted into Postfix Expression as follows...
D A B C * + =
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion using Stack Data Structure
To convert Infix Expression into Postfix Expression using a stack data structure, We can use the following steps
Read all the symbols one by one from left to right in the given Infix Expression.
If the reading symbol is operand, then directly print it to the result (Output).
If the reading symbol is left parenthesis '(', then Push it on to the Stack.
If the reading symbol is right parenthesis ')', then Pop all the contents of stack until respective left parenthesis is poped and print each poped symbol to the result.
If the reading symbol is operator (+ , - , * , / etc.,), then Push it on to the Stack. However, first pop the operators which are already on the stack that have higher or equal precedence than current operator and print them to the result.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion Example
Consider the following Infix Expression...
( A + B ) * ( C - D )
The given infix expression can be converted into postfix expression using Stack data Structure as follows.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Infix to Postfix Conversion Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
The final Postfix Expression is as follows...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Evaluation
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Evaluation
A postfix expression is a collection of operators and operands in which the operator is placed after the operands.
That means, in a postfix expression the operator follows the operands.
Postfix Expression has following general structure
Operand1 Operand2 Operator
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Evaluation Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Evaluation using Stack Data Structure
A postfix expression can be evaluated using the Stack data structure.
To evaluate a postfix expression using Stack data structure we can use the following steps
Read all the symbols one by one from left to right in the given Postfix Expression
If the reading symbol is operand, then push it on to the Stack.
If the reading symbol is operator (+ , - , * , / etc.,), then perform TWO pop operations and store the two popped oparands in two different variables (operand1 and operand2). Then perform reading symbol operation using operand1 and operand2 and push result back on to the Stack.
Finally! perform a pop operation and display the popped value as final result.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Evaluation Example
Consider the following Expression.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Evaluation Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Evaluation Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Evaluation Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Evaluation Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Evaluation Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Evaluation Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Evaluation Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Evaluation Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Evaluation Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Postfix Expression Evaluation Example
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue ADT
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Data Structure
In this tutorial, you will learn what a queue is. Also, you will find implementation of queue in C, C++, Java and Python.
A queue is a useful data structure in programming. It is similar to the ticket queue outside a cinema hall, where the first person entering the queue is the first person who gets the ticket.
Queue follows the First In First Out (FIFO) rule - the item that goes in first is the item that comes out first.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Data Structure
FIFO Representation of Queue
In the above image, since 1 was kept in the queue before 2,
it is the first to be removed from the queue as well.
It follows the FIFO rule.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Data Structure
In programming terms, putting items in the queue is called enqueue , and removing items from the queue is called dequeue .
We can implement the queue in any programming language like C, C++, Java, Python or C#, but the specification is pretty much the same.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Basic Operations of Queue
A queue is an object (an abstract data structure - ADT) that allows the following operations:
Enqueue : Add an element to the end of the queue
Dequeue : Remove an element from the front of the queue
IsEmpty : Check if the queue is empty
IsFull : Check if the queue is full
Peek : Get the value of the front of the queue without removing it
RTEU CE205 Week-3
CE205 Data Structures Week-3
Working of Queue
Queue operations work as follows:
two pointers FRONT and REAR
FRONT track the first element of the queue
REAR track the last element of the queue
initially, set value of FRONT and REAR to -1
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue Operation
check if the queue is full
for the first element, set the value of FRONT to 0
increase the REAR index by 1
add the new element in the position pointed to by REAR
RTEU CE205 Week-3
CE205 Data Structures Week-3
Dequeue Operation
check if the queue is empty
return the value pointed by FRONT
increase the FRONT index by 1
for the last element, reset the values of FRONT and REAR to -1
RTEU CE205 Week-3
CE205 Data Structures Week-3
Dequeue Operation
RTEU CE205 Week-3
CE205 Data Structures Week-3
Dequeue Operation
RTEU CE205 Week-3
CE205 Data Structures Week-3
Dequeue Operation
RTEU CE205 Week-3
CE205 Data Structures Week-3
Dequeue Operation
RTEU CE205 Week-3
CE205 Data Structures Week-3
Dequeue Operation
RTEU CE205 Week-3
CE205 Data Structures Week-3
Dequeue Operation
RTEU CE205 Week-3
CE205 Data Structures Week-3
Dequeue Operation
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in C
RTEU CE205 Week-3
CE205 Data Structures Week-3
#include <stdio.h>
#define SIZE 5
void enQueue (int ) ;
void deQueue () ;
void display () ;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in C
RTEU CE205 Week-3
CE205 Data Structures Week-3
...
int items[SIZE], front = -1 , rear = -1 ;
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in C
RTEU CE205 Week-3
CE205 Data Structures Week-3
...
int main () {
deQueue();
enQueue(1 );
enQueue(2 );
enQueue(3 );
enQueue(4 );
enQueue(5 );
enQueue(6 );
display();
deQueue();
display();
return 0 ;
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in C
RTEU CE205 Week-3
CE205 Data Structures Week-3
...
void enQueue (int value) {
if (rear == SIZE - 1 )
printf ("\nQueue is Full!!" );
else {
if (front == -1 )
front = 0 ;
rear++;
items[rear] = value;
printf ("\nInserted -> %d" , value);
}
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in C
RTEU CE205 Week-3
CE205 Data Structures Week-3
...
void deQueue () {
if (front == -1 )
printf ("\nQueue is Empty!!" );
else {
printf ("\nDeleted : %d" , items[front]);
front++;
if (front > rear)
front = rear = -1 ;
}
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in C
RTEU CE205 Week-3
CE205 Data Structures Week-3
...
void display () {
if (rear == -1 )
printf ("\nQueue is Empty!!!" );
else {
int i;
printf ("\nQueue elements are:\n" );
for (i = front; i <= rear; i++)
printf ("%d " , items[i]);
}
printf ("\n" );
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in C++
RTEU CE205 Week-3
CE205 Data Structures Week-3
...
#include <iostream>
#define SIZE 5
using namespace std;
class Queue {
private :
int items[SIZE], front, rear;
public :
Queue () {
front = -1 ;
rear = -1 ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in C++
RTEU CE205 Week-3
CE205 Data Structures Week-3
...
bool isFull () {
if (front == 0 && rear == SIZE - 1 ) {
return true ;
}
return false ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in C++
RTEU CE205 Week-3
CE205 Data Structures Week-3
...
bool isEmpty () {
if (front == -1 )
return true ;
else
return false ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in C++
RTEU CE205 Week-3
CE205 Data Structures Week-3
...
void enQueue (int element) {
if (isFull ()) {
cout << "Queue is full" ;
} else {
if (front == -1 ) front = 0 ;
rear++;
items[rear] = element;
cout << endl
<< "Inserted " << element << endl;
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in C++
RTEU CE205 Week-3
CE205 Data Structures Week-3
...
int deQueue () {
int element;
if (isEmpty ()) {
cout << "Queue is empty" << endl;
return (-1 );
} else {
element = items[front];
if (front >= rear) {
front = -1 ;
rear = -1 ;
}
else {
front++;
}
cout << endl
<< "Deleted -> " << element << endl;
return (element);
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in C++
RTEU CE205 Week-3
CE205 Data Structures Week-3
...
void display () {
int i;
if (isEmpty ()) {
cout << endl
<< "Empty Queue" << endl;
} else {
cout << endl
<< "Front index-> " << front;
cout << endl
<< "Items -> " ;
for (i = front; i <= rear; i++)
cout << items[i] << " " ;
cout << endl
<< "Rear index-> " << rear << endl;
}
}
};
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in C++
RTEU CE205 Week-3
CE205 Data Structures Week-3
...
int main () {
Queue q;
q.deQueue ();
q.enQueue (1 );
q.enQueue (2 );
q.enQueue (3 );
q.enQueue (4 );
q.enQueue (5 );
q.enQueue (6 );
q.display ();
q.deQueue ();
q.display ();
return 0 ;
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in Java
public class Queue {
int SIZE = 5 ;
int items[] = new int [SIZE];
int front, rear;
Queue() {
front = -1 ;
rear = -1 ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in Java
...
boolean isFull () {
if (front == 0 && rear == SIZE - 1 ) {
return true ;
}
return false ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in Java
...
boolean isEmpty () {
if (front == -1 )
return true ;
else
return false ;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in Java
...
void enQueue (int element) {
if (isFull()) {
System.out.println("Queue is full" );
} else {
if (front == -1 )
front = 0 ;
rear++;
items[rear] = element;
System.out.println("Inserted " + element);
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in Java
...
int deQueue () {
int element;
if (isEmpty()) {
System.out.println("Queue is empty" );
return (-1 );
} else {
element = items[front];
if (front >= rear) {
front = -1 ;
rear = -1 ;
}
else {
front++;
}
System.out.println("Deleted -> " + element);
return (element);
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in Java
...
void display () {
int i;
if (isEmpty()) {
System.out.println("Empty Queue" );
} else {
System.out.println("\nFront index-> " + front);
System.out.println("Items -> " );
for (i = front; i <= rear; i++)
System.out.print(items[i] + " " );
System.out.println("\nRear index-> " + rear);
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Enqueue and Dequeue Operations in Java
...
public static void main (String[] args) {
Queue q = new Queue ();
q.deQueue();
q.enQueue(1 );
q.enQueue(2 );
q.enQueue(3 );
q.enQueue(4 );
q.enQueue(5 );
q.enQueue(6 );
q.display();
q.deQueue();
q.display();
}
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Limitations of Queue
As you can see in the image below, after a bit of enqueuing and dequeuing, the size of the queue has been reduced.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Limitations of Queue
And we can only add indexes 0 and 1 only when the queue is reset (when all the elements have been dequeued).
After REAR reaches the last index,
if we can store extra elements in the empty spaces (0 and 1),
we can make use of the empty spaces. This is implemented by a modified queue called the circular queue .
RTEU CE205 Week-3
CE205 Data Structures Week-3
Complexity Analysis
The complexity of enqueue and dequeue operations in a queue using an array is O(1)
. If you use pop(N)
in python code,
then the complexity might be O(n)
depending on the position of the item to be popped.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Applications of Queue
CPU scheduling, Disk Scheduling
When data is transferred asynchronously between two processes.The queue is used for synchronization. For example: IO Buffers, pipes, file IO, etc
Handling of interrupts in real-time systems.
Call Center phone systems use Queues to hold people calling them in order.
RTEU CE205 Week-3
CE205 Data Structures Week-3
What is a Queue?
Queue is a linear data structure in which the insertion and deletion operations are performed at two different ends.
In a queue data structure, adding and removing elements are performed at two different positions.
The insertion is performed at one end and deletion is performed at another end. - In a queue data structure, the insertion operation is performed at a position which is known as 'rear ' and the deletion operation is performed at a position which is known as 'front '.
In queue data structure, the insertion and deletion operations are performed based on FIFO (First In First Out) principle.
RTEU CE205 Week-3
CE205 Data Structures Week-3
What is a Queue?
In a queue data structure,
the insertion operation is performed using a function called "enQueue() " and
deletion operation is performed using a function called "deQueue() ".
RTEU CE205 Week-3
CE205 Data Structures Week-3
What is a Queue?
Queue data structure can be defined as follows.
Queue data structure is a linear data structure in which the operations are performed based on FIFO principle.
A queue data structure can also be defined as
"Queue data structure is a collection of similar data items in which insertion and deletion operations are performed based on FIFO principle".
RTEU CE205 Week-3
CE205 Data Structures Week-3
What is a Queue?
Queue after inserting 25, 30, 51, 60 and 85.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Operations on a Queue
The following operations are performed on a queue data structure
enQueue(value) - (To insert an element into the queue)
deQueue() - (To delete an element from the queue)
display() - (To display the elements of the queue)
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Implementation
Queue data structure can be implemented in two ways. They are as follows...
Using Array
Using Linked List
When a queue is implemented using an array, that queue can organize an only limited number of elements.
When a queue is implemented using a linked list, that queue can organize an unlimited number of elements.
RTEU CE205 Week-3
CE205 Data Structures Week-3
First Come First Serve, FCFS, FIFO
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Data structure Using Array
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Datastructure Using Array
A queue data structure can be implemented using one dimensional array.
The queue implemented using array stores only fixed number of data values.
The implementation of queue data structure using array is very simple.
Just define a one dimensional array of specific size and insert or delete the values into that array by using FIFO (First In First Out) principle with the help of variables 'front' and 'rear '.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Datastructure Using Array
Initially both 'front ' and 'rear ' are set to -1. Whenever, we want to insert a new value into the queue, increment 'rear ' value by one and then insert at that position.
Whenever we want to delete a value from the queue, then delete the element which is at 'front' position and increment 'front' value by one.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Operations using Array
Queue data structure using array can be implemented as follows.
Before we implement actual operations, first follow the below steps to create an empty queue.
Step 1 - Include all the header files which are used in the program and define a constant 'SIZE' with specific value.
Step 2 - Declare all the user defined functions which are used in queue implementation.
Step 3 - Create a one dimensional array with above defined SIZE (int queue[SIZE] )
Step 4 - Define two integer variables 'front' and 'rear ' and initialize both with '-1' . (int front = -1, rear = -1 )
Step 5 - Then implement main method by displaying menu of operations list and make suitable function calls to perform operation selected by the user on queue.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Operations using Array
enQueue(value) - Inserting value into the queue
In a queue data structure, enQueue() is a function used to insert a new element into the queue.
In a queue, the new element is always inserted at rear position.
The enQueue() function takes one integer value as a parameter and inserts that value into the queue. We can use the following steps to insert an element into the queue
Step 1 - Check whether queue is FULL . (rear == SIZE-1 )
Step 2 - If it is FULL , then display "Queue is FULL!!! Insertion is not possible!!!" and terminate the function.
Step 3 - If it is NOT FULL , then increment rear value by one (rear++ ) and set queue[rear] = value .
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Operations using Array
deQueue() - Deleting a value from the Queue
In a queue data structure, deQueue() is a function used to delete an element from the queue.
In a queue, the element is always deleted from front position.
The deQueue() function does not take any value as parameter.
We can use the following steps to delete an element from the queue.
Step 1 - Check whether queue is EMPTY . (front == rear )
Step 2 - If it is EMPTY , then display "Queue is EMPTY!!! Deletion is not possible!!!" and terminate the function.
Step 3 - If it is NOT EMPTY , then increment the front value by one (front ++ ). Then display queue[front] as deleted element. Then check whether both front and rear are equal (front == rear ), if it TRUE , then set both front and rear to '-1 ' (front = rear = -1 ).
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Operations using Array
display() - Displays the elements of a Queue
-We can use the following steps to display the elements of a queue...
Step 1 - Check whether queue is EMPTY . (front == rear )
Step 2 - If it is EMPTY , then display "Queue is EMPTY!!!" and terminate the function.
Step 3 - If it is NOT EMPTY , then define an integer variable 'i ' and set 'i = front+1 '.
Step 4 - Display 'queue[i] ' value and increment 'i ' value by one (i++ ). Repeat the same until 'i ' value reaches to rear (i <= rear )
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Datastructure using Array in C
#include <stdio.h>
#include <conio.h>
#define SIZE 10
void enQueue (int ) ;
void deQueue () ;
void display () ;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Datastructure using Array in C
...
int queue [SIZE], front = -1 , rear = -1 ;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Datastructure using Array in C
...
void main ()
{
int value, choice;
clrscr();
while (1 ){
printf ("\n\n***** MENU *****\n" );
printf ("1. Insertion\n2. Deletion\n3. Display\n4. Exit" );
printf ("\nEnter your choice: " );
scanf ("%d" ,&choice);
switch (choice){
case 1 : printf ("Enter the value to be insert: " );
scanf ("%d" ,&value);
enQueue(value);
break ;
case 2 : deQueue();
break ;
case 3 : display();
break ;
case 4 : exit (0 );
default : printf ("\nWrong selection!!! Try again!!!" );
}
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Datastructure using Array in C
...
void enQueue (int value) {
if (rear == SIZE-1 )
printf ("\nQueue is Full!!! Insertion is not possible!!!" );
else {
if (front == -1 )
front = 0 ;
rear++;
queue [rear] = value;
printf ("\nInsertion success!!!" );
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Datastructure using Array in C
...
void deQueue () {
if (front == rear)
printf ("\nQueue is Empty!!! Deletion is not possible!!!" );
else {
printf ("\nDeleted : %d" , queue [front]);
front++;
if (front == rear)
front = rear = -1 ;
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Datastructure using Array in C
...
void display () {
if (rear == -1 )
printf ("\nQueue is Empty!!!" );
else {
int i;
printf ("\nQueue elements are:\n" );
for (i=front; i<=rear; i++)
printf ("%d\t" ,queue [i]);
}
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Using Linked List
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Using Linked List
The major problem with the queue implemented using an array is,
It will work for an only fixed number of data values.
That means, the amount of data must be specified at the beginning itself.
Queue using an array is not suitable when we don't know the size of data which we are going to use.
A queue data structure can be implemented using a linked list data structure. The queue which is implemented using a linked list can work for an unlimited number of values.
That means, queue using linked list can work for the variable size of data (No need to fix the size at the beginning of the implementation).
The Queue implemented using linked list can organize as many data values as we want.
In linked list implementation of a queue, the last inserted node is always pointed by 'rear ' and the first node is always pointed by 'front '.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue Using Linked List Example
In above example, the last inserted node is 50 and it is pointed by 'rear ' and the first inserted node is 10 and it is pointed by 'front '. The order of elements inserted is 10, 15, 22 and 50.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Operations
To implement queue using linked list, we need to set the following things before implementing actual operations.
Step 1 - Include all the header files which are used in the program. And declare all the user defined functions .
Step 2 - Define a 'Node ' structure with two members data and next .
Step 3 - Define two Node pointers 'front ' and 'rear ' and set both to NULL .
Step 4 - Implement the main method by displaying Menu of list of operations and make suitable function calls in the main method to perform user selected operation.
RTEU CE205 Week-3
CE205 Data Structures Week-3
enQueue(value) - Inserting an element into the Queue
We can use the following steps to insert a new node into the queue...
Step 1 - Create a newNode with given value and set 'newNode → next ' to NULL .
Step 2 - Check whether queue is Empty (rear == NULL )
Step 3 - If it is Empty then, set front = newNode and rear = newNode .
Step 4 - If it is Not Empty then, set rear → next = newNode and rear = newNode .
RTEU CE205 Week-3
CE205 Data Structures Week-3
deQueue() - Deleting an Element from Queue
We can use the following steps to delete a node from the queue...
Step 1 - Check whether queue is Empty (front == NULL ).
Step 2 - If it is Empty , then display "Queue is Empty!!! Deletion is not possible!!!" and terminate from the function
Step 3 - If it is Not Empty then, define a Node pointer 'temp ' and set it to 'front '.
Step 4 - Then set 'front = front → next ' and delete 'temp ' (free(temp) ).
RTEU CE205 Week-3
CE205 Data Structures Week-3
display() - Displaying the elements of Queue
We can use the following steps to display the elements (nodes) of a queue...
Step 1 - Check whether queue is Empty (front == NULL ).
Step 2 - If it is Empty then, display 'Queue is Empty!!!' and terminate the function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with front .
Step 4 - Display 'temp → data --->' and move it to the next node. Repeat the same until 'temp ' reaches to 'rear ' (temp → next != NULL ).
Step 5 - Finally! Display 'temp → data ---> NULL '.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue using Linked List in C
#include <stdio.h>
#include <conio.h>
struct Node
{
int data;
struct Node *next ;
}*front = NULL ,*rear = NULL ;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue using Linked List in C
...
void insert (int ) ;
void delete () ;
void display () ;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue using Linked List in C
...
void main ()
{
int choice, value;
clrscr();
printf ("\n:: Queue Implementation using Linked List ::\n" );
while (1 ){
printf ("\n****** MENU ******\n" );
printf ("1. Insert\n2. Delete\n3. Display\n4. Exit\n" );
printf ("Enter your choice: " );
scanf ("%d" ,&choice);
switch (choice){
case 1 : printf ("Enter the value to be insert: " );
scanf ("%d" , &value);
insert(value);
break ;
case 2 : delete(); break ;
case 3 : display(); break ;
case 4 : exit (0 );
default : printf ("\nWrong selection!!! Please try again!!!\n" );
}
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue using Linked List in C
...
void insert (int value)
{
struct Node *newNode ;
newNode = (struct Node*)malloc (sizeof (struct Node));
newNode->data = value;
newNode -> next = NULL ;
if (front == NULL )
front = rear = newNode;
else {
rear -> next = newNode;
rear = newNode;
}
printf ("\nInsertion is Success!!!\n" );
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue using Linked List in C
...
void delete ()
{
if (front == NULL )
printf ("\nQueue is Empty!!!\n" );
else {
struct Node *temp = front;
front = front -> next;
printf ("\nDeleted element: %d\n" , temp->data);
free (temp);
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Queue using Linked List in C
...
void display ()
{
if (front == NULL )
printf ("\nQueue is Empty!!!\n" );
else {
struct Node *temp = front;
while (temp->next != NULL ){
printf ("%d--->" ,temp->data);
temp = temp -> next;
}
printf ("%d--->NULL\n" ,temp->data);
}
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Output
RTEU CE205 Week-3
CE205 Data Structures Week-3
Output
RTEU CE205 Week-3
CE205 Data Structures Week-3
Circular Queue Data structure
RTEU CE205 Week-3
CE205 Data Structures Week-3
Circular Queue Datastructure
In a normal Queue Data Structure, we can insert elements until queue becomes full.
But once the queue becomes full, we can not insert the next element until all the elements are deleted from the queue.
For example, consider the queue below.
The queue after inserting all the elements into it is as follows...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Circular Queue Datastructure
Now consider the following situation after deleting three elements from the queue.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Circular Queue Datastructure
RTEU CE205 Week-3
CE205 Data Structures Week-3
Circular Queue Datastructure
This situation also says that Queue is Full and we cannot insert the new element because 'rear ' is still at last position.
In the above situation, even though we have empty positions in the queue we can not make use of them to insert the new element.
This is the major problem in a normal queue data structure.
To overcome this problem we use a circular queue data structure.
RTEU CE205 Week-3
CE205 Data Structures Week-3
What is Circular Queue?
A Circular Queue can be defined as follows.
A circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle.
Graphical representation of a circular queue is as follows.
![center h:300px](assets/Circular Q.png)
RTEU CE205 Week-3
CE205 Data Structures Week-3
Implementation of Circular Queue
To implement a circular queue data structure using an array, we first perform the following steps before we implement actual operations.
Step 1 - Include all the header files which are used in the program and define a constant 'SIZE' with specific value.
Step 2 - Declare all user defined functions used in circular queue implementation.
Step 3 - Create a one dimensional array with above defined SIZE (int cQueue[SIZE] )
Step 4 - Define two integer variables 'front' and 'rear ' and initialize both with '-1' . (int front = -1, rear = -1 )
Step 5 - Implement main method by displaying menu of operations list and make suitable function calls to perform operation selected by the user on circular queue.
RTEU CE205 Week-3
CE205 Data Structures Week-3
enQueue(value) - Inserting value into the Circular Queue
In a circular queue, enQueue() is a function which is used to insert an element into the circular queue. In a circular queue, the new element is always inserted at rear position. The enQueue() function takes one integer value as parameter and inserts that value into the circular queue. We can use the following steps to insert an element into the circular queue...
RTEU CE205 Week-3
CE205 Data Structures Week-3
enQueue(value) - Inserting value into the Circular Queue
Step 1 - Check whether queue is FULL . ((rear == SIZE-1 && front == 0) || (front == rear+1) )
Step 2 - If it is FULL , then display "Queue is FULL!!! Insertion is not possible!!!" and terminate the function.
Step 3 - If it is NOT FULL , then check rear == SIZE - 1 && front != 0 if it is TRUE , then set rear = -1 .
Step 4 - Increment rear value by one (rear++ ), set queue[rear] = value and check 'front == -1 ' if it is TRUE , then set front = 0 .
RTEU CE205 Week-3
CE205 Data Structures Week-3
deQueue() - Deleting a value from the Circular Queue
In a circular queue, deQueue() is a function used to delete an element from the circular queue. In a circular queue, the element is always deleted from front position. The deQueue() function doesn't take any value as a parameter. We can use the following steps to delete an element from the circular queue.
RTEU CE205 Week-3
CE205 Data Structures Week-3
deQueue() - Deleting a value from the Circular Queue
Step 1 - Check whether queue is EMPTY . (front == -1 && rear == -1 )
Step 2 - If it is EMPTY , then display "Queue is EMPTY!!! Deletion is not possible!!!" and terminate the function.
Step 3 - If it is NOT EMPTY , then display queue[front] as deleted element and increment the front value by one (front ++ ). Then check whether front == SIZE , if it is TRUE , then set front = 0 . Then check whether both front - 1 and rear are equal (front -1 == rear ), if it TRUE , then set both front and rear to '-1 ' (front = rear = -1 ).
RTEU CE205 Week-3
CE205 Data Structures Week-3
display() - Displays the elements of a Circular Queue
We can use the following steps to display the elements of a circular queue...
Step 1 - Check whether queue is EMPTY . (front == -1 )
Step 2 - If it is EMPTY , then display "Queue is EMPTY!!!" and terminate the function.
Step 3 - If it is NOT EMPTY , then define an integer variable 'i ' and set 'i = front '.
Step 4 - Check whether 'front <= rear ', if it is TRUE , then display 'queue[i] ' value and increment 'i ' value by one (i++ ). Repeat the same until 'i <= rear ' becomes FALSE .
Step 5 - If 'front <= rear ' is FALSE , then display 'queue[i] ' value and increment 'i ' value by one (i++ ). Repeat the same until'i <= SIZE - 1 ' becomes FALSE .
Step 6 - Set i to 0 .
Step 7 - Again display 'cQueue[i] ' value and increment i value by one (i++ ). Repeat the same until 'i <= rear ' becomes FALSE .
RTEU CE205 Week-3
CE205 Data Structures Week-3
Circular Queue using array in C
#include <stdio.h>
#include <conio.h>
#define SIZE 5
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Circular Queue using array in C
...
void enQueue (int ) ;
void deQueue () ;
void display () ;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Circular Queue using array in C
...
int cQueue[SIZE], front = -1 , rear = -1 ;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Circular Queue using array in C
...
void main ()
{
int choice, value;
clrscr();
while (1 ){
printf ("\n****** MENU ******\n" );
printf ("1. Insert\n2. Delete\n3. Display\n4. Exit\n" );
printf ("Enter your choice: " );
scanf ("%d" ,&choice);
switch (choice){
case 1 : printf ("\nEnter the value to be insert: " );
scanf ("%d" ,&value);
enQueue(value);
break ;
case 2 : deQueue();
break ;
case 3 : display();
break ;
case 4 : exit (0 );
default : printf ("\nPlease select the correct choice!!!\n" );
}
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Circular Queue using array in C
...
void enQueue (int value)
{
if ((front == 0 && rear == SIZE - 1 ) || (front == rear+1 ))
printf ("\nCircular Queue is Full! Insertion not possible!!!\n" );
else {
if (rear == SIZE-1 && front != 0 )
rear = -1 ;
cQueue[++rear] = value;
printf ("\nInsertion Success!!!\n" );
if (front == -1 )
front = 0 ;
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Circular Queue using array in C
...
void deQueue ()
{
if (front == -1 && rear == -1 )
printf ("\nCircular Queue is Empty! Deletion is not possible!!!\n" );
else {
printf ("\nDeleted element : %d\n" ,cQueue[front++]);
if (front == SIZE)
front = 0 ;
if (front-1 == rear)
front = rear = -1 ;
}
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Circular Queue using array in C
...
void display ()
{
if (front == -1 )
printf ("\nCircular Queue is Empty!!!\n" );
else {
int i = front;
printf ("\nCircular Queue Elements are : \n" );
if (front <= rear){
while (i <= rear)
printf ("%d\t" ,cQueue[i++]);
}
else {
while (i <= SIZE - 1 )
printf ("%d\t" , cQueue[i++]);
i = 0 ;
while (i <= rear)
printf ("%d\t" ,cQueue[i++]);
}
}
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Output
RTEU CE205 Week-3
CE205 Data Structures Week-3
Output
RTEU CE205 Week-3
CE205 Data Structures Week-3
Double Ended Queue Data structure
RTEU CE205 Week-3
CE205 Data Structures Week-3
Double Ended Queue Datastructure
Double Ended Queue is also a Queue data structure in which the insertion and deletion operations are performed at
both the ends (front and rear ).
That means, we can insert at both front and rear positions and can delete from both front and rear positions.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Double Ended Queue Datastructure
RTEU CE205 Week-3
CE205 Data Structures Week-3
Double Ended Queue Datastructure
Double Ended Queue can be represented in TWO ways, those are as follows.
Input Restricted Double Ended Queue
Output Restricted Double Ended Queue
RTEU CE205 Week-3
CE205 Data Structures Week-3
In input restricted double-ended queue, the insertion operation is performed at only one end and deletion operation is performed at both the ends.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Output Restricted Double Ended Queue
In output restricted double ended queue, the deletion operation is performed at only one end and insertion operation is performed at both the ends.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Double Ended Queue using Array in C
#include <stdio.h>
#include <conio.h>
#define SIZE 100
void enQueue (int ) ;
int deQueueFront () ;
int deQueueRear () ;
void enQueueRear (int ) ;
void enQueueFront (int ) ;
void display () ;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Double Ended Queue using Array in C
...
int queue [SIZE];
int rear = 0 , front = 0 ;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Double Ended Queue using Array in C
...
int main ()
{
char ch;
int choice1, choice2, value;
printf ("\n******* Type of Double Ended Queue *******\n" );
do
{
printf ("\n1.Input-restricted deque \n" );
printf ("2.output-restricted deque \n" );
printf ("\nEnter your choice of Queue Type : " );
scanf ("%d" ,&choice1);
switch (choice1)
{
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Double Ended Queue using Array in C
...
case 1 :
printf ("\nSelect the Operation\n" );
printf ("1.Insert\n2.Delete from Rear\n3.Delete from Front\n4. Display" );
do
{
printf ("\nEnter your choice for the operation in c deque: " );
scanf ("%d" ,&choice2);
switch (choice2)
{
case 1 : enQueueRear(value);
display();
break ;
case 2 : value = deQueueRear();
printf ("\nThe value deleted is %d" ,value);
display();
break ;
case 3 : value=deQueueFront();
printf ("\nThe value deleted is %d" ,value);
display();
break ;
case 4 : display();
break ;
default :printf ("Wrong choice" );
}
printf ("\nDo you want to perform another operation (Y/N): " );
ch=getch();
}while (ch=='y' ||ch=='Y' );
getch();
break ;
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Double Ended Queue using Array in C
...
case 2 :
printf ("\n---- Select the Operation ----\n" );
printf ("1. Insert at Rear\n2. Insert at Front\n3. Delete\n4. Display" );
do
{
printf ("\nEnter your choice for the operation: " );
scanf ("%d" ,&choice2);
switch (choice2)
{
case 1 : enQueueRear(value);
display();
break ;
case 2 : enQueueFront(value);
display();
break ;
case 3 : value = deQueueFront();
printf ("\nThe value deleted is %d" ,value);
display();
break ;
case 4 : display();
break ;
default :printf ("Wrong choice" );
}
printf ("\nDo you want to perform another operation (Y/N): " );
ch=getch();
} while (ch=='y' ||ch=='Y' );
getch();
break ;
}
printf ("\nDo you want to continue(y/n):" );
ch=getch();
}while (ch=='y' ||ch=='Y' );
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Double Ended Queue using Array in C
...
void enQueueRear (int value)
{
char ch;
if (front == SIZE/2 )
{
printf ("\nQueue is full!!! Insertion is not possible!!! " );
return ;
}
do
{
printf ("\nEnter the value to be inserted:" );
scanf ("%d" ,&value);
queue [front] = value;
front++;
printf ("Do you want to continue insertion Y/N" );
ch=getch();
}while (ch=='y' );
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Double Ended Queue using Array in C
...
void enQueueFront (int value)
{
char ch;
if (front==SIZE/2 )
{
printf ("\nQueue is full!!! Insertion is not possible!!!" );
return ;
}
do
{
printf ("\nEnter the value to be inserted:" );
scanf ("%d" ,&value);
rear--;
queue [rear] = value;
printf ("Do you want to continue insertion Y/N" );
ch = getch();
}
while (ch == 'y' );
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Double Ended Queue using Array in C
...
int deQueueRear ()
{
int deleted;
if (front == rear)
{
printf ("\nQueue is Empty!!! Deletion is not possible!!!" );
return 0 ;
}
front--;
deleted = queue [front+1 ];
return deleted;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Double Ended Queue using Array in C
...
int deQueueFront ()
{
int deleted;
if (front == rear)
{
printf ("\nQueue is Empty!!! Deletion is not possible!!!" );
return 0 ;
}
rear++;
deleted = queue [rear-1 ];
return deleted;
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Double Ended Queue using Array in C
...
void display ()
{
int i;
if (front == rear)
printf ("\nQueue is Empty!!! Deletion is not possible!!!" );
else {
printf ("\nThe Queue elements are:" );
for (i=rear; i < front; i++)
{
printf ("%d\t " ,queue [i]);
}
}
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Output
RTEU CE205 Week-3
CE205 Data Structures Week-3
Multilevel Queue (MLQ)
RTEU CE205 Week-3
CE205 Data Structures Week-3
Priority Queue
A priority queue is a special type of queue in which each element is associated with a priority and is served according to its priority. If elements with the same priority occur, they are served according to their order in the queue.
Insertion occurs based on the arrival of the values and removal occurs based on priority.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Hanoi Tower
Geeks for Geeks
Recursive Version
Iterative Version
RTEU CE205 Week-3
CE205 Data Structures Week-3
Hanoi Tower Iterative Algorithm:
S = Source
A = Aux
D = Dest
Calculate the total number of moves required i.e.
here n is number of disks.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Hanoi Tower Iterative Algorithm:
If number of disks (i.e. n) is even then interchange destination
pole and auxiliary pole .
for i = 1 to total number of moves:
if i%3 == 1:
legal movement of top disk between source pole and destination pole
if i%3 == 2:
legal movement top disk between source pole and auxiliary pole
if i%3 == 0:
legal movement top disk between auxiliary pole and destination pole
RTEU CE205 Week-3
CE205 Data Structures Week-3
Hanoi Tower Iterative Algorithm
Tower of Hanoi is a mathematical puzzle where we have three rods (A , B , and C ) and N disks.
Initially, all the disks are stacked in decreasing value of diameter
i.e., the smallest disk is placed on the top and they are on rod A .
The objective of the puzzle is to move the entire stack to another rod (here considered C ), obeying the following simple rules
Only one disk can be moved at a time.
Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
No disk may be placed on top of a smaller disk.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Hanoi Tower Iterative Algorithm Examples
**Input**: 2
**Output:** Disk 1 moved from A to B
Disk 2 moved from A to C
Disk 1 moved from B to C
**Input:** 3
**Output:** Disk 1 moved from A to C
Disk 2 moved from A to B
Disk 1 moved from C to B
Disk 3 moved from A to C
Disk 1 moved from B to A
Disk 2 moved from B to C
Disk 1 moved from A to C
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi using Recursion
The idea is to use the helper node to reach the destination using recursion. Below is the pattern for this problem:
Shift N-1
disks from A
to B
, using C
.
Shift last disk from A
to C
.
Shift N-1
disks from B
to C
, using A
.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi using Recursion
Image illustration for 3 disks
RTEU CE205 Week-3
CE205 Data Structures Week-3
Follow the steps below to solve the problem
Create a function towerOfHanoi where pass the N (current number of disk), from_rod , to_rod , aux_rod.
Make a function call for N – 1 th disk.
Then print the current the disk along with from_rod and to_rod
Again make a function call for N – 1 th disk.
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi in C++ Recursive
#include <bits/stdc++.h>
using namespace std;
void towerOfHanoi (int n, char from_rod, char to_rod,
char aux_rod)
{
if (n == 0 ) {
return ;
}
towerOfHanoi (n - 1 , from_rod, aux_rod, to_rod);
cout << "Move disk " << n << " from rod " << from_rod
<< " to rod " << to_rod << endl;
towerOfHanoi (n - 1 , aux_rod, to_rod, from_rod);
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi in C++ Recursive
...
int main ()
{
int N = 3 ;
towerOfHanoi (N, 'A' , 'C' , 'B' );
return 0 ;
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi in Java Recursive
import java.io.*;
import java.math.*;
import java.util.*;
class GFG {
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi in Java Recursive
...
static void towerOfHanoi (int n, char from_rod,
char to_rod, char aux_rod)
{
if (n == 0 ) {
return ;
}
towerOfHanoi(n - 1 , from_rod, aux_rod, to_rod);
System.out.println("Move disk " + n + " from rod "
+ from_rod + " to rod "
+ to_rod);
towerOfHanoi(n - 1 , aux_rod, to_rod, from_rod);
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi in Java Recursive
...
public static void main (String args[])
{
int N = 3 ;
towerOfHanoi(N, 'A' , 'C' , 'B' );
}
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi in C# Recursive
using System;
class GFG {
static void towerOfHanoi (int n, char from_rod,
char to_rod, char aux_rod )
{
if (n == 0 ) {
return ;
}
towerOfHanoi(n - 1 , from_rod, aux_rod, to_rod);
Console.WriteLine("Move disk " + n + " from rod "
+ from_rod + " to rod " + to_rod);
towerOfHanoi(n - 1 , aux_rod, to_rod, from_rod);
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi in C# Recursive
...
public static void Main (String[] args )
{
int N = 3 ;
towerOfHanoi(N, 'A' , 'C' , 'B' );
}
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Output
Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 3 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 2 from rod B to rod C
Move disk 1 from rod A to rod C
RTEU CE205 Week-3
CE205 Data Structures Week-3
Time complexity
There are two possibilities for every disk.
Therefore, 2 * 2 * 2 * . . . * 2(N times) is 2N
Auxiliary Space
Function call stack space
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi in Java Recursive
class GFG {
static void tower (int n, char sourcePole,
char destinationPole, char auxiliaryPole)
{
if (0 == n)
return ;
tower(n - 1 , sourcePole, auxiliaryPole,
destinationPole);
System.out.printf("Move the disk %d from %c to %c\n" ,
n, sourcePole, destinationPole);
tower(n - 1 , auxiliaryPole, destinationPole, sourcePole);
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi in Java Recursive
public static void main (String[] args)
{
tower(3 , 'S' , 'D' , 'A' );
}
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi in C# Recursive
using System;
class GFG {
static void tower (int n, char sourcePole,
char destinationPole,
char auxiliaryPole )
{
if (0 == n)
return ;
tower(n - 1 , sourcePole, auxiliaryPole,
destinationPole);
Console.WriteLine("Move the disk " + n
+ "from " + sourcePole + "to "
+ destinationPole);
tower(n - 1 , auxiliaryPole,
destinationPole, sourcePole);
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi in C# Recursive
public static void Main ()
{
tower(3 , 'S' , 'D' , 'A' );
}
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi in C Recursive
#include <stdio.h>
void tower (int n, char sourcePole, char destinationPole, char auxiliaryPole)
{
if (0 == n)
return ;
tower(n-1 , sourcePole, auxiliaryPole,
destinationPole);
printf ("Move the disk %d from %c to %c\n" ,
n,sourcePole, destinationPole);
tower(n-1 , auxiliaryPole, destinationPole,
sourcePole);
}
...
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi in C Recursive
...
int main ()
{
tower(3 , 'S' , 'D' , 'A' );
return 0 ;
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi in C# Recursive
using System;
class GFG {
static void tower (int n, char sourcePole,
char destinationPole,
char auxiliaryPole )
{
if (0 == n)
return ;
tower(n - 1 , sourcePole, auxiliaryPole,
destinationPole);
Console.WriteLine("Move the disk " + n
+ "from " + sourcePole + "to "
+ destinationPole);
tower(n - 1 , auxiliaryPole,
destinationPole, sourcePole);
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
Tower of Hanoi in C# Recursive
public static void Main ()
{
tower(3 , 'S' , 'D' , 'A' );
}
}
RTEU CE205 Week-3
CE205 Data Structures Week-3
RTEU CE205 Week-3
![center h:auto](https://cdn.programiz.com/sites/tutorial2program/files/stack-of-plates_0.png)
![represent the LIFO principle by using push and pop operation](https://cdn.programiz.com/sites/tutorial2program/files/stack.png "Stack operations")
![Adding elements to the top of stack and removing elements from the top of stack](https://cdn.programiz.com/sites/tutorial2program/files/stack-operations.png)
![Stack Representation](http://www.btechsmartclass.com/data_structures/ds_images/stack_representation.png)
![Stack ADT Example](http://www.btechsmartclass.com/data_structures/ds_images/stack_adt_example.png)
![stack using array program](http://www.btechsmartclass.com/data_structures/ds_images/Stack_Output.PNG)
![stack implementation using linked list](http://www.btechsmartclass.com/data_structures/ds_images/stack_implementation_using_linked_list.png)
![stack implementation using linked list](http://www.btechsmartclass.com/data_structures/ds_images/stack_using_linked_list_output_1.PNG)
![stack program implemented using linked list](http://www.btechsmartclass.com/data_structures/ds_images/stack_using_linked_list_output_2.PNG)
![infix expression](http://www.btechsmartclass.com/data_structures/ds_images/infix_expression.png)
![postfix exression](http://www.btechsmartclass.com/data_structures/ds_images/postfix_expression.png)
![prefix expression](http://www.btechsmartclass.com/data_structures/ds_images/prefix_expression.png)
![postfix expression evaluation](http://www.btechsmartclass.com/data_structures/ds_images/postfix_expression.png)
![Representation of Queue in first in first out principle](https://cdn.programiz.com/sites/tutorial2program/files/queue.png "Queue")
![center h:200px](https://cdn.programiz.com/sites/tutorial2program/files/why-circular-queue_0.png)
![Queue ADT](http://www.btechsmartclass.com/data_structures/ds_images/Queue_Datastructure.png)
![queue data structure](http://www.btechsmartclass.com/data_structures/
ds_images/Queue_ADT_Example_6.png)
![queue datastructure using array](http://www.btechsmartclass.com/data_structures/ds_images/Queue_Output.PNG)
![queue using linked list](http://www.btechsmartclass.com/data_structures/ds_images/Queue_using_linked_list.png)
![queue datastructure using linked list](http://www.btechsmartclass.com/data_structures/ds_images/Queue_using_linked_list_output_1.PNG)
![queue datastructure using linked list](http://www.btechsmartclass.com/data_structures/ds_images/Queue_using_linked_list_output_2.PNG)
![circular queue data structure](http://www.btechsmartclass.com/data_structures/ds_images/U2_T3_P4.png)
![](http://www.btechsmartclass.com/data_structures/ds_images/U2_T3_P5.png)
![](http://www.btechsmartclass.com/data_structures/ds_images/Circular%20Q.png)
![circular queue datastructure](http://www.btechsmartclass.com/data_structures/ds_images/circular_Queue_Output_1.PNG)
![circular queue datastructure](http://www.btechsmartclass.com/data_structures/ds_images/circular_Queue_Output_2.PNG)
![double ended queue data structure](http://www.btechsmartclass.com/data_structures/ds_images/Double%20Ended%20Q.png)
![input restricted double ended queue](http://www.btechsmartclass.com/data_structures/ds_images/In%20Re%20DeQ.png)
![output restricted double ended queue](http://www.btechsmartclass.com/data_structures/ds_images/Out%20Re%20DeQ.png)
![Double ended queue program](http://www.btechsmartclass.com/data_structures/ds_images/Double_Ended_Queue_Program.png)
![Priority queue](https://cdn.programiz.com/sites/tutorial2program/files/priority-queue.png "Priority queue")
![faq.disk3](https://media.geeksforgeeks.org/wp-content/uploads/tower-of-hanoi.png)