You are on page 1of 196

Department of

Computer Science and Engineering

DATA STRUCTURES
Year : 2019-23
Course Code : 1151CS102
Course Category : Program Core
Unit No : I
Topic : Linear data Structures
Faculty Name : Mrs.A.SANGEETHA

School of Computing
Vel Tech Rangarajan Dr. Sagunthala R&D Institute of
Science and Technology
COURSE DETAILS
Preamble:
This course provides an introduction to the basic concepts and techniques of
Linear and nonlinear data Structures and Analyze the various algorithm.
•Prerequisite Courses:

1150CS201 Problem Solving using C

•Related Courses:

Sl. No Course Code Course Name

1 1151CS105 System Software

2 1151CS106 Design and Analysis of Algorithm


and Project
3 1151CS107 Management
Database Management System
(SEPM)

4 1151CS108 Operating Systems

5
07/09/2021
1151CS111 Department
ComputerofNetworks
Computer Science and Engineering 2
COURSE OUTCOMES
 Identify and explain user defined data types, linear data structures for
solving real world problems.

 Design modular programs on non linear data structures and


algorithms for solving engineering problems efficiently.

 Illustrate special trees and Hashing Techniques.

 Apply searching techniques in graph traversal

 Apply sorting techniques for real world problems.

07/09/2021 Department of Computer Science and Engineering 3


CORRELATION OF CO WITH PO

COs PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3

CO1 H M L M M M M M M M L

CO2 M M M M L M M M M L

CO3 M M M L L M L M M M

CO4 M M L M M M H

CO5 M M M L L M M M M M H

H- High; M-Medium; L-Low

07/09/2021 Department of Computer Science and Engineering 4


RESOURCES
i. Text Books:
1. M. A. Weiss, “Data Structures and Algorithm Analysis in C”, Second Edition,
Pearson Education, 2007.

ii. Reference:
1. A. V. Aho, J. E. Hopcroft, and J. D. Ullman, “Data Structures and Algorithms”,
Pearson Education, First Edition Reprint 2003.
2. R. F. Gilberg, B. A. Forouzan, “Data Structures”, Second Edition, Thomson
India Edition, 2005.
3. Ellis Horowitz, SartajSahni, Dinesh Mehta, “Fundamentals of Data Structure”,
Computer Science Press, 1995. 

07/09/2021 Department of Computer Science and Engineering 5


RESOURCES
iii. Online resources
1. http://simplenotions.wordpress.com/2009/05/13/java-standard-data-structures-
big-o-notation/
2. http://mathworld.wolfram.com/DataStructure.html

07/09/2021 Department of Computer Science and Engineering 6


WHY TO STUDY ?
 Computer science is all about storing and computing from a given data. So
studying data structures helps you deal with different ways of arranging,
processing and storing data.

 All codes are made for real time purpose so data structure allow user to
provide/use/handle data in different ways.
 To deal with BIG DATA
 Computers are basically tool to solve problems which has data to process
on to make some decisions. In real life this data would be very large. So we
need to organize data for better accessing

07/09/2021 Department of Computer Science and Engineering 7


LINEAR DATA STRUCTRES -
AGENDA
 Introduction
 Time and space complexity analysis
 Abstract Data Type (ADT)
 The List ADT
 Array Implementation
 Linked List Implementation
 Stack ADT
and Project
 The Queue ADT Management
(SEPM)
 Applications of Stack, Queue and List.
07/09/2021 Department of Computer Science and Engineering 8
DATA

Data is a set of values of qualitative or quantitative variables about


one or more persons or objects

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 9


DATA

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 10


DATA STRUCTURES

• Data structure is a representation of data and the operations

allowed on that data.

• Data-Collection of raw facts.

• Data structure is a way to store and organize data in order to

facilitate the access and modifications.

• Program= Algorithm + Data Structure


and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 11


DATA STRUCTURES

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 12


DATA STRUCTURES

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 13


STORING DATA

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 14


STORING DATA

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 15


STORING DATA

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 16


NEED FOR DATA STRUCTURES

• Processing speed:  To handle very large data, high-speed


processing is required
• Data Search: Getting a particular record from 
database should be quick and with optimum use of resources.
• Multiple requests: To handle simultaneous requests from
multiple users

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 17


TIME AND SPACE COMPLEXITY

• Time complexity : Amount of time required by the algorithm to


execute to completion.
• Best case - Minimum number of steps on input data of n
elements.
• Worst case - Maximum number of steps on input data of size n.
• Average case - Average number of steps on input data of n
elements.
• Space complexity : Amount of memory space needed the algorithm
and Project
in its life cycle. Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 18


ASYMPTOTIC NOTATION
• Mathematical notations used to describe the running time of an algorithm
• Theta Notation (Θ-notation)
 Represents the upper and the lower bound of the running time of an
algorithm
 Used for analyzing the average case complexity of an algorithm.
• Big-O Notation (O-notation)
 Represents the upper bound of the running time of an algorithm.
 Gives the worst case complexity of an algorithm.
• Omega Notation (Ω-notation)
and Project
 Represents the lower bound of the running time of an algorithm.
Management
 Provides best case(SEPM)
complexity of an algorithm.

07/09/2021 Department of Computer Science and Engineering 19


EXAMPLE
#include <stdio.h>
int main()
O(1)
{
    printf("Hello World");
}

#include <stdio.h>
void main()
{ O(N)
    int i, n ;
scanf(“%d”,&n); and Project
    for (i = 1; i <= n; i++) { Management
(SEPM)
        printf("Hello Word !!!");
    }
}07/09/2021 Department of Computer Science and Engineering 20
EXAMPLE

O(N^2)

07/09/2021 Department of Computer Science and Engineering 21


TIME COMPLEXITY
O(n2) - quadratic time
The number of operations is proportional to the size of the task squared.
Examples:
A. Some more simplistic sorting algorithms, for instance a selection sort
of n elements
B. Comparing two two-dimensional arrays of size n by n
C. Finding duplicates in an unsorted list of n elements (implemented
with two nested loops). I
O(log n) - logarithmic time
Examples:
A. Binary search in a sorted list of n elements
and Project
B. Insert and FindManagement
operations for a binary search tree with n nodes
(SEPM)
C. Insert and Remove operations for a heap with n nodes.

07/09/2021 Department of Computer Science and Engineering 22


TIME COMPLEXITY
O(n log n) - "n log n " time
Examples:
A. More advanced sorting algorithms - quicksort, mergesort
O(a^n) (a > 1) - exponential time
Examples:
A. Recursive Fibonacci implementation
B. Towers of Hanoi
C. Generating all permutations of n symbols

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 23


TIME COMPLEXITY

BETTER
• O(1) constant time
• O(log n) log time
• O(n) linear time
• O(n log n) log linear time
• O(n2) quadratic time
• O(n3) cubic time
• O(2n) exponential time
WORSE and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 24


TYPES

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 25


PRMITIVE DATA STRUCTURE
• Primitive Data Structures are the basic data structures that directly operate upon
the machine instructions.

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 26


NON PRMITIVE DATA STRUCTURE
• The items which are collection of other data structures are called
non-primitive items.
– If more than one values are combined in the single name is
called non-primitive data structures
– Example
Arrays, Stack, Queues etc.
• Types of non-Primitive Data Structures
– Linear and Project
Management
– Non-Linear (SEPM)

07/09/2021 Department of Computer Science and Engineering 27


LINEAR DATA STRUCTURES
• Data elements arranged in sequential manner and each member element
is connected to its previous and next element.
• This connection helps to traverse a linear data structure in a single level
and in single run.
• Types
• Arrays
• Queues
• Stacks
• Linked lists and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 28


ARRAY
• Group of similar data items
• Types
• One Dimensional Array
• Two Dimensional Array
• Multi Dimensional Array
• Syntax:
Datatype var_name[size];
Ex:
int a[100]; and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 29


ONE D ARRAY
A type of array in which all elements are arranged in the
form of a list is known as 1-D array or single dimensional
array or linear list.

Declaring 1-D Array:


data_type identifier[length]; e.g: int marks[5];

o Data _type: Data type of values to be stored in the array.


o Identifier: Name of the array.
o Length: Number of elements.
and Project
Management
0 1
(SEPM) 2 3 4

07/09/2021 Department of Computer Science and Engineering 30


ONE D ARRAY
A type of array in which all elements are arranged in the
form of a list is known as 1-D array or single dimensional
array or linear list.

Declaring 1-D Array:


data_type identifier[length]; e.g: int marks[5];

o Data _type: Data type of values to be stored in the array.


o Identifier: Name of the array.
o Length: Number of elements.
and Project
Management
0 1
(SEPM) 2 3 4

07/09/2021 Department of Computer Science and Engineering 31


ONE D ARRAY
One-D array Intialization
The process of assigning values to array elements at the time of
array declaration is called array initialization.

Syntax:
data_type identifier[length]={ List of values };
o Data _type: Data type of values to be stored in the array.
o Identifier: Name of the array.
o Length: Number of elements
o List of values: Values to initialize the array. Initializing values
must be constant
e.g: int marks[5]={70,54,82,96,49}; 70 54 82 96 49
07/09/2021 Department of Computer Science and Engineering 32
2 D ARRAY

The two-D array can also be initialized at the time of declaration.


Initialization is performed by assigning the initial values in braces
seperated by commas.

o The elements of each row are enclosed within braces and seperated
by comma.
o All rows are enclosed within braces.
o For number arrays, if all elements are not specified , the un specified
elements are initialized by zero.

07/09/2021 Department of Computer Science and Engineering 33


2 D ARRAY

Syntax: Row
indexes
int arr[4][3]={ {12,5,22},
0 1 2
{95,3,41}, Column
indexes 0 12 5 22
{77,6,53},
1 95 3 41
{84,59,62} }
2 77 6 53

3 84 59 62

07/09/2021 Department of Computer Science and Engineering 34


MULTI DIMENSIONAL ARRAY

 A variable which represent the list of items using more than


two index (subscript) is called multi-dimensional array.

 The general form of multi dimensional array is :

type array-name[s1][s2][s3]…….[sn];

07/09/2021 Department of Computer Science and Engineering 35


MULTI DIMENSIONAL ARRAY

 Where S is the size. Some examples are :

int survey[3][5][6];
float table[5][4][5][3];

 Here survey is a three-dimensional array And table is a


four-dimensional array.

07/09/2021 Department of Computer Science and Engineering 36


LINKED LIST

• A linked list is a linear data structure.


• Nodes make up linked lists.
• Nodes are structures made up of data and a pointer
to another node.
• Pointer is called next.

07/09/2021 Department of Computer Science and Engineering 37


Stack ADT

Example:

07/09/2021 Department of Computer Science and Engineering 38


QUEUE ADT

Deletion Insertion
(DEQUEUE) (ENQUEUE)
10 20 30 40 50

A[0] A[1] A[2] A[3] A[4]

07/09/2021 Department of Computer Science and Engineering 39


TREE
• Tree is a hierarchical data structure which stores the

information naturally in the form of hierarchy style

• Collection of nodes (starting at a root node) together with a

list of references to nodes (the "children")

07/09/2021 Department of Computer Science and Engineering 40


WHY?
Employees Table

07/09/2021 Department of Computer Science and Engineering 41


WHY?
Employees Table

07/09/2021 Department of Computer Science and Engineering 42


TERMINOLOGIES

07/09/2021 Department of Computer Science and Engineering 43


EXAMPLE

07/09/2021 Department of Computer Science and Engineering 44


GRAPH

• A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of

edges E.

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 45


REPRESENTATION - EXAMPLE

07/09/2021 Department of Computer Science and Engineering 46


IMPLEMENTATION OF LIST ADT

1. Array Implementation

2. Linked List Implementation

3. Cursor Implementation.

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 47


ABSTRACT DATA TYPE

• Stores data and allow various operations on the data to

access and change it.

• A mathematical model, together with various operations

defined on the model

• An ADT is a collection of data and associated operations for

manipulating thatand
data
Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 48


ARRAY IMPLEMENTATION OF LIST
ADT
Array is a collection of specific number of data stored in a
consecutive memory locations.
* Insertion and Deletion operation are expensive as it
requires more data movement
* Find and Printlist operations takes constant time.
* Even if the array is dynamically allocated, an estimate
of the maximum size of the list is required which considerably
and Project
wastes the memoryManagement
space.
(SEPM)

07/09/2021 Department of Computer Science and Engineering 49


ARRAY ADT-INSERTION
Insert()
declare array A
read n
for (i=0;i<n;i++)
read A[i]
set i=0,j=n
get position k
n=n+1
while( j >= k)
A[j+1] = A[j]
j=j-1
A[k] =item
and Project
print array A Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 50


ARRAY ADT-INSERTION
main()
{
int A[] = {1,3,5,7};
int item = 8, k = 3, n = 5;
int i = 0, j = n;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
{
printf("A[%d] = %d \n", i, A[i]);
} n = n + 1;
while( j >= k)
{
A[j+1] = A[j]; and Project
j = j - 1; Management
(SEPM)
}

07/09/2021 Department of Computer Science and Engineering 51


ARRAY ADT-INSERTION
A[k] = item;
printf("The array elements after insertion :\n");
for(i = 0; i<n; i++)
{
printf("A[%d] = %d \n", i, A[i]);
}
}

1 3 5 7
Before Insertion

After Insertion 1 3 5 7 8
and Project
Management
(SEPM)
07/09/2021 Department of Computer Science and Engineering 52
ARRAY ADT-DELETION
delete()
declare array A
read n
for (i=0;i<n;i++)
read A[i]
get position k
j=k
while( j < n)
A[j-1] = A[j]
j=j+1
n=n-1
print array A
and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 53


ARRAY ADT-DELETION
main()
{
int A[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
{
printf("A[%d] = %d \n", i, A[i]);
}
j = k;
while( j < n)
{ and Project
A[j-1] = A[j]; Management
(SEPM)
j = j + 1;
}
07/09/2021 Department of Computer Science and Engineering 54
ARRAY ADT-DELETION
n = n -1;
printf("The array elements after deletion :\n");
for(i = 0; i<n; i++)
{
printf("A[%d] = %d \n", i, A[i]);
}}

Before Deletion 1 3 5 7 8

After Deletion 1 3 7 8
and Project
Management
(SEPM)
07/09/2021 Department of Computer Science and Engineering 55
ARRAY ADT- SEARCH
search()
declare array A
read n
for (i=0;i<n;i++)
read A[i]
read k(element to be searched)
set f=0
for(i=0;i<n;i++)
if(A[i]==k)
f=1
print i
break
if(f==0)
print “Element notandfound”
Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 56


ARRAY ADT- SEARCH

main()
{
int list[20],size,i,sElement;
printf("Enter size of the list: ");
scanf("%d",&size);
printf("Enter any %d integer values: ",size);
for(i = 0; i < size; i++)
scanf("%d",&list[i]);
printf("Enter the element toandbeProject
Search: ");
Management
scanf("%d",&sElement); (SEPM)

07/09/2021 Department of Computer Science and Engineering 57


ARRAY ADT- SEARCH

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


{
if(sElement == list[i])
{
printf("Element is found at %d index", i);
break;
}
}
if(i == size) and Project
Management
printf("Given element is not found
(SEPM) in the list!!!");
}
07/09/2021 Department of Computer Science and Engineering 58
ARRAY ADT- SEARCH

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 59


LINKED LIST

• A linked list is a linear data structure.


• Nodes make up linked lists.
• Nodes are structures made up of data and a pointer
to another node.
• Pointer is called next.

07/09/2021 Department of Computer Science and Engineering 60


DRAWBACKS OF ARRAY

•Fixed size: Resizing is expensive

•Insertions and Deletions are inefficient:

Elements are usually shifted

•No memory waste if the array is full or almost full; otherwise

may result in much memory waste.

07/09/2021 Department of Computer Science and Engineering 61


WHY LINKED LIST?

•Dynamic size
•Insertions and Deletions are efficient: No Shifting
•Linked List can grow and shrink during run time.
•Since memory is allocated dynamically there is no waste of
memory.
•Faster Access time,can be expanded in constant time without
memory overhead

07/09/2021 Department of Computer Science and Engineering 62


REPRESENTATION OF LINKED LIST

•Static or sequential or array representation.


• The linked list will be maintained or represented by two
parallel arrays of same sizes.
•Dynamic or pointers or linked representation.
• The size of the linked list may increase or decrease according
to the requirement

07/09/2021 Department of Computer Science and Engineering 63


TYPES

• Singly Linked List

• Doubly Linked List

• Circular Linked List

 Singly Circular Linked List

 Doubly Circular Linked List

07/09/2021 Department of Computer Science and Engineering 64


SINGLY LINKED LIST

• Two field
 Data
 Next pointer

07/09/2021 Department of Computer Science and Engineering 65


SINGLY LINKED LIST

• Link part of the last node contains NULL value


which signifies the end of the node

07/09/2021 Department of Computer Science and Engineering 66


DECLARATION

Struct node ;
typedef struct Node *List ;
typedef struct Node *Position ;
int IsLast (List L) ;
int IsEmpty (List L) ;
position Find(int X, List L) ;
void Delete(int X, List L) ;
position FindPrevious(int X, List L) ;
position FindNext(int X, List L) ;
void Insert(int X, List L, Position P) ;
void DeleteList(List L) ;
07/09/2021 Department of Computer Science and Engineering 67
STORING DATA IN A NODE

Node declaration:
struct node
{
int data; node
*next; 50
}
Storing data: nptr
node *nptr;
nptr= new(node);
nptr->data= 50;
nptr->next = NULL;

07/09/2021 Department of Computer Science and Engineering 68


INSERTING A NEW NODE

Insertion of node into a linked list requires a free node


in which the information can be inserted and then the node
can be inserted into the linked list.

 At beginning.
 At the end of list.
 At the specified position.

07/09/2021 Department of Computer Science and Engineering 69


INSERTION AT BEGINNING

void insert(item)
{
struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->data = item;
newNode->next = head;
head = newNode;
}

07/09/2021 Department of Computer Science and Engineering 70


INSERTION AT BEGINNING

HEAD 48 17 142 //

Step 1 Step 2

Step 3

HEAD 93

07/09/2021 Department of Computer Science and Engineering 71


INSERTION AT BEGINNING

07/09/2021 Department of Computer Science and Engineering 72


INSERTION AT END
void insert(item)
{
struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->data = item;
newNode->next = NULL;
struct node *temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
07/09/2021 Department of Computer Science and Engineering 73
INSERTION AT END

HEAD 48 17 142 //

Step 1 Step 2

Step 3

HEAD 48 17 142 93 //
07/09/2021 Department of Computer Science and Engineering 74
INSERTION AT END

07/09/2021 Department of Computer Science and Engineering 75


INSERTION AT END
void Insert (int X, List L, Position P)
{
position Newnode;
Newnode = malloc (size of (Struct Node));
If (Newnode! = NULL)
{
Newnode ->Element = X;
Newnode ->Next = P-> Next;
P-> Next = Newnode;
}
}

07/09/2021 Department of Computer Science and Engineering 76


INSERTION AT SPECIFIED POSITION

HEAD 48 17 142 //

Step 1 Step 2

HEAD 48 17 //
142

07/09/2021 Department of Computer Science and Engineering 77


INSERTION AT SPECIFIED POSITION

07/09/2021 Department of Computer Science and Engineering 78


INSERTION AT SPECIFIED POSITION

07/09/2021 Department of Computer Science and Engineering 79


DELETION
void Delete(int X, List L)
{
position P, Temp;
P = Findprevious (X,L);
If (!IsLast(P,L))
{
Temp = P→Next;
P →Next = Temp→Next;
Free (Temp);
}
}

07/09/2021 Department of Computer Science and Engineering 80


DELETION

HEAD 48 17 142

Step 1

Step 2

HEAD 48 17 142 //

07/09/2021 Department of Computer Science and Engineering 81


DELETION

07/09/2021 Department of Computer Science and Engineering 82


DELETION

07/09/2021 Department of Computer Science and Engineering 83


DELETE THE LIST
void DeleteList (List L)
{
position P, Temp;
P = L →Next;
L→Next = NULL;
while (P! = NULL)
{
Temp = P→Next
free (P);
P = Temp;
}
}
07/09/2021 Department of Computer Science and Engineering 84
FIND
position Find (int X, List L)
{
position P;
P = L-> Next;
while (P! = NULL && P -> Element ! = X)
P = P->Next;
return P;
}

07/09/2021 Department of Computer Science and Engineering 85


FIND PREVIOUS
position FindPrevious (int X, List L)
{
position P;
P = L;
while (P -> Next ! = Null && P ->Next -> Element ! = X)
P = P ->Next;
return P;
}

07/09/2021 Department of Computer Science and Engineering 86


FIND NEXT

position FindNext (int X, List L)


{
P = L ->Next;
while (P -> Next! = NULL && P-> Element ! = X)
P = P→Next;
return P→Next;
}

07/09/2021 Department of Computer Science and Engineering 87


IsLast()

int IsLast (position P, List L)


{
if (P->Next = = NULL)
return;
}

07/09/2021 Department of Computer Science and Engineering 88


IsEmpty()

int IsEmpty (List L)


{
if (L -> Next = = NULL)
return (1);
}

07/09/2021 Department of Computer Science and Engineering 89


ADVANTAGES

1) Insertions and Deletions can be done easily.


2) It space is not wasted as we can get space according to
our requirements.
3) Its size is not fixed.
4) Elements may or may not be stored in consecutive
memory available, even then we can store the data in
computer.

07/09/2021 Department of Computer Science and Engineering 90


DISADVANTAGES

1) It requires more space as pointers  are also stored  with


information.
2) Different amount of time is required to access each
element.
3) If we have to go to a particular element then we have to
go through all those elements that come before that
element.
4) Can not traverse it from last & only from the beginning.

07/09/2021 Department of Computer Science and Engineering 91


DOUBLY LINKED LIST

 Node Contains
Info : The user’s data.
Prev/Blink: The address of previous node
Next/Flink: The address of next node

Prev Next
Data

07/09/2021 Department of Computer Science and Engineering 92


ADVANTAGES OVER SLL

•  A DLL can be traversed in both forward and backward direction.


•  The delete operation in DLL is more efficient
• Easy to insert a new node before a given node.
• In singly linked list, to delete a node, pointer to the previous
node is needed.
• In DLL, we can get the previous node using previous pointer.

07/09/2021 Department of Computer Science and Engineering 93


CREATING A NEW NODE

• Create a head node and assign some data to its data field.


• Make sure that the previous and next address field of
the head node must point to NULL.

struct Node {
    int data;
    struct Node* next; // Pointer to next node in DLL
    struct Node* prev; // Pointer to previous node in DLL
};
07/09/2021 Department of Computer Science and Engineering 94
INSERTION
void Insert (int X, list L, position P)
{
Struct Node * Newnode;
Newnode = malloc (size of (Struct Node));
If (Newnode ! = NULL)
{
Newnode →Element = X;
Newnode →Flink = P Flink;
P →Flink →Blink = Newnode;
P →Flink = Newnode ;
Newnode →Blink = P;
}}
07/09/2021 Department of Computer Science and Engineering 95
INSERTION

07/09/2021 Department of Computer Science and Engineering 96


DELETION
void Delete (int X, List L)
{
position P;
P = Find (X, L);
If ( IsLast (P, L))
{
Temp = P;
P →Blink →Flink = NULL;
free (Temp);
}

07/09/2021 Department of Computer Science and Engineering 97


DELETION
else
{
Temp = P;
P →Blink→ Flink = P→Flink;
P →Flink →Blink = P→Blink;
free (Temp);
}
}

07/09/2021 Department of Computer Science and Engineering 98


DELETION AT SPECIFIED POSITION

07/09/2021 Department of Computer Science and Engineering 99


PROS & CONS

PROS:
1) Deletion operation is easier.
2) Finding the predecessor & Successor of a node is easier.
CONS:
 Every node of DLL Require extra space for an previous pointer.

07/09/2021 Department of Computer Science and Engineering 100


APPLICATIONS

1) It is used by browsers to implement backward and forward

navigation of visited web pages i.e. back and forward button

07/09/2021 Department of Computer Science and Engineering 101


CIRCULAR LINKED LIST

• Last node points to the first


• Both singly and doubly linked list can be made into a
circular linked list.
• Circular linked list can be used to help traverse the same
list again and again if needed.

07/09/2021 Department of Computer Science and Engineering 102


WHY?

• To represent arrays that are naturally circular, e.g. the corners of

a polygon, a pool of buffers.

• With a circular list, a pointer to the last node gives easy access

also to the first node, by following one link.

• Some problems are circular and a circular data structure would

be more natural when used to represent it.

07/09/2021 Department of Computer Science and Engineering 103


REPRESENTATION

07/09/2021 Department of Computer Science and Engineering 104


TYPES

1. Singly: The last node points to the first node and there is only
link between the nodes of linked list.

2. Doubly: The last node points to the first node and


there are two links between the nodes of linked list.

07/09/2021 Department of Computer Science and Engineering 105


CREATING A NEW NODE- SINGLY

Node declaration:
struct node
{
int data; node
*next;
};
Declare variable (pointer type) that point to the node:
node *nptr;
Allocate memory for new node:
nptr = new (node);
Enter value:
nptr→data = item;
nptr→next = NULL;

07/09/2021 Department of Computer Science and Engineering 106


BASIC OPERATIONS

1) Insert element at beginning in list.

2) Insert element at end in list.

3) Insert element at any place in list.

4) Delete element from the beginning of list.

5) Delete element from the end of list.

6) Delete element from any place from list.

7) Display

07/09/2021 Department of Computer Science and Engineering 107


INSERTING A NEW NODE

Insertion of node into a linked list requires a free node


in which the information can be inserted and then the node
can be inserted into the linked list.

 At beginning.
 At the end of list.
 At the specified position.

07/09/2021 Department of Computer Science and Engineering 108


INSERTION AT BEGINNING ALGORITHM
• Declare struct node *t.
• Set t:=start.
• Create a new node n by malloc function and enter the
information in info part.
• Check if start=NULL set start=n
set start->next=start. else
• Set n->next=start.
• Repeat step(a)
while(t->next!=start)
▫ (a) set t:=t->next.
• Set t->next=n.
• Set start=n. [end if]

07/09/2021 Department of Computer Science and Engineering 109


INSERTION AT BEGINNING CODE
Void addfront()
{
struct node *t=start;
struct node *n=(struct node*)malloc(sizeof(struct node));
printf(“\nenter the information”);
scanf(“%d”,&n->info);

if(start==NULL)

{
start=n;
start->next=start;
}

07/09/2021 Department of Computer Science and Engineering 110


INSERTION AT BEGINNING CODE
else
{
n->next=start;
while(t->next!=start)
t=t->next;
t->next=n;
start=n;
}

07/09/2021 Department of Computer Science and Engineering 111


INSERTION AT BEGINNING

07/09/2021 Department of Computer Science and Engineering 112


INSERTION AT END ALGORITHM
• Declare struct node *t.
• Set t:=start.
• Create a new node n by malloc function and enter the information
in info part.
• Check if start=NULL then,
set start:=n.
set start->next:=start. Otherwise
• Set n->next:=start.
• Repeat step(a)
while(t!=NULL)
▫ (a) set t:=t->next.
• [end of loop]
• Set t->next=n. [end if]

07/09/2021 Department of Computer Science and Engineering 113


INSERTION AT END

07/09/2021 Department of Computer Science and Engineering 114


INSERTION AT END
Void addlast()
{
struct node *t=start;
struct node *n=(struct node*)malloc(sizeof(struct node));
printf(“\nenter the information”); scanf(“%d”,&n->info);
if(start==NULL)
{
start=n;
start->next=start;
}
else
{
n->next=start;
while(t!=NULL)
t=t->next;
t->next=n;
}

07/09/2021 Department of Computer Science and Engineering 115


INSERTION AT SPECIFIED POSITION
let
*head - pointer to first node
*p -pointer to the node after which we want to insert a new node.
1. Create a new node using malloc function
NewNode=(NodeType*)malloc(sizeof(NodeType));
2. Assign data to the info field of new node
NewNode->info=newItem;
3. Set next of new node to next of p
NewNode->next=p->next;
4. Set next of p to NewNode
p->next =NewNode
5. End

07/09/2021 Department of Computer Science and Engineering 116


INSERTION AT SPECIFIED POSITION
void insertAfter(struct Node* prev_node, int new_data)
{
    if (prev_node == NULL) 
    { 
       printf("the given previous node cannot be NULL");       
       return;  
    }  
    struct Node* new_node =(struct Node*) malloc(sizeof(struct Node));
    new_node->data  = new_data;
    new_node->next = prev_node->next; 
    prev_node->next = new_node;
}
07/09/2021 Department of Computer Science and Engineering 117
INSERTION AT SPECIFIED POSITION

07/09/2021 Department of Computer Science and Engineering 118


DELETION OF A NODE

If the node be deleted, that element should be search all


over the list still the node find. Then it should be deleted.

 Deletion at the beginning position of the linked list.


 Deletion at the ending position of linked list.
 Deletion at a specified position in linked list.

07/09/2021 Department of Computer Science and Engineering 119


DELETION AT BEGINNING
• Check if (start=NULL) then,
print “empty list”
• Check if
(start->next=start) then,
declare free (t)
set start:=NULL otherwise
• Repeat step(a) while(t->next!=start)
▫ (a) set t:=t->next
• [end of loop]
• set start:=start->next
• Declare free(t-next).
• Set t->next:=start.
• [end of if]

07/09/2021 Department of Computer Science and Engineering 120


DELETION AT BEGINNING

07/09/2021 Department of Computer Science and Engineering 121


DELETION AT BEGINNING
Void delfront()
{
struct node *t=start;
if (start==NULL)
printf (“\nempty list”);
else if (start->next==start)
{
free (t); start=NULL;
}
else{
while(t->next!=start)
t=t->next;
start=start->next;
free(t->next);
t->next=start;
}}
07/09/2021 Department of Computer Science and Engineering 122
DELETION AT END
• Check if (start=NULL) then, print “empty list”
• Check if (start->next=start)
then,
declare free (t) set start:=NULL otherwise
• Repeat step(a)
while(t->next->next!=start)
▫ (a) set t:=t->next
• [end of loop]
• Declare free(t->next).
• Set t->next:=start.
• [end if]
07/09/2021 Department of Computer Science and Engineering 123
DELETION AT END

07/09/2021 Department of Computer Science and Engineering 124


DELETION AT END
Void dellast()
{
struct node *t=start;
if (start==NULL)
printf(“\nempty list”);
else if (start->next==start)
{
free (t); start=NULL ;
}
else
{
while(t->next->next!=start)
t=t->next;
free(t->next);
t->next=start;
}
}

07/09/2021 Department of Computer Science and Engineering 125


DELETION AT SPECIFIED POSITION

07/09/2021 Department of Computer Science and Engineering 126


DELETION AT SPECIFIED POSITION
void deleteNode(struct Node **head, int key)
{
    struct Node* temp = *head *prev;
      if (temp != NULL && temp->data == key)
    {
        *head_ref = temp->next;  
        free(temp);              
        return;
    }
    while (temp != NULL && temp->data != key)
    {
        prev = temp;
        temp = temp->next;
    }
    if (temp == NULL) return;
     prev->next = temp->next;
     free(temp); 
}
07/09/2021 Department of Computer Science and Engineering 127
DELETION AT SPECIFIED POSITION

07/09/2021 Department of Computer Science and Engineering 128


FINDING A N ELEMENT
Step 1: IF HEAD == NULL
  WRITE "UNDERFLOW"
 GOTO STEP 8
 [END OF IF]
Step 2: Set PTR = HEAD
Step 3: Set i = 0
Step 4: Repeat step 5 to 7 while PTR != NULL
Step 5: IF PTR → data = item
return i
[END OF IF]
Step 6: i = i + 1
Step 7: PTR = PTR → next
Step 8: Exit
07/09/2021 Department of Computer Science and Engineering 129
CREATING A NEW NODE – DOUB LY

• Create a head node and assign some data to its data field.


• Make sure that the previous and next address field of
the head node must point to NULL.
• Make the head node as last node.

struct Node {
    int data;
    struct Node* next; // Pointer to next node in DLL
    struct Node* prev; // Pointer to previous node in DLL
};
07/09/2021 Department of Computer Science and Engineering 130
INSERTION
let
*head - pointer to first node
*p -pointer to the node after which we want to insert a new node.
1. Create a new node using malloc function
2. Assign data to the info field of new node
NewNode->info=newItem;
3. Set next of new node to next of p
NewNode->next=p->next;
4. Set next of p to NewNode
p->next =NewNode
5. End

07/09/2021 Department of Computer Science and Engineering 131


INSERTION

07/09/2021 Department of Computer Science and Engineering 132


DELETION

07/09/2021 Department of Computer Science and Engineering 133


DELETION

07/09/2021 Department of Computer Science and Engineering 134


APPLICATIONS OF LIST

1. Polynomial ADT
2. Radix Sort
3. Multilist

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 135


Stack ADT

• Principle-LIFO
•Top pointer
• Operations
• Push()
• Pop()
• Conditions
• Underflow-Empty stack
• Overflow – Full stack
and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 136


Stack ADT

Example:

07/09/2021 Department of Computer Science and Engineering 137


Why it is Used?

• Used for
 Reversing elements
 MS paint and Editing apps.
 Expression evaluation
 Backtracking algorithms
To check the string is well formed parenthesis.

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 138


Stack Operation
• Push-Insertion
To push x:
top++;
s[top]=x;
• Pop –Deletion
To pop(x)
s[top]=x
top--;
• Peek() – return the top element
• isEmpty()-to check whether stack is empty or not
and Project
Management
• isFull()- to check(SEPM)
whether stack is full or not

07/09/2021 Department of Computer Science and Engineering 139


Stack Implementation

Two types:
Array Implementation
Linked List Implementation

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 140


Array Implementation

Array Implementation of Stack ADT:


• peek( )
• push( )
• pop( )
• isempty( )
• isfull( )

int peek()
{
return stack[top];
}
and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 141


Array Implementation
To check whether stack is empty:
bool isfull()
{
if(top == MAXSIZE-1)
return true;
else
return false;
}

To check whether stack is empty:


bool isempty()
{
if(top == -1)
return true; and Project
else Management
(SEPM)
return false;
}
07/09/2021 Department of Computer Science and Engineering 142
Array Implementation
push():

void push(int data)


{
if(!isfull())
{
top = top + 1;
stack[top] = data;
}
else
{
printf(“Stack is full.\n");
}
} and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 143


Array Implementation
Push(50)

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 144


Array Implementation
pop():

int pop(int data)


{
if(!isempty())
{
data = stack[top];
top = top - 1;
return data;
}
else
{
printf("Stack is empty.\n");
} and Project
} Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 145


Array Implementation
Pop()

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 146


Linked List Implementation

Node Creation:

Step 1 - Define a 'Node' structure with two members data and next.

Step 2 - Define a Node pointer 'top' and set it to NULL.

DATA NEXT
and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 147


CREATE AN EMPTY STACK
Stack CreateStack ( )
{
Stack S;
S = malloc (Sizeof (Struct Node));
if (S = = NULL)
Error (" Outof Space");
MakeEmpty (s);
return S;
}

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 148


MAKE EMPTY

void MakeEmpty (Stack S)


{
if (S = = NULL)
Error (" Create Stack First");
else
while (! IsEmpty (s))
pop (s);
}

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 149


LINKED LIST IMPLEMENTATION - PUSH
void push (int X, Stack S)
{
Struct Node * Tempcell;
Tempcell = malloc (sizeof (Struct Node));
If (Tempcell = = NULL)
Error ("Out of Space");
else
{
Tempcell → Element = X;
Tempcell → Next = S → and
Next;
Project
Management
S→Next = Tempcell; (SEPM)

}}
07/09/2021 Department of Computer Science and Engineering 150
LINKED LIST IMPLEMENTATION - PUSH

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 151


LINKED LIST IMPLEMENTATION - POP
void pop (Stack S)
{
Struct Node *Tempcell;
If (IsEmpty (S))
Error ("Empty Stack");
else
{
Tempcell = S→Next;
S→Next = S→Next→Next;
Free (Tempcell); and Project
Management
}} (SEPM)

07/09/2021 Department of Computer Science and Engineering 152


LINKED LIST IMPLEMENTATION - POP

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 153


RETURN TOP ELEMENT

int Top (Stack S)


{
If (! IsEmpty (s))
return S→Next→Element;
Error ("Empty Stack");
return 0;
}

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 154


Applications

 Function call
 Evaluation Arithmetic Expression
• Infix to postfix
• Evaluating postfix
 Balancing Parenthesis
Reversing a word
8 Queen problem
Towers of Hanoi
and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 155


EVALUATING ARITHMETIC EXPRESSIONS

INFIX notation:
• Operator presents between operands: A+B
POSTFIX notation:
• Operator follows its two operands: AB+
PREFIX notation:
• Operator precedes its two operands: +AB
Operator Precedence
$, ^
*,/
+,-

07/09/2021 Department of Computer Science and Engineering 156


INFIX TO POSTFIX -ALGORITHM

1. Scan the infix expression

2. If the scanned character is an operand, output it.

3. Else,
3.1 If (precedence of scanned operator) >(precedence of
stack operator) - push it.
3.2 Else, Pop all the operators from the stack which are
greater than or equal to in precedence than that of the
scanned operator.

07/09/2021 Department of Computer Science and Engineering 157


INFIX TO POSTFIX –ALGORITHM

4. If the scanned character is an ‘(‘, push it to the stack.

5. If the scanned character is an ‘)’, pop the stack and and output

it until a ‘(‘ is encountered, and discard both the parenthesis.

6. Repeat steps 2-6 until infix expression is scanned.

7. Print the output

8. Pop and output from the stack until it is not empty.

07/09/2021 Department of Computer Science and Engineering 158


CONVERSION OF INFIX INTO POSTFIX
(2+(4-1)*3) into 241-3*+
CURRENT ACTION STACK STATUS POSTFIX
SYMBOL PERFORMED EXPRESSIO
N
( PUSH ( ( 2
2 2
+ PUSH + (+ 2
( PUSH ( (+( 24
4 24
- PUSH - (+(- 241
1 POP 241-
) (+ 241-
* PUSH * (+* 241-
3 241-3
POP * 24 1- 3 *
POP + 241-3*+
)
07/09/2021 Department of Computer Science and Engineering 159
EVALUATING POSTFIX EXPRESSION

1) Scan the expression from left to right.

2) If an operand is seen, push it onto stack.

3) If an operator is seen, pop of top two elements

(operands) [ x & y ] from stack and perform z = operand y 

Push z onto stack.

4) Repeat steps (2) & (3) till scanning is over.

07/09/2021 Department of Computer Science and Engineering 160


EVALUATING POSTFIX EXPRESSION

07/09/2021 Department of Computer Science and Engineering 161


BALANCING PARENTHESIS

• Declare a character stack S.
• Now traverse the expression string exp.
 If the current character is a starting bracket (‘(‘ or ‘{‘ or ‘[‘)
then push it to stack.
 If the current character is a closing bracket (‘)’ or ‘}’ or ‘]’) then
pop from stack and if the popped character is the matching starting
bracket then fine else parenthesis are not balanced.
• After complete traversal, if there is some starting bracket left in
stack then “not balanced”
Balancing Parenthesis
07/09/2021 Department of Computer Science and Engineering 162
BALANCING PARENTHESIS

VALID INPUTS INVALID INPUTS

{} {(}
({[]}) ([(()])
{[]()} {}[])
[{({}[]({ [{)}(]}]
})}]
07/09/2021 Department of Computer Science and Engineering 163
TOWERS OF HANOI
void hanoi (int n, char s, char d, char i)
{
if (n = = 1)
{
print (s, d);
return;
}
else
{
hanoi (n - 1, s, i, d);
print (s, d) ;
hanoi (n-1, i, d, s);
return; and Project
Management
} (SEPM)
}

07/09/2021 Department of Computer Science and Engineering 164


TOWERS OF HANOI

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 165


FUNCTION CALLS
When a call is made to a new function all the variables local to the
calling routine need to be saved, otherwise the new function will overwrite
the calling routine variables.
RECURSIVE FUNCTION TO FIND FACTORIAL : -
int fact (int n)
{
int s;
if (n = = 1)
return (1);
else
s = n * fact (n - 1);
return (s); and Project
Management
} (SEPM)

07/09/2021 Department of Computer Science and Engineering 166


Queue ADT

• FIFO

• Front is a variable which refers to first position in queue

• Rear is a variable which refers to last position in queue.

• Element is component which has data.

• MaxQueue is variable that describes maximum number


and Project
of elements Management
in a queue
(SEPM)

07/09/2021 Department of Computer Science and Engineering 167


Queue ADT

•Two pointers
Front
Rear
• Basic Operations
enqueue() – Rear end
dequeue() – Front end
• Conditions
Underflow-Empty Queue
and Project
OverflowManagement
- Full Queue
(SEPM)

07/09/2021 Department of Computer Science and Engineering 168


QUEUE ADT

Deletion Insertion
(DEQUEUE) (ENQUEUE)
10 20 30 40 50

A[0] A[1] A[2] A[3] A[4]

07/09/2021 Department of Computer Science and Engineering 169


ENQUEUE

• Placing an item in a queue is called “Insertion or

Enqueue”, which is done at the end of the queue called

“Rear”.

Front
Rear
07/09/2021 Department of Computer Science and Engineering 170
DEQUEUE

• Removing an item from a queue is called

“deletion or dequeue”, which is done at the other end

of the queue called “front”.

Front
Rear

07/09/2021 Department of Computer Science and Engineering 171


TYPES OF QUEUE

• Linear Queue

• Circular Queue

• Priority Queue

• Deque – Double Ended Queue

 Input Restricted Deque

 Output Restricted Deque

07/09/2021 Department of Computer Science and Engineering 172


LINEAR QUEUE

• A normal queue
• insertion - Rear 
• deletion –Front 

07/09/2021 Department of Computer Science and Engineering 173


QUEUE IMPLEMENTATION

Two types:
Array Implementation
Linked List Implementation

Array Implementation
arr 0 1 3 6 7 8 9
2 4 5

A B C D E F G

back
front and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 174


ENQUEUE

void enqueue(item)
{
if (rear = = maxsize-1 )
print (“queue overflow”);
else
rear = rear + 1;
Queue [rear] = item;
}

07/09/2021 Department of Computer Science and Engineering 175


ENQUEUE

07/09/2021 Department of Computer Science and Engineering 176


DEQUEUE
void dequeue()
{
if (front= = -1)
print “queue empty”;
else if(front == rear)
front=rear=-1;
else
{
front = front + 1 ;
item = queue [front]; return
item;
}}
07/09/2021 Department of Computer Science and Engineering 177
DEQUEUE

07/09/2021 Department of Computer Science and Engineering 178


LIST IMPLEMENTATION QUEUE

using namespace std;


 struct QNode {
    int data;
    QNode* next;
    QNode(int d)
    {
        data = d;
        next = NULL;
    }
};
 struct Queue {
    QNode *front, *rear;
    Queue()
    {
        front = rear = NULL;
    }

07/09/2021 Department of Computer Science and Engineering 179


LIST IMPLEMENTATION QUEUE

void enQueue(int x)
    {
       QNode* temp = new QNode(x);
          if (rear == NULL) {
            front = rear = temp;
            return;
        }
          rear->next = temp;
        rear = temp;
    }
  
07/09/2021 Department of Computer Science and Engineering 180
LIST IMPLEMENTATION QUEUE

rear

After inserting 4

07/09/2021 Department of Computer Science and Engineering 181


LIST IMPLEMENTATION QUEUE

 void deQueue()
    {       
        if (front == NULL)
            return;
          QNode* temp = front;
        front = front->next;
          if (front == NULL)
            rear = NULL;
          delete (temp);
    }
};   
07/09/2021 Department of Computer Science and Engineering 182
LIST IMPLEMENTATION QUEUE

front

07/09/2021 Department of Computer Science and Engineering 183


CIRCULAR QUEUE

• The last element points to the first element.

• Ring Buffer

Use:

consumes less memory than linear queue

07/09/2021 Department of Computer Science and Engineering 184


CIRCULAR QUEUE

07/09/2021 Department of Computer Science and Engineering 185


ENQUEUE -ALGORITHM

Insert-Circular-Q(CQueue, Rear, Front, N, Item)


Initailly Rear = 0 and Front = 0.
1. If Front = 0 and Rear = 0 then Set Front := 1 and go to step 4.
2. If Front =1 and Rear = N or Front = Rear + 1
then Print: “Circular Queue Overflow” and Return.
3. If Rear = N then Set Rear := 1 and go to step 5.
4. Set Rear := Rear + 1
5. Set CQueue [Rear] := Item.

6. Return

07/09/2021 Department of Computer Science and Engineering 186


ENQUEUE
void CEnqueue (int X)
{
if (Front = = (rear + 1) % Maxsize)
print ("Queue is overflow");
else
{
if (front = = -1)
front = rear = 0;
else
rear = (rear + 1)% Maxsize;
CQueue [rear] = X;
}}
07/09/2021 Department of Computer Science and Engineering 187
DEQUEUE - ALGORITHM

Delete-Circular-Q(CQueue, Front, Rear, Item)


Initially, Front = 1.

1. If Front = 0 then
Print: “Circular Queue Underflow” and
Return.
2. Set Item := CQueue [Front]
3. If Front = N then Set Front = 1 and Return.
4. If Front = Rear then Set Front = 0 and Rear = 0 and Return.
5. Set Front := Front + 1
6. Return.

07/09/2021 Department of Computer Science and Engineering 188


DEQUEUE
int CDequeue ( )
{
if (front = = -1)
print ("Queue is underflow");
else
{
X = CQueue [Front];
if (Front = = Rear)
Front = Rear = -1;
else
Front = (Front + 1)% maxsize;
}
return (X);
}

07/09/2021 Department of Computer Science and Engineering 189


EXAMPLE

07/09/2021 Department of Computer Science and Engineering 190


EXAMPLE

07/09/2021 Department of Computer Science and Engineering 191


APPLICATIONS OF QUEUE
• Batch processing in an operating system
• To implement Priority Queues.
• Priority Queues can be used to sort the elements using Heap
Sort.
• Simulation.
• Mathematics user Queueing theory.
• Computer networks where the server takes the jobs of the
client as per the queue strategy.
and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering 192


RESEARCH APPLICATION
• Image processing

-Watershed transformation implementation by hierarchical queues

• The HQ is initialized by storing tokens corresponding to the pixels

labelled in image gin their respective queues.

• In the case of the watershed transform (WTS), this priority level of each

queue corresponds to the grey level of the token (pixel).

• pixels of lower grey level have the highest priority (queues are

numbered according to the grey levels, queue 0 has the highest priority).

07/09/2021 Department of Computer Science and Engineering 193


RESEARCH APPLICATION

07/09/2021 Department of Computer Science and Engineering 194


QUEUEING THEORY

07/09/2021 Department of Computer Science and Engineering 195


Thank You

Department of Computer Science and Engineering

You might also like