dynamically linked stacks

4
Dynamically Linked Stacks Steve Paks

Upload: jonghoon-park

Post on 18-Jul-2015

62 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dynamically linked stacks

Dynamically Linked Stacks

Steve Paks

Page 2: Dynamically linked stacks

Dynamically Linked Stacks

• Linked Stack

int MAX_STACKS 10; /* maximum number of stacks */

Class Element{

int key;

/* other fields */

}

Class Stack{

element item;

Stack link;

}

Stack top[MAX_STACKS];

Page 3: Dynamically linked stacks

Dynamically Linked

Stacks(Cont’d)• Add to a Linked Stack

void add(Stack top, Element item){

Stack temp = new Stack();

if(IS_FULL(temp)){

print(“The memory is full”);

exit(1);

}

temp.item = item;

temp.link = top;

top = temp;

}

item

temp

item

top

top

∙∙∙∙ (a)∙∙∙∙ (b)

(a)

(b)

Page 4: Dynamically linked stacks

Dynamically Linked

Stacks(Cont’d)• delete from a Linked Stack

Element delete(){

Stack temp = top;

Element item;

if(IS_EMPTY(temp)){

print(“The Stack is empty”);

exit(1);

}

item = temp.item;

top = temp.link;

return item;

}

a

temp

top

top

∙∙∙∙ (a)∙∙∙∙ (b)

(a)

(b)

item a