0% found this document useful (0 votes)
6 views6 pages

DS Lab Sessions 7

The document outlines a lab session for a Data Structures course, focusing on dynamic vs static memory allocation, and algorithms for stack and queue operations using linked lists. It includes implementation code for a stack and a queue, demonstrating push, pop, enqueue, and dequeue functionalities. The document also contains sections for pre-lab, in-lab, and post-lab activities, along with space for evaluator comments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

DS Lab Sessions 7

The document outlines a lab session for a Data Structures course, focusing on dynamic vs static memory allocation, and algorithms for stack and queue operations using linked lists. It includes implementation code for a stack and a queue, demonstrating push, pop, enqueue, and dequeue functionalities. The document also contains sections for pre-lab, in-lab, and post-lab activities, along with space for evaluator comments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

A.Y: 2025-26 Regd.

No:

Lab Session 7
Date:
Pre-Lab

1. Differentiate Dynamic Memory Allocation Vs Static Memory Allocatio

2. Write the algorithm to push node in stack using linked list

3. Write an algorithm to dequeue node in queue using linked list

24CS285: Data Structures Page No.


A.Y: 2025-26 Regd. No:

In-Lab
1. Implement a STACK using a LINKED LIST
Program:
#include<stdio.h>
#include<stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};

// Pointer to the top of the stack


struct Node* top = NULL;

// Function to push an element onto the stack


void push(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Stack overflow! Memory allocation failed.\n");
return;
}
newNode->data = value;
newNode->next = top;
top = newNode;
printf("%d pushed to stack.\n", value);
}

// Function to pop an element from the stack


void pop() {
if (top == NULL) {
printf("Stack underflow! No element to pop.\n");
return;
}
struct Node* temp = top;
printf("%d popped from stack.\n", top->data);
top = top->next;
free(temp);
}
void peek() {
if (top == NULL) {
printf("Stack is empty.\n");
return;
}

24CS285: Data Structures Page No.


A.Y: 2025-26 Regd. No:

printf("Top element: %d\n", top->data);


}
void display() {
if (top == NULL) {
printf("Stack is empty.\n");
return;
}
struct Node* temp = top;
printf("Stack: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
push(10);
push(20);
push(30);
display();
peek();
pop();
display();
pop();
pop();
pop(); // extra pop to check underflow
return 0;
}
Output:

24CS285: Data Structures Page No.


A.Y: 2025-26 Regd. No:

Post-Lab
1. Implement a QUEUE using LINKED LIST

#include<stdio.h>
#include<stdlib.h>
//node structure
struct Node{
int data;
struct Node* next;
};
// Pointers to the front and rear of the queue
struct Node* front = NULL;
struct Node* rear = NULL;

// Function to enqueue (insert at rear)


void enqueue(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed!\n");
return;
}
newNode->data = value;
newNode->next = NULL;

if (rear == NULL) { // Queue is empty


front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("%d enqueued to queue.\n", value);
}

// Function to dequeue (remove from front)


void dequeue() {
if (front == NULL) {
printf("Queue underflow! No element to dequeue.\n");
return;
}
struct Node* temp = front;
printf("%d dequeued from queue.\n", front->data);
front = front->next;

24CS285: Data Structures Page No.


A.Y: 2025-26 Regd. No:

if (front == NULL) { // Queue became empty


rear = NULL;
}
free(temp);
}

// Function to peek at the front element


void peek() {
if (front == NULL) {
printf("Queue is empty.\n");
return;
}
printf("Front element: %d\n", front->data);
}

// Function to display the queue


void display() {
if (front == NULL) {
printf("Queue is empty.\n");
return;
}
struct Node* temp = front;
printf("Queue: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Main function
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
peek();
dequeue();
display();
dequeue();
dequeue();
dequeue(); // extra dequeue to check underflow
return 0;

24CS285: Data Structures Page No.


A.Y: 2025-26 Regd. No:

}
Output:

(Faculty use only)

Comments of the Evaluator: Evaluator’s Observation

Marks Secured: out of

Marks Secured: out of

24CS285: Data Structures Page No.

You might also like