Pathak DS
Pathak DS
INDEX
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");
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");
}
}
}
list->size = 0;
list->capacity = initialCapacity;
return 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;
}
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");
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;
}
#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;
}
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");
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;
}
if (isEmpty(queue)) {
queue->front = 0;
}
queue->arr[++queue->rear] = element;
printf("Element enqueued successfully.\n");
}
int queue[SIZE];
int front = -1, rear = -1;
dequeue();
dequeue();
display();
return 0;
}
Output:
p-7: Program for circular queue implementation through dynamic memory
allocation.