Professional Documents
Culture Documents
All Chapters (Data Starcture)
All Chapters (Data Starcture)
College of Computing
and Informatics
stack of Books
Queue
An ordered collection of items where the addition of
new items happens at one end, called the “rear,” and
the removal of existing items occurs at the “front”
end.
A finite set of instructions that takes some raw data as input and
transforms it into refined data.
o Input Size
o Input Properties
Operating Environment
Time Complexity Functions
for(i=0;i<10;i++)
for(j=0; j<=i;j++)
statement block;
The inner loop will execute just once in the first iteration, twice in the
second iteration, thrice in the third iteration, so on and so forth.
Exercise
Show that f(n) is Big Oh of g(n) for the
functions
f(n)=n2+n and g(n)=n2
Big-Omega Notation
Just as O-notation provides an asymptotic upper bound on a
function, Ω notation provides an asymptotic lower bound.
f(n)<=C1.g(n)……Big O notation
C2.g(n)<=f(n)……Big Ω notation
Therefore,C1.g(n)<=f(n)<=C2.g(n)
o
C1=10, C2= 16 ,n =1
Common Asymptotic Notations
(1) constant
O(log n) logarithmic
O(n) linear
O(n log n) linear logarithmic,
O(n2) quadratic
O(nc) polynomial or algebraic
O(cn); where c>1 exponential
O(n!) factorial
! !
nd
E
NE
O
ER
PT
H A
C
Searching Algorithm
Searching is an operation or a technique that helps
finds the place of a given element or value in the list.
In the best case, the desired element is present in the first position
of the array, i.e., only one comparison is made.
So f (n) = O(1).
But in the worst case the desired element is present in the nth (or
last) position of the array, so n comparisons are made.
key = 11
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
list 2 4 7 10 11 45 50 59 60 66 69 70 79
key = 11 mid
Lab Work
1. Implement sequential (linear search) for numeric
data.
And compare third value with the first value. If the third
value is greater than the first value place the third value to
second place, otherwise place the first value to second place.
And place the third value to first place and so on.
Cont…
Let A be a linear array of n numbers A [0], A [1], A [2], ......
A[n-1].
The algorithm scan the array A from A [0] to A [n-1], by
inserting each element A[k], into the proper position of the
previously sorted sub list. A [0], A [1], A [2], ...... A [k – 1]
Step 1: As the single element A [0] by itself is sorted array.
Step 2:A [1] is inserted either before or after A [0] by
comparing it so that A[0], A[1] is sorted array.
Step 3: A [2] is inserted into the proper place in A [0], A [1],
that is A [2] will be compared with A [0] and A [1] and
placed before A [0], between A [0] and A [1], or after A [1]
so that A [0], A [1], A [2] is a sorted array.
Cont…
Step 4: A [3] is inserted in to a proper place in A [0], A [1],
A [2] by comparing it; so that A [0], A [1], A [2], A [3] is a
sorted array.
LINKED LIST
Review on Pointers
When a variable is declared, a specific block of memory
within the computer is allocated to hold the value of that
variable.
Suppose, for example, that you have declared and initialized the following
variables:
double x = 2.5;
double *px = &x;
At this point, the variable px points to the variable x, and the expression *px
is synonymous with the variable x.
Assigning values to pointers
Assigning values to pointers is similar to that of assigning values to normal
variable, except that the variable is pointer variable.
int main()
Output
{ The address stored in numAddr is 0012FEC8
int *numAddr; The value pointed to by numAddr is 22
The address now stored in numAddr is
int miles, dist;
0012FEBC
dist = 158; The value now pointed to by numAddr is 158
miles = 22;
numAddr = &miles;
cout << "The address stored in numAddr is " << numAddr << endl;
cout << "The value pointed to by numAddr is " << *numAddr << "\n\n";
numAddr = &dist; // now store the address of dist in numAddr
cout << "The address now stored in numAddr is " << numAddr << endl;
cout << "The value now pointed to by numAddr is " << *numAddr << endl;
return 0;
}
Pointer Arithmetic
As we know pointer stores the address of another
variable, memory location we can perform the following
operations.
Pointer increment ,p++ or ++p
Pointer decrement, p- - or --p
Constant addition, p+k
Constant subtraction , p-k
Pointer Increment/Decrement
If p is a pointer then ++p,p++,p– and --p is possible with pointer.
Pointer increment, p++ or ++p does not mean adding 1 to the pointer but
adding the size of that data type to the pointer p the same for p-- or--p
eg int x,*p,*q;
short int y;
p=&x;
q =&y;
++q;
p++;
Here if both p and q has an address of 1000 after the statement p=&x
then p++ will be 1004 which is incrementing the size of (x) and ++q will
be 1002 ;
Null Pointers
A pointer variable is a pointer to a variable of some data type.
However, in some cases, we may prefer to have a null pointer which
is a special pointer value and does not point to any value.
This means that a null pointer does not point to any valid memory
address.
To declare a null pointer, you may use the predefined constant
NULL
int *ptr = NULL;
You can always check whether a given pointer variable stores the
address of some variable or contains NULL by writing,
if (ptr == NULL)
{
Statement block;
}
Introduction to Linked List
Array is a linear collection of data elements in which the
elements are stored in consecutive memory locations.
While declaring arrays, we have to specify the size of the array,
which will restrict the number of elements that the array can
store.
For example, if we declare an array as int marks[10], then the
array can store a maximum of 10 data elements but not more
than that.
But what if we are not sure of the number of elements in
advance?
Moreover, to make efficient use of memory, the elements must
be stored randomly at any location rather than in consecutive
locations.
Cont…
So, there must be a data structure that removes the restrictions on the
maximum number of elements and the storage condition to write
efficient programs.
However, unlike an array, a linked list does not allow random access
of data.
1 2 3 4 5 6 7 X
Cont…
Every node contains two parts, an integer and a pointer to the
next node.
Info Link
The left part of the node which contains data may include a
simple data type, an array, or a structure.
The right part of the node contains a pointer to the next node
(or address of the next node in sequence).
The last node will have no next node connected to it, so it will
store a special value called NULL.
START
1 Data Next
1 H 4
2
3
4 E 7
5
6
7 L 8
8 L 1
9
1 O –1
ADVANTAGES AND DISADVANTAGES
Linked list have many advantages and some of them are:
Linked list are dynamic data structure. That is, they can grow or shrink
during the execution of a program.
Efficient memory utilization: In linked list (or dynamic) representation,
memory is not pre-allocated. Memory is allocated whenever it is
required. And it is deallocated (or removed) when it is not needed.
Insertion and deletion are easier and efficient. Linked list provides
flexibility in inserting a data item at a specified position and deletion of
a data item from the given position.
Many complex applications can be easily carried out with linked list.
Linked list has following disadvantages
More memory: to store an integer number, a node with integer data and
address
field is allocated. That is more memory space is needed.
An element can not be accessed randomly
OPERATION ON LINKED LIST
Creation operation is used to create a linked list. Once a linked list is
created with one node, insertion operation can be used to add more
elements in a node.
Insertion operation is used to insert a new node at any specified
location in the linked list. A new node may be inserted.
• At the beginning of the linked list
• At the end of the linked list
• At any specified position in between in a linked list
Deletion operation is used to delete an item (or node) from the linked
list. A node may be deleted from the
• Beginning of a linked list
• End of a linked list
• Specified location of the linked list
Cont…
Traversing is the process of going through all the nodes from
one end to another end of a linked list.
In a singly linked list we can visit from left to right, forward
traversing, and nodes only.
But in doubly linked list forward and backward traversing is
possible.
Concatenation is the process of appending the second list to the
end of the first list.
Consider a list A having n nodes and B with m nodes. Then the
operation concatenation will place the 1st node of B in the
(n+1)th node in A. After concatenation A will contain ( n+m)
nodes
TYPES OF LINKED LIST
Singly linked list
Doubly linked list
Circular linked list
Singly linked list
A singly linked list is the simplest type of linked list in
which every node contains some data and a pointer to the
next node of the same data type.
By saying that the node contains a pointer to the next node,
we mean that the node stores the address of the next node in
sequence.
A singly linked list allows traversal of data only in one way.
Operations on Singly Linked list
1. Inserting a new node in a Linked List
four cases
Case 1: The new node is inserted at the beginning.
Case 2: The new node is inserted at the end.
Case 3: The new node is inserted after a given node.
Case 4: The new node is inserted before a given node.
Traversing a linked list means accessing the nodes of the list in order
to perform some processing on them.
Algorithm
1. If (START is equal to NULL)
(a) Display “The list is Empty”
(b) Exit
2. Initialize TEMP = START
3. Repeat the step 4 and 5 until (TEMP == NULL)
4. Display “TEMP → DATA”
5. TEMP = TEMP → Next
6. Exit
4. Counting Linked List nodes
To count nodes in the linked lists ,traverse each and every node of the list and
while traversing every individual node, we will increment the counter by 1.
Once we reach NULL, that is, when all the nodes of the linked list have been
traversed, the final value of the counter will be displayed.
Algorithm
Step 1: [INITIALIZE] SET COUNT =0
Step 2: [INITIALIZE] SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR! = NULL
Step 4: SET COUNT = +1
Step 5: SET PTR = PTR-> NEXT
[END OF LOOP]
Step 6: Write COUNT
Step 7: EXIT
5. Searching for a value in a Linked List
Searching means finding whether a given value is present in the
information part of the node or not.
If it is present, the algorithm returns the address of the node that
contains the value.
Algorithm
1. Input the DATA to be searched
2. Initialize TEMP = START; POS =1;
3. Repeat the step 4, 5 and 6 until (TEMP is equal to NULL)
4. If (TEMP → DATA is equal to DATA)
(a) Display “The data is found at POS”
(b) Exit
5. TEMP = TEMP → Next
6. POS = POS+1
7. If (TEMP is equal to NULL)
(a) Display “The data is not found in the list”
8. Exit
Doubly Linked List
A doubly linked list is one in which all nodes are linked together by
multiple links which help in accessing both the successor (next) and
predecessor (previous) node for any arbitrary node within the list.
Every nodes in the doubly linked list has three fields: Left_Pointer,
Right _Pointer and DATA.
Step 3: Else
a) ptr = head
b) head=head->next
c) head->prev=null
d) ptr->next=null
Step 4: free(ptr);
Step 5: Exit
Case 1: Deleting from End
Step 1: If head =null
a) Write the list empty
b) Go to Step 8
Step 2: If (head->next==null)
a) SET PTR=head
b) head = null
c) tail=null
Step 3: Else
a) ptr = head
b) ptr2=ptr->prev
Step 4 while (ptr->next! =null) do the following
a) ptr2=ptr
b) ptr = ptr->next
Step 6: a) ptr ->prev = null
b) ptr2->next=null
c) tail=ptr2
Step 7: free(ptr)
Step 8: Exit
Forward and backward Traversal
A) Forward Traversal
1. If (head is equal to null)
(a) Display “The list is Empty”
(b) Exit
2. Initialize temp=head
3. Repeat the step 4 and 5 until (temp == null)
4. Display “temp → DATA”
5. temp = temp→ Next
6. Exit
B) Backward Traversal
1. If (tail is equal to null)
(a) Display “The list is Empty”
(b) Exit
2. Initialize temp=tail
3. Repeat the step 4 and 5 until (temp == null)
4. Display “temp → DATA”
5. temp = temp→ Prev
6. Exit
CHAPTER FOUR
Once all the numbers have been read, the numbers can
be popped one at a time and then stored in the array
starting from the first index.
Evaluation of Arithmetic Expressions
Algebraic Expression
An algebraic expression is a legal combination of
operands and the operators.
Operand is the quantity (unit of data) on which a
mathematical operation is performed. Operand may be a
variable like x, y, z or a constant like 5, 4,0,9,1 etc.
Operator is a symbol which signifies a mathematical or
logical operation between the operands. Example of
familiar operators include +,-,*, /, ^
An example of expression as x+y*z.
Infix, Postfix and Prefix Expressions
INFIX: the expressions in which operands surround the
operator, e.g. x+y, 6*3 etc this way of writing the
expressions is called infix notation.
POSTFIX: Postfix notation are also Known as Reverse
Polish Notation (RPN).
They are different from the infix and prefix notations in the
sense that in the postfix notation, operator comes after the
operands, e.g. xy+,xyz+* etc.
PREFIX: Prefix notation also Known as Polish notation.
In the prefix notation, operator comes before the operands,
e.g. +xy, *+xyz etc.
Advantages of using postfix notation
Human beings are quite used to work with mathematical
expressions in infix notation, which is rather complex.
One has to remember a set of nontrivial rules while using this
notation and it must be applied to expressions in order to
determine the final value.
These rules include precedence, BODMAS, and associativity.
Using infix notation, one cannot tell the order in which
operators should be applied.
Whenever an infix expression consists of more than one
operator, the precedence rules (BODMAS) should be applied
to decide which operator (and operand associated with that
operator) is evaluated first.
Cont…
But in a postfix expression operands appear before the operator, so there
is no need for operator precedence and other rules.
Notation Conversions
Let A + B * C be the given expression, which is an infix notation. To
calculate this expression for values 4, 3, 7 for A, B, C respectively we
must follow certain rule (called BODMAS in general mathematics) in
order to have the right result.
For example: A + B * C = 4 + 3 * 7 = 7 * 7 = 49
The answer is not correct; multiplication is to be done before the
addition, because multiplication has higher precedence over addition.
An expression is calculated according to the operator’s precedence not
the order as they look like.
The error in the above calculation occurred, since there were no braces
to define the precedence of the operators.
Converting Infix to Postfix Expression
Answer:
a) I= A B C / D E F ^ * - G + H * +
b) I= A B C / D E F *%G /+H
c) I= 23*21-/53*+
Queue
Data structure in which the elements are added at one end,
called the rear, and deleted from the other end, called the
front
A queue is a First In First Out data structure
As in a stack, the middle elements of the queue are
inaccessible.
The basic operations that can be performed on queue are
1. Insert (or add) an element to the queue (en-queue)
2. Delete (or remove) an element from a queue (de-queue)
Real Life example of Queue
A queue of people at ticket-window
Telephone waiting call
Vehicles on toll-tax bridge
Applications of Queue
Round Robin scheduling
Job scheduling (FIFO)
Printer server routines (in drivers) are designed using queue
All types of customer service software (like Railway/Air tic
reservation) are designed using queue to give proper service
the customers.
Algorithms of Queue Operations Using Array
Let Q be the array of some specified size say SIZE
INSERTING AN ELEMENT INTO THE QUEUE
1. Initialize front=-1 rear = –1
2. Input the value to be inserted and assign to variable “data”
3. If (rear = SIZE-1)
(a) Display “Queue overflow”
(b) Exit
4. Else
if(Rear==-1)
front=rear=0;
else
rear = rear +1
5. Q[rear] = data
6. Exit
Cont..
DELETING AN ELEMENT FROM QUEUE
1. If (front==-1) or (front>rear)
(a) Display “The queue is empty”
(c) Exit
2. Else
(a) data = Q[front]
3. front = front +1
4. Exit
Algorithms of Queue Operations Using Linked List
ALGORITHM FOR PUSHING AN ELEMENT TO A QUEUE
REAR is a pointer in queue where the new elements are added.
FRONT is a pointer, which is pointing to the queue where the
elements are popped. DATA is an element to be pushed.
1. Input the DATA element to be pushed
2. Create a New Node
3. NewNode → DATA = DATA
4. NewNode → Next = NULL why??
5. If(REAR not equal to NULL)
(a) REAR → next = NewNode;
6. REAR =NewNode;
7. Exit
ALGORITHM FOR POPPING AN ELEMENT FROM A QUEUE
REAR is a pointer in queue where the new elements are added.
FRONT is a pointer, which is pointing to the queue where the
elements are popped. DATA is an element popped from the queue.
1. If (FRONT is equal to NULL)
(a) Display “The Queue is empty”
2. Else
(a) Display “The popped element is FRONT → DATA”
(b) If(FRONT is not equal to REAR) what does it mean?
(i) FRONT = FRONT → Next why??
(c) Else
(d) FRONT = NULL;
3. Exit
Circular Queue
Suppose a queue Q has maximum size 5, say 5 elements
pushed and 2 elements popped.
There are two types of deque depending upon the restriction to perform insertion
or deletion operations at the two ends.
1. Input restricted deque
2. Output restricted deque
Cont…
An input restricted deque is a deque, which allows
insertion at only one end, rear end, but allows deletion
at both ends, rear and front end of the lists.
An output-restricted deque is a deque, which allows
deletion at only one end, front end, but allows insertion
at both ends, rear and front ends, of the lists.
1. If (Front == – 1)
(a) Display “Queue Underflow”
(b) Exit
2. DATA = Q [Front
3. If(Front == Rear)
(a) Front = – 1
(b) Rear = – 1
4. Else
(i) Front = (Front +1)%SIZE
5. Exit
Tree.pptx
A directed graph G is defined as an ordered pair (V, E) where, V is a set of
vertices and the ordered pairs in E are called edges on V.
A directed graph can be represented geometrically as a set of marked points
(called vertices) V with a set of arrows (called edges) E between pairs of points
(or vertex or nodes) so that there is at most one arrow from one vertex to
another vertex.
Application of Graph
1. Social network graphs:- Graphs that represent who knows
whom, who communicates with whom, who influences whom
or other relationships in social structures.
2. Transportation networks :- In road networks vertices are
intersections and edges are the road segments between them,
and for public transportation networks vertices are stops and
edges are the links between them.
3. Document link graphs:- The best known example is the link
graph of the web, where each web page is a vertex, and each
hyperlink a directed edge.
4. Network packet traffic graphs :-Vertices are IP (Internet
protocol) addresses and edges are the packets that flow
between them.
5. Neural networks :-Vertices represent neurons and edges the
synapses between them
Basic Terminologies
Suppose the source vertex is a. Then following steps will illustrate the
BFS.
Step 1: Initially push a (the source vertex) to the queue.
F=0,R=0
Step 2: Pop (or remove) the front element A from the queue (by
incrementing front = front +1) and display it. Then push (or add) the
neighboring vertices of a to the queue, (by incrementing Rear = Rear
+1) if it is not in queue.
Cont…
F=1 R=2
Step 3: Pop the front element b from the queue and display
it. Then add the neighboring vertices of b to the queue, if
it is not in queue.
F=2 R=4
Step 4: Remove the front element c and display it. Add the
neighboring vertices of c, if it is not present in queue.
Cont…
F=3 R=6