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

2.2-Static Dynamic Atrray for Stack

The document discusses two types of stacks: fixed size and dynamic size. Fixed size stacks have a predetermined size and can lead to overflow or underflow errors, while dynamic size stacks can grow and shrink as needed, typically implemented using linked lists. It also covers stack operations, implementations using arrays, pros and cons of each method, and applications of stacks in data structures.

Uploaded by

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

2.2-Static Dynamic Atrray for Stack

The document discusses two types of stacks: fixed size and dynamic size. Fixed size stacks have a predetermined size and can lead to overflow or underflow errors, while dynamic size stacks can grow and shrink as needed, typically implemented using linked lists. It also covers stack operations, implementations using arrays, pros and cons of each method, and applications of stacks in data structures.

Uploaded by

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

Types of Stacks:

• Fixed Size 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

underflow error occurs.


Types of Stacks:
• Dynamic Size Stack:

• A dynamic size stack can grow or shrink dynamically.

• When the stack is full, it automatically increases its size to accommodate the new

element.

• When the stack is empty, it decreases its size.

• 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’.

• Initially, the top is set to -1.

• 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 2 - Declare all the functions used in stack implementation.

• 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

• In a stack, push() is a function used to insert an element into the stack.

• In a stack, the new element is always inserted at top position.

• Push function takes one integer value as parameter and inserts that value into

the stack.
push(value) - Inserting value into the stack

We can use the following steps to push an element on to the stack...

Step 1 - Check whether stack is FULL. (top == SIZE-1)

Step 2 - If it is FULL, then display "Stack is FULL!!! Insertion is not

possible!!!" and terminate the function.

Step 3 - If it is NOT FULL, then increment top value by one (top++) and set

stack[top] to value (stack[top] = value).


• Adding an element on the top of the stack is termed a push operation. Push
operation has the following two steps:

• Increment the top variable of the stack so that it can refer to the next memory
location.

• Add a data element at the increment top position.

• 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.

• In a stack, the element is always deleted from top position.

• Pop function does not take any value as parameter.


pop() - Delete a value from the
Stack
We can use the following steps to pop an element from the stack...

Step 1 - Check whether stack is EMPTY. (top == -1)

Step 2 - If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not

possible!!!" and terminate the function.

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

We can use the following steps to display the elements of a stack...

Step 1 - Check whether stack is EMPTY. (top == -1)

Step 2 - If it is EMPTY, then display "Stack is EMPTY!!!" and terminate

the function.

Step 3 - If it is NOT EMPTY, then define a variable 'i' and initialize with top.

Display stack[i] value and decrement i value by one (i--).

Step 3 - Repeat above step until i value becomes '0'.


• #include<stdio.h> void main()
• #include<conio.h> {
int value, choice;
• #define SIZE 10 clrscr();
while(1){
• void push(int); printf("\n\n***** MENU *****\n");
• void pop(); printf("1. Push\n2. Pop\n3. Display\n4. Exit");
• void display(); printf("\nEnter your choice: ");
• int stack[SIZE], top = -1; scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value
void push(int value){
to be insert: "); if(top == SIZE-1)
scanf("%d",&value); printf("\nStack is Full!!! Insertion is
push(value); not possible!!!");
break;
case 2: pop();
else{
break; top++;
case 3: display(); stack[top] = value;
break; printf("\nInsertion success!!!");
case 4: exit(0);
}
default: printf("\nWrong
selection!!! Try again!!!"); }
}
}
}
void pop(){ void display(){
if(top == -1) if(top == -1)
printf("\nStack is Empty!!! Deletion printf("\nStack is Empty!!!");
else{
is not possible!!!"); int i;
else{ printf("\nStack elements are:\n");
printf("\nDeleted : %d", stack[top]); for(i=top; i>=0; i--)
top--; printf("%d\n",stack[i]);
} }
} }
Pros and Cons of Stack Implementation Using Array
Pros:
• It requires no extra memory to store the pointers in stack implementation using
an array.
• More efficient in terms of time, compared to stack implementation using
linked-list.
Cons:
• The size of the stack is fixed, so it cannot increase and decrease stack
implementation using an array.
• Insertion and deletion in an array are quite difficult as it stores the elements in
consecutive memory locations.
• With this, we have reached the end of the stack implementation using an array
tutorial.
• When a stack is implemented by using static arrays the size of the stack is bound
to MAXSTK(i.e maximum elements a stack can have)but some time stack need
be dynamic.

• This can be achieved by using dynamic arrays

• 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:

In array implementation, the stack is formed using an array.

All the operations are performed using arrays.

You will see how all operations can be implemented on the


stack in data structures using an array data structure.
Reasons to implement stacks using arrays:

• Memory Efficient: Array elements do not hold the next


elements address like linked list nodes do.

• Easier to implement and understand: Using arrays to


implement stacks require less code than using linked lists, and
for this reason it is typically easier to understand as well.

A reason for not using arrays to implement stacks:

• Fixed size: An array occupies a fixed part of the memory. This


means that it could take up more memory than needed, or if the
Linked-List:

Every new element is inserted as a top element in the

linked list implementation of stacks in data structures.

That means every newly inserted element is pointed to the top.

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.

Reasons for not using linked lists to implement stacks:

• Extra memory: Each stack element must contain the address to

the next element (the next linked list node).

• Readability: The code might be harder to read and write for some

because it is longer and more complex.


Application of Stack in Data
Structures
• Expression Evaluation and Conversion

• Backtracking

• Function Call

• Parentheses Checking

• String Reversal

• Syntax Parsing

• Memory Management

You might also like