Chap1 Data Structures and Their Operations
Chap1 Data Structures and Their Operations
Presented By
Ms. Sujata S. Kullur
Assist. Prof. IT Department
Definition Of Data Structures
A Data structure is a particular way of storing and organising
data in a computer so that it can be used efficiently.
Top=0
Stack empty
Stack: Insertion(PUSH)
Top=Top+1
10
20 Top=Top+1
10
30 Top=Top+1
20
10
30
20
10
Top=Top-1
30
20
10
Element Deleted
Stack: Deletion(POP)
Top=Top-1
20
10
Element Deleted
Stack: Deletion(POP)
10 Top=Top-1
Element Deleted
Stack: Deletion(POP)
Top=Top-1
Stack Under Flow
Stack Empty
Queue
A Queue is an Ordered List in which all
insertions can take place at one end called
REAR and all deletions take place at other
end called FRONT.
It is called as First In First Out List.
Procedure QINSERT(Q,F,R,N,Y)
1 Overflow?
If R>= N
Then Write “OVERFLOW”
return
2 Increment the rear pointer
R R+1
3 Insert element
Q[R] Y
4 Is front pointer properly set?
If F=0
F
Return
Queue Insertion
F R
Queue Insertion
10
F R
Queue Insertion
10 20
F R
Queue Insertion
10 20 30
F R
Queue Insertion
10 20 30 40
F R
20 30 40
F R
30 40
F R
40
F R
F R
FIRST
A B C NULL
Linked List
• There is a pointer (called FIRST) points the first
element (also called node)
• Successive nodes are connected by pointers.
• Last element points to NULL.
• It can grow or shrink in size during execution of a
program.
• It can be made just as long as required.
• It does not waste memory space, consume exactly
what it needs.
Arrays versus Linked Lists
Array: Contagious Storage
Array versus Linked Lists
• In arrays
• elements are stored in a contagious memory locations
a
15
b
45
c
25
d
50
e
22
f
35
g
30
h
10
i
20
j
k
40
Array versus Linked Lists
• In Linked lists
• adjacency between any two elements are maintained by means
of links or pointers
Insertion steps:
• Create a new node
• Start from the header node
• Manage links to
• Insert at front
• Insert at end
• Insert at any position
FIRST Singly Linked Lists
LINK LINK LINK LINK
a b c d
a
Availability Stack
NEW
AVAIL d
a
Insertion of node First
INSERT(X, FIRST)
1.Underflow?
if AVAIL=NULL
then Write (‘Availability stack is empty’)
Return (FIRST)
2. Obtain address of next free node
NEW AVAIL
3. Remove node from availability stack
AVAIL LINK( AVAIL)
4. Initialize fields of new node
INFO(NEW) X
LINK(NEW)FIRST
5. Return address of new node
Return(NEW)
Insertion of node at end
INSEND(X, FIRST)
1.Underflow?
if AVAIL=NULL
then Write (‘Availability stack is empty’)
Return (FIRST)
2. Obtain address of next free node
NEW AVAIL
3. Remove node from availability stack
AVAIL LINK( AVAIL)
4. Initialize fields of new node
INFO(NEW) X
LINK(NEW)NULL
5. Is the list empty
if FIRST=NULL
then Return(NEW)
6. Initiate search for end of list
SAVE FIRST
7. Search for end of list
Repeat while LINK(SAVE) ≠ NULL
SAVE LINK(SAVE)
8. Set LINK field of last node to new
LINK(SAVE) NEW
9. Return first node pointer
Return(FIRST)
Insertion of node in order
INSORD(X, FIRST)
1.Underflow?
if AVAIL=NULL
then Write (‘Availability stack is empty’)
Return (FIRST)
2. Obtain address of next free node
NEW AVAIL
3. Remove node from availability stack
AVAIL LINK( AVAIL)
4. Initialize fields of new node
INFO(NEW) X
5. Is the list empty
if FIRST=NULL
LINK(NEW) NULL
then Return(NEW)
6. Does the new node precede all others in the List
If INFO(NEW) ≤ INFO(FIRST)
Then LINK(NEW) FIRST
Return(NEW)
7. Initiate search predecessor of new node
SAVE FIRST
8. Search for end of list
Repeat while LINK(SAVE)≠ NULL and INFO(LINK(SAVE)) ≤ INFO(NEW)
SAVE LINK(SAVE)
9. Set LINK field of last node to new
LINK(NEW) LINK(SAVE)
LINK(SAVE) NEW
10. Return first node pointer
Return(FIRST)
Deletion from a Linked List
Single Linked List: Deletion
Deletion steps
• Start from the header node
• Manage links to
• Delete at front
• Delete at end
• Delete at any position
• freeingup the node as free space.
Free Memory after Deletion
Hash Function
• A Hashing Function is a key to address transformation
which acts upon a given Key to complete the relative position
of the Key in a array
Hashing
fv(X) = X modM
• A Key can be a member of a String etc..
• A Hash Function Formula is
• Hash (Key Value ) =(Key Values % Table Size)
• Hash(10) =10 % 5=0
• Hash(33) =33 % 5 =3
• Hash(21)= 21 % 5=1
• Hash(11)=11 % 5=1
Hashing
2. Closed Hashing
eg : Open Addressing ,Rehashing and Extendable hashing.
Hashing
Open Hashing :
• Each Bucket in the Hash table is the head of a Linked List.
• All Elements that hash to a Particular Bucket are Placed on
the Buckets Linked List .
Closed Hashing:
• Ensures that all elements are stored directly in to the Hash
Table.
Linear Hashing
Linear hashing:
• Collide elements are stored at another slot in the table.
Ensures that all elements stored directly in the hash table.
Linear hashing
Procedure LINSEARCH(X,HT,b,j)
//search the hash table HT(0:b-1)(each bucket has //exactly 1 slot)
using linear probing .If HT(j)=0 // //then the j-th bucket is empty
and X can be entered // //into the table .Otherwise HT(j)=X and X is
//already in the table .F is the hash function//
i f(X);ji
while HT(j) X and HT(j) 0 do
j (j+1) mod b //treat the table as circular//
if j=I then call TABLE-FULL endif //no empty slot//
repeat
end LINSEARCH
Chaining
• Chaining is an open hashing Technique
• A Pointer fields is added to each record Location.
• In this method the table can never overflow since the
linked are only extended upon or New Keys.
• Traverse the list to check whether it is already present.
• If is not already present, insert at end of the list similarly,
the rest of the elements are inserted.
Example: 10 , 11 , 81 , 10 , 7 , 34 , 94 , 17 , 29 , 89 , 99
CHAPTER 1 69
Chaining
Advantages
• More number of elements can be inserted as it uses of
linked list.
• Collision resolution is simple and efficient.
Disadvantages
It requires pointers, which occupies more memory space.
Disadvantages:
It requires pointers, which occupies more memory space.
Hashing With chaining
Procedure chnsrch(X,HT,b,j)
//search the hash table HT(0:b-1) for X.//
//Either HT(i) =0 or it is a pointer to the list of // //identifiers X such
that f(X) = i. List nodes have fields// //IDENT and LINK.Either j
points to the node // //already containing X or j=0//
j HT(f(x)) //compute head node address//
//search the chain starting at j//
while j 0 and IDENT(j) X do
j LINK(j)
repeat
end CHNSRCH