Professional Documents
Culture Documents
Algorithms (DSA)
Dr Suryaprakash Kompalli
Agenda
• Abstract Data Types
– Data Structures and What you can do with them?
• Lists
• Stack (LIFO: Last in first out)
• Queue (FIFO: First in first out)
Abstract Data Types
• All algorithms need a way to store data using the most optimal memory
possible
• All algorithms need an implementation method for some of the processing
steps; example:
– Add an element; the addition may be based on:
●
Value of the element (Generally used in sorted data structures)
●
Location of the element in the data structure. ie. Add element to start of the data structure, add element to end of data
structure, or add element at a random location
– Remove an element; the removal may be based on:
●
Value of the element
●
Location of the element in the data structure. ie. Remove first element, remove last element, or remove element at a
random location
– Find out if an element is present
– Move an element to a different position
– Throw errors for certain illegal conditions
– Check status of data structure: Number of elements, resize data structure
Abstract Data Types
• In order to maintain a data structure, the implementation needs to have
several building blocks
User-facing functions
Data
Representation
(e.x: Linked list, array)
Internal functions
Implementation of an ADT
• User-facing functions are the ones which an user would use to access the
data structure
• The data representation and internal functions cannot directly be accessed
by a user
Agenda
• Abstract Data Types
– Data Structures and What you can do with them?
• Lists
• Stack (LIFO: Last in first out)
• Queue (FIFO: First in first out)
Abstract Data Types: Lists
• A list is a sequence of elements of a given type
– Example: integer, floating point, char, string etc
• Lists
• Stack (LIFO: Last in first out)
• Queue (FIFO: First in first out)
Abstract Data Types: Stacks
• Stack can be considered to be a list where elements are added or removed
only from top of the list
• It may be noticed that the most recently added element is returned from the
stack. There fore the stack is said to implement “Last In First Out”
Top of stack
18
6 6 6
9 9 9
1 1 1 1
15 15 15 15
From left to right: (1) A stack with 4 elements; top of the stack is shown in green color, (2) A new
element containing the value 18 is added to the stack, (3) One element is popped from the stack
(4) Two elements are popped from the stack
Abstract Data Types: Stacks
• User-facing functions implemented in a stack:
Allot(S, Size) Create / allot a stack “S” of a specific size
Empty(S) Remove all elements from stack “S” In these cases, error may
Pop(S) Return the top most element from stack “S” and be thrown if the stack is
remove it from the stack. empty
Peek(S) Return the top most element from stack “S”
Push(e, S) Push element “e” into stack “S”
GetEmptyCount(S) Return amount of empty space present in stack “S”
GetSize(S) Get the size of stack “S”
• It is trivial to see that just like lists, these functions can also be implemented
using Arrays, linked lists or any other underlying data structures.
• The run time of each user-facing function may change based on the data
structure used
Abstract Data Types: Use of Stacks
• Lists
• Stack (LIFO: Last in first out)
• Queue (FIFO: First in first out)
Abstract Data Types: Queue
• Queue can be considered to be a list where elements are added to the
bottom and removed from bottom of the list
• It may be noticed that the oldest added element is returned from the queue.
There fore the queue is said to implement “First In First Out”
18
6 6 18
9 9 6
Bottom of 1 1 9 18
queue 15 15 1 6
From left to right: (1) A queue with 4 elements; bottom of the queue is shown in green color, (2) A
new element containing the value 18 is added to the queue, (3) One element is popped from the
queue (4) Two elements are popped from the queue
Abstract Data Types: Queue
• User-facing functions implemented in a queue:
Allot(S, Size) Create / allot a queue “S” of a specific size
Empty(S) Remove all elements from queue “S” In these cases, error may
Pop(S) Return the bottom most element from queue “S” be thrown if the queue is
and remove it from the queue. empty
Peek(S) Return the bottom most element from queue “S”
Push(e, S) Add element “e” into queue “S”
GetEmptyCount(S) Return amount of empty space present in queue
“S”
GetSize(S) Get the size of queue “S”
• It is trivial to see that just like lists, these functions can also be implemented
using Arrays, linked lists or any other underlying data structures.
• The run time of each user-facing function may change based on the data
structure used
Abstract Data Types: Use of Queues