Unit1 DS
Unit1 DS
Unit1 DS
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.
Data:
Any raw fact or figure is called as data.
Information:
The processed data is called as information.
Structure:
Representation of data is called as structure.
Abstract data types(ADT)
Ø 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. .
Ø An ADT specifies what each operation does, but not
how it does it.
Ø The ADT can be implemented using one of many
different data structure(primitive) like;
stack
queue
list etc.
Data Structure
• The data type of the element may be any valid data type
like char, int, float or double.
•
Linked List
A node contains two fields i.e., data stored at that particular address
and the pointer which contains the address of the next node in the
memory.
Ø Example :
q Tree
qGraph
Tree
1.Traversal 4.Searching
2.Insertion 5.Sorting
3.Deletion 6.Merging.
Operations On Data structure
1) Traversing: Every data structure contains the set of data
elements. Traversing the data structure means visiting each
element of the data structure in order to perform some
specific operation like searching or sorting.
int main()
{
Return 0;
}
By using an array, we just declare like this,
int studMark[1000];
Ramesh 45 48 42
Gopal 48 44 44
Sita 20 14 2
type array_name[row_size][column_size];
TWO DIMENSIONAL ARRAY
int MyArray[3][3];
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
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
Algorithm:
Consider LA is a linear array with N elements and K is a positive integer
such that K<=N. Following is the algorithm to delete an element
available at the Kth position of LA.
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
Search Operations
We can perform a search for an array element based on its
value or its index.
Algorithm :
Consider LA is a linear array with N elements and K is a
positive integer such that K<=N. Following is the algorithm
to find an element with a value of ITEM using sequential
search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
Update Operations
Update operation refers to updating an existing element
from the array at a given index.
Algorithm
Consider LA is a linear array with N elements and K is a
positive integer such that K<=N. Following is the algorithm
to update an element available at the kth position of LA.
1. Start
2. Set LA[K-1] = ITEM
3. Stop
v Fundamentals of Stack.
v Representation using Array.
v Operations on Stack
PUSH Operation
POP Operation
stack PEEP Operation
v Applications of stack:
Recursion
Expression Conversions
and Evaluations etc.
Stack: A Last-in First-out (LIFO) List
A stack is a data structure in which data is
added and removed at only one end called the
top.
Is
top=MAX-
1
?
PUSH
Enter an element to be
inserted
top
Set top=top+1
Print “Stack
Overflow” top
Set stack [Top]
=Item
STOP
PUSH Operation Algorithm
ALGORITHM:
PUSH (STACK, TOP, MAX_SIZE, ITEM)
Input: STACK is the linear array with maximum size
MAX_SIZE.
Output: This algorithm pushes (or inserts) an
element ITEM on to the STACK.
Step 1: If TOP=MAX_SIZE-1, then
Print “Stack Overflow” and exit.
Step 2: Set TOP=TOP+1
Step 3: Set STACK [TOP] =ITEM
Step 4: Exit
Pop Operation
POP
top
top
POP Operation Algorithm
ALGORITHM: POP (STACK, TOP, MAX_SIZE, ITEM)
Input: STACK is the linear array with maximum size
MAX_SIZE.
Output: This algorithm deletes the top element of the STACK
and assigns it to the variable ITEM.
Step 1: If TOP=-1, then
Print “Stack Underflow” and exit.
Step 2: Set ITEM=STACK [TOP]
Step 3: Set TOP=TOP-1
Step 4: Exit
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
Application Of Stack
on
Ex: A + B
Prefix Notation
• In Prefix notation, the operator comes before the
operand.
Operand.
Postfix Notation.
Step 1: Add ‘)’ to the end of the infix expression I and push
‘(‘on to the STACK.
Step 2: Scan I from left to right and repeat Step 3 to Step 6 for
each element of I until the STACK is empty.
Step 3: If an operand is encountered, add it to P.
Step 4: If a left parentheses ‘(‘is encountered, push it on to the
STACK.
Contd…
Step 5: If an operator OP is encountered, then
(a)Repeatedly pop from the STACK and add each operator
(on the top of the STACK) to the Postfix expression ‘P’ which has
same precedence as or higher precedence than OP.
(b)Push the operator OP on to the STACK.
[End of if]
Step 6: If a right parentheses ‘)’ is encountered, then
(a) Repeatedly pop from the STACK and add each operator
on the top of the STACK) to the Postfix expression ‘P’ until a left
parentheses ‘(‘ is encountered.
(b) Remove the left parentheses ‘(‘. [Don’t add it to the
postfix expression P.]
[End of if]
[End of Step 2 loop]
Step 7: Exit
Evaluation of Postfix Expression
ALGORITHM: EVALUATE_POSTFIX (P)
Input: P is an arithmetic expression in postfix notation.
Output: This algorithm evaluates the postfix expression P and assigns the result to the variable
VALUE.
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.
It is similar to the linear Queue except that the last element of the
queue is connected to the first element. It is also known as Ring Buffer,
as all the ends are connected to another end.
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.
Insertion (ENQUEUE) Operation
ALGORITHM: C_Q_INSERT(C_QUEUE,MAX,FRONT,REAR, ITEM)
Input: C_QUEUE is the Circular Queue with maximum size MAX.FRONT and REAR
are the pointer variables pointing to Front end and Rear end respectively.
Output: This algorithm inserts an element ITEM on to the circular queue
C_QUEUE.
Input: C_QUEUE is the Circular Queue 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 Circular Queue C_QUEUE
and stores it in the variable ITEM.
Step 1: If FRONT=-1
Print “Circular Queue Underflow” and Exit.
Step 2: [Delete an element from the Queue]
Set ITEM=C_QUEUE[FRONT]
Step 3: If FRONT= REAR , then
Set FRONT=-1 and REAR=-1
Else
Set FRONT=MAX-1
set FRONT=0
Else
set FRONT=FRONT+1
Step 4: Exit
DOUBLE ENDED QUEUE(DEQUE)
• If the queue is empty, both rear and front are initialized with 0.
Now, both will point to the first element.
If the deque has only one element, set rear = -1 and front =
-1.
Else if front is at end (that means front = size - 1), set front
= 0.
Else increment the front by 1, (i.e., front = front + 1).
Deletion at the front end
Deletion at the rear end
In this operation, the element is deleted from the rear
end of the queue. Before implementing the operation,
we first have to check whether the queue is empty or
not.
If the queue is empty, i.e., front = -1, it is the
underflow condition, and we cannot perform the
deletion.