Dsu Ex 17
Dsu Ex 17
ENGINEERING
Experiment No: 17
Title of Write a 'C' Program to perform PUSH and POP Operations
Experiment on
Stack using Linked List.
1 | Page
Traverse the linked list from top to the end and print each element to
display the current state of the stack.
2 | Page
3 | Page
4 | Page
Practical Related Questions:-
1. Write a C program to perform following operations on Stack as Linked List.
PUSH (10), PUSH (20), POP, PUSH (10), PUSH (20), POP, PUSH (20), POP.
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
} Stack;
5 | Page
void initialize(Stack* stack) {
stack->top = NULL;
newNode->data = value;
newNode->next = stack->top;
stack->top = newNode;
if (isEmpty(stack)) {
printf("Stack Underflow\n");
stack->top = stack->top->next;
free(temp);
return poppedValue;
6 | Page
}
if (isEmpty(stack)) {
printf("Stack is empty\n");
return;
current = current->next;
printf("\n");
int main() {
Stack stack;
initialize(&stack);
push(&stack, 10);
push(&stack, 20);
pop(&stack);
push(&stack, 10);
push(&stack, 20);
pop(&stack);
7 | Page
push(&stack, 20);
pop(&stack);
display(&stack);
return 0;
2. What is the time complexity of the push, pop, and peek operations in your
implementation?
1. Push Operation:
2. Pop Operation:
8 | Page
Summary:
Push: O(1)
Pop: O(1)
Peek: O(1)
All these operations are efficient and run in constant time due to the nature of linked
list implementations for stacks.
Exercise:-
1. What are the advantages of using a linked list to implement a stack?
Dynamic Size:
A linked list can grow and shrink in size dynamically, unlike an array-based
stack, which has a fixed size. This means that a linked list stack can
accommodate as many elements as the system's memory allows, eliminating
the risk of stack overflow (as long as memory is available).
No Wasted Space:
With a linked list, memory is allocated only for the elements that are actually
in the stack. In contrast, an array may allocate more memory than needed,
leading to wasted space.
Memory can be allocated for each new node as it is pushed onto the stack,
and freed when popped, which allows for more efficient use of memory,
particularly in scenarios where the number of elements varies significantly.
Ease of Implementation:
Flexibility:
The linked list structure allows for easy extension or modification. For
example, if you need to implement additional functionality, such as tracking
the minimum or maximum element, you can do so with minimal disruption
to the existing structure.
9 | Page
No Resizing Overhead:
With an array-based implementation, if the array becomes full, you may need
to create a new larger array and copy the existing elements over. This
resizing operation can be costly. In contrast, a linked list does not require
such resizing.
Linked lists can easily be used to implement more complex data structures
that may combine multiple stacks or include additional features, such as
history tracking in a browser or undo functionality in applications.
Memory Overhead:
Each node in a linked list requires additional memory for storing pointers
(e.g., the next node). This overhead can be significant when the stack
contains many elements, leading to inefficient memory usage compared to an
array implementation.
Fragmentation:
Cache Locality:
Array-based stacks benefit from better cache locality, as elements are stored
in contiguous memory locations. Linked lists, on the other hand, may have
nodes spread across different memory locations, which can lead to more
cache misses and reduced performance.
Complexity of Implementation:
While basic stack operations (push, pop) are relatively simple, the
implementation of a linked list can introduce additional complexity in terms
of memory management (allocation and deallocation) and handling edge
cases (e.g., when the stack is empty).
Pointer Management:
10 | P a g e
Careful management of pointers is required to avoid issues such as memory
leaks (failing to free memory) and dangling pointers (accessing freed
memory). This adds to the potential for bugs compared to simpler array-
based implementations.
Performance Overhead:
Unlike arrays, linked lists do not allow for direct access to elements based on
their index. This means that you cannot quickly access elements other than
the top of the stack, which may be a limitation in certain scenarios.
11 | P a g e