BITS F232
aft
Foundations of
Data Structures and Algorithms
Lect 11
Sameera Muhamed Salam
Dept of CSIS, BITS Pilani, Hyderabad Campus
Dr
Basic Data Structures
BITS F232 Sameera M S 2
Abstract Data Type
▶ Abstract Data type (ADT) is a type (or class) for objects whose behavior is
defined by a set of values and a set of operations.
▶ The definition of ADT only mentions what operations are to be performed but not
how these operations will be implemented.
▶ It does not specify how data will be organized in memory and what algorithms will
be used for implementing the operations.
▶ It is called “abstract” because it gives an implementation-independent view.
[ Reference: Geeksforgeeks]
BITS F232 Sameera M S 3
Stack ADT
▶ A stack is a container of bjects that are inserted and removed according to the
last-in first-out (LIFO) principle.
▶ Top to point last element.
▶ Operations
▶ Isempty()
▶ Push: Inserting into Stack
▶ Pop: Deleting from Stack
▶ Size():
▶ Top():
BITS F232 Sameera M S 4
Stack: Array Implementation
Time required?
BITS F232 Sameera M S 5
Stack: Linked list Implementation
▶ Instead of head, we have Top
▶ Isempty():
BITS F232 Sameera M S 6
Stack: Linked list Implementation
▶ Instead of head, we have Top
▶ Isempty(): Top=NULL
▶ Push:
BITS F232 Sameera M S 7
Stack: Linked list Implementation
▶ Instead of head, we have Top
▶ Isempty(): Top=NULL
▶ Push:Insertion at the beginning
▶ Pop:
BITS F232 Sameera M S 8
Stack: Linked list Implementation
▶ Instead of head, we have Top
▶ Isempty(): Top=NULL
▶ Push:Insertion at the beginning
▶ Pop: Delete from beginning
BITS F232 Sameera M S 9
Stack Applications
BITS F232 Sameera M S 10
Stack Applications
BITS F232 Sameera M S 11
Stack Applications
▶ Function calls
▶ Paranthesis Matching
▶ Recently visited sites
BITS F232 Sameera M S 12
Queue
▶ A containèr of objects that are inserted and removed according to the first-in
first-out (FIFO) prı̀nciple.
▶ Rear and Front pointer
▶ Operations
▶ enqueue
▶ dequeue
▶ size()
▶ isempty()
▶ front()
Figure: [Ref:programiz]
BITS F232 Sameera M S 13
Queue:Array Implementation
▶ Initially, f=r=0
BITS F232 Sameera M S 14
Queue:Array Implementation
▶ Initially, f=r=0
▶ Condition for empty queue?
BITS F232 Sameera M S 15
Queue:Array Implementation
▶ Initially, f=r=0
▶ Condition for empty queue?
▶ Condition for full queue?
BITS F232 Sameera M S 16
Queue:Array Implementation
▶ Initially, f=r=0
▶ Condition for empty queue?
▶ Condition for full queue?
▶ Disadvantage?
BITS F232 Sameera M S 17
Queue:Array Implementation
▶ Initially, f=r=0
▶ Condition for empty queue?
▶ Condition for full queue?
▶ Disadvantage?
BITS F232 Sameera M S 18
References:
▶ Michael T. Goodrich and Roberto Tamassia, “ Algorithm Design Foundations,
Analysis, and Internet Examples”, Wiley Student Edition.
▶ Jon Kleinberg and Eva Tardos, “ Algorithm Design”, Pearson Publishers.
▶ Thomas H. Chorman, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein, “
Introduction to algorithms”, MIT Press Publishers.
▶ Sanjoy Dasgupta, Umesh Vazirani, Christos Papadimitriou, “ Algorithms”,
McGraw-Hill Education Publishers.
BITS F232 Sameera M S 19
Discussion Time!
BITS F232 Sameera M S 20
BITS F232 Sameera M S 21