Bird
0
0
DSA Cprogramming~30 mins

Dynamic Stack Using Resizable Array in DSA C - Build from Scratch

Choose your learning style9 modes available
Dynamic Stack Using Resizable Array
📖 Scenario: You are building a stack data structure that can grow dynamically as more items are added. This is like a stack of plates that can get taller when you add more plates, and shrink when you remove plates.
🎯 Goal: Create a dynamic stack using a resizable array in C. You will initialize the stack, set its initial capacity, implement push and pop operations that resize the array when needed, and finally print the stack contents.
📋 What You'll Learn
Create a struct called Stack with an integer pointer items, integer capacity, and integer top.
Initialize the stack with a capacity of 2 and top as -1.
Write a function resize to double the capacity of the stack's array.
Write a function push that adds an element to the stack and resizes if full.
Write a function pop that removes the top element and returns it.
Print the stack contents from bottom to top.
💡 Why This Matters
🌍 Real World
Dynamic stacks are used in programming language interpreters, undo features in applications, and expression evaluation where the number of elements is not fixed.
💼 Career
Understanding dynamic data structures like resizable stacks is important for software developers working on performance-critical applications and memory management.
Progress0 / 4 steps
1
Create the Stack struct and initialize it
Create a struct called Stack with an integer pointer items, integer capacity, and integer top. Then create a function initStack that initializes a Stack with capacity 2, top -1, and allocates memory for items accordingly.
DSA C
Hint

Use typedef struct to define Stack. Initialize capacity to 2 and top to -1. Allocate memory for items using malloc.

2
Add a resize function to double the stack capacity
Write a function resize that takes a pointer to Stack and doubles its capacity. It should allocate a new array with double size, copy existing items, free the old array, and update the stack's items pointer and capacity.
DSA C
Hint

Allocate a new array with double capacity, copy old items with a loop, free old memory, then update items and capacity.

3
Implement push and pop functions with dynamic resizing
Write a function push that adds an integer to the stack. If the stack is full (top == capacity - 1), call resize before adding. Also write a function pop that removes and returns the top element. If the stack is empty (top == -1), return -1.
DSA C
Hint

Check if stack is full before pushing and call resize. For pop, check if empty and return -1 if so, else return top item and decrease top.

4
Push values, pop one, and print the stack contents
In main, create a Stack variable called stack and initialize it with initStack. Push the values 10, 20, 30, and 40 onto the stack. Then pop one value and store it in popped. Finally, print the stack contents from bottom to top separated by spaces.
DSA C
Hint

Use a for loop from 0 to stack.top to print each item separated by spaces.