dynamically linked stacks
TRANSCRIPT
![Page 1: Dynamically linked stacks](https://reader035.vdocuments.site/reader035/viewer/2022071815/55a945871a28ab9b3e8b45be/html5/thumbnails/1.jpg)
Dynamically Linked Stacks
Steve Paks
![Page 2: Dynamically linked stacks](https://reader035.vdocuments.site/reader035/viewer/2022071815/55a945871a28ab9b3e8b45be/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022071815/55a945871a28ab9b3e8b45be/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022071815/55a945871a28ab9b3e8b45be/html5/thumbnails/4.jpg)
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