data structures1
TRANSCRIPT
Data Structures
V.PARTHIPAN AP/CSE ,
SAVEETHA UNIVERSITY
Unit II LINEAR DATA STRUCTURE
Abstract Data StructureList ADTStack ADTQueue ADT
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
List adt
List is an ordered set of elementsGeneral 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
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
List adt
Implementation Array implementation Linked List implementation Cursor implementation
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
List adt
Linked list Implementation Consists of series of nodes Singly Linked List Doubly Linked List Circular Linked List
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
List adt
Cursor Implementation
List adt
Cursor Implementation
List adt
Cursor Implementation Declaration
List adt
Cursor Implementation
List adt
Cursor Implementation
List adt
Cursor Implementation
List adt
Cursor Implementation
List adt
Cursor Implementation
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
STACK adt
Operations on Stack Push Pop
STACK adt
Push Process of inserting new element to the top of the
stack For every push operation the top is incremented by 1
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
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
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
STACK adt
Routine to Push an element into stack
STACK adt
Routine to Pop an element from stack
STACK adt
Routine to return top element of the stack
STACK adtLinked 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
STACK adtDeclaration for Linked List Implementation of the stack
STACK adtRoutine to check whether stack is empty
STACK adtRoutine to create an empty stack
STACK adtRoutine to push element onto a stack
STACK adtRoutine to return top element in a stack
STACK adtRoutine to pop element from a stack
STACK adt
Applications of stackEvaluating arithmetic expressionsBalancing the symbolsTowers of HannoiFunction calls8 Queen Problem
STACK adt
Types of notations to represent arithmetic Expression
Infix notationPrefix notationPostfix notationInfix notationArithmetic operator appears between the two
operandsEg. A+B/C Postfix notationArithmetic operator appears directly after two
operandsAlso called reverse polish notation((A/B)+C) AB/C+
STACK adt
Prefix notationArithmetic operator is placed before the
two operandsAlso called polish notation((A/B)+C) +/ABC
STACK adt
Towers of HanoiOne of the example illustrating the
Recursion techniqueMoving collection of N disks of decreasing
size from one pillar to another pillarMovement 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 source to destination
STACK adt
STACK adtTowers of Hanoi – Recursive solutionN – represents the no. of disksSteps
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 intermediateIn 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 an intermediate
STACK adt
Towers of Hanoi – Recursive solution
STACK adt
STACK adt
Function Callsto 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