0% found this document useful (0 votes)
8 views

Unit 1

Uploaded by

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

Unit 1

Uploaded by

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

DATA STRUCTURE

By:
Dr. Mamata Wagh
Deptt. of CSE
C. V. Raman Global University
Bhubaneswar
INTRODUCTION TO
DATA STRCTURE
Unit-1
COURSE OUTCOMES:
Upon successful completion of this course, students will be able to:
⚫ CO1: Analyze and design of linear data structure such as Queue and
Stack for solving problems.

⚫ CO2: Analyze and design of linear data structure such as Linked List
for solving problems.

⚫ CO3: Analyze and design of non-linear data structure such as Tree for
solving problems.

⚫ CO4: Analyze and design of non-linear data structure such as Graph


for solving problems.

⚫ CO5: Use different sorting and searching mechanisms by analyzing


suitability.
CONTENTS:
⚫ Introduction
Abstract data types with example
⚫ Types: Primitive
Non primitive
Linear
Nonlinear
Static
Dynamic Data structures
⚫ Stack: Fundamentals of stack
representation using array
Applications of stack: Recursion, Expression conversions and evaluations
etc.,
⚫ Queue: Fundamentals of queue
representation using array
Circular queues
Double ended queues concepts and operations
Applications of queue to solve problems.
Basic Concepts:
DATA:
Any raw fact or figure is called as data.
INFORMATION:
The processed data is called information.
DATA ITEM:
Refers to single unit of values.

Elementary
Data items
items Group items
Abstract data types
⮚ ADT, is a logical description of how we view the data and the
operations that are allowed without regard to how they will be
implemented. .
⮚ The ADT is made of with primitive data types , but operation logics are
hidden.
⮚ Example: stack
queue
list etc.
Data Structure
⮚ A data structure is a specialized format for organizing, processing,
retrieving and storing data.

⮚ The logical or mathematical model of a particular organization of data


is called data structure.

⮚ Data Structure can be defined as the group of data elements which


provides an efficient way of storing and organizing data in the
computer so that it can be used efficiently.
Adavantages of Data structure
⚫ Searching of Data becomes easy.

⚫ Accessing data becomes quick and efficient.

⚫ It allows proper use of the available space.


Example of Data Structure
R
o
o
t

T
1
T
2
T
3
T
4
… T
n

Sub
Stack of CDs
Trees

Stack of books
10 and plates
Types Of Data Structure
Types Of Data structure
The DS are divided into following types:

⚫ Primitive and Non primitive.


⚫ Linear and Non linear .
⚫ Static and Dynamic.
Primitive data structure

⚫ Primitive data structures can hold only a single value in one specific
location, unlike the non-primitive data structures which can be in a linear
and non-linear order.
⚫ Primitive data structures are the predefined types of data that are
supported in the programming language.
⚫ examples : float, character, integer and pointer.

Non-primitive data structure

⚫ The Non-Primitive Data Structures are created with the help of the
primitive data structures.
⚫ The non-primitive data structure is a kind of data structure that can hold
multiple values either in a contiguous or random location.
⚫ examples Arrays, Stacks, Queues
Linear and Non-linear Data structure
Linear Data structure
⮚ A data structure is said to be linear if its elements combine to form
any specific order.

Example : Arrays
Stacks
Ques
Linked list
Non linear Data structure
⮚ This structure is mostly used for representing data that contains a
hierarchical relationship among various elements.

⮚ Example :
Tree
Graph
Array:
An array is a linear data structure that collects elements of the same
data type and stores them in contiguous and adjacent memory
locations.
Stack
⚫ Stack is a data structure in which insertion and deletion operations are
performed at one end only.

⚫ The insertion operation is referred to as ‘PUSH’ and deletion is referred


as ‘POP’ operation.

⚫ Stack is also called as Last In First Out (LIFO) data structure.


queue
⚫ The data structure which permits the insertion at one and deletion at
another end, known as Queue.

⚫ End at which deletion is occurs is known as FRONT end and another


end at which insertion occurs is known as REAR end.

⚫ Queue is also called as First In First Out (FIFO)


Non Linear Data structure
Tree
⚫ A Tree can be define as finite data items (nodes) in which data items
are arranged in branches and sub branches.

⚫ Tree represent the hierarchical relationship between various elements.

⚫ Tree consist of nodes connected by edge, the represented by circle and


edge lives connecting to circle.
Graph

⚫ Graph is collection of nodes (information) and connecting edges


(Logical relation) between nodes.

⚫ A tree can be viewed as restricted graph


Static data structure
⮚ In static data structures, the size of the data structure is fixed.

⮚ The size of the structure has to be specified at the time of initialization,


and it cannot be changed later. However, we can modify the content of
the data structure without modifying the memory allocated to it.
⮚ Example: Arrays

Dynamic data structure


⮚ In dynamic data structures, the size of the data structure can be
modified after initialization.

⮚ It means we can add/delete elements from the data structure, thus


modifying its size at runtime. Dynamic data structures are memory
efficient and especially useful when we don't know the data size
beforehand.
⮚ Example: Linked list
Operations on Data structure
The possible operations on the linear data structure are:

⮚ Traversal
⮚ Insertion
⮚ Deletion
⮚ Searching
⮚ Sorting
⮚ Merging.
ARRAY
Why we need array ?

"We have a list of 1000 students' marks of an integer type. If using


the basic data type (int), we will declare something like the
following…"

int studMark0, studMark1,


studMark2, ..., studMark999;
Contd…
Can you imagine how long we have to write the
declaration part by using normal variable
declaration?
int main(void)
{
int studMark1, studMark2, studMark3,
studMark4, …, …, studMark998,
stuMark999, studMark1000;


return 0;
}
Why we need array ?

"We have a list of 1000 students'


marks of an integer type. If using
the basic data type (int), we will
declare something like the
following…"

int studMark0, studMark1,


studMark2, ..., studMark999;
Contd…
▪ By using an array, we just declare like this,
int studMark[1000];

It will reserve 1000 contiguous memory locations for storing the students’
marks
Types of Arrays
⚫ One - Dimensional Array

Two - Dimensional Array

Multi - Dimensional Array


Arrays

⮚ An array is a linear data structure that collects elements of the same


data type and stores them in contiguous and adjacent memory
locations.
⮚ Element − Each item stored in an array is called an
element.
Index − Each location of an element in an array has a
numerical index, which is used to identify the element.
Declaration of Arrays: int arr[10]

⚫ Where int specifies the data type or type of elements arrays stores.

⚫ “arr” is the name of array & the number specified inside the square
brackets is the number of elements an array can store, this is also called
sized or length of array.
Arrays
Arrays
⚫ Following are some of the concepts to be remembered about
arrays:

⚫ The individual element of an array can be accessed by


specifying name of the array, following by index or subscript
inside square brackets.

⚫ The first element of the array has index zero[0]. It means the
first element and last element will be specified as: arr[0] &
arr[9] respectively.
Arrays:
⚫ The elements of array will always be stored in the consecutive
(continues) memory location.

⚫ The number of elements that can be stored in an array, that is


the size of array or its length is given by the following
equation:
(Upper bound - lower bound)+1
Arrays
⚫ For the above array it would be
(9-0)+1=10,where 0 is the lower bound of array and 9 is the
upper bound of array.

⚫ Array can always be read or written through loop. If we read a


one-dimensional array it require one loop for reading and
other for writing the array.
Arrays
⚫ If we are reading or writing two-dimensional array it would require two
loops. And similarly the array of a N dimension would required N
loops.

⚫ Some common operation performed on array are:


⚫ Creation of an array
⚫ Traversing an array
Memory Representation of an Array
⚫ The array elements are stored in contigeous memory location

1000 1002 1004 1006 1008

⚫ Address of the ith element


(Address(A[i]))=B+w*(i-LB)
where B=Base address of the array A
w=Size of the each element

LB = Lower index of the array


Two-Dimensional Array

⚫ There could be situations where a table of values will have to be


stored.
C allows us to define such tables of items by using two-
dimensional arrays.
The two dimensional array are declared as follows
type array_name[row_size][column_size];
int MyArray[3][3];
Memory Representation of Matrix

Matrices are stored in contigous memory locations

The matrices are stored in the computer memory in two different


representations:

⚫ Row-major Order Representation


⚫ Column-major Order Representation
Address calculation of Matrix Elements in
Memory

Row-major Order Representation:

A[r][c] is a matrix
where r=Row size(ranges from 0 to r-1)
c=Column size(ranges from 0 to c-1)
B=base address of the array A
Address(A[i][j])=B+(i*c + j) *w
where w=size of each element
Address calculation of Matrix Elements in
Memory
Column-major Order Representation:
A[r][c] is a matrix
where r=Row size(ranges from 0 to R-1)
c=Column size(ranges from 0 to
C-1)
B=base address of the array A
Address(A[i][j])=B+(j*r + i) *w
where w=size of each element
Arrays

⚫ Insertion of new element


⚫ Deletion of required element
⚫ Modification of an element
⚫ Merging of arrays
TWO DIMENTIONAL ARRAYS
int Array [row][col]

for(row=0; row < maxrow; row++)


{
for(col=0; col < maxcol; col++)
{
cout<<"array[row][col]";
}
}
THREE DIMENTIONAL ARRAYS
int Array [row][col][depth]

for(row=0; row < maxrow; row++)


{
for(col=0; col < maxcol; col++)
{
for(depth=0 ; depth < maxdepth ; depth++)
{
cout<<"array[row][col][depth]";
}
}
}
STACKS
Contents:
⮚ Fundamentals of stack.
⮚ Representation using array.
⮚ Operations on Stack
PUSH Operation
POP Operation
PEEP Operation
⮚ Applications of stack:
Recursion
Expression conversions
and evaluations etc.
Stack: A Last-in First-out (LIFO) List
Stack
⮚ Stack is a linear type of data structure that follows the
LIFO (Last-In-First-Out) principle.

⮚ It allows insertion and deletion operations from one end of


the stack that is top.

⮚ Implementation of the stack


⮚ Using array

⮚ Using linked list


Real life examples of stack

C. V. Raman Global University DATA


STRCTURE
Operations on Stack
push() − inserting an element into the stack.

pop() − Removing an element from the stack.

peek() − get the top data element of the stack, without


removing it.

isFull() − check if stack is full.

isEmpty() − check if stack is empty.


Push operation in stack

PUSH

top
top
Push operation in stack
Pop Operation

POP

top
top
PEEP Operation Algorithm
ALGORITHM:PEEP (STACK, TOP, MAX_SIZE, ITEM)
Input: STACK is the linear array with maximum size
MAX_SIZE.
Output: This algorithm returns the top most
element of the STACK.
Step 1: If TOP=-1, then
Print “Stack is empty” and exit.
Step 2: Return STACK [TOP]
Step 3: Exit
Operations on Stack

Creation
Insertion
Deletion
Displaying
0 Creation #define SIZE 5 0

int stack[SIZE];
Insertion
void push(element){ 4
if(Stack is full)
{ 3
printf(“FULL!!!”);
} 2
else
{ 1
Top++;
stack[Top] = element; 0
}
}
Deletion int pop( ){
if(Stack is Empty)
{
printf(“EMPTY!!!”);
return Top;
}
else
{
deleted = stack[Top];
Top--;
return deleted;
}
}
Displaying void display( ){
if(Stack is Empty)
{
printf(“EMPTY!!!”);
}
else
{
for(i=Top; i>-1; i--)
printf(“%d\
n”,stack[i]);
}
}
Application Of Stack

1. Conversion of Infix expression to equivalent Postfix


Expression
2. Evaluation of Postfix Expression
3. Conversion of Infix expression to equivalent Prefix
Expression
4. Evaluation of Prefix Expression
5. Recursion: The recursion means that the function is
calling itself again. To maintain the previous states,
the compiler creates a system stack in which all the
previous records of the function are maintained.
Arithmetic Expressions
Infix notation in which operators are written in
between operands
Ex: A + B
Prefix notation in which operators are written before
the operands. It is also known as polish notation.
Ex: +AB
Postfix notation in which operators are written after
the operands.
Ex: AB+
Infix Notation
 Infix notation is the common arithmetic and

logical formula notation, in which operators are

written infix-style between the operands they

act on

Ex: A + B
Prefix Notation

• In Prefix notation, the operator comes before the

operand.

• The Infix expression A+B will be written as +AB in

its Prefix Notation.

• Prefix is also called ‘Polish Notation’


Postfix Notation

• In Postfix notation, the operator comes after the

Operand.

• The Infix expression A+B will be written as AB+ in its

Postfix Notation.

• Postfix is also called ‘Reverse Polish Notation’


Examples of infix to prefix and postfix

Infix PostFix Prefix

A+B AB+ +AB

(A+B) * (C + D) AB+CD+* *+AB+CD

A-B/(C*D^E) ABCDE^*/- -A/B*C^DE


Evaluation of postfix expression
Step 1 − scan the expression from left to right
Step 2 − if it is an operand push it to stack
Step 3 − if it is an operator pull operand from stack and
perform operation
Step 4 − store the output of step 3, back to stack
Step 5 − scan the expression until all operands are
consumed
Step 6 − pop the stack and perform operation
Conversion of Infix to postfix
•Use a stack for processing operators (push and
pop operations).
•Scan the sequence of operators and operands
from left to right and perform one of the
following:
•output the operand,
•push an operator of higher precedence,
•pop an operator and output, till the stack top
contains operator of a lower precedence and
push the present operator
Algorithm
1. Print operands as they arrive.
2. If the stack is empty or contains a left parenthesis on top, push the
incoming operator onto the stack.
3. If the incoming symbol is a left parenthesis, push it on the stack.
4. If the incoming symbol is a right parenthesis, pop the stack and print
the operators until you see a left parenthesis. Discard the pair of
parentheses.
5. If the incoming symbol has higher precedence than the top of the
stack, push it on the stack.
6. If the incoming symbol has equal precedence with the top of the stack,
use association. If the association is left to right, pop and print the top
of the stack and then push the incoming operator. If the association is
right to left, push the incoming operator.
7. If the incoming symbol has lower precedence than the symbol on the
top of the stack, pop the stack and print the top operator. Then test the
incoming operator against the new top of stack.
8. At the end of the expression, pop and print all operators on the stack.
(No parentheses should remain.)
Recurssion
When a function or module call itself. This technique is
known as recursion. In recursion, a function α either calls
itself directly or
calls a function β that in turn calls the original function α.
The function α is called
recursive function.
Example − a function calling itself.

int function(int value) {


if(value < 1)
return;
function(value - 1);
printf("%d ",value);
}
QUEUE
Contents:
⮚ Fundamentals of queue
⮚ representation using array
⮚ Circular queues
⮚ Double ended queues concepts and
operations
⮚ Applications of queue to solve problems.
⮚ questions.
What’s this ?
Queue
Queue
⮚A queue is a linear list of elements in which deletions can take place
only at one end, called the front and insertions can take place only at the
other end, called the rear.

⮚The terms “front” and “rear” are used in describing a linear list only
when it is implemented as a queue.

⮚ Queues are also called first-in-first-out (FIFO) lists. In other words, the
order in which elements enter a queue is the order in which they leave.
Types of Queue
Linear Queue

Circular Queue

Double-Ended Queue(Deque)

Priority Queue
Representation of Queue

•As in stacks, a queue can also be implemented using Arrays,


Linked-lists, Pointers and Structures.
Representation of Queue
Operations on Queue
Insertion Operation
ALGORITHM: Q_INSERT(QUEUE,MAX,FRONT,REAR, ITEM)

Input: QUEUE is the linear array with maximum size MAX.FRONT and
REAR are the pointer variables pointing to Front end and Rear
end.
Output: This algorithm inserts an element ITEM on to end of the queue QUEUE.

Step 1: If REAR=MAX-1, then


Print “Queue Overflow” and Exit.
Step 2: If FRONT=-1 and REAR=-1,then
Set FRONT=0 and REAR=0
Else
Set REAR=REAR+1
Step 3: [Insert an element in to the Queue]
Set QUEUE[REAR]=ITEM
Step 4: Exit
Deletion operation
ALGORITHM: Q_DELETE(QUEUE,MAX,FRONT,REAR, ITEM)
Input: QUEUE is the linear array with maximum size MAX.FRONT and REAR are the
pointer variables pointing to Front end and Rear end respectively.

Output: This algorithm deletes an element from the queue


QUEUE and stores it in the variable ITEM.

Step 1: If FRONT=-1 and REAR=-1, then


Print “Queue Underflow” and Exit.

Step 2: [Delete an element from the Queue]


Set ITEM=QUEUE[FRONT]

Step 3: If FRONT= REAR , then


Set FRONT=-1 and REAR=-1
Else
Set FRONT=FRONT+1
Step 4: Exit
Double Ended Queue(DEQUE)
⚫Linear list of elements in which insertion and deletion operations are
performed on both ends.

⚫Inserting an element from both front end rear end.

⚫Deleting an element from both the front and rear end


Representation of Dequeue
Double Ended Queue
⚫ Input Restricted Deque
Allows insertion at only one end(rear) of the list
Allows deletion at the both front and rear ends

⚫ Output Restricted Deque


Allows deletion at only one end(front) of the list
Allows insertion at the both front and rear ends
Circular queue
⚫A queue, in which the last node is connected back to the first node to
form a cycle, is called as circular queue.

⚫Circular queue are the queues implemented in circular form rather than
in a straight line.

⚫ Circular queues overcome the problem of unutilized space in linear


queue implemented as an array.

⚫ The main disadvantage of linear queue using array is that when


elements are deleted from the queue, new elements cannot be added in
their place in the queue, i.e. the position cannot be reused.
Circular queue
Circular queue implementation
⚫After rear reaches the last position, i.e. MAX-1 in order to reuse the
vacant positions, we can bring rear back to the 0th position, if it is empty,
and continue incrementing rear in same manner as earlier.

⚫Thus rear will have to be incremented circularly.

⚫ For deletion, front will also have to be incremented circularly..


Insert operation in circular queue
⚫ Step 1: Check for queue full
If rear=max–1 and front=0 or if front=rear+1 then
circular queue is full and insertion operation is not possible.
otherwise go to step 2
⚫ Step 2: Check position of rear pointer
If rear=max–1
then set rear=0 otherwise increment rear by 1.
rear=(rear+1)%MAX
⚫ Step 3: Insert element at the position pointer by rear pointer.
q[rear]=Data
⚫ Step 4: Check the position of front pointer
If front=–1 then set front as 0.
Delete operation in circular queue
⚫ Step 1: Check for queue empty if (front = -1)
then circular queue is empty and deletion operation
is not possible. otherwise go to step 2
⚫ Step 2: Check for position of front and rear pointers.
if front = rear then
Data = q[front];
set front=rear=-1
⚫ Step 3: Check position of front
if front = Max-1
Data = q[front];
then set front=0;
otherwise
Data = q[front]; front = (front+1)%MAX
Application Of Queues
⮚ Job scheduling
⮚ Asynchronous transfer of data
⮚ Traffic System

You might also like