2. Unit II LINEAR DATA STRUCTURE
Abstract Data Structure
List ADT
Stack ADT
Queue ADT
3. Abstract data type
Mathematical model for a certain class of data
structures that have similar behavior
Specify memory needed to store data & type of data
that will be stored in that memory location
Defined only by the operations that may be
performed on it & by mathematical constraints of
operation
Often implemented as modules; declares procedure
that corresponds to ADT operations
4. List adt
List is an ordered set of elements
General form of the list is A1, A2,A3…., An
A1 : first element of list
AN: last element of list
N: size of list
If elements position is Ai, successor is Ai+1 &
predecessor Ai-1
5. List adt
Various operations on the list
Insert (x,5): insert element x after position 5
Delete(x): element x deleted
Find(x): returns position of x
Next(i): returns the position of its successor element i+1
Previous(i): returns the position of its predecessor element i-1
printlist: contents of the list is displayed
Makeempty: makes the list empty
7. List adt
Array Implementation
Collection of specific no. of data stored in a consecutive memory
locations
Insertion & deletion are expensive as it requires more data
elements
Find & printlist operations takes constant time
Max. size of list considerably wastes the memory wastes the
memory space
8. List adt
Linked list Implementation
Consists of series of nodes
Singly Linked List
Doubly Linked List
Circular Linked List
9. List adt
Cursor Implementation
Useful where linked list concept has to be implemented without
pointers
Data are stored in a global array of structures. Here array index is
considered as address
Maintains a list of free cells called cursor space
Slot 0 is considered as a header and next is equivalent to the pointer
which points to the next slot
18. STACK adt
Linear data structure which follows Last In First Out
(LIFO)
Both insertion and deletion occur at only one end of the
list called TOP
Eg: Pile of coins, stack of tray in cafeteria
20. STACK adt
Push
Process of inserting new element to the top of the stack
For every push operation the top is incremented by 1
21. STACK adt
Pop
Process of deleting an element from the top of the stack
For every push operation the top pointer is decremented by 1
22. STACK adt
Exceptional conditions
Overflow
Attempt to insert an element when the stack is full
Underflow
Attempt to delete an element when the stack is empty
23. STACK adt
Implementation
Array
Pointers
Array Implementation
Each stack is associated with top pointer which is -1 for empty
stack
To push Element X onto the stack, Top pointer is incremented and
set stack[top]=X
To pop an element, the stack[top] value is returned and top pointer
is decremented
Pop an empty stack or push on a full stack will exceed the array
bounds
27. STACK adt
Linked List Implementation of the stack
• Push operation is performed by inserting an element at the front
of the list
• Pop operation is done by deleting an element at the front of the
list
• Top operation returns the element at the front of the list
34. STACK adt
Applications of stack
Evaluating arithmetic expressions
Balancing the symbols
Towers of Hannoi
Function calls
8 Queen Problem
35. STACK adt
Types of notations to represent arithmetic Expression
Infix notation
Prefix notation
Postfix notation
Infix notation
Arithmetic operator appears between the two
operands
Eg. A+B/C
Postfix notation
Arithmetic operator appears directly after two
operands
Also called reverse polish notation
36. STACK adt
Prefix notation
Arithmetic operator is placed before the two
operands
Also called polish notation
((A/B)+C) +/ABC
37. STACK adt
Towers of Hanoi
One of the example illustrating the Recursion
technique
Moving collection of N disks of decreasing size
from one pillar to another pillar
Movement restricted by following rules
Only one disk could be moved at a time
No larger disk could ever reside on a pillar on top of a
smaller disk
A 3rd pillar can be used as an intermediate to store one
or more disks, while they were being moved from
39. STACK adt
Towers of Hanoi – Recursive solution
N – represents the no. of disks
Steps
If N=1, move disk from A to C
If N=2, move the 1st disk from A to B, then move the 2nd disk
from A to C, then move the 1st disk from B to C
If N=3, repeat the step (2) to move the first 2 disks from A
to B using C as intermediate
then 3rd disk is moved from A to C. then repeat the step (2)
to move 2 disks from B to C using A as intermediate
In general, to move N disks. Apply the recursive technique to
move N-1 disks from A to B using C as an intermediate. Then
move the Nth disk from A to C. Then again apply the
recursive technique to move N-1 disks from B to C using A as
42. STACK adt
Function Calls
to keep track of the point to which each active
subroutine should return control when it finishes
executing
active subroutine is one that has been called but is yet
to complete execution after which control should be
handed back to the point of call