Unit 1
Unit 1
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:
•Related Courses:
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.
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
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.
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
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)
#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)
and Project
Management
(SEPM)
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)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
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
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.
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
type array-name[s1][s2][s3]…….[sn];
int survey[3][5][6];
float table[5][4][5][3];
Example:
Deletion Insertion
(DEQUEUE) (ENQUEUE)
10 20 30 40 50
edges E.
and Project
Management
(SEPM)
1. Array Implementation
3. Cursor Implementation.
and Project
Management
(SEPM)
manipulating thatand
data
Project
Management
(SEPM)
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)
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)
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)
and Project
Management
(SEPM)
•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
• Two field
Data
Next pointer
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;
At beginning.
At the end of list.
At the specified position.
void insert(item)
{
struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->data = item;
newNode->next = head;
head = newNode;
}
HEAD 48 17 142 //
Step 1 Step 2
Step 3
HEAD 93
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
HEAD 48 17 142 //
Step 1 Step 2
HEAD 48 17 //
142
HEAD 48 17 142
Step 1
Step 2
HEAD 48 17 142 //
Node Contains
Info : The user’s data.
Prev/Blink: The address of previous node
Next/Flink: The address of next node
Prev Next
Data
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
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.
• With a circular list, a pointer to the last node gives easy access
1. Singly: The last node points to the first node and there is only
link between the nodes of linked list.
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;
7) Display
At beginning.
At the end of list.
At the specified position.
if(start==NULL)
{
start=n;
start->next=start;
}
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
1. Polynomial ADT
2. Radix Sort
3. Multilist
and Project
Management
(SEPM)
• Principle-LIFO
•Top pointer
• Operations
• Push()
• Pop()
• Conditions
• Underflow-Empty stack
• Overflow – Full stack
and Project
Management
(SEPM)
Example:
• 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)
Two types:
Array Implementation
Linked List Implementation
and Project
Management
(SEPM)
int peek()
{
return stack[top];
}
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
Node Creation:
DATA NEXT
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
}}
07/09/2021 Department of Computer Science and Engineering 150
LINKED LIST IMPLEMENTATION - PUSH
and Project
Management
(SEPM)
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.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.
5. If the scanned character is an ‘)’, pop the stack and and output
• 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
{} {(}
({[]}) ([(()])
{[]()} {}[])
[{({}[]({ [{)}(]}]
})}]
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)
}
and Project
Management
(SEPM)
• FIFO
•Two pointers
Front
Rear
• Basic Operations
enqueue() – Rear end
dequeue() – Front end
• Conditions
Underflow-Empty Queue
and Project
OverflowManagement
- Full Queue
(SEPM)
Deletion Insertion
(DEQUEUE) (ENQUEUE)
10 20 30 40 50
“Rear”.
Front
Rear
07/09/2021 Department of Computer Science and Engineering 170
DEQUEUE
Front
Rear
• Linear Queue
• Circular Queue
• Priority Queue
• A normal queue
• insertion - Rear
• deletion –Front
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)
void enqueue(item)
{
if (rear = = maxsize-1 )
print (“queue overflow”);
else
rear = rear + 1;
Queue [rear] = item;
}
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
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
• Ring Buffer
Use:
6. Return
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.
• 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 according to the grey levels, queue 0 has the highest priority).