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

Lab 08

This document provides an overview of stack data structures, detailing their Last In, First Out (LIFO) principle and common operations such as push, pop, peek, and isEmpty. It includes pseudocode for both array-based and linked list-based stack implementations, outlining how to perform each operation. Additionally, it lists tasks for further development, including dynamic resizing, stack printing, and using stacks for expression evaluation and string reversal.

Uploaded by

sm-malik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 08

This document provides an overview of stack data structures, detailing their Last In, First Out (LIFO) principle and common operations such as push, pop, peek, and isEmpty. It includes pseudocode for both array-based and linked list-based stack implementations, outlining how to perform each operation. Additionally, it lists tasks for further development, including dynamic resizing, stack printing, and using stacks for expression evaluation and string reversal.

Uploaded by

sm-malik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 8: Stack Implementation in C++

Introduction to Stacks
A stack is a data structure following the Last In, First Out (LIFO) principle, where the last
item added is the first to be removed. Common stack operations include:

• Push: Adds an item to the top of the stack.


• Pop: Removes the top item from the stack.
• Peek/Top: Retrieves the top item without removing it.
• isEmpty: Checks if the stack is empty.

Array-based Stack Implementation (Pseudocode)


1. Initialize:
a. Define a fixed-size array stack and a variable top initialized to -1.
2. Push Operation:
a. Check if top has reached the maximum stack size.
b. If not, increment top and add the element to stack[top].
function push(x):
if top >= MAX_SIZE - 1:
print "Stack Overflow"
else:
top = top + 1
stack[top] = x

3. Pop Operation:
a. Check if top is -1, indicating the stack is empty.
b. If not, return stack[top] and decrement top.
function pop():
if top < 0:
print "Stack Underflow"
else:
item = stack[top]
top = top - 1
return item

4. Peek Operation:
a. If top is -1, stack is empty.
b. Otherwise, return stack[top].
function peek():
if top < 0:
print "Stack is Empty"
else:
return stack[top]

5. isEmpty Operation:
a. Return true if top is -1, else return false.
function isEmpty():
return top < 0

Linked List-based Stack Implementation (Pseudocode)


1. Initialize:
a. Define Node with properties data and next.
b. Set a top pointer initialized to null.
2. Push Operation:
a. Create a new node with given data.
b. Set newNode.next to top and update top to newNode.
function push(x):
newNode = Node(x)
newNode.next = top
top = newNode
3. Pop Operation:
a. If top is null, the stack is empty.
b. Otherwise, retrieve top.data, set top to top.next, and return data.
function pop():
if top == null:
print "Stack Underflow"
else:
item = top.data
top = top.next
return item

4. Peek Operation:
a. If top is null, the stack is empty.
b. Otherwise, return top.data.
function peek():
if top == null:
print "Stack is Empty"
else:
return top.data

5. isEmpty Operation:
a. Return true if top is null, otherwise false.
function isEmpty():
return top == null

Tasks
1. Implement a function to check if the stack is full.
2. Modify the stack implementation to allow for dynamic resizing.
3. Create a stack implementation using a linked list.
4. Write a printStack function to display all elements in the stack from top to bottom.
5. Develop a postfix expression evaluator using a stack.
6. Write a function to reverse a string using a stack.

You might also like