2.2-Static Dynamic Atrray for Stack
2.2-Static Dynamic Atrray for Stack
• A fixed size stack has a fixed size and cannot grow or shrink dynamically.
• If the stack is full and an attempt is made to add an element to it, an overflow error
occurs.
• If the stack is empty and an attempt is made to remove an element from it, an
• When the stack is full, it automatically increases its size to accommodate the new
element.
• This type of stack is implemented using a linked list, as it allows for easy resizing of the
stack.
Stacks, whether implemented using static arrays or dynamic arrays, are abstract data types
that follow the Last In, First Out ( LIFO) principle. This means that the last element
added to the stack is the first one to be removed.
Stacks with Static Arrays:
Fixed Size:
Static arrays have a fixed size that is determined at the time of declaration.
The size cannot be changed during runtime.
Memory Allocation:
Memory for the entire array is allocated at compile-time.
This means that even if the stack is not full, the allocated memory remains
constant.
Efficiency:
Stacks with static arrays can be more memory-efficient in terms of overhead, as
they don't require extra space for dynamic resizing.
Usage:
Suitable when the maximum number of elements in the stack is known and
doesn't change.
Stacks with Dynamic Arrays:
Dynamic Size:
Dynamic arrays can resize themselves during runtime to accommodate a changing
number of elements.
They can grow or shrink as needed.
Memory Allocation:
Memory for dynamic arrays is typically allocated on the heap, and the size can be
adjusted as the stack grows or shrinks.
Flexibility:
Dynamic arrays offer more flexibility in terms of handling variable amounts of data.
Cost of Resizing:
Resizing a dynamic array involves copying elements to a new location with a larger or
smaller size. This operation can be computationally expensive.
Usage:
Suitable when the number of elements in the stack is unpredictable or varies dynamically.
Performance:
Stacks with static arrays might have better performance for small, fixed-size
stacks due to less overhead.
Stacks with dynamic arrays provide flexibility but may incur a performance cost
during resizing operations.
Memory Management:
Dynamic arrays require careful memory management to avoid memory leaks
and inefficient resizing.
• Implementation:
The choice between static and dynamic arrays often depends on the specific
requirements of the application and the expected behavior of the stack.
Stack Using Array
• A stack data structure can be implemented using a one-dimensional array.
• But stack implemented using array stores only a fixed number of data values. This
implementation is very simple.
• Just define a one dimensional array of specific size and insert or delete the values into that
array by using LIFO principle with the help of a variable called 'top’.
• Whenever we want to insert a value into the stack, increment the top value by one and then
insert.
• Whenever we want to delete a value from the stack, then delete the top value and decrement
the top value by one.
Stack Operations using Array
Steps to create an empty stack.
• Step 1 - Include all the header files which are used in the program and define a
constant 'SIZE' with specific value.
• Step 3 - Create a one dimensional array with fixed size (int stack[SIZE])
• Step 4 - Define a integer variable 'top' and initialize with '-1'. (int top = -1)
• Step 5 - In main method, display menu with list of operations and make
suitable function calls to perform operation selected by the user on the stack.
push(value) - Inserting value into the stack
• Push function takes one integer value as parameter and inserts that value into
the stack.
push(value) - Inserting value into the stack
Step 3 - If it is NOT FULL, then increment top value by one (top++) and set
• Increment the top variable of the stack so that it can refer to the next memory
location.
• Stack data structure states an overflow condition when you try to insert an
element into the stack when complete.
pop() - Delete a value from the
Stack
• In a stack, pop() is a function used to delete an element from the stack.
Step 3 - If it is NOT EMPTY, then delete stack[top] and decrement top value
by one (top--).
• Removing a data element from the stack data structure is called a pop
operation.
The pop operation has following steps:
• The value of the top variable will be incremented by one whenever you delete
an item from the stack.
• The topmost variable of the stack is stored in another variable, and then the
value of the top variable will be decremented by one.
• The pop operation returns the deleted element that was stored in another
variable as a result.
• Stack data structure states an underflow condition when you try to delete a
data element when the stack is already empty.
display() - Displays the elements of a Stack
the function.
Step 3 - If it is NOT EMPTY, then define a variable 'i' and initialize with top.
• The memory for the stack is dynamically allocated by using memory allocation
functions such as malloc or calloc
• When the stack is full , memory of the stack is doubled by using realloc()
function and the capacity of the stack is doubled(MAXSTK is doubled)
#include<stdio.h> void push()
#include<conio.h> {
#include<stdlib.h> if(top == capacity-1)
{
int *stack;
stackfull();//which double the memory when stack is full
int capacity=1;
}
int top=-1, item; printf("enter an item to insert\n");
scanf("%d", &item);
stack[++top] = item;
}
void pop() void stackfull()
{ {
stack=realloc(stack,capacity*2*sizeof(int));
if(top == -1)
// doubling the memory
{ if(stack==NULL)// IF MEMORY IS IN SUFFICIENT
printf("underflow\n"); {
return; printf(“memory is insuffient\n”);
} exit(0);
}
item = stack[top--];
capacity=capacity*2;
printf("item deleted is %d \n", item); // doubling the stack size
} }
void display()
void main()
{
{
int i;
int choice=1;
if(top==-1)
{ stack=malloc(capacity*sizeof(int));
printf(“stack is empty \n”); //allocating memory dynamically
Return; while(choice)
} {
for(i=top;i>=0;i--) printf("enter your choice\n 1.push\n 2.pop\n
printf("%d",*(stack+i)); 3.display \n 4 exit\n");
} scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:printf("invalid operation\n");
exit(0);
}
}
free(stack);// deallocating memory
stack=NULL; //avoiding dangling pointer
}
Implementation of Stack in Data Structures
The implementation of stacks in data structures using two data
structures that are an array and a linked list.
Array:
Whenever you want to remove an element from the stack, remove the node
indicated by the top, by moving the top to its previous node in the list.
A reason for using linked lists to implement stacks:
• Dynamic size: The stack can grow and shrink dynamically, unlike
with arrays.
• Readability: The code might be harder to read and write for some
• Backtracking
• Function Call
• Parentheses Checking
• String Reversal
• Syntax Parsing
• Memory Management