0% found this document useful (0 votes)
18 views78 pages

Ds First & Second Unit Aug 2020

Uploaded by

Aransh Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views78 pages

Ds First & Second Unit Aug 2020

Uploaded by

Aransh Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

S.D.

COLLEGE OF MANAGEMENTS STUDIES MUZAFFARNAGAR (926)

Dr. Sanjeev Tayal


(HOD)
BCA&B.Sc(Computer science)

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)
 Data Structure is a representation of the
logical relationship existing between
individual elements of data . In other words,
Data Structure is a way of organising of data
item that considers not only the elements
stored but also their relationship to each
other.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Data Structure are mainly divided into two
broad categories which are :-
1.Primitive Data Structure
2.Non-Primitive Data Structure

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 These are basic structure and are directly
operated on the machine instructions These
structured have different representation on
the computers.Integer,floating point numbers
character,constants,string constants , pointer
all in this category.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 These are more complex data structure .
These are derived from the primitive data
structure .The non-primitive data structure
emphasize on structure on a group of
homogenous or heterogeneous data items. Array
,List and Files are the example of non-primitive
data structure.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Type of Data Structure

Data
Structure

Non-
Primitive
Primitive

Integer, Array,
Character
Float List,File

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Type of list

Lists

Non-
Linear
Linear

Tree,
Stack Queue
Graph

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)
Array

List

Queue

Stack

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


1.Creation
2.Selection
3.Deletion
4.Updation
5.Searching
6.Sorting
7.Merging

Dr. Sanjeev Tayal (HOD) BCA &


B.Sc(C.S)
 The create operation result in reversing
memory for the programme elements. The
creation of data structure may take place
either during compile time or during
executing time.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 The selection operation deals with accessing
a particular data within a data structure.

Update operation
The update operation update on modifies the
data in the data structure.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 The delete operation destroyed the memory
space allocation for the specify
data structure . Free() function in C language
is used for performing this operation.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 Searching operation finds the presence of
the world of the desired data item to the
list of data items .It may also find the
location of all elements that satisfy
certain condition.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 Sorting is the process of the arranging all
data items in data structure in a particular
order either in ascending order or descending
order.
Merging Operation
Merging operation is a process of combing
the data item of two different sorted list into
a single sorted list.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


An array is a list of finite number of elements
of same data type such that integer , string
etc . The individual elements of an array are
access using an index to the array depending
on the number of index required to access an
individual elements of an array.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 One-Dimensional Array (Linear Array) that
required only 1-index to access an individual
element of the array.
 Two-Dimensional Array that requires two
index to access an individual element of an
array.
 The array for which we need two or more
index are generally known as Multi-
Dimensional Array.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 A Linear Array is a list of a finite number n of
homogenous data elements such that the
elements of the array are stored in
consecutive memory location.
Example:- int arr[5]={1,2,3,4,5};
char name[3]={‘B’,’C’,’A’};

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Two-Dimensional Array is a list of finite
number of m*n of homogenous data
elements such that :-
The element of the array are referenced by
Two index sets consisting of m and n
consecutive int number stored in consecutive
memory location.

Dr. Sanjeev Tayal (HOD) BCA &


B.Sc(C.S)
The size of two-dimensional array is denoted
by m*n Suppose arr is the name of the 2-D
array than its elements in the ith row and jth
column is denoted as a[i][j].
2-D array are called Matrixes in mathematics
and table in business applications.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 An m*n matrix is a 2-D array whose m,n
elements are arranged in m rows and n
columns. A matrix is denoted by capital
letters Such as:- A,B,C------ and its element
are denoted by corresponding lower letters
suffix with rows index and column index such
as :-aij,bij--------respectively.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Int Arr[3][3]={
1,2,3
4,5,6
7,8,9
};
a00 a01 a02
A=
a10 a11 a12
a21 a22 a23
3*3

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


There are various type of matrixes such as:-
1.Square Matrix
2.Diagonal Matrix
3.TriDiagonal Matrix
4. LowerTriangular Matrix
5. Upper Triangular Matrix
6.Sparse Matrix

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 The square matrix has the same number of
rows and column.
Example:-

A[3][3]= 1 2 3

4 5 6

5 7 7
3*3

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 A matrix Mat is diagonal Matrix
iff Mat(i,j)=0 for i !=j
Example:-

Mat[3][3] = 1 0 0
0 1 0
0 0 1 3*3

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 A matrix Mat is Tridiagonal Matrix
iff Mat(i,j)=0 for |i-j|>1
Example:-

Mat[3][3] = 1 1 0
5 2 4
0 6 3
3*3

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 A matrix Mat is lower triangular Matrix
iff Mat(i,j)=0 for (i <j)
Example:-

Mat[3][3] = 1 0 0
3 5 0
6 7 1 3*3

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 A matrix Mat is upper triangular Matrix
iff Mat(i,j)=0 for (i >j)
Example:-

Mat = 1 5 3
0 5 2
0 0 7 3*3

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 An m*n matrix with a relatively high proportion
of zero’0’ element are called sparse matrix An
matrix that is not sparse is called “Dense matrix”.
The diagonal , tridiagonal matrix have sufficient
fit into the category of sparse matrix.

 for example
Example: mat[4][4];
0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0 4*4

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Representing a sparse matrix by a 2D array leads to wastage of lots of
memory as zeroes in the matrix are of no use in most of the cases. So,
instead of storing zeroes with non-zero elements, we only store non-
zero elements. This means storing non-zero elements with triples-
(Row, Column, value).
Sparse Matrix Representations can be done in many ways following are
two common representations:

• Array representation
•Linked list representation
Method 1: Using Arrays

2D array is used to represent a sparse matrix in which there are


three rows named as
Row: Index of row, where non-zero element is located
Column: Index of column, where non-zero element is located
Value: Value of the non zero element located at index – (row,column)

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Method 2: Using Linked Lists

In linked list, each node has four fields. These four fields are
defined as:
Row: Index of row, where non-zero element is located

Column: Index of column, where non-zero element is located

Value: Value of the non zero element located at index –


(row,column)

Next node: Address of the next node

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)
Void main()
{
Int n,m,a[20][20],i,j,count=0;
clrscr();
Printf(“How many row and column you want:”);
Scanf(“%d%d”,&n,&m);
Printf(“enter the %d elements”,n*m);
For(i=0;i<n;i++){
For(j=0;j<m;j++){
Scanf(“%d”,&a[i][j]);
If(a[i][j]==0)
count++;
}
}
If(count>=(n*m)/2)
Printf(“Sparse Matrix”);
Else
Printf(“Dense Matrix”);;
getch();
}

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Multidimensional Arrays

A multidimensional array is an array with more than two


dimensions. Multidimensional arrays are an extension of 2-D
matrixes and use additional subscripts for indexing. A 3-D array,
uses three subscripts.The first index represent pages or sheets
of elements , but second the third dimension represents just like
a matrix.
we can define multidimensional arrays in simple words as
array of arrays. Data in multidimensional arrays are stored
in tabular form (in row major order).
General form of declaring N-dimensional arrays:
Data_type array_name[size1][size2]....[sizeN];
int a[3][3][3];

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Three-Dimensional Array

initializing Three-Dimensional Array: Initialization in Three-


Dimensional array is same as that of Two-dimensional arrays. The
difference is as the number of dimension increases so the number of
nested braces will also increase.
Method 1:
int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23};
Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)
/ C Program to store and print 12 values entered by the user
#include <stdio.h>
int main()
{
int test[2][3][2];
printf("Enter 12 values: \n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
scanf("%d", &test[i][j][k]);
} } }
// Printing values with proper index.
printf("\nDisplaying values:\n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
} } }
return 0;
}
Dr. Sanjeev Tayal (HOD) BCA &
B.Sc(C.S)
UNIT ll

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


A stack is a list of elements in which an element
may be inserted or deleted only at the one end
this end is called the top of the Stack . This
means ,that elements are removed from a stack
in the reverse order of that in which they were
inserted into the Stack. Their are two basic
operations associated with the stack these
operations are:- stack are also called Last In
First Out (LIFO).
1.Push Operation
2.Pop Operation

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


The Push Operation is the term use to
insert an element a stack.

TOP
D

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Algorithm for inserting an item into the stack(PUSH)
Step1. Lat stack[size] is an array form implementing
the stack ,Top=-1
Step2 . [Check the stack for overflow]
If Top =size-1
Then print “stack overflow” and exit
Else
Step3. Set Top =Top+1
Step4. Set stack[Top]=item [insert item in new Top
Position]
Step5. Exit

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


push D Push E PoP PoP

E
D D D
C C C C C
B B B B B
A A A A A

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 This term is used to delete an elements from
the stack.

D
TOP

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Algorithm for Deleting an item into the stack(POP)
Step1. [Check for the Underflow]
If Top < 0 or Top=-1 then
Print ” Stack Underflow” and exit
else
set item=stack[Top]
Step2. Top=Top-1 [Decrement the stack top]
Step3. return(item) [return the delete item from the stack]
Step4. Exit
Top Status of stack
position

-1 Empty
0 Only one element in the
stack
Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S) N-1 Stack is full
 There are various type of notation for writing
mathematical expressions . First of all we will
consider the following set of operations.
Symbol Operation Perform Precedence
^ Exponentation Highest
* Multiplication Highest
/ Divison Highest
% Modulus Highest
+ Addition Lowest
- Subtraction Lowest

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


1. Infix notation (A+B)
2. Prefix notation (+AB)
3. Postfix notation(AB+)

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 Infix operation the operator symbol is placed
between its two operands for ex:-
 To add A to B we can write as A+B or B+A
 To subtract D from C we can write as
C-D but we can not write D-C

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 Prefix operation the operator symbol is
placed before its two operands for ex:-
 To add A to B we can write as +AB or +BA
 To subtract D from C we can write as
-CD but we can not write -DC

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 Postfix operation the operator symbol is
placed after its two operands for ex:-
 To add A to B we can write as AB+ or BA+
 To subtract D from C we can write as
CD- but we can not write DC-

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


A+B-C (Infix Notation)
(A+B)-C
(+AB)-C
-+ABC (Prefix Notation)
Similarly
A+B-C (Infix Notation)
A+(B-C)
A+(BC-)
A(BC)-+ (Postfix Notation)

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Prefix Notation
Postfix Notation
(a-b/c)*(d*e-f)
(a-b/c)*(d*e-f)
(a-/bc)*(*de-f)
(a-bc/)*(de*-f)
(a-/bc)*(-*def)
(abc/-)*(de*f-)
(-a/bc)*(-*def)
abc/-*de*f-
-a/bc*(-*def)
Abc/-de*f-X
*-a/bc-*def
Abc/-de*f-*

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 A+[(B+C)+(D+E)*F]/G  (A+B)*C/D+E^F/G
 A+[(BC+)+(DE+)F*]/G  (AB+)*C/D+E^F/G
 A+[BC+DE+F*+]/G  (AB+)*C/D+EF^/G
 A+BC+DE+F*+G/  AB+C*/D+EF^/G
 ABC+DE+F*+G/+  AB+C*D/+EF^/G
 AB+C*D/+EF^G/
 AB+C*D/EF^G/+

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 A+(B*C-(D/E^F)*G)*H
 A+(B*C-(D/EF^)*G)*H
 A+(B*C-(DEF^/)*G)*H
 A+(BC*-(DEF^/)*G)*H
 A+(BC*-DEF^/G*)*H
 A+BC*DEF^/G*- *H
 A+BC*DEF^/G*-H*
 ABC*DEF^/G*-H*+

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 A/(B^C)+D  (A-B/C)*(D*E-F)
 A/(^BC)+D  (A-/BC)*(*DE-F)
 /A^BC+D  (-A/BC)*(-*DEF)
 +/A^BCD  *-A/BC-*DEF

 (A*B+(C/D))-F  A/(B^C)-D
 (*AB+(/CD))-F  A/(^BC)-D
 (+*AB/CD)-F  /A^BC-D
 -+*AB/CDF  -/A^BCD

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Suppose Q is an arithmetic expression written in infix notation . The
following algorithm finds the equivalent postfix expression P
1.Push “ ( “ onto the stack and Add “)” to the end of Q.
2.Scan Q from left to right and repeat step 3 and 6 for each element of
Q until the stack is empty.
3.If an operand(variable) is encountered .add it on stack p.
4.If a left parenthesis is encountered push it onto stack
5.If an operator @ is encountered then
a. add @ to stack [end of if]
b. Repeatedly pop from stack and add p each operator which has the
same precedence as or higher precedence than @
6. If a right parenthesis is encountered then
a. repeatedly pop from stack and add to P each operator until the
left parenthesis is encountered.
b. remove the left parenthesis.
[End of structure]
[End of step 2. loop]
7.Exit
Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)
Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)
Dr. Sanjeev Tayal (HOD) BCA &
B.Sc(C.S)
Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)
Dr. Sanjeev Tayal (HOD) BCA &
B.Sc(C.S)
Infix to Perfix

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)
Evaluate the postfix expression:
P: 5,6,2,+,*,12,4,/,-
Symbol Scanned Stack
5 5
6 5,6
2 5,6,2
+ 5,8
* 40
12 40,12
4 40,12,4
/ 40,3
- 37
)

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)
A queue is a liner list of elements in which
deletion can take place only at one end that is
called Front and Insertion can take place only
at the other end this end is called Rear end .
Queue are also called First In First Out (FIFO).

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 Algorithm to insert an element into the Queue
Step1. Initialize Front=-1 and Rear=-1
Step2.[check overflow condition]
If Rear>=size or Rear=size-1 then
Print “Queue Overflow” and exit
Else
Set Rear=Rear+1[increment rear pointer]
Step3. Queue[Rear]=item [insert an element ]
Step4. If Front=-1 then
Set Front= 0
Step5.Exit
Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)
 Algorithm to deleting an item into the Queue
Step1. If Front<0 then [check underflow condition]
Print “Queue Underflow “and exit
Else
Step2. Item=Queue[Front]
If(Front==Rear) [Check for empty queue]
Set Front=Rear=-1 [Re-initialize the pointer]
Else
Front=Front + 1
Step3. Return(item)
Step4.Exit
Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)
 Circular 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. It is also called ‘Ring Buffer’.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


There are three scenario of inserting an element in
a queue.
 If (rear + 1)%maxsize = front, the queue is full. In
that case, overflow occurs and therefore,
insertion can not be performed in the queue.
 If rear != max - 1, then rear will be incremented
to the mod(maxsize) and the new value will be
inserted at the rear end of the queue.
 If front != 0 and rear = max - 1, then it means
that queue is not full therefore, set the value of
rear to 0 and insert the new element there.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Algorithm to insert an element into the Circular
Queue
Step1. [check for overflow]
If Front=(Rear+1)%size then
Print “ Circular queue is overflow “ and exit
Step2. If (Front=-1) then
Front=Rear=0
Else
Rear=(Rear+1)%size
CQueue[Rear]=Item
Step3.Exit

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Delectation in Circular queue
To delete an element from the circular queue, we
must check for the three following conditions.
 If front = -1, then there are no elements in the
queue and therefore this will be the case of an
underflow condition.
 If there is only one element in the queue, in this
case, the condition rear = front holds and
therefore, both are set to -1 and the queue is
deleted completely.
 If front = max -1 then, the value is deleted from
the front end the value of front is set to 0.
 Otherwise, the value of front is incremented by 1
and then delete the element at the front end.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Step1. [check for Underflow]
If (front< 0) then
Print “Circular Queue Underflow” and Exit
Step2. Else
Item=CQueue [Front]
Step3. If(Front=Rear) then
Front=Rear=-1
Else
Front=(Front+1)%size
Step4. Return(item)
Step5.Exit

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


A deque is a kind of queue in which elements can
be added or removed from at either end but not
in the middle. This deque is a contraction of the
double ended queue.

There are two variation of a deque . These are:-


1.Input restricted Queue
2.Output Restricted Queue

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


 Input Restricted Queue which allow insertion
only at the one end but allow deletion at both
ends.

 Output Restricted Queue which allows


insertion at both ends, but allow deletion only
at one end.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Algorithm for output restricted
Dequeue(insertion)
Step1. [check for overflow condition]
If Rear>=size-1 and Front=0 then
Print ” overflow” and Exit
Step2.[check Front pointer value]
If Front >0 then
Front=Front-1

Step3. Dequeue[Front]=item [insert element at the front end]

Step4. If Rear< size-1


Rear=rear+1

Step5. Dequeue[Rear]=Item [insert element at the Rear end]

Step6.Exit

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Algorithm for input restricted Dequeue(deletion)
Step1.[check for Underflow]
If Front=Rear=-1 then
Print “Underflow” and Exit
Step2.[Delete element at front end]
If Front>=0 then
Item=Dequeue[front]
Return(item)
Step 3.[Check dequeue for empty]
If(Front==Rear) then
Front=Rear=-1
Else
Front=Front+1
Step4.[Deletion element at Rear end]
If rear>=0
Item=Dequeue[Rear]
Return(item)
Step5.[check dequeue for empty]
If Front=Rear then
Front=Rear=-1
Else
Rear=Rear-1
Step6. Exit
Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)
A Priority Queue is a kind of queue in which
each element is assign a priority and the
order in which elements are deleted or
processed come from the following rules:-
1.An element with highest priority is processed
first before any element of lower priority.
2.Two or more elements with the same priority
are processed according to the order in which
they are inserted to the queue.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


Priority Queue perform following operations:-
Inert (item,priority):-Inserts an item with given
priority.
getHighestPriority():-Returns the highest
priority item.
deleteHighestPriority():-Removes the highest
priority item.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)


1.CPU Scheduling.
2.Graph Algorithm like Shortest Path Algorithm
etc.
3. All queue applications where priority is
involved.

Dr. Sanjeev Tayal (HOD) BCA & B.Sc(C.S)

You might also like