+ All Categories
Home > Engineering > Data structures1

Data structures1

Date post: 28-Jun-2015
Category:
Upload: parthipan-parthi
View: 40 times
Download: 2 times
Share this document with a friend
Popular Tags:
42
Data Structures V.PARTHIPAN AP/CSE , SAVEETHA UNIVERSITY
Transcript
Page 1: Data structures1

Data Structures

V.PARTHIPAN AP/CSE ,

SAVEETHA UNIVERSITY

Page 2: Data structures1

Unit II LINEAR DATA STRUCTURE

Abstract Data StructureList ADTStack ADTQueue ADT

Page 3: Data structures1

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

Page 4: Data structures1

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

Page 5: Data structures1

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

Page 6: Data structures1

List adt

Implementation Array implementation Linked List implementation Cursor implementation

Page 7: Data structures1

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

Page 8: Data structures1

List adt

Linked list Implementation Consists of series of nodes Singly Linked List Doubly Linked List Circular Linked List

Page 9: Data structures1

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

Page 10: Data structures1

List adt

Cursor Implementation

Page 11: Data structures1

List adt

Cursor Implementation

Page 12: Data structures1

List adt

Cursor Implementation Declaration

Page 13: Data structures1

List adt

Cursor Implementation

Page 14: Data structures1

List adt

Cursor Implementation

Page 15: Data structures1

List adt

Cursor Implementation

Page 16: Data structures1

List adt

Cursor Implementation

Page 17: Data structures1

List adt

Cursor Implementation

Page 18: Data structures1

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

Page 19: Data structures1

STACK adt

Operations on Stack Push Pop

Page 20: Data structures1

STACK adt

Push Process of inserting new element to the top of the

stack For every push operation the top is incremented by 1

Page 21: Data structures1

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

Page 22: Data structures1

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

Page 23: Data structures1

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

Page 24: Data structures1

STACK adt

Routine to Push an element into stack

Page 25: Data structures1

STACK adt

Routine to Pop an element from stack

Page 26: Data structures1

STACK adt

Routine to return top element of the stack

Page 27: Data structures1

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

Page 28: Data structures1

STACK adtDeclaration for Linked List Implementation of the stack

Page 29: Data structures1

STACK adtRoutine to check whether stack is empty

Page 30: Data structures1

STACK adtRoutine to create an empty stack

Page 31: Data structures1

STACK adtRoutine to push element onto a stack

Page 32: Data structures1

STACK adtRoutine to return top element in a stack

Page 33: Data structures1

STACK adtRoutine to pop element from a stack

Page 34: Data structures1

STACK adt

Applications of stackEvaluating arithmetic expressionsBalancing the symbolsTowers of HannoiFunction calls8 Queen Problem

Page 35: Data structures1

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+

Page 36: Data structures1

STACK adt

Prefix notationArithmetic operator is placed before the

two operandsAlso called polish notation((A/B)+C) +/ABC

Page 37: Data structures1

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

Page 38: Data structures1

STACK adt

Page 39: Data structures1

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

Page 40: Data structures1

STACK adt

Towers of Hanoi – Recursive solution

Page 41: Data structures1

STACK adt

Page 42: Data structures1

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


Recommended