Text Books:
1.Data Structures Using C, Second Edition ReemaThereja
OXFORD higher Education
2. Data Structures,A Pseudo codeApproach with C, Richard
F.Gillberg & Behrouz A. Forouzan, Cengage Learning, India
Edition, Second Edition, 2005.
4.
Course Outcomes:
Atthe end of the course student would be able to
1. Understand the concepts of Stacks and Queues with their
applications.
2.AnalyzeVarious operations on Binary trees.
3. Examine of various concepts of binary trees with real time
applications.
4.Analyze the shortest path algorithm on graph data
structures.
5. Outline the concepts of hashing, collision and its resolution
methods using hash functions
5.
Unit-1 Data Structure
Data structure is a representation of data and the operations
allowed on that data
A data structure is a way to store and organize data in order
to facilitate the access and modifications
A data structure is a special way of organizing and
storing data in a computer so that it can be used efficiently.
6.
Advantages of DS
Data Organization: Organizing the data so that it can
accessed efficiently when we need that particular data.
It is secure way of storage of data.
It allows information stored on disk very efficiently
7.
Data Structure Types
Thereare two types of data structures:
1. Linear Data Structure
2. Non-linear Data Structure
Linear data structures: Elements of Linear data structure are
accessed in a sequential manner, however the elements can be stored
in these data structure in any order.
Eg:LinkedList, Stack, Queue and Array
Non-linear data structures: Elements of non-linear data
structures are stores and accessed in non-linear order.
Eg:Tree and Graph
Representation of DataStructure
We can represent DS inTwo ways:
1. Static Representation
2. Dynamic Representation
Static Representation:
In Static data structure the size of the structure is fixed.The
content of the data structure can be modified but without
changing the memory space allocated to it.
Example of Static Data Structures: Array
10.
Representation of DataStructure
Dynamic Representation:
In Dynamic data structure the size of the structure in not fixed
and can be modified during the operations performed on it.
Dynamic data structures are designed to facilitate change of data
structures in the run time.
Example of Dynamic Data Structures: Linked List
12.
Linked List
Alinked list is a linear data structure, in which the elements are
not stored at contiguous memory locations.
Why Linked List?
Arrays can be used to store linear data of similar types, but arrays have the following
limitations.
1) The size of the arrays is fixed: So we must know the upper limit on the number of
elements in advance.Also, generally, the allocated memory is equal to the upper limit
irrespective of the usage.
2) Inserting a new element in an array of elements is expensive because the room has to
be created for the new elements and to create room existing elements have to be shifted.
13.
Advantages overarrays
1) Dynamic size
2) Ease of insertion/deletion
Drawbacks of Linked list:
1) Random access is not allowed.We have to access elements
sequentially starting from the first node. So we cannot do binary
search with linked lists efficiently with its default implementation.
2) Extra memory space for a pointer is required with each element
of the list.
3) Not cache friendly. Since array elements are contiguous
locations, there is locality of reference which is not there in case of
linked lists.
Linked List
14.
A linkedlist is represented by a pointer to the first node of the linked list.
The first node is called the head.
If the linked list is empty, then the value of the head is NULL.
Each node in a list consists of at least two parts:
1) data
2) Pointer (Or Reference) to the next node
// A linked list node
struct Node
{
int data;
struct Node* next;
};
Linked List Representation
15.
Types of LinkedList
Single linked List
Double Linked List
Circular Linked List
16.
Basic Operations onLinked List
Traversal:To traverse all the nodes one after another.
Insertion:To add a node at the given position.
Deletion:To delete a node.
Searching:To search an element(s) by value.
17.
A Singly-linkedlist is a collection of nodes linked together in
a sequential way where each node of the singly linked list
contains a data field and an address field that contains the
reference of the next node
The structure of the node in the Singly Linked List is
Single Linked List
18.
A single linkedlist consists of nodes where each node
contains a data field and a reference(link) to the next node in
the list.
Single Linked List
19.
Single Linked List
The nodes are connected to each other in this form where
the value of the next variable of the last node is NULL
i.e. next = NULL, which indicates the end of the linked list.
20.
Operations on SLL
Insertion
Insertion at the beginning
Insertion at the end. (Append)
Insertion after a given node
Deletion
Delete at the beginning
Delete at the end.
Delete a given node
Search
Search the node
Traverse
To traverse all the nodes one after another.
21.
Insertion
Insertion atthe beginning:
If the list is empty, the new node becomes the head of the
list. Otherwise, connect the new node to the current head of
the list and make the new node, the head of the list.
22.
Insertion
Insertion atend:
Traverse the list until find the last node.Then insert the new
node to the end of the list.
In case of a list being empty, return the updated head of the
linked list because in this case, the inserted node is the first as
well as the last node of the linked list.
23.
Insertion
Insertion aftera given node
We are given the reference to a node, and the new node is
inserted after the given node.
24.
Deletion
Delete abeginning node:
First node is pointed by the head, move the head to the next
node, then free the first node
If there is only one node then free the node and head becomes
NULL
25.
Deletion
Delete theend node:
Move the pointer to the last node and free the node.
Keep the NULL in the before that node
26.
Deletion
Delete aparticular node
To delete a node from linked list, do following steps.
1) Find previous node of the node to be deleted.
2) Change the next of previous node.
3) Free memory for the node to be deleted.
27.
Search
To searchany value in the linked list, traverse the linked list
and compares the value present in the node.
Traverse
•To traverse all the nodes one after another.
•Start with the head of the list.Access the content of the head node
if it is not null.
•Then go to the next node(if exists) and access the node
information
•Continue until no more nodes (that is, you have reached the null
node)
28.
Double Linked List
A Doubly Linked List contains an extra memory to store the
address of the previous node, together with the address of the
next node and data which are there in the singly linked list. So,
here we are storing the address of the next as well as the
previous nodes.
29.
Double Linked List
The nodes are connected to each other in this form where
the first node has prev = NULL and the last node has next
= NULL.
•Advantages over Singly Linked List-
•It can be traversed both forward and backward direction.
•Disadvantages over Singly Linked List-
•It will require more space as each node has an extra memory to store the
address of the previous node.
•The number of modification increase while doing various operations like
insertion, deletion, etc.
30.
Double Linked List
Insertion
A node can be added in four ways
1) At the front of the DLL
2) After a given node.
3) At the end of the DLL
4) Before a given node.
31.
Insertion in DLL
At the front of the DLL:
The new node is always added before the head of the given
Linked List.And newly added node becomes the new head of
DLL.
32.
Insertion in DLL
At the end of the DLL:
A Linked List is typically represented by the head of it, we
have to traverse the list till end and then change the next of
last node to new node.
33.
Insertion in DLL
Add a node after a given node:
We are given pointer to a node as prevnode, and the new node
is inserted after the given node.
34.
Insertion in DLL
Add a node before a given node:
We are given pointer to a node as prevnode, and the new node is
inserted after the before given node.
35.
Double linked list
Deletion
Delete the front node
Delete the end node
Delete the given node
36.
Deletion in DLL
Delete a First node:
If the node to be deleted is the head node then make the next node as
head
After deleting the first node the linked list becomes:
Search
To searchany value in the linked list, traverse the linked list
and compares the value present in the node.
Traverse
•To traverse all the nodes one after another.
•Start with the head of the list.Access the content of the head node
if it is not null.
•Then go to the next node(if exists) and access the node
information
•Continue until no more nodes (that is, you have reached the null
node)
40.
Circular Linked List
Circular linked list is a linked list where all nodes are
connected to form a circle.There is no NULL at the end.
A circular linked list can be a singly circular linked list or
doubly circular linked list.
Circular Single linked List
Circular Double linked List
41.
Advantages of CircularLinked Lists:
1) Any node can be a starting point.We can traverse the whole
list by starting from any point.We just need to stop when the
first visited node is visited again.
2) Useful for implementation of queue. No need to maintain
two pointers for front and rear if we use circular linked list.
We can maintain a pointer to the last inserted node and front
can always be obtained as next of last.
3) Circular lists are useful in applications to repeatedly go
around the list.
42.
Stacks
Stack isa linear data structure which follows a particular
order in which the operations are performed.The order may
be LIFO(Last In First Out) or FILO(First In Last Out).
43.
Operations on Stack
Elements can be inserted or deleted only from one end of the
stack i.e. from the top.
The element at the top is called the top element.
The operations of inserting and deleting elements are
called push() and pop() respectively.
44.
Operations on Stack
PUSH:
Push operation refers to inserting an element in the stack.
The new element is inserted at the top of the stack.
POP:
Pop operation Removes an element from the stack
The top of the element is removed
PEEK:
Get the top data element of the stack, without removing it.
45.
Implementation of Stack
Stack can be implemented in two ways:
1.UsingArray
2. Using Linked List
Array Implementation:
PUSH Operation:
The size of the stack is specified at the beginning
Initially stack is empty and top=-1
When a new element is pushed it checks the condition whether
the stack is full,if it is true it gives “Overflow”
Otherwise the element is inserted from top end
Top variable is increased by 1
46.
Array Implementation-PUSH
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).
47.
Pop():
Todelete an element from any , it checks whether the stack is
empty, if it is true it gives “Underflow”
Otherwise top of the element is removed from the stack
Top is decreased by 1
If there are no more elements top will becomes -1
Array Implementation-POP
48.
Pop
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--).
Display
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'.
49.
Array Implementation-PEEK
Peek:
If the stack is empty it displays “ Stack is empty”
Otherwise it displays the top of the element in the stack
50.
Linked List Representation
The main advantage of using linked list over an arrays is that
it is possible to implements a stack that can shrink or grow as
much as needed.
In using array will put a restriction to the maximum capacity
of the array which can lead to stack overflow.
Here each new node will be dynamically allocate.
so overflow is not possible.
The stack implemented using linked list can work for an
unlimited number of values.
51.
Linked List Representation
A stack can be easily implemented
through the linked list.
In stack Implementation, a stack
contains a top pointer. which is “head”
of the stack where pushing and popping
items happens at the head of the list.
First node have null in link field and
second node link have first node
address in link field and so on and last
node address in “top” pointer.
52.
PUSH
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.
53.
POP
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)).
54.
Display
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'.
55.
Applications of Stack
Stacks can be used for expression evaluation.
Stacks can be used to check parenthesis matching in an
expression.
Stacks can be used for Conversion from one form of
expression to another.
Stacks can be used for Memory Management.
Stack data structures are used in backtracking problems.
56.
Representation of anexpression
An expression can be represented in three ways
Infix expression
Prefix expression
Postfix expression
57.
Infix Notation
Operators are written in-between their operands
This is the usual way we write expressions
Eg:A * ( B + C ) / D
Post fixNotation
Operators are written after their operands
The postfix notation is called as suffix notation and is also referred to
reverse polish notation.
Eg:A B C + * D /
Prefix Notation
Operators are written before their operands
The prefix notation is called as polish notation
Eg: / * A + B C D
59.
Algorithm to convertInfix to Postfix
1. Read all the symbols one by one from left to right in the given Infix
Expression.
2. If the reading symbol is operand, then directly print it to the result
(Output).
3. If the reading symbol is left parenthesis '(', then Push it on to the Stack.
4. 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.
5. 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.
6. Repeat the process until the last symbol
More Problems
Thefollowing infix expressions convert to Postfix and Prefix
expressions
1. (A + B) * (C + D)
2. (A - B/C) * (A/K-L)
3.(1+4/2*1+2)*3/2
4.9*(12+4)/5*6
5.(a*b/c)-(d^e+f)
65.
Postfix Evaluation
Apostfix expression can be evaluated using the Stack data structure.
Follow the below steps
1. Read all the symbols one by one from left to right in the given
Postfix Expression
2. If the reading symbol is operand, then push it on to the Stack.
3. 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.
4. Finally! perform a pop operation and display the popped value as
final result.
Prefix Evaluation
Step1: Put a pointer P at the end of the end
Step 2: If character at P is an operand push it to Stack
Step 3: If the character at P is an operator pop two elements
from the Stack. Operate on these elements according to the
operator, and push the result back to the Stack
Step 4: Decrement P by 1 and go to Step 2 as long as there
are characters left to be scanned in the expression.
Step 5:The Result is stored at the top of the Stack, return it
Step 6: End
Queue
Queue isa 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.
71.
Operations on aQueue
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)
72.
Implementation of Queue
Queue data structure can be implemented in two ways.They
are as follows...
1.Using Array
2.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.
73.
Queue Operations usingArray
Queue data structure using array can be implemented as follows...
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.
74.
Insertion
In aqueue 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.
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.
75.
Deletion
In aqueue 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
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).
76.
Display
Use thefollowing 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)
77.
Queue using Linkedlist
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.
78.
Queue Operations usingLinked list
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’
79.
Operations
To implementqueue 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.
80.
use thefollowing 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.
enQueue(value) - Inserting an element into the Queue
81.
deQueue() - Deletingan Element from Queue
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)).
82.
display() - Displayingthe elements of Queue
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'.
83.
Applications of Queue
1. It is used to schedule the jobs to be processed by the
CPU.
2.When multiple users send print jobs to a printer, each
printing job is kept in the printing queue.Then the printer
prints those jobs according to first in first out (FIFO) basis.
3. Breadth first search uses a queue data structure to find an
element from a graph.
84.
Types of Queue
There are four types of Queue:
Simple Queue
Circular Queue
Priority Queue
Dequeue (Double Ended Queue)
85.
Circular Queue
Ina 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.
The queue after inserting all the elements into it is as follows
86.
Circular Queue
Afterdeleting three elements from the queue...
•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.
87.
Circular Queue
Acircular 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..
88.
Implementation of CircularQueue
To implement a circular queue data structure using an 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 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.
89.
enQueue(value) - Insertingvalue 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.
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.
90.
deQueue() - Deletinga 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.
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).
91.
display() - Displaysthe 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.
92.
Double Ended Queue-Deque
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.
93.
Operations on Deque
Mainlythe following four basic operations are performed on
queue:
insertFront():Adds an item at the front of Deque.
insertLast():Adds an item at the rear of Deque.
deleteFront(): Deletes an item from front of Deque.
deleteLast(): Deletes an item from rear of Deque.
94.
Deque
Double EndedQueue can be represented inTWO ways,
those are as follows...
Input Restricted Double Ended Queue
Output Restricted Double Ended Queue
95.
Input Restricted DoubleEnded Queue
In input restricted double-ended queue, the insertion operation is
performed at only one end and deletion operation is performed at
both the ends.
96.
Output Restricted DoubleEnded 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.