0% found this document useful (0 votes)
3 views

Stacks

Stacks are a linear data structure that operates on a Last In First Out (LIFO) principle, commonly used in web browsers and word processors for managing history and undo/redo actions. Key operations include push, pop, peek, isEmpty, and size, with implementations possible via fixed or dynamic sizes using arrays or linked lists. Each implementation has its advantages and disadvantages, such as ease of use and memory efficiency for arrays, versus dynamic sizing and efficient memory usage for linked lists.

Uploaded by

kgaumogoje351
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Stacks

Stacks are a linear data structure that operates on a Last In First Out (LIFO) principle, commonly used in web browsers and word processors for managing history and undo/redo actions. Key operations include push, pop, peek, isEmpty, and size, with implementations possible via fixed or dynamic sizes using arrays or linked lists. Each implementation has its advantages and disadvantages, such as ease of use and memory efficiency for arrays, versus dynamic sizing and efficient memory usage for linked lists.

Uploaded by

kgaumogoje351
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Stacks are a linear data structure that follows a particular order in which the operations are

performed. This particular order is referred to as LIFO (Last In First Out) - this order implies that
the element which is inserted last, will be the first one to be deleted

Some real-world scenarios of the uses of Stacks:


➢ Web browsers: to keep track of the history of web pages you visit in a single session, the
back and forward buttons in the browser use Stacks. When you click either button it will
either immediately take you to the previous or the next web page

➢ Undo/Redo operations in word processors: when using word processors like Microsoft
Word, you have the ability to undo and/or redo an action, this uses the Stacks functionality

What operations are performed on Stacks?


➢ Push: to insert an element into the Stack
➢ Pop: to remove an element from the Stack
➢ Peek: returns the top element of the Stack
➢ IsEmpty: tells us whether a Stack is empty or not
➢ Size: returns the size of the Stack

Why types of Stacks are there?


➢ Fixed size Stack: In this type of Stack, we cannot increase or decrease its size. If the Stack is
already full and we try to add an element to it, we experience an overflow error. If the
Stack is empty and we try to remove an element from it, we experience an underflow error
➢ Dynamic size Stack: in this type of Stack we can increase and decrease the size
dynamically to accommodate the insertion or deletion of an element

Implementation of Stacks:

The operations that can be performed on a Stack being push, pop and peek, we can say that it’s
possible to implement Stacks using arrays and linked lists

How so?

In an array-based implementation, the push operation is implemented by incrementing the index of


the top element and then storing the new element at that index. The pop operation is performed by
decrementing the index of the top element
In a linked list-based implementation, the push operation is implemented by creating a new node,
with a new element and assigning the pointer of the current top node to the new node. The pop
operation is implemented by assigning the pointer of the current top node to the next node

Advantages of array implementation:


➢ Easy to implement
➢ Memory is saved since pointer aren’t involved

Disadvantages of array implementation:


➢ It is not dynamic
➢ The size of the stack needs to be defined beforehand

Advantages of linked list implementation:


• It is dynamic
• It’s used in many virtual machines – for instance, the ability to assign memory to a VM
depending on what its requirements are, this makes the memory usage efficient

Disadvantages of linked list implementation:


➢ It requires extra memory due to the involvement of pointers
➢ Random access is not possible

You might also like