CS23312 - DS - Unit - 1
CS23312 - DS - Unit - 1
CS23312-DATA STRUCTURES
Unit-1
Department of CSE
Rajalakshmi Institute of Technology
DATA STRUCTURES
Prerequisite Courses :
GE23121 Problem Solving using C
CO Course Outcomes Knowl
Nos. edge
Level
ii. Reference:
1. A. V. Aho, J. E. Hopcroft, and J. D. Ullman, “Data Structures and Algorithms”,
Pearson Education, First Editionand Reprint 2003.
Project
2. R. F. Gilberg, B. A. Forouzan, “Data Structures”, Second Edition, Thomson India Edition, 2005.
Management
3. Ellis Horowitz, SartajSahni, Dinesh Mehta, “Fundamentals of Data Structure”,
(SEPM)
Computer Science Press, 1995.
and Project
Management
(SEPM)
and Project
Management
(SEPM)
A Set of Instructions
Data Structures + Algorithms
Data Structure = A Container stores Data
Algoirthm = Logic + Control
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
int temp = x;
x = y;
y = temp;
21
O(n)
22
O(nc)
• nested loops is equal to the number of times the
innermost statement is executed
for (int i = 1; i <=n; i += c)
{
for (int j = 1; j <=n; j += c)
{
// some expressions
}
}
Selection sort and Insertion Sort have O(n2) time
complexity
23
O(Logn)
• The loop variables is divided / multiplied by a
constant amount
for (int i = 1; i <=n; i *= c)
{
// some expressions
}
for (int i = n; i > 0; i /= c)
{
// some expressions
}
Binary Search has O(Logn) time complexity.
24
DATA STRUCTURES
and Project
Management
(SEPM)
DATA STRUCTURES
array
Linked list
and Project
Management
(SEPM)
queue
tree stack
•Integer
•Float
•Character
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
How?????
and Project
Management
(SEPM)
X=100
and Project
Management
(SEPM) p
X=100
and Project
Management
(SEPM) p i
X=100
and Project
Management
(SEPM) p i
X=100
and Project
Management
(SEPM) p i
X=100
and Project
Management
(SEPM) p i
X=100
and Project
Management
(SEPM) p i
X=100
and Project
Management
(SEPM) p i
X=100
and Project
Management
(SEPM) p i
X=100
and Project
Management
(SEPM) p i
X=100
and Project
Management
(SEPM) p i
X=100
and Project
Management
(SEPM) p i
X=100
and Project
Management
(SEPM) p i Next iteration i=1
Is not greater than 1
X=100
and Project
Management
(SEPM) p i
X=100
and Project
Management
(SEPM) p i
and Project
Management
(SEPM)
Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 22 33 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
n=n-1;
}
and Project
Management
(SEPM)
Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 22 33 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
}
and Project
Management
(SEPM)
Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 22 33 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
} i
and Project
Management
(SEPM)
Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 22 33 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
} i
and Project
Management
(SEPM)
Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 33 33 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
} i
and Project
Management
(SEPM)
Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 33 33 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
}
i
and Project
Management
(SEPM)
Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 33 44 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
}
i
and Project
Management
(SEPM)
Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 33 44 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
}
i
and Project
Management
(SEPM)
Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 33 44 55 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
}
i
and Project
Management
(SEPM)
Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 33 44 55 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
}
i
and Project
Management
(SEPM)
Delete Element
void delet(int a[],int p) 0 1 2 3 4
{ 11 100 33 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
}
Decrease size
and Project
Management
(SEPM)
35 42 12 5 \\
12 Found!
64
Searching in an Unordered
Collection
• Let’s determine if the value 13 is in the
collection:
35 42 12 5
13 Not Found!
65
ARRAY - ADT
Search API
void search(int a[],int x)
{
int i;
for(i=0;i<n;i++)
{
if(x==a[i])
{
printf("Value is present in:%d\n\n\n",i);
break;
}
}
if(i==n)
printf(“Value not present”);
and Project
} Management
(SEPM)
and Project
Management
(SEPM)
and Project
•Element search is slow.
Management
(SEPM)
and Project
Management
(SEPM)
}
p
Found !!!
Reached End
Not found!!!
8/17/2020 Department of Computer Science and Engineering 74
Singly Linked List - FINDPREVIOUS
Position find_previous( element_type x, LIST L )
{
position p;
p = L;
while( (p->next != NULL) && (p->next->element != x) )
p = p->next; Findprevious(63,L)
return p; List
} p 40 48 55 59 63
p p p
p
List
40 48 59 63
p
X
newnode
Making link between the new node and the node after the tptr.
List
40 48 59 63
p
X
newnode
40 48 X 59 63
40 48 55 59 63
An existing Linked list
Node that is to be deleted
List
40 48 55 59 63
p tempcell
Searching the target element
1. making link between previous and next
node of the node to be deleted
List
40 48 55 59 63
40 48 59 63
Updated List
8/17/2020 Department of Computer Science and Engineering 78
Deletion of a particular node
Tmp_cell
{ L
11 22 33 X
Newnode →Element = X;
Newnode →Flink = P → Flink;
P →Flink →Blink = Newnode;
P →Flink = Newnode ; newnode
Newnode →Blink = P;
}}
Department of Computer Science and Engineering 87
INSERTION
void Insert (int X, list L, position P)
{
Struct Node * Newnode;
Insert(25,L,2)
Newnode = malloc (size of (Struct Node));
If (Newnode ! = NULL) P
{ L
11 22 33 X
Newnode →Element = X;
Newnode →Flink = P → Flink;
25
P →Flink →Blink = Newnode;
P →Flink = Newnode ; newnode
Newnode →Blink = P;
}}
Department of Computer Science and Engineering 88
INSERTION
void Insert (int X, list L, position P)
{
Struct Node * Newnode;
Insert(25,L,2)
Newnode = malloc (size of (Struct Node));
If (Newnode ! = NULL) P
{ L
11 22 33 X
Newnode →Element = X;
Newnode →Flink = P → Flink;
25
P →Flink →Blink = Newnode;
P →Flink = Newnode ; newnode
Newnode →Blink = P;
}}
Department of Computer Science and Engineering 89
INSERTION
void Insert (int X, list L, position P)
{
Struct Node * Newnode;
Insert(25,L,2)
Newnode = malloc (size of (Struct Node));
If (Newnode ! = NULL) P
{ L
11 22 33 X
Newnode →Element = X;
Newnode →Flink = P → Flink;
25
P →Flink →Blink = Newnode;
P →Flink = Newnode ; newnode
Newnode →Blink = P;
}}
Department of Computer Science and Engineering 90
INSERTION
void Insert (int X, list L, position P)
{
Struct Node * Newnode;
Insert(25,L,2)
Newnode = malloc (size of (Struct Node));
If (Newnode ! = NULL) P
{ L
11 22 33 X
Newnode →Element = X;
Newnode →Flink = P → Flink;
25
P →Flink →Blink = Newnode;
P →Flink = Newnode ; newnode
Newnode →Blink = P;
}}
Department of Computer Science and Engineering 91
INSERTION
void Insert (int X, list L, position P)
{
Struct Node * Newnode;
Insert(25,L,2)
Newnode = malloc (size of (Struct Node));
If (Newnode ! = NULL) P
{ L
11 22 33 X
Newnode →Element = X;
Newnode →Flink = P → Flink;
25
P →Flink →Blink = Newnode;
P →Flink = Newnode ; newnode
Newnode →Blink = P;
}}
Department of Computer Science and Engineering 92
DELETION AT SPECIFIED POSITION
Temp
Temp
Temp
Image viewer – Previous and next images are linked, hence can be accessed by
Previous and next page in web browser – We can access previous and next url
searched in web browser by pressing back and next button since, they are linked as
linked list.
Music Player – Songs in music player are linked to previous and next song. you can
• Principle-LIFO
•Top pointer
•Operations
•Push()
•Pop()
•Conditions
•Underflow-Empty stack
•Overflow – Full stack
and Project
Management
(SEPM)
•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)
and Project
Management
(SEPM)
Two types:
Array Implementation
Linked List Implementation
and Project
Management
(SEPM)
int s[size];
int top=-1;
Top=-1
and Project
Management
(SEPM)
1 20
0 10
and Project
Management
(SEPM)
Node Creation:
DATA NEXT
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
Tempcell → Next = S →
andNext;
Project
Management
S→Next = Tempcell; (SEPM)
}}
Department of Computer Science and Engineering 122
LINKED LIST IMPLEMENTATION - POP
and Project
Management
(SEPM)
and Project
Management
(SEPM)
❑ 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)
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
$, ^
*,/
+,-
3. Else,
3.2 Else, Pop all the operators from the stack which are
scanned operator.
parenthesis.
( PUSH ( ( 2
2 2
+ PUSH + (+ 2
( PUSH ( (+( 24
4 24
- PUSH - (+(- 241
1 POP 241-
) (+ 241-
* PUSH * (+* 241-
3 241-3
POP * 241-3*
POP + 241-3*+
)
130
EVALUATING POSTFIX EXPRESSION
Balancing Parenthesis
{} {(}
({[]}) ([(()])
{[]()} {}[])
[{({}[]({ [{)}(]}]
})}]
Department of Computer Science and Engineering 134
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)
}
and Project
Management
(SEPM)
• FIFO
and Project
Management
(SEPM)
scheduling etc.
and Project
Management
(SEPM)
•Basic Operations
❑enqueue() – Rear end
❑dequeue() – Front end
•Conditions
❑Underflow-Empty Queue
❑Overflow - Full Queue
and Project
Management
(SEPM)
• Linear Queue
• Circular Queue
• Priority Queue
•A normal queue
insertion - Rear
deletion –Front
int queue[maxsize];
Example:
int queue[5];
if (front == - 1)
front = 0;
rear = rear + 1
Queue [rear] = item
}
Department of Computer Science and Engineering 144
ENQUEUE
void enqueue(item)
{
0 1 2 3 4
if (rear = = maxsize-1 )
print (“queue overflow”) f
else r = -1;
if (front == - 1)
front = 0;
rear = rear + 1
Queue [rear] = item
}
Department of Computer Science and Engineering 145
ENQUEUE
void enqueue(item)
{
0 1 2 3 4
if (rear = = maxsize-1 )
print (“queue overflow”) f
else r
if (front == - 1)
front = 0;
rear = rear + 1
Queue [rear] = item
}
Department of Computer Science and Engineering 146
ENQUEUE
void enqueue(item)
{
0 1 2 3 4
if (rear = = maxsize-1 ) item
else r
if (front == - 1)
front = 0;
rear = rear + 1
Queue [rear] = item
}
Department of Computer Science and Engineering 147
ENQUEUE
void enqueue(item)
{
0 1 2 3 4
if (rear = = maxsize-1 ) item
if (front == - 1)
front = 0;
rear = rear + 1
Queue [rear] = item
}
Department of Computer Science and Engineering 148
ENQUEUE
void enqueue(item)
{
0 1 2 3 4
if (rear = = maxsize-1 ) item item item item item
if (front == - 1)
front = 0;
rear = rear + 1
Queue [rear] = item
}
Department of Computer Science and Engineering 149
DEQUEUE
void dequeue()
{ 0 1 2 3 4
item item item item item
if (front= = -1)
print “queue empty” f r
front=rear=-1
else
{
item = queue [front];
front = front + 1
return item;
}}
Department of Computer Science and Engineering 152
DEQUEUE
void dequeue()
{ 0 1 2 3 4
Data 1 Data 2 Data 3 Data 4 Data 5
if (front= = -1)
print “queue empty” f r
front=rear=-1
else
{
item = queue [front];
front = front + 1
return item;
}}
Department of Computer Science and Engineering 153
DEQUEUE
void dequeue()
{ 0 1 2 3 4
Data 2 Data 3 Data 4 Data 5
if (front= = -1)
print “queue empty” f f f r f
if (front= = -1)
print “queue empty” f,r=-1
if (front= = -1)
print “queue empty” f,r=-1
struct Node {
int data;
struct Node *next;
}*front = NULL,*rear = NULL;
rear
After inserting 4
front
void dequeue()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
Else
{
struct Node *temp = front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp); }
}
•Ring Buffer
Use:
• In the case of the watershed transform (WTS), this priority level of each
• pixels of lower grey level have the highest priority (queues are numbered
if(start->next != NULL )
fun(start->next->next);
printf("%d ", start->data);
}
A146641
B135135
C1235
D135531
if(start->next != NULL )
fun(start->next->next);
printf("%d ", start->data);
}
A146641
B135135
C1235
D135531
Input: 15 25 10 55 90
8/17/2020 Department of Computer Science and Engineering 176
Linked List
The following steps in a linked list
p = getnode()
info (p) = 10
next (p) = list
list = p
result in which type of operation?
A pop operation in stack
B removal of a node
C inserting a node
D modifying an existing node