DSA Lecture - 2
DSA Lecture - 2
LECTURE - 2
ABSTRACT DATA TYPES
• An Abstract data type (ADT) refers to a set of data values and associated operations that are specified accurately,
independent of any particular implementation.
• With an ADT, we know what a specific data type can do, but how it actually does it is hidden.
• Stack: operations are "push an item onto the stack", "pop an item from the stack", "ask if the stack is empty";
implementation may be as array or linked list or whatever.
• Queue: operations are "add to the end of the queue", "delete from the beginning of the queue", "ask if the queue is
empty"; implementation may be as array or linked list or heap.
• Search structure: operations are "insert an item", "ask if an item is in the structure", and "delete an item";
implementation may be as array, linked list, tree, hash table, ...
STATIC AND DYNAMIC IMPLEMENTATION OF DATA STRUCTURE
LINKED LIST
• A linked list or one-way list is a linear collection of data elements, called nodes, where the linear
order is given by means of pointers.
MEMORY ALLOCATION: GARBAGE COLLECTION
• Together with the linked lists in memory , a special list is maintained which consists of unused memory
cells. This list, which has its own pointer, is called list of available space or the free storage list or the free
pool.
• This free storage list will also be called the AVAIL list.
OVERFLOW AND UNDERFLOW CONDITIONS
• Overflow will occur with linked lists when AVAIL = NULL and there is an insertion.
• Underflow will occur with linked lists when START = NULL and there is a deletion.
STACK
A stack is a list of elements in which an element may be inserted or deleted only at one end, called the top of the stack.
Special terminology is used for two basic operations associated with stacks:
“Push” is the term used to insert an element into a stack.
“Pop” is the term used to delete an element from a stack.
STACK
ARRAY REPRESENTATION OF STACKS
Stack may be represented in the computer in various ways, usually by means of a one-way list or a linear array..
STACK
XXX YYY ZZZ
TOP 3 MAXSTK 8
4. Return.
ALGORITHM FOR DELETION (POP OPERATION)
This procedure deletes the top element of the STACK and assign it to the variable ITEM.
4. Return.
MINIMIZING OVERFLOW
• The number of elements in a stack fluctuates as elements are added or removed from a stack.
• Accordingly, the particular choice of the amount of memory for a given stack involves a time- space tradeoff.
LINKED REPRESENTATION OF STACKS
The linked representation of a stack, commonly termed linked stack is a stack that is implemented using a singly linked list.
Top(START)
4. Set LINK[NEW] := TOP [ New node points to the original top node in the stack].
5. Set TOP = NEW [ Reset TOP to point to the new node at the top of the stack].
6. Exit.
ALGORITHM FOR DELETION
This procedure deletes the top element of a linked stack and assigns it to the variable ITEM.
2. Set ITEM:= INFO[TOP]. [ Copies the top element of stack into ITEM].
5. Exit.