You are on page 1of 18

Data Structures and

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

• A list may be ordered or unordered


• User-facing functions implemented in a list:
Declare(L) Create an empty list
End(L) Return the index or pointer to end of the list
Empty(L) Remove all elements from the list
IsEmpty(L) Return true if list is empty, else return false
First(L) Return the index or pointer to first element of the list
Next(i,L) Return the element after position “i” in list “L”. If end of
the list is crossed, then throw an error
Previous(i,L) Return the element before position “i” in list “L”. If start
of the list is crossed, then throw an error
Insert(e,i,L) Insert element “e” at position “i” in list “L” In these cases, the
Delete(i,L) Delete element at position “i” from list L following error applies: if “i”
GetElement(i,L) Return element at position “i” from list “L” is not between start of list
of end of list, then throw an
error
Abstract Data Types: Lists
• Additional user functions may also be added, based on what “features” need
to be supported
Overwrite(e, i, L) Overwrite the element at index “i” in linke list “L”
with element “e”. Throw an error if “i” is outside the
valid index values of “L”
Sort(L, condition) Sort list “L” based on “condition” (Increasing /
decreasing)
Search(e, L) Return the index of element “e” in list L. If the
element is not found, an error may be thrown or a
pre-decided value (example -1) may be returned
Copy(L1, L2) Copy all elements of list L1 to list L2 (L2 may be
overwritten)
Copy(L1, s1, e1,
L2, s2) Copy elements of list L1, starting from index s1 to
index e1 to list L2 starting at position s2. Errors
may be thrown if s1 amd e1 are outside the limits
of list L1, or is s2 is outside the limits of L2
Abstract Data Types: Lists
• The user functions we discussed can be implemented using several
underlying data structures
• We have already looked at how lists can be implemented using Arrays, Linked
lists and Skip linked lists
• Using different underlying data structures will require different “internal
functions”
Function Internal Internal
functions with functions with
array Linked list
Delete(i,L) For k=i to endUse linear
traversal to goto
Overwrite(Get node at position
Element(i+1), i-1.
k,L) Set Node-
>nextPtr =
Node->nextPtr-
>nextPtr
Abstract Data Types: Lists
• Since the internal functions and data structures are different, this effects the
run-time as well
Function Runtime with Runtime with
Array Linked list
Can you get best of both array
Insert(e,i,L) O(n) O(n) and linked list implementation
using skip-lists?
Delete(i,L) O(n) O(n)
GetElement(i,L) O(1) O(n)
Search(e,L) O(lg (n)) O(n)
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: 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

• When a recursion is executed, the variables, function that is called is written


to a stack. This ensures that when the most recent recursion returns, the
previous recursion is available for access to the CPU
Recursion call 4, and all of its data

Recursion call 3, and all of its data

Recursion call 2, and all of its data

Recursion call 1, and all of its data

• As a data structure to store intermediate values in compilers, parsers,


applications that manage undo/redo function
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: 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

• Scheduling: In any system where multiple processes need to wait for a


resource, queues may be used to maintain a list of processes that are making
a request for the resource. The earliest process which asked for the resource
will be fulfilled first
Request 4 and its usage needs

Request 3 and its usage needs

Request 2 and its usage needs

Request 1 and its usage needs

• Manage message queues, priority queues, buffers


Thank You !!!

You might also like