0% found this document useful (0 votes)
7 views11 pages

Dsu Ex 17

Uploaded by

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

Dsu Ex 17

Uploaded by

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

DEPARTMENT OF COMPUTER

ENGINEERING

Subject: - DSU Subject Code: 313334


Semester: - III Course: DATA STRUCTURES
Laboratory No: V120 Name of Subject Teacher: IMRAN S
Name of Student: Paresh Roll Id: - 23203B0057
Parkar

Experiment No: 17
Title of Write a 'C' Program to perform PUSH and POP Operations
Experiment on
Stack using Linked List.

 Algorithm for Performing PUSH and POP Operations on a


Stack Using a Linked List:

1. Define a Node Structure


 Each node contains two fields: data to store the stack element and next to
link to the next node.
2. Create an Empty Stack
 The stack will be represented by a linked list where the top pointer points
to the top element of the stack (the head of the linked list).
3. PUSH Operation (Insert Element into Stack)
 Allocate memory for a new node.
 Set the data of the new node to the value being pushed.
 Make the next pointer of the new node point to the current top.
 Update top to point to the new node (the new element becomes the top of
the stack).
4. POP Operation (Remove Element from Stack)
 Check if the stack is empty (i.e., if top is NULL). If the stack is empty,
print "Stack Underflow."
 If the stack is not empty, store the value of the top node.
 Update top to point to the next node (i.e., remove the top node).
 Free the memory of the removed node.
5. Display the Stack

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>

typedef struct Node {

int data;

struct Node* next;

} Node;

typedef struct Stack {


Node* top;

} Stack;

5 | Page
void initialize(Stack* stack) {

stack->top = NULL;

int isEmpty(Stack* stack) {

return stack->top == NULL;

void push(Stack* stack, int value) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = value;

newNode->next = stack->top;

stack->top = newNode;

printf("Pushed %d to stack\n", value);

int pop(Stack* stack) {

if (isEmpty(stack)) {

printf("Stack Underflow\n");

return -1; // Return an error value

Node* temp = stack->top;

int poppedValue = temp->data;

stack->top = stack->top->next;

free(temp);

printf("Popped %d from stack\n", poppedValue);

return poppedValue;

6 | Page
}

void display(Stack* stack) {

Node* current = stack->top;

if (isEmpty(stack)) {

printf("Stack is empty\n");

return;

printf("Stack elements: ");

while (current != NULL) {

printf("%d ", current->data);

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:

o Time Complexity: O(1)

o Explanation: The push operation involves creating a new node and


updating the top pointer to point to this new node. This operation
does not depend on the size of the stack, making it a constant-time
operation.

2. Pop Operation:

o Time Complexity: O(1)

o Explanation: The pop operation involves checking if the stack is


empty, retrieving the value from the top node, updating the top
pointer to the next node, and freeing the memory of the popped node.
Again, this is a constant-time operation, regardless of the stack's size.

3. Peek Operation (if implemented):

o Time Complexity: O(1)

o Explanation: The peek operation, which retrieves the value of the


top node without removing it, would also have a time complexity of
O(1) because it simply involves accessing the data in the top node.

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.

 Efficient Memory Utilization:

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

 Implementing a stack using a linked list can be straightforward since the


operations (push and pop) only involve manipulating pointers, rather than
needing to handle index calculations and resizing (as with arrays).

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

 Implementation of Complex Data Structures:

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

2. Give limitations of using Linked List based Stack.

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

 Linked lists may suffer from memory fragmentation, especially in systems


with limited memory allocation. This can lead to inefficient use of memory,
as free blocks of memory may be scattered throughout, making it harder to
allocate larger contiguous blocks.

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

 In environments where the overhead of dynamic memory allocation is


significant, linked list-based implementations may perform worse than array-
based stacks. Allocating and deallocating memory can introduce additional
computational overhead.

 No Direct Access to Elements:

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

Grade and Process Product Dated Sign


Dated Related (35) Related (15)
Signature of
Teacher

11 | P a g e

You might also like