0% found this document useful (0 votes)
10 views60 pages

B.C.A Sem 2 DS

The document provides an introduction to data structures, detailing primitive and non-primitive types, including linear structures like arrays, stacks, and queues, as well as non-linear structures like trees and graphs. It explains the operations of stacks, including push and pop, and discusses recursion and its relationship with stack memory. Additionally, it covers the evaluation of expressions using stacks and the concept of Polish notation.

Uploaded by

yuvrajdhakan5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views60 pages

B.C.A Sem 2 DS

The document provides an introduction to data structures, detailing primitive and non-primitive types, including linear structures like arrays, stacks, and queues, as well as non-linear structures like trees and graphs. It explains the operations of stacks, including push and pop, and discusses recursion and its relationship with stack memory. Additionally, it covers the evaluation of expressions using stacks and the concept of Polish notation.

Uploaded by

yuvrajdhakan5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Introduction To data Structure And Elementary Data Structure

Primitive and simple structures


Primitive data structure is a kind of data structure that stores the data of only one type.

Examples of primitive data structure are integer, character, float.

Primitive data structure will contain some value, i.e., it cannot be NULL.

The size depends on the type of the data structure.

 Integer: The integer data type contains the numeric values. It contains the whole
numbers that can be either negative or positive. When the range of integer data type is not
large enough then in that case, we can use long.
 Float: The float is a data type that can hold decimal values. When the precision of
decimal value increases then the Double data type is used.
 Boolean: It is a data type that can hold either a True or a False value. It is mainly used
for checking the condition.
 Character: It is a data type that can hold a single character value both uppercase and
lowercase such as 'A' or 'a'.

Prepared by:travadi education-amreli Page 1


Introduction To data Structure And Elementary Data Structure

Non-primitive data structure


The non-primitive data structure is a kind of data structure that can hold multiple values either in
a contiguous or random location. The non-primitive data types are defined by the programmer.
The non-primitive data structure is further classified into two categories, i.e., linear and non-
linear data structure.

What is the Linear data structure?

A linear data structure is a structure in which the elements are stored sequentially, and the
elements are connected to the previous and the next element. As the elements are stored
sequentially, so they can be traversed or accessed in a single run. The implementation of linear
data structures is easier as the elements are sequentially organized in memory. The data elements
in an array are traversed one after another and can access only one element at a time.

The types of linear data structures are Array, Queue, Stack, Linked List.

 Array: An array consists of data elements of a same data type. For example, if we want
to store the roll numbers of 10 students, so instead of creating 10 integer type variables,
we will create an array having size 10. Therefore, we can say that an array saves a lot of
memory and reduces the length of the code.
 Stack: It is linear data structure that uses the LIFO (Last In-First Out) rule in which the
data added last will be removed first. The addition of data element in a stack is known as
a push operation, and the deletion of data element form the list is known as pop
operation.
 Queue: It is a data structure that uses the FIFO rule (First In-First Out). In this rule, the
element which is added first will be removed first. There are two terms used in the queue
front end and rear The insertion operation performed at the back end is known ad
enqueue, and the deletion operation performed at the front end is known as dequeue.
 Linked list: It is a collection of nodes that are made up of two parts, i.e., data element
and reference to the next node in the sequence.

What is a Non-linear data structure?

A non-linear data structure is also another type of data structure in which the data elements are
not arranged in a contiguous manner. As the arrangement is nonsequential, so the data elements
cannot be traversed or accessed in a single run. In the case of linear data structure, element is
connected to two elements (previous and the next element), whereas, in the non-linear data
structure, an element can be connected to more than two elements.

Trees and Graphs are the types of non-linear data structure.

Prepared by:travadi education-amreli Page 2


Introduction To data Structure And Elementary Data Structure

 Tree

It is a non-linear data structure that consists of various linked nodes. It has a hierarchical tree
structure that forms a parent-child relationship. The diagrammatic representation of a tree data
structure is shown below:

Prepared by:travadi education-amreli Page 3


Introduction To data Structure And Elementary Data Structure

Graph

A graph is a non-linear data structure that has a finite number of vertices and edges, and these
edges are used to connect the vertices. The vertices are used to store the data elements, while the
edges represent the relationship between the vertices. A graph is used in various real-world
problems like telephone networks, circuit networks, social networks like LinkedIn, Facebook. In
the case of facebook, a single user can be considered as a node, and the connection of a user with
others is known as edges.

Prepared by:travadi education-amreli Page 4


Introduction To data Structure And Elementary Data Structure

Stack
A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle.

Stack has one end, whereas the Queue has two ends (front and rear).

It contains only one pointer top pointer pointing to the topmost element of the stack.

Whenever an element is added in the stack, it is added on the top of the stack, and the element can be
deleted only from the stack. In other words, a stack can be defined as a container in which insertion
and deletion can be done from the one end known as the top of the stack.

Working of Stack

Stack works on the LIFO pattern. As we can observe in the below figure there are five memory
blocks in the stack; therefore, the size of the stack is 5.

Suppose we want to store the elements in a stack and let's assume that stack is empty. We have
taken the stack of size 5 as shown below in which we are pushing the elements one by one until
the stack becomes full.

since our stack is full as the size of the stack is 5. In the above cases, we can observe that it goes from
the top to the bottom when we were entering the new element in the stack. The stack gets filled up
from the bottom to the top. When we perform the delete operation on the stack, there is only one way
for entry and exit as the other end is closed. It follows the LIFO pattern, which means that the value
entered first will be removed last. In the above case, the value 5 is entered first, so it will be removed
only after the deletion of all the other elements.

Prepared by:travadi education-amreli Page 5


Introduction To data Structure And Elementary Data Structure

Standard Stack Operations

The following are some common operations implemented on the stack:

 push(): When we insert an element in a stack then the operation is known as a push. If the stack
is full then the overflow condition occurs.
 pop(): When we delete an element from the stack, the operation is known as a pop. If the stack
is empty means that no element exists in the stack, this state is known as an underflow state.
 isEmpty(): It determines whether the stack is empty or not.
 isFull(): It determines whether the stack is full or not.'
 peek(): It returns the element at the given position.
 count(): It returns the total number of elements available in a stack.
 change(): It changes the element at the given position.
 display(): It prints all the elements available in the stack.

PUSH operation

The steps involved in the PUSH operation is given below:

 Before inserting an element in a stack, we check whether the stack is full.


 If we try to insert the element in a stack, and the stack is full, then the overflow condition
occurs.
 When we initialize a stack, we set the value of top as -1 to check that the stack is empty.
 When the new element is pushed in a stack, first, the value of the top gets incremented, i.e.,
top=top+1, and the element will be placed at the new position of the top.
 The elements will be inserted until we reach the max size of the stack.

Prepared by:travadi education-amreli Page 6


Introduction To data Structure And Elementary Data Structure

POP operation

The steps involved in the POP operation is given below:

 Before deleting the element from the stack, we check whether the stack is empty.
 If we try to delete the element from the empty stack, then the underflow condition occurs.
 If the stack is not empty, we first access the element which is pointed by the top
 Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1.

Prepared by:travadi education-amreli Page 7


Introduction To data Structure And Elementary Data Structure

Types of Stacks:

 Fixed Size Stack: As the name suggests, a fixed size stack has a fixed size and cannot grow or
shrink dynamically. If the stack is full and an attempt is made to add an element to it, an
overflow error occurs. If the stack is empty and an attempt is made to remove an element from
it, an underflow error occurs.
 Dynamic Size Stack: A dynamic size stack can grow or shrink dynamically. When the stack is full,
it automatically increases its size to accommodate the new element, and when the stack is
empty, it decreases its size. This type of stack is implemented using a linked list, as it allows for
easy resizing of the stack.

Applications of the stack:


 Redo-undo features at many places like editors, photoshop.

 Forward and backward features in web browsers

 Stack also helps in implementing function call in computers. The last called function is always
completed first.
 Stacks are also used to implement the undo/redo operation in text editor.

Implementing Stack using Arrays example:

// C program for array implementation of stack

#include <limits.h>

#include <stdio.h>

#include <stdlib.h>

// A structure to represent a stack

struct Stack {

int top;

Prepared by:travadi education-amreli Page 8


Introduction To data Structure And Elementary Data Structure

unsigned capacity;

int* array;

};

// function to create a stack of given capacity. It initializes size

of

// stack as 0

struct Stack* createStack(unsigned capacity)

struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));

stack->capacity = capacity;

stack->top = -1;

stack->array = (int*)malloc(stack->capacity * sizeof(int));

return stack;

// Stack is full when top is equal to the last index

int isFull(struct Stack* stack)

Prepared by:travadi education-amreli Page 9


Introduction To data Structure And Elementary Data Structure

return stack->top == stack->capacity - 1;

// Stack is empty when top is equal to -1

int isEmpty(struct Stack* stack)

return stack->top == -1;

// Function to add an item to stack. It increases top by 1

void push(struct Stack* stack, int item)

if (isFull(stack))

return;

stack->array[++stack->top] = item;

printf("%d pushed to stack\n", item);

Prepared by:travadi education-amreli Page 10


Introduction To data Structure And Elementary Data Structure

// Function to remove an item from stack. It decreases top by 1

int pop(struct Stack* stack)

if (isEmpty(stack))

return INT_MIN;

return stack->array[stack->top--];

// Function to return the top from stack without removing it

int peek(struct Stack* stack)

if (isEmpty(stack))

return INT_MIN;

return stack->array[stack->top];

// Driver program to test above functions

Prepared by:travadi education-amreli Page 11


Introduction To data Structure And Elementary Data Structure

void main()

struct Stack* stack = createStack(100);

push(stack, 10);

push(stack, 20);

push(stack, 30);

printf("%d popped from stack\n", pop(stack));

getch();

return 0;

Prepared by:travadi education-amreli Page 12


Introduction To data Structure And Elementary Data Structure

Recursion and stack


A recursive function is one that calls itself.

How Recursion is managed ?

The recursion use the stack implementation. The stack is a data structure which can be
manipulated by pushing (adding) and popping(removing) data of the top of the list. In fact, the
stack stands for LIFO or Last In First Out

So, when a function is called, this one go to the top of the stack. And then, as the operation of a
stack requires, the function at the top of the stack will be the first to be executed. This means that
the last function called will be the first one executed.

We must consider when a function requests space for local variables, the allocated space is added
to the top of the stack. So when the end of the function is reached, the space allocated for local
variables appears at the top of the stack.
Therefore, we must bear in mind that variables that are stored in stack-based memory cannot be
dynamically allocated pieces of memory during program execution. Instead, memory of this type
uses a heap-based storage allocation.

It is important to note that stack overflow can occur due to limited space available for stack-based
memory. One of the most common causes of stack overflow is infinite recursion, a behavior that we
must consider when programming recursive functions.

Recursion VS Iteration

SR
Recursion Iteration
No.
Terminates when the condition
1) Terminates when the base case becomes true.
becomes false.
2) Used with functions. Used with loops.
Every recursive call needs extra space in the Every iteration does not require any
3)
stack memory. extra space.
4) Smaller code size. Larger code size.

Prepared by:travadi education-amreli Page 13


Introduction To data Structure And Elementary Data Structure

Example

// C code to implement Fibonacci series

#include <stdio.h>

// Function for fibonacci

int fib(int n)

// Stop condition

if (n == 0)

return 0;

// Stop condition

if (n == 1 || n == 2)

return 1;

// Recursion function

else

return (fib(n - 1) + fib(n - 2));

Prepared by:travadi education-amreli Page 14


Introduction To data Structure And Elementary Data Structure

// Driver Code

void main()

// Initialize variable n.

int n = 5;

int i;

printf("Fibonacci series of %d numbers is: ",n);

// for loop to print the fibonacci series.

for (i = 0; i < n; i++) {

printf("%d ", fib(i));

getch();

return 0;

Prepared by:travadi education-amreli Page 15


Introduction To data Structure And Elementary Data Structure

Prepared by:travadi education-amreli Page 16


Introduction To data Structure And Elementary Data Structure

Evaluation of expressions using stacks

Polish Notation:- The process of writing the operators either before or after the
operands in known as polish notation. It has 3 categories:
1. Infix : Operators are written between two operands.
2. Prefix: operators are written before their operands.
3. Postfix: Operators are written after their operands.(RPN= reverse polish
notation)
Example:
Infix: a+b*c
Prefix: +a*bc
Postfix: abc*+

Example

1)Convert infix to postfix

3+4+3
Sr No Expression Stack Postfix
0 3 3
1 + + 3
2 4 + 34
3 + + 34+
4 3 + 34+3
5 34+3+

Prepared by:travadi education-amreli Page 17


Introduction To data Structure And Elementary Data Structure

2) Convert infix to postfix

3+(4+3)

Sr No Expression Stack Postfix


0 3 3
1 + + 3
2 ( +( 3
3 4 +( 34
4 + +(+ 34
5 3 +(+ 343
6 ) + 343+
7 343++

3) Convert infix to postfix

5+6/(6-2)*{9-3}

Infix to Postfix table

Sr No Expression Stack Postfix


0 5 5
1 + + 5
2 6 + 56
3 / +/ 56
4 ( +/( 56
5 6 +/( 566
6 - +/(- 566
7 2 +/(- 5662
8 ) +/ 5662-
9 * +* 5662-/
10 { { 5662-/*+
11 9 { 5662-/*+9
12 - {- 5662-/*+9
13 3 {- 5662-/*+93
14 } } 5662-/*+93-{
15 5662-/*+93-{}

Prepared by:travadi education-amreli Page 18


Introduction To data Structure And Elementary Data Structure

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

Queue is referred to be as First In First Out list.

For example, people waiting in line for a rail ticket form a queue.

Applications of Queue
 Queues are widely used as waiting lists for a single shared resource like printer, disk, CPU.

 Queues are used in asynchronous transfer of data (where data is not being transferred at the
same rate between two processes) for eg. pipes, file IO, sockets.

 Queues are used as buffers in most of the applications like MP3 media player, CD player, etc.

Basic Operations on Queue:

 enqueue(): Inserts an element at the end of the queue i.e. at the rear end.
 dequeue(): This operation removes and returns an element that is at the
front end of the queue.
 front(): This operation returns the element at the front end without
removing it.
 rear(): This operation returns the element at the rear end without
removing it.
 isEmpty(): This operation indicates whether the queue is empty or not.
 isFull(): This operation indicates whether the queue is full or not.

Prepared by:travadi education-amreli Page 19


Introduction To data Structure And Elementary Data Structure

 size(): This operation returns the size of the queue i.e. the total number of
elements it contains.

Types of Queues:

 Simple Queue: Simple queue also known as a linear queue is the most basic
version of a queue. Here, insertion of an element i.e. the Enqueue
operation takes place at the rear end and removal of an element i.e. the
Dequeue operation takes place at the front end. Here problem is that if we
pop some item from front and then rear reach to the capacity of the queue
and although there are empty spaces before front means the queue is not
full but as per condition in isFull() function, it will show that the queue is
full then. To solve this problem we use circular queue.

 Circular Queue: In a circular queue, the element of the queue act as a


circular ring. The working of a circular queue is similar to the linear queue
except for the fact that the last element is connected to the first element.
Its advantage is that the memory is utilized in a better way. This is because
if there is an empty space i.e. if no element is present at a certain position
in the queue, then an element can be easily added at that position using
modulo capacity(%n).

 Priority Queue: This queue is a special type of queue. Its specialty is that it
arranges the elements in a queue based on some priority. The priority can
be something where the element with the highest value has the priority so
it creates a queue with decreasing order of values. The priority can also be
such that the element with the lowest value gets the highest priority so in
turn it creates a queue with increasing order of values. In pre-define
priority queue, C++ gives priority to highest value whereas Java gives
priority to lowest value.

Prepared by:travadi education-amreli Page 20


Introduction To data Structure And Elementary Data Structure

 Dequeue: Dequeue is also known as Double Ended Queue. As the name


suggests double ended, it means that an element can be inserted or
removed from both ends of the queue, unlike the other queues in which it
can be done only from one end. Because of this property, it may not obey
the First In First Out property.

Array implementation Of Queue:

For implementing queue, we need to keep track of two indices, front and rear. We enqueue an
item at the rear and dequeue an item from the front. If we simply increment front and rear
indices, then there may be problems, the front may reach the end of the array. The solution to
this problem is to increase front and rear in circular manner.

Algorithm to insert any element in a queue

 Step 1: IF REAR = MAX - 1


Write OVERFLOW
Go to step
[END OF IF]
 Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
 Step 3: Set QUEUE[REAR] = NUM
 Step 4: EXIT

Algorithm to delete an element from the queue

 Step 1: IF FRONT = -1 or FRONT > REAR


Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
 Step 2: EXIT

Prepared by:travadi education-amreli Page 21


Introduction To data Structure And Elementary Data Structure

Example
#include<stdio.h>

#include<stdlib.h>

#define maxsize 5

void insert();

void delete();

void display();

int front = -1, rear = -1;

int queue[maxsize];

void main ()

int choice;

while(choice != 4)

printf("\n*************************Main Menu*****************************\n");

printf("\n=================================================================\n");

printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");

printf("\nEnter your choice ?");

scanf("%d",&choice);

switch(choice)

case 1:

insert();

break;

Prepared by:travadi education-amreli Page 22


Introduction To data Structure And Elementary Data Structure

case 2:

delete();

break;

case 3:

display();

break;

case 4:

exit(0);

break;

default:

printf("\nEnter valid choice??\n");

getch();

void insert()

int item;

printf("\nEnter the element\n");

scanf("\n%d",&item);

if(rear == maxsize-1)

printf("\nOVERFLOW\n");

return;

Prepared by:travadi education-amreli Page 23


Introduction To data Structure And Elementary Data Structure

if(front == -1 && rear == -1)

front = 0;

rear = 0;

else

rear = rear+1;

queue[rear] = item;

printf("\nValue inserted ");

void delete()

int item;

if (front == -1 || front > rear)

printf("\nUNDERFLOW\n");

return;

else

item = queue[front];

Prepared by:travadi education-amreli Page 24


Introduction To data Structure And Elementary Data Structure

if(front == rear)

front = -1;

rear = -1 ;

else

front = front + 1;

printf("\nvalue deleted ");

void display()

int i;

if(rear == -1)

printf("\nEmpty queue\n");

else

{ printf("\nprinting values .....\n");

for(i=front;i<=rear;i++)

Prepared by:travadi education-amreli Page 25


Introduction To data Structure And Elementary Data Structure

printf("\n%d\n",queue[i]);

Prepared by:travadi education-amreli Page 26


Introduction To data Structure And Elementary Data Structure

Circular queue
A circular queue is similar to a linear queue as it is also based on the FIFO (First In First Out) principle
except that the last position is connected to the first position in a circular queue that forms a circle. It is
also known as a Ring Buffer.

Operations on Circular Queue

 Front: It is used to get the front element from the Queue.


 Rear: It is used to get the rear element from the Queue.

Prepared by:travadi education-amreli Page 27


Introduction To data Structure And Elementary Data Structure

 enQueue(value): This function is used to insert the new value in the Queue. The new element is
always inserted from the rear end.
 deQueue(): This function deletes an element from the Queue. The deletion in a Queue always
takes place from the front end.

Applications of Circular Queue

The circular Queue can be used in the following scenarios:

 Memory management: The circular queue provides memory management. As we have already
seen that in linear queue, the memory is not managed very efficiently. But in case of a circular
queue, the memory is managed efficiently by placing the elements in a location which is unused.
 CPU Scheduling: The operating system also uses the circular queue to insert the processes and
then execute them.
 Traffic system: In a computer-control traffic system, traffic light is one of the best examples of
the circular queue. Each light of traffic light gets ON one by one after every jinterval of time. Like
red light gets ON for one minute then yellow light for one minute and then green light. After
green light, the red light gets ON.

Enqueue operation

The steps of enqueue operation are given below:

 First, we will check whether the Queue is full or not.


 Initially the front and rear are set to -1. When we insert the first element in a Queue, front and
rear both are set to 0.
 When we insert a new element, the rear gets incremented, i.e., rear=rear+1.

Algorithm to insert an element in a circular queue

Step 1: IF (REAR+1)%MAX = FRONT


Write " OVERFLOW "
Goto step 4
[End OF IF]

Step 2: IF FRONT = -1 and REAR = -1


SET FRONT = REAR = 0
ELSE IF REAR = MAX - 1 and FRONT ! = 0
SET REAR = 0
ELSE
SET REAR = (REAR + 1) % MAX
[END OF IF]

Step 3: SET QUEUE[REAR] = VAL

Step 4: EXIT

Prepared by:travadi education-amreli Page 28


Introduction To data Structure And Elementary Data Structure

Dequeue Operation

The steps of dequeue operation are given below:

 First, we check whether the Queue is empty or not. If the queue is empty, we cannot perform
the dequeue operation.
 When the element is deleted, the value of front gets decremented by 1.
 If there is only one element left which is to be deleted, then the front and rear are reset to -1.

Algorithm to delete an element from the circular queue

Step 1: IF FRONT = -1
Write " UNDERFLOW "
Goto Step 4
[END of IF]

Step 2: SET VAL = QUEUE[FRONT]

Step 3: IF FRONT = REAR


SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END of IF]
[END OF IF]

Step 4: EXIT

Prepared by:travadi education-amreli Page 29


Introduction To data Structure And Elementary Data Structure

diagrammatic representation.

Prepared by:travadi education-amreli Page 30


Introduction To data Structure And Elementary Data Structure

Prepared by:travadi education-amreli Page 31


Introduction To data Structure And Elementary Data Structure

Prepared by:travadi education-amreli Page 32


Introduction To data Structure And Elementary Data Structure

Prepared by:travadi education-amreli Page 33


Introduction To data Structure And Elementary Data Structure

Example

#include <stdio.h>

# define max 6

int queue[max]; // array declaration

int front=-1;

int rear=-1;

// function to insert an element in a circular queue

void enqueue(int element)

if(front==-1 && rear==-1) // condition to check queue is

empty

front=0;

rear=0;

queue[rear]=element;

Prepared by:travadi education-amreli Page 34


Introduction To data Structure And Elementary Data Structure

else if((rear+1)%max==front) // condition to check queue is

full

printf("Queue is overflow..");

else

rear=(rear+1)%max; // rear is incremented

queue[rear]=element; // assigning a value to the queue

at the rear position.

// function to delete the element from the queue

int dequeue()

if((front==-1) && (rear==-1)) // condition to check queue is

empty

Prepared by:travadi education-amreli Page 35


Introduction To data Structure And Elementary Data Structure

printf("\nQueue is underflow..");

else if(front==rear)

printf("\nThe dequeued element is %d", queue[front]);

front=-1;

rear=-1;

else

printf("\nThe dequeued element is %d", queue[front]);

front=(front+1)%max;

// function to display the elements of a queue

void display()

Prepared by:travadi education-amreli Page 36


Introduction To data Structure And Elementary Data Structure

int i=front;

if(front==-1 && rear==-1)

printf("\n Queue is empty..");

else

printf("\nElements in a Queue are :");

while(i<=rear)

printf("%d,", queue[i]);

i=(i+1)%max;

void main()

int choice=1,x; // variables declaration

Prepared by:travadi education-amreli Page 37


Introduction To data Structure And Elementary Data Structure

while(choice<4 && choice!=0) // while loop

printf("\n Press 1: Insert an element");

printf("\nPress 2: Delete an element");

printf("\nPress 3: Display the element");

printf("\nEnter your choice");

scanf("%d", &choice);

switch(choice)

case 1:

printf("Enter the element which is to be inserted");

scanf("%d", &x);

enqueue(x);

break;

Prepared by:travadi education-amreli Page 38


Introduction To data Structure And Elementary Data Structure

case 2:

dequeue();

break;

case 3:

display();

}}

getch();

return 0;

Prepared by:travadi education-amreli Page 39


Introduction To data Structure And Elementary Data Structure

Deques
The deque stands for Double Ended Queue. Deque is a linear data structure where the insertion
and deletion operations are performed from both ends. We can say that deque is a generalized
version of the queue.

Though the insertion and deletion in a deque can be performed on both ends, it does not follow
the FIFO rule. The representation of a deque is given as follows -

Types of deque

There are two types of deque

 Input restricted queue


 Output restricted queue

Input restricted Queue

In input restricted queue, insertion operation can be performed at only one end, while deletion
can be performed from both ends.

Prepared by:travadi education-amreli Page 40


Introduction To data Structure And Elementary Data Structure

Output restricted Queue

In output restricted queue, deletion operation can be performed at only one end, while insertion
can be performed from both ends.

Operations performed on deque

There are the following operations that can be applied on a deque -

 Insertion at front
 Insertion at rear
 Deletion at front
 Deletion at rear

1) Insertion at the front end

 If the queue is empty, both rear and front are initialized with 0. Now, both will point to
the first element.
 Otherwise, check the position of the front if the front is less than 1 (front < 1), then
reinitialize it by front = n - 1, i.e., the last index of the array.

Prepared by:travadi education-amreli Page 41


Introduction To data Structure And Elementary Data Structure

2) Insertion at the rear end

 If the queue is empty, both rear and front are initialized with 0. Now, both will point to the
first element.

 Otherwise, increment the rear by 1. If the rear is at last index (or size - 1), then instead of
increasing it by 1, we have to make it equal to 0

Prepared by:travadi education-amreli Page 42


Introduction To data Structure And Elementary Data Structure

3) Deletion at the front end

in this operation, the element is deleted from the front end of the queue. Before implementing the
operation, we first have to check whether the queue is empty or not.

If the queue is empty, i.e., front = -1, it is the underflow condition, and we cannot perform the
deletion. If the queue is not full, then the element can be inserted from the front end by using the
below conditions –

If the deque has only one element, set rear = -1 and front = -1.

Else if front is at end (that means front = size - 1), set front = 0.

Prepared by:travadi education-amreli Page 43


Introduction To data Structure And Elementary Data Structure

4)Deletion at the rear end


In this operation, the element is deleted from the rear end of the queue. Before implementing the
operation, we first have to check whether the queue is empty or not.

If the queue is empty, i.e., front = -1, it is the underflow condition, and we cannot perform the
deletion.

If the deque has only one element, set rear = -1 and front = -1.

If rear = 0 (rear is at front), then set rear = n - 1.

Applications of deque

 Deque can be used as both stack and queue, as it supports both operations.
 Deque can be used as a palindrome checker means that if we read the string from both ends,
the string would be the same.

Prepared by:travadi education-amreli Page 44


Introduction To data Structure And Elementary Data Structure

Example
#include <stdio.h>

#define size 5

int deque[size];

int f = -1, r = -1;

// insert_front function will insert the value from the front

void insert_front(int x)

if((f==0 && r==size-1) || (f==r+1))

printf("Overflow");

else if((f==-1) && (r==-1))

f=r=0;

deque[f]=x;

else if(f==0)

f=size-1;

deque[f]=x;

else

Prepared by:travadi education-amreli Page 45


Introduction To data Structure And Elementary Data Structure

f=f-1;

deque[f]=x;

// insert_rear function will insert the value from the rear

void insert_rear(int x)

if((f==0 && r==size-1) || (f==r+1))

printf("Overflow");

else if((f==-1) && (r==-1))

r=0;

deque[r]=x;

else if(r==size-1)

r=0;

deque[r]=x;

Prepared by:travadi education-amreli Page 46


Introduction To data Structure And Elementary Data Structure

else

r++;

deque[r]=x;

// display function prints all the value of deque.

void display()

int i=f;

printf("\nElements in a deque are: ");

while(i!=r)

printf("%d ",deque[i]);

i=(i+1)%size;

printf("%d",deque[r]);

// getfront function retrieves the first value of the deque.

Prepared by:travadi education-amreli Page 47


Introduction To data Structure And Elementary Data Structure

void getfront()

if((f==-1) && (r==-1))

printf("Deque is empty");

else

printf("\nThe value of the element at front is: %d", deque[f]);

// getrear function retrieves the last value of the deque.

void getrear()

if((f==-1) && (r==-1))

printf("Deque is empty");

else

printf("\nThe value of the element at rear is %d", deque[r]);

Prepared by:travadi education-amreli Page 48


Introduction To data Structure And Elementary Data Structure

// delete_front() function deletes the element from the front

void delete_front()

if((f==-1) && (r==-1))

printf("Deque is empty");

else if(f==r)

printf("\nThe deleted element is %d", deque[f]);

f=-1;

r=-1;

else if(f==(size-1))

printf("\nThe deleted element is %d", deque[f]);

f=0;

Prepared by:travadi education-amreli Page 49


Introduction To data Structure And Elementary Data Structure

else

printf("\nThe deleted element is %d", deque[f]);

f=f+1;

// delete_rear() function deletes the element from the rear

void delete_rear()

if((f==-1) && (r==-1))

printf("Deque is empty");

else if(f==r)

printf("\nThe deleted element is %d", deque[r]);

f=-1;

r=-1;

else if(r==0)

Prepared by:travadi education-amreli Page 50


Introduction To data Structure And Elementary Data Structure

printf("\nThe deleted element is %d", deque[r]);

r=size-1;

else

printf("\nThe deleted element is %d", deque[r]);

r=r-1;

void main()

insert_front(20);

insert_front(10);

insert_rear(30);

insert_rear(50);

insert_rear(80);

display(); // Calling the display function to retrieve the values of deque

getfront(); // Retrieve the value at front-end

getrear(); // Retrieve the value at rear-end

delete_front();

delete_rear();

display(); // calling display function to retrieve values after deletion

Prepared by:travadi education-amreli Page 51


Introduction To data Structure And Elementary Data Structure

getch();

Prepared by:travadi education-amreli Page 52


Introduction To data Structure And Elementary Data Structure

Priority queue
A priority queue is an abstract data type that behaves similarly to the normal queue except that
each element has some priority, i.e., the element with the highest priority would come first in a
priority queue. The priority of the elements in a priority queue will determine the order in which
elements are removed from the priority queue.

The priority queue supports only comparable elements, which means that the elements are either
arranged in an ascending or descending order.

For example, suppose we have some values like 1, 3, 4, 8, 14, 22 inserted in a priority queue
with an ordering imposed on the values is from least to the greatest. Therefore, the 1 number
would be having the highest priority while 22 will be having the lowest priority.

Characteristics of a Priority queue

 Every element in a priority queue has some priority associated with it.
 An element with the higher priority will be deleted before the deletion of the lesser
priority.
 If two elements in a priority queue have the same priority, they will be arranged using the
FIFO principle.

Types of Priority Queue

There are two types of priority queue:

 Ascending order priority queue: In ascending order priority queue, a lower priority number is
given as a higher priority in a priority. For example, we take the numbers from 1 to 5 arranged in
an ascending order like 1,2,3,4,5; therefore, the smallest number, i.e., 1 is given as the highest
priority in a priority queue.

Prepared by:travadi education-amreli Page 53


Introduction To data Structure And Elementary Data Structure

Descending order priority queue: In descending order priority queue, a higher priority number
is given as a higher priority in a priority. For example, we take the numbers from 1 to 5 arranged
in descending order like 5, 4, 3, 2, 1; therefore, the largest number, i.e., 5 is given as the highest
priority in a priority queue.

Applications of Priority queue

The following are the applications of the priority queue:

 It is used in the Dijkstra's shortest path algorithm.


 It is used in prim's algorithm
 It is used in data compression techniques like Huffman code.
 It is used in heap sort.
 It is also used in operating system like priority scheduling, load balancing and interrupt handling.

Prepared by:travadi education-amreli Page 54


Introduction To data Structure And Elementary Data Structure

Example
#include <stdio.h>

#include <stdlib.h>

#define MAX 10

//priority queue

void create_queue();

void insert_element(int);

void delete_element(int);

void check_priority(int);

void display_priorityqueue();

int pqueue[MAX];

int front, rear;

void main()

int n, choice;

printf("\nEnter 1 to insert element by priority ");

printf("\nEnter 2 to delete element by priority ");

printf("\nEnter 3 to display priority queue ");

printf("\nEnter 4 to exit");

create_queue();

while (1)

Prepared by:travadi education-amreli Page 55


Introduction To data Structure And Elementary Data Structure

printf("\nEnter your choice : ");

scanf("%d", &choice);

switch(choice)

case 1:

printf("\nEnter element to insert : ");

scanf("%d",&n);

insert_element(n);

break;

case 2:

printf("\nEnter element to delete : ");

scanf("%d",&n);

delete_element(n);

break;

case 3:

display_priorityqueue();

break;

case 4:

exit(0);

default:

printf("\n Please enter valid choice");

Prepared by:travadi education-amreli Page 56


Introduction To data Structure And Elementary Data Structure

void create_queue()

front = rear = -1;

void insert_element(int data)

if (rear >= MAX - 1)

printf("\nQUEUE OVERFLOW");

return;

if ((front == -1) && (rear == -1))

front++;

rear++;

pqueue[rear] = data;

return;

else

check_priority(data);

rear++;

void check_priority(int data)

Prepared by:travadi education-amreli Page 57


Introduction To data Structure And Elementary Data Structure

int i,j;

for (i = 0; i <= rear; i++)

if (data >= pqueue[i])

for (j = rear + 1; j > i; j--)

pqueue[j] = pqueue[j - 1];

pqueue[i] = data;

return;

pqueue[i] = data;

void delete_element(int data)

int i;

if ((front==-1) && (rear==-1))

printf("\nEmpty Queue");

return;

for (i = 0; i <= rear; i++)

Prepared by:travadi education-amreli Page 58


Introduction To data Structure And Elementary Data Structure

if (data == pqueue[i])

for (; i < rear; i++)

pqueue[i] = pqueue[i + 1];

pqueue[i] = -99;

rear--;

if (rear == -1)

front = -1;

return;

printf("\n%d element not found in queue", data);

void display_priorityqueue()

if ((front == -1) && (rear == -1))

printf("\nEmpty Queue ");

return;

for (; front <= rear; front++)

Prepared by:travadi education-amreli Page 59


Introduction To data Structure And Elementary Data Structure

printf(" %d ", pqueue[front]);

front = 0;

Prepared by:travadi education-amreli Page 60

You might also like