0% found this document useful (0 votes)
8 views38 pages

Pathak DS

Uploaded by

Raghav Agarwal
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)
8 views38 pages

Pathak DS

Uploaded by

Raghav Agarwal
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/ 38

DETAILS OF THE EXPERIMENTS CONDUCTED

(TO BE USED BY THE STUDENTS IN THEIR RECORDS)

INDEX

S. No TITLE OF THE EXPERIMENT DATE OF FACULTY


SUBMISSION SIGNATURE

1 Program for List implementation using dynamic


memory allocation.
2 Program for Stack implementation through array.

3 Program for Stack implementation through


dynamic memory allocation.
4 Program for queue implementation through array.

5 Program for Queue Implementation through


Dynamic Memory Allocation.

6 Program for circular queue implementation


through array.

7 Program for circular queue implementation


through dynamic memory allocation.

8 Program for binary and linear search algorithm.

9 Program for implementation of Bubble sort


Algorithm.

10 Program for Implementation of Selection Sort


Algorithm.

11 Program for implementation of quick sort


algorithm
12 Program for implementation of merge sort
algorithm
13 Program for implementation of binary tree.

14 Program for tree traversal in preorder, in order and


post order.

15 Program for implementation of binary search tree.

16 Program for implementation of breadth first


search algorithm.
17 Program for implementation depth first search
algorithm.
P-1 : Program for List implementation using dynamic memory allocation.
#include <stdio.h>
#include <stdlib.h>

typedef struct {
int *array; // Pointer to the dynamically allocated array
int size; // Current number of elements in the list
int capacity; // Maximum capacity of the list
} DynamicList;

// Function prototypes
DynamicList* createList(int initialCapacity);
void addElement(DynamicList *list, int element);
void removeElement(DynamicList *list, int index);
void displayList(DynamicList *list);
void freeList(DynamicList *list);

int main() {
// Add student details to the output
printf("Raghav Pathak | 2301430130060 | IT-2\n");

int choice, element, index;


DynamicList *list = createList(5); // Initialize with a capacity of 5

if (list == NULL) {
printf("Failed to initialize the list. Exiting...\n");
return -1;
}

while (1) {
printf("\n--- List Operations ---\n");
printf("1. Add Element\n");
printf("2. Remove Element\n");
printf("3. Display List\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the element to add: ");
scanf("%d", &element);
addElement(list, element);
break;

case 2:
printf("Enter the index to remove: ");
scanf("%d", &index);
removeElement(list, index);
break;

case 3:
displayList(list);
break;

case 4:
freeList(list);
printf("Exiting program.\n");
return 0;

default:
printf("Invalid choice. Please try again.\n");
}
}
}

// Function to create a dynamic list


DynamicList* createList(int initialCapacity) {
DynamicList *list = (DynamicList *)malloc(sizeof(DynamicList));
if (!list) {
printf("Memory allocation for list failed.\n");
return NULL;
}

list->array = (int *)malloc(initialCapacity * sizeof(int));


if (!list->array) {
printf("Memory allocation for array failed.\n");
free(list);
return NULL;
}

list->size = 0;
list->capacity = initialCapacity;
return list;
}

// Function to add an element to the list


void addElement(DynamicList *list, int element) {
if (list->size == list->capacity) {
// Resize the array when full
int newCapacity = list->capacity * 2;
int *newArray = (int *)realloc(list->array, newCapacity * sizeof(int));
if (!newArray) {
printf("Memory reallocation failed. Element not added.\n");
return;
}
list->array = newArray;
list->capacity = newCapacity;
}
list->array[list->size++] = element;
printf("Element added successfully.\n");
}

// Function to remove an element at a given index


void removeElement(DynamicList *list, int index) {
if (index < 0 || index >= list->size) {
printf("Invalid index. Please try again.\n");
return;
}
for (int i = index; i < list->size - 1; i++) {
list->array[i] = list->array[i + 1];
}
list->size--;
printf("Element removed successfully.\n");
}

// Function to display all elements in the list


void displayList(DynamicList *list) {
if (list->size == 0) {
printf("List is empty.\n");
return;
}
printf("List elements: ");
for (int i = 0; i < list->size; i++) {
printf("%d ", list->array[i]);
}
printf("\n");
}

// Function to free the memory allocated for the list


void freeList(DynamicList *list) {
if (list) {
free(list->array);
free(list);
}
}

Output:
p-2: Program for Stack implementation through array
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100 // Maximum size of the stack

typedef struct {
int arr[MAX_SIZE];
int top;
} Stack;

// Function prototypes
void initialize(Stack *stack);
int isFull(Stack *stack);
int isEmpty(Stack *stack);
void push(Stack *stack, int element);
int pop(Stack *stack);
int peek(Stack *stack);
void display(Stack *stack);

int main() {
// Add student details to the output
printf("Raghav Pathak | 2301430130060 | IT-2\n");

Stack stack;
int choice, element;

initialize(&stack);

while (1) {
printf("\n--- Stack Operations ---\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to push: ");
scanf("%d", &element);
push(&stack, element);
break;

case 2:
element = pop(&stack);
if (element != -1) {
printf("Popped element: %d\n", element);
}
break;

case 3:
element = peek(&stack);
if (element != -1) {
printf("Top element: %d\n", element);
}
break;

case 4:
display(&stack);
break;

case 5:
printf("Exiting program.\n");
exit(0);

default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}

// Function to initialize the stack


void initialize(Stack *stack) {
stack->top = -1; // Stack is empty initially
}

// Function to check if the stack is full


int isFull(Stack *stack) {
return stack->top == MAX_SIZE - 1;
}

// Function to check if the stack is empty


int isEmpty(Stack *stack) {
return stack->top == -1;
}

// Function to push an element onto the stack


void push(Stack *stack, int element) {
if (isFull(stack)) {
printf("Stack overflow. Cannot push element.\n");
return;
}
stack->arr[++stack->top] = element;
printf("Element pushed successfully.\n");
}

// Function to pop an element from the stack


int pop(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack underflow. No elements to pop.\n");
return -1;
}
return stack->arr[stack->top--];
}

// Function to get the top element of the stack


int peek(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty. No top element.\n");
return -1;
}
return stack->arr[stack->top];
}

// Function to display all elements in the stack


void display(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements: ");
for (int i = 0; i <= stack->top; i++) {
printf("%d ", stack->arr[i]);
}
printf("\n");
}

Output:
P-3 : Program for Stack implementation through dynamic
memory allocation.
#include <stdio.h>
#include <stdlib.h>

typedef struct {
int *array; // Pointer to the dynamically allocated array
int top; // Index of the top element in the stack
int capacity; // Current capacity of the stack
} Stack;

// Function prototypes
Stack* createStack(int initialCapacity);
int isFull(Stack *stack);
int isEmpty(Stack *stack);
void push(Stack *stack, int element);
int pop(Stack *stack);
int peek(Stack *stack);
void display(Stack *stack);
void freeStack(Stack *stack);

int main() {
// Add student details to the output
printf("Raghav Pathak | 2301430130060 | IT-2\n");

int choice, element;


Stack *stack = createStack(5); // Initial capacity of 5

if (!stack) {
printf("Failed to initialize stack. Exiting...\n");
return -1;
}

while (1) {
printf("\n--- Stack Operations ---\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the element to push: ");
scanf("%d", &element);
push(stack, element);
break;

case 2:
element = pop(stack);
if (element != -1) {
printf("Popped element: %d\n", element);
}
break;

case 3:
element = peek(stack);
if (element != -1) {
printf("Top element: %d\n", element);
}
break;

case 4:
display(stack);
break;

case 5:
freeStack(stack);
printf("Exiting program.\n");
return 0;

default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}

// Function to create a stack with a given initial capacity


Stack* createStack(int initialCapacity) {
Stack *stack = (Stack *)malloc(sizeof(Stack));
if (!stack) {
printf("Memory allocation for stack failed.\n");
return NULL;
}
stack->array = (int *)malloc(initialCapacity * sizeof(int));
if (!stack->array) {
printf("Memory allocation for stack array failed.\n");
free(stack);
return NULL;
}
stack->top = -1;
stack->capacity = initialCapacity;
return stack;
}

// Function to check if the stack is full


int isFull(Stack *stack) {
return stack->top == stack->capacity - 1;
}

// Function to check if the stack is empty


int isEmpty(Stack *stack) {
return stack->top == -1;
}

// Function to push an element onto the stack


void push(Stack *stack, int element) {
if (isFull(stack)) {
// Double the capacity
int newCapacity = stack->capacity * 2;
int *newArray = (int *)realloc(stack->array, newCapacity * sizeof(int));
if (!newArray) {
printf("Memory reallocation failed. Cannot push element.\n");
return;
}
stack->array = newArray;
stack->capacity = newCapacity;
}
stack->array[++stack->top] = element;
printf("Element pushed successfully.\n");
}

// Function to pop an element from the stack


int pop(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack underflow. No elements to pop.\n");
return -1;
}
return stack->array[stack->top--];
}

// Function to get the top element of the stack


int peek(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty. No top element.\n");
return -1;
}
return stack->array[stack->top];
}

// Function to display all elements in the stack


void display(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements: ");
for (int i = 0; i <= stack->top; i++) {
printf("%d ", stack->array[i]);
}
printf("\n");
}

// Function to free the memory allocated for the stack


void freeStack(Stack *stack) {
if (stack) {
free(stack->array);
free(stack);
}
}
Output:
P-4 : Program for queue implementation through array.

#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5 // Maximum size of the queue

typedef struct {
int arr[MAX_SIZE];
int front;
int rear;
} Queue;

// Function prototypes
void initialize(Queue *queue);
int isFull(Queue *queue);
int isEmpty(Queue *queue);
void enqueue(Queue *queue, int element);
int dequeue(Queue *queue);
int peek(Queue *queue);
void display(Queue *queue);

int main() {
// Add student details to the output
printf("Raghav Pathak | 2301430130060 | IT-2\n");

Queue queue;
int choice, element;

initialize(&queue);
while (1) {
printf("\n--- Queue Operations ---\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the element to enqueue: ");
scanf("%d", &element);
enqueue(&queue, element);
break;

case 2:
element = dequeue(&queue);
if (element != -1) {
printf("Dequeued element: %d\n", element);
}
break;

case 3:
element = peek(&queue);
if (element != -1) {
printf("Front element: %d\n", element);
}
break;
case 4:
display(&queue);
break;

case 5:
printf("Exiting program.\n");
exit(0);

default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}

// Function to initialize the queue


void initialize(Queue *queue) {
queue->front = -1;
queue->rear = -1;
}

// Function to check if the queue is full


int isFull(Queue *queue) {
return queue->rear == MAX_SIZE - 1;
}

// Function to check if the queue is empty


int isEmpty(Queue *queue) {
return queue->front == -1;
}

// Function to enqueue an element


void enqueue(Queue *queue, int element) {
if (isFull(queue)) {
printf("Queue overflow. Cannot enqueue element.\n");
return;
}
if (queue->front == -1) { // If queue is empty, set front to 0
queue->front = 0;
}
queue->arr[++queue->rear] = element;
printf("Element enqueued successfully.\n");
}

// Function to dequeue an element


int dequeue(Queue *queue) {
if (isEmpty(queue)) {
printf("Queue underflow. No elements to dequeue.\n");
return -1;
}
int element = queue->arr[queue->front];
if (queue->front == queue->rear) { // If there is only one element
queue->front = queue->rear = -1; // Reset the queue
} else {
queue->front++;
}
return element;
}
// Function to get the front element of the queue
int peek(Queue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty. No front element.\n");
return -1;
}
return queue->arr[queue->front];
}

// Function to display all elements in the queue


void display(Queue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty.\n");
return;
}
printf("Queue elements: ");
for (int i = queue->front; i <= queue->rear; i++) {
printf("%d ", queue->arr[i]);
}
printf("\n");
}
Output:
P-5: Program for Queue Implementation through Dynamic Memory
Allocation.
#include <stdio.h>
#include <stdlib.h>

typedef struct {
int *arr; // Pointer to the dynamically allocated array
int front; // Index of the front element
int rear; // Index of the last element
int capacity; // Current capacity of the queue
} Queue;

// Function prototypes
Queue* createQueue(int initialCapacity);
int isFull(Queue *queue);
int isEmpty(Queue *queue);
void enqueue(Queue *queue, int element);
int dequeue(Queue *queue);
int peek(Queue *queue);
void display(Queue *queue);
void freeQueue(Queue *queue);

int main() {
// Add student details to the output
printf("Raghav Pathak | 2301430130060 | IT-2\n");

int choice, element;


Queue *queue = createQueue(5); // Initial capacity of 5

if (!queue) {
printf("Failed to initialize queue. Exiting...\n");
return -1;
}

while (1) {
printf("\n--- Queue Operations ---\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the element to enqueue: ");
scanf("%d", &element);
enqueue(queue, element);
break;

case 2:
element = dequeue(queue);
if (element != -1) {
printf("Dequeued element: %d\n", element);
}
break;

case 3:
element = peek(queue);
if (element != -1) {
printf("Front element: %d\n", element);
}
break;

case 4:
display(queue);
break;

case 5:
freeQueue(queue);
printf("Exiting program.\n");
return 0;

default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}

// Function to create a queue with a given initial capacity


Queue* createQueue(int initialCapacity) {
Queue *queue = (Queue *)malloc(sizeof(Queue));
if (!queue) {
printf("Memory allocation for queue failed.\n");
return NULL;
}
queue->arr = (int *)malloc(initialCapacity * sizeof(int));
if (!queue->arr) {
printf("Memory allocation for queue array failed.\n");
free(queue);
return NULL;
}
queue->front = -1;
queue->rear = -1;
queue->capacity = initialCapacity;
return queue;
}

// Function to check if the queue is full


int isFull(Queue *queue) {
return queue->rear == queue->capacity - 1;
}

// Function to check if the queue is empty


int isEmpty(Queue *queue) {
return queue->front == -1;
}

// Function to enqueue an element into the queue


void enqueue(Queue *queue, int element) {
if (isFull(queue)) {
// Double the capacity of the queue
int newCapacity = queue->capacity * 2;
int *newArr = (int *)realloc(queue->arr, newCapacity * sizeof(int));
if (!newArr) {
printf("Memory reallocation failed. Cannot enqueue element.\n");
return;
}
queue->arr = newArr;
queue->capacity = newCapacity;
}

if (isEmpty(queue)) {
queue->front = 0;
}
queue->arr[++queue->rear] = element;
printf("Element enqueued successfully.\n");
}

// Function to dequeue an element from the queue


int dequeue(Queue *queue) {
if (isEmpty(queue)) {
printf("Queue underflow. No elements to dequeue.\n");
return -1;
}
int element = queue->arr[queue->front];
if (queue->front == queue->rear) {
// Reset the queue if it's empty
queue->front = queue->rear = -1;
} else {
queue->front++;
}
return element;
}

// Function to get the front element of the queue


int peek(Queue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty. No front element.\n");
return -1;
}
return queue->arr[queue->front];
}

// Function to display all elements in the queue


void display(Queue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty.\n");
return;
}
printf("Queue elements: ");
for (int i = queue->front; i <= queue->rear; i++) {
printf("%d ", queue->arr[i]);
}
printf("\n");
}

// Function to free the memory allocated for the queue


void freeQueue(Queue *queue) {
if (queue) {
free(queue->arr);
free(queue);
}
}
Output:
p-6 : Program for circular queue implementation through array.
#include <stdio.h>
#define SIZE 5 // Define the maximum size of the circular queue

int queue[SIZE];
int front = -1, rear = -1;

// Function to check if the queue is empty


int isEmpty() {
return front == -1;
}

// Function to check if the queue is full


int isFull() {
return (rear + 1) % SIZE == front;
}

// Function to add an element to the queue


void enqueue(int value) {
if (isFull()) {
printf("Queue is full! Cannot enqueue %d\n", value);
return;
}
if (isEmpty()) {
front = rear = 0; // Initialize front and rear if queue is empty
} else {
rear = (rear + 1) % SIZE; // Increment rear circularly
}
queue[rear] = value;
printf("Enqueued %d\n", value);
}

// Function to remove an element from the queue


int dequeue() {
if (isEmpty()) {
printf("Queue is empty! Cannot dequeue.\n");
return -1;
}
int value = queue[front];
if (front == rear) {
// If only one element is present, reset front and rear
front = rear = -1;
} else {
front = (front + 1) % SIZE; // Increment front circularly
}
printf("Dequeued %d\n", value);
return value;
}

// Function to display the queue elements


void display() {
if (isEmpty()) {
printf("Queue is empty!\n");
return;
}
printf("Queue elements: ");
int i = front;
while (1) {
printf("%d ", queue[i]);
if (i == rear) break;
i = (i + 1) % SIZE;
}
printf("\n");
}

// Main function to test the circular queue


int main() {
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50); // Queue is now full
display();

dequeue();
dequeue();
display();

enqueue(60); // Adds an element after dequeuing


enqueue(70); // Adds another element
display();

return 0;
}
Output:
p-7: Program for circular queue implementation through dynamic memory
allocation.

You might also like