0% found this document useful (0 votes)
12 views

dslab

pplouh

Uploaded by

PK Ki Vines
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)
12 views

dslab

pplouh

Uploaded by

PK Ki Vines
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/ 65

LAB PRACTICAL FILE CP

NAME: RADHA RAMAN

ENROLLMENT NO.: 2023BITE065

SEMESTER: 3rd

SUBMITTED TO: DR. DEEBHA MUMTAAZ

DATE:25/11/2024
LAB PRACTICAL FILE CP

INDEX
LAB PRACTICAL FILE CP

PROGRAM 1: OPERATIONS IN SINGLY LINKED LIST

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* createNode(int data) {

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

newNode->data = data;

newNode->next = NULL;

return newNode;

struct Node* insertAtBeginning(struct Node* head, int data) {

struct Node* newNode = createNode(data);

newNode->next = head;

return newNode;

struct Node* insertAtEnd(struct Node* head, int data) {

struct Node* newNode = createNode(data);

if (head == NULL) return newNode;

struct Node* temp = head;

while (temp->next != NULL) temp = temp->next;

temp->next = newNode;

return head;

}
LAB PRACTICAL FILE CP

struct Node* deleteNode(struct Node* head, int key) {

if (head == NULL) return NULL;

if (head->data == key) {

struct Node* temp = head;

head = head->next;

free(temp);

return head;

struct Node* temp = head;

while (temp->next != NULL && temp->next->data != key) temp = temp->next;

if (temp->next == NULL) return head;

struct Node* toDelete = temp->next;

temp->next = temp->next->next;

free(toDelete);

return head;

void traverseList(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

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

temp = temp->next;

printf("NULL\n");

int main() {

struct Node* head = NULL;

head = insertAtBeginning(head, 10);

head = insertAtBeginning(head, 20);

head = insertAtEnd(head, 30);


LAB PRACTICAL FILE CP

traverseList(head);

head = deleteNode(head, 20);

traverseList(head);

return 0;

OUTPUT
LAB PRACTICAL FILE CP

PROGRAM 2: DOUBLY LINKED LIST

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

struct Node* prev;

};

struct Node* createNode(int data) {

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

newNode->data = data;

newNode->next = NULL;

newNode->prev = NULL;

return newNode;

struct Node* insertAtBeginning(struct Node* head, int data) {

struct Node* newNode = createNode(data);

if (head != NULL) head->prev = newNode;

newNode->next = head;

return newNode;

struct Node* insertAtEnd(struct Node* head, int data) {

struct Node* newNode = createNode(data);

if (head == NULL) return newNode;

struct Node* temp = head;

while (temp->next != NULL) temp = temp->next;

temp->next = newNode;
LAB PRACTICAL FILE CP

newNode->prev = temp;

return head;

struct Node* deleteNode(struct Node* head, int key) {

struct Node* temp = head;

while (temp != NULL && temp->data != key) temp = temp->next;

if (temp == NULL) return head;

if (temp->prev != NULL) temp->prev->next = temp->next;

else head = temp->next;

if (temp->next != NULL) temp->next->prev = temp->prev;

free(temp);

return head;

void traverseList(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

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

temp = temp->next;

printf("NULL\n");

int main() {

struct Node* head = NULL;

head = insertAtBeginning(head, 10);

head = insertAtBeginning(head, 20);

head = insertAtEnd(head, 30);

traverseList(head);

head = deleteNode(head, 20);


LAB PRACTICAL FILE CP

traverseList(head);

return 0;

OUTPUT:
LAB PRACTICAL FILE CP

PROGRAM 3: CIRCULAR LINKED LIST

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* createNode(int data) {

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

newNode->data = data;

newNode->next = newNode;

return newNode;

struct Node* insertAtBeginning(struct Node* head, int data) {

struct Node* newNode = createNode(data);

if (head == NULL) return newNode;

struct Node* temp = head;

while (temp->next != head) temp = temp->next;

newNode->next = head;

temp->next = newNode;

return newNode;

struct Node* insertAtEnd(struct Node* head, int data) {

struct Node* newNode = createNode(data);

if (head == NULL) return newNode;

struct Node* temp = head;

while (temp->next != head) temp = temp->next;


LAB PRACTICAL FILE CP

temp->next = newNode;

newNode->next = head;

return head;

struct Node* deleteNode(struct Node* head, int key) {

if (head == NULL) return NULL;

if (head->data == key && head->next == head) {

free(head);

return NULL;

struct Node* temp = head;

struct Node* prev = NULL;

while (temp->data != key) {

if (temp->next == head) return head;

prev = temp;

temp = temp->next;

if (temp == head) {

struct Node* last = head;

while (last->next != head) last = last->next;

head = head->next;

last->next = head;

} else {

prev->next = temp->next;

free(temp);

return head;

void traverseList(struct Node* head) {


LAB PRACTICAL FILE CP

if (head == NULL) {

printf("List is empty\n");

return;

struct Node* temp = head;

do {

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

temp = temp->next;

} while (temp != head);

printf("\n");

int main() {

struct Node* head = NULL;

head = insertAtBeginning(head, 10);

head = insertAtBeginning(head, 20);

head = insertAtEnd(head, 30);

traverseList(head);

head = deleteNode(head, 20);

traverseList(head);

return 0;

OUTPUT:
LAB PRACTICAL FILE CP

PROGRAM 4 : HEADER LINKED LIST

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* createHeader() {

struct Node* header = (struct Node*)malloc(sizeof(struct Node));

header->next = NULL;

return header;

struct Node* createNode(int data) {

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

newNode->data = data;

newNode->next = NULL;

return newNode;

void insertAtBeginning(struct Node* header, int data) {

struct Node* newNode = createNode(data);

newNode->next = header->next;

header->next = newNode;

void insertAtEnd(struct Node* header, int data) {

struct Node* newNode = createNode(data);

struct Node* temp = header;


LAB PRACTICAL FILE CP

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

void deleteNode(struct Node* header, int key) {

struct Node* temp = header;

while (temp->next != NULL && temp->next->data != key) {

temp = temp->next;

if (temp->next != NULL) {

struct Node* toDelete = temp->next;

temp->next = temp->next->next;

free(toDelete);

void traverseList(struct Node* header) {

struct Node* temp = header->next;

while (temp != NULL) {

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

temp = temp->next;

printf("NULL\n");

int main() {

struct Node* header = createHeader();

insertAtBeginning(header, 10);

insertAtBeginning(header, 20);
LAB PRACTICAL FILE CP

insertAtEnd(header, 30);

traverseList(header);

deleteNode(header, 20);

traverseList(header);

return 0;

OUTPUT:
LAB PRACTICAL FILE CP

PROGRAM 5 : TO STORE POLYNOMIAL USING LINKED LIST

#include <stdio.h>

#include <stdlib.h>

struct Term {

int coeff;

int exp;

struct Term* next;

};

struct Term* createTerm(int coeff, int exp) {

struct Term* newTerm = (struct Term*)malloc(sizeof(struct Term));

newTerm->coeff = coeff;

newTerm->exp = exp;

newTerm->next = NULL;

return newTerm;

void insertTerm(struct Term** head, int coeff, int exp) {

struct Term* newTerm = createTerm(coeff, exp);

if (*head == NULL || (*head)->exp < exp) {

newTerm->next = *head;

*head = newTerm;

} else {

struct Term* temp = *head;

while (temp->next != NULL && temp->next->exp > exp) {

temp = temp->next;

if (temp->next != NULL && temp->next->exp == exp) {

temp->next->coeff += coeff;

free(newTerm);
LAB PRACTICAL FILE CP

} else {

newTerm->next = temp->next;

temp->next = newTerm;

void displayPolynomial(struct Term* head) {

struct Term* temp = head;

while (temp != NULL) {

if (temp->coeff > 0 && temp != head) printf("+");

printf("%dx^%d ", temp->coeff, temp->exp);

temp = temp->next;

printf("\n");

int main() {

struct Term* poly = NULL;

insertTerm(&poly, 5, 2);

insertTerm(&poly, 3, 1);

insertTerm(&poly, 4, 3);

insertTerm(&poly, 2, 0);

displayPolynomial(poly);

insertTerm(&poly, 6, 1);

displayPolynomial(poly);

return 0;
LAB PRACTICAL FILE CP

OUTPUT:
LAB PRACTICAL FILE CP

PROGRAM 6 :

#include <stdio.h>

#include <stdlib.h>

#define MAX 10

#define K 3

int arr[MAX];

int top[K];

int isFull(int stackNum) {

return top[stackNum] == MAX / K;

int isEmpty(int stackNum) {

return top[stackNum] == -1;

void push(int stackNum, int value) {

if (isFull(stackNum)) {

printf("Stack %d is full\n", stackNum);

return;

arr[stackNum * (MAX / K) + top[stackNum]] = value;

top[stackNum]++;

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

int pop(int stackNum) {

if (isEmpty(stackNum)) {

printf("Stack %d is empty\n", stackNum);


LAB PRACTICAL FILE CP

return -1;

top[stackNum]--;

return arr[stackNum * (MAX / K) + top[stackNum] + 1];

int peek(int stackNum) {

if (isEmpty(stackNum)) {

printf("Stack %d is empty\n", stackNum);

return -1;

return arr[stackNum * (MAX / K) + top[stackNum]];

void display(int stackNum) {

if (isEmpty(stackNum)) {

printf("Stack %d is empty\n", stackNum);

return;

printf("Stack %d: ", stackNum);

for (int i = 0; i <= top[stackNum]; i++) {

printf("%d ", arr[stackNum * (MAX / K) + i]);

printf("\n");

int main() {

for (int i = 0; i < K; i++) top[i] = -1;

push(0, 10);

push(1, 20);
LAB PRACTICAL FILE CP

push(2, 30);

push(0, 40);

push(1, 50);

push(2, 60);

display(0);

display(1);

display(2);

printf("Popped from stack 1: %d\n", pop(1));

display(1);

printf("Top of stack 2: %d\n", peek(2));

return 0;

OUTPUT:
LAB PRACTICAL FILE CP

PROGRAM 7: STACK USING LINKED LIST

#include <stdio.h>

#include <stdlib.h>

#define MAX 3

struct Node {

int data;

struct Node* next;

};

struct Stack {

struct Node* top;

};

void initialize(struct Stack* stacks) {

for (int i = 0; i < MAX; i++) {

stacks[i].top = NULL;

int isEmpty(struct Stack* stack) {

return stack->top == NULL;

void push(struct Stack* stack, int value) {

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

newNode->data = value;

newNode->next = stack->top;

stack->top = newNode;

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


LAB PRACTICAL FILE CP

int pop(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack is empty\n");

return -1;

struct Node* temp = stack->top;

int poppedValue = temp->data;

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

free(temp);

return poppedValue;

int peek(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack is empty\n");

return -1;

return stack->top->data;

void display(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack is empty\n");

return;

struct Node* temp = stack->top;

printf("Stack: ");

while (temp != NULL) {

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


LAB PRACTICAL FILE CP

temp = temp->next;

printf("\n");

int main() {

struct Stack stacks[MAX];

initialize(stacks);

push(&stacks[0], 10);

push(&stacks[1], 20);

push(&stacks[2], 30);

push(&stacks[0], 40);

push(&stacks[1], 50);

push(&stacks[2], 60);

display(&stacks[0]);

display(&stacks[1]);

display(&stacks[2]);

printf("Popped from stack 1: %d\n", pop(&stacks[1]));

display(&stacks[1]);

printf("Top of stack 2: %d\n", peek(&stacks[2]));

return 0;

}
LAB PRACTICAL FILE CP
LAB PRACTICAL FILE CP

PROGRAM 7 : TO CHECK NESTING OF PARENTHESIS

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

struct Stack {

char arr[MAX];

int top;

};

void initialize(struct Stack* stack) {

stack->top = -1;

int isFull(struct Stack* stack) {

return stack->top == MAX - 1;

int isEmpty(struct Stack* stack) {

return stack->top == -1;

void push(struct Stack* stack, char value) {

if (isFull(stack)) {

printf("Stack Overflow\n");

return;

stack->arr[++stack->top] = value;

}
LAB PRACTICAL FILE CP

char pop(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack Underflow\n");

return -1;

return stack->arr[stack->top--];

int isMatchingPair(char opening, char closing) {

if (opening == '(' && closing == ')') return 1;

if (opening == '{' && closing == '}') return 1;

if (opening == '[' && closing == ']') return 1;

return 0;

int checkParenthesesBalance(char* expr) {

struct Stack stack;

initialize(&stack);

for (int i = 0; expr[i] != '\0'; i++) {

if (expr[i] == '(' || expr[i] == '{' || expr[i] == '[') {

push(&stack, expr[i]);

} else if (expr[i] == ')' || expr[i] == '}' || expr[i] == ']') {

if (isEmpty(&stack)) {

return 0;

char top = pop(&stack);

if (!isMatchingPair(top, expr[i])) {

return 0;

}
LAB PRACTICAL FILE CP

if (isEmpty(&stack)) {

return 1;

} else {

return 0;

int main() {

char expr[MAX];

printf("Enter an expression: ");

scanf("%s", expr);

if (checkParenthesesBalance(expr)) {

printf("The parentheses are balanced.\n");

} else {

printf("The parentheses are unbalanced.\n");

return 0;

}
LAB PRACTICAL FILE CP
LAB PRACTICAL FILE CP

PROGRAM 8: INFIX TO POSTFIX

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#define MAX 100

struct Stack {

char arr[MAX];

int top;

};

void initialize(struct Stack* stack) {

stack->top = -1;

int isFull(struct Stack* stack) {

return stack->top == MAX - 1;

int isEmpty(struct Stack* stack) {

return stack->top == -1;

void push(struct Stack* stack, char value) {

if (isFull(stack)) {

printf("Stack Overflow\n");

return;

stack->arr[++stack->top] = value;

}
LAB PRACTICAL FILE CP

char pop(struct Stack* stack) {

if (isEmpty(stack)) {

return -1;

return stack->arr[stack->top--];

char peek(struct Stack* stack) {

if (isEmpty(stack)) {

return -1;

return stack->arr[stack->top];

int precedence(char c) {

if (c == '+' || c == '-') return 1;

if (c == '*' || c == '/') return 2;

if (c == '^') return 3;

return 0;

int isOperator(char c) {

return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');

void infixToPostfix(char* infix, char* postfix) {

struct Stack stack;

initialize(&stack);

int i = 0, j = 0;
LAB PRACTICAL FILE CP

while (infix[i] != '\0') {

if (isalnum(infix[i])) {

postfix[j++] = infix[i]; // If operand, add it to postfix expression

} else if (infix[i] == '(') {

push(&stack, infix[i]); // If '(', push it to the stack

} else if (infix[i] == ')') {

while (!isEmpty(&stack) && peek(&stack) != '(') {

postfix[j++] = pop(&stack); // Pop from stack until '(' is found

pop(&stack); // Remove '(' from the stack

} else if (isOperator(infix[i])) {

while (!isEmpty(&stack) && precedence(peek(&stack)) >= precedence(infix[i])) {

postfix[j++] = pop(&stack);

push(&stack, infix[i]);

i++;

while (!isEmpty(&stack)) {

postfix[j++] = pop(&stack);

postfix[j] = '\0';

int main() {

char infix[MAX], postfix[MAX];

printf("Enter infix expression: ");

scanf("%s", infix);
LAB PRACTICAL FILE CP

infixToPostfix(infix, postfix);

printf("Postfix expression: %s\n", postfix);

return 0;

}
LAB PRACTICAL FILE CP

PROGRAM 8 :INFIX TO PREFIX

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <string.h>

#define MAX 100

struct Stack {

char arr[MAX];

int top;

};

void initialize(struct Stack* stack) {

stack->top = -1;

int isFull(struct Stack* stack) {

return stack->top == MAX - 1;

int isEmpty(struct Stack* stack) {

return stack->top == -1;

void push(struct Stack* stack, char value) {

if (isFull(stack)) {

printf("Stack Overflow\n");

return;

stack->arr[++stack->top] = value;
LAB PRACTICAL FILE CP

char pop(struct Stack* stack) {

if (isEmpty(stack)) {

return -1;

return stack->arr[stack->top--];

char peek(struct Stack* stack) {

if (isEmpty(stack)) {

return -1;

return stack->arr[stack->top];

int precedence(char c) {

if (c == '+' || c == '-') return 1;

if (c == '*' || c == '/') return 2;

if (c == '^') return 3;

return 0;

int isOperator(char c) {

return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');

void reverse(char* exp) {

int length = strlen(exp);

for (int i = 0; i < length / 2; i++) {

char temp = exp[i];


LAB PRACTICAL FILE CP

exp[i] = exp[length - 1 - i];

exp[length - 1 - i] = temp;

void infixToPrefix(char* infix, char* prefix) {

struct Stack stack;

initialize(&stack);

int i = 0, j = 0;

reverse(infix);

while (infix[i] != '\0') {

if (isalnum(infix[i])) {

prefix[j++] = infix[i];

} else if (infix[i] == ')') {

push(&stack, infix[i]);

} else if (infix[i] == '(') {

while (!isEmpty(&stack) && peek(&stack) != ')') {

prefix[j++] = pop(&stack);

pop(&stack);

} else if (isOperator(infix[i])) {

while (!isEmpty(&stack) && precedence(peek(&stack)) > precedence(infix[i])) {

prefix[j++] = pop(&stack);

push(&stack, infix[i]);

i++;

while (!isEmpty(&stack)) {
LAB PRACTICAL FILE CP

prefix[j++] = pop(&stack);

prefix[j] = '\0';

reverse(prefix);

int main() {

char infix[MAX], prefix[MAX];

printf("Enter infix expression: ");

scanf("%s", infix);

infixToPrefix(infix, prefix);

printf("Prefix expression: %s\n", prefix);

return 0;

}
LAB PRACTICAL FILE CP

PROGRAM 9: LINEAR QUEUE USING ARRAY

#include <stdio.h>

#include <stdlib.h>

#define MAX 5

struct Queue {

int arr[MAX];

int front;

int rear;

};

void initialize(struct Queue* q) {

q->front = -1;

q->rear = -1;

int isFull(struct Queue* q) {

return q->rear == MAX - 1;

int isEmpty(struct Queue* q) {

return q->front == -1 || q->front > q->rear;

void enqueue(struct Queue* q, int value) {

if (isFull(q)) {

printf("Queue Overflow\n");

return;

if (q->front == -1) {
LAB PRACTICAL FILE CP

q->front = 0;

q->arr[++(q->rear)] = value;

int dequeue(struct Queue* q) {

if (isEmpty(q)) {

printf("Queue Underflow\n");

return -1;

int value = q->arr[q->front++];

if (q->front > q->rear) {

q->front = q->rear = -1;

return value;

void display(struct Queue* q) {

if (isEmpty(q)) {

printf("Queue is empty\n");

return;

for (int i = q->front; i <= q->rear; i++) {

printf("%d ", q->arr[i]);

printf("\n");

int main() {

struct Queue q;

initialize(&q);
LAB PRACTICAL FILE CP

enqueue(&q, 10);

enqueue(&q, 20);

enqueue(&q, 30);

enqueue(&q, 40);

enqueue(&q, 50);

printf("Queue after enqueues: ");

display(&q);

printf("Dequeued element: %d\n", dequeue(&q));

printf("Queue after dequeue: ");

display(&q);

enqueue(&q, 60);

printf("Queue after enqueue 60: ");

display(&q);

return 0;

}
LAB PRACTICAL FILE CP
LAB PRACTICAL FILE CP

PROGRAM 10: QUEUE USING LINKED LIST

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Queue {

struct Node* front;

struct Node* rear;

};

void initialize(struct Queue* q) {

q->front = NULL;

q->rear = NULL;

int isEmpty(struct Queue* q) {

return q->front == NULL;

void enqueue(struct Queue* q, 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;
LAB PRACTICAL FILE CP

if (q->rear == NULL) {

q->front = q->rear = newNode;

} else {

q->rear->next = newNode;

q->rear = newNode;

int dequeue(struct Queue* q) {

if (isEmpty(q)) {

printf("Queue Underflow\n");

return -1;

struct Node* temp = q->front;

int value = temp->data;

q->front = q->front->next;

if (q->front == NULL) {

q->rear = NULL;

free(temp);

return value;

void display(struct Queue* q) {

if (isEmpty(q)) {

printf("Queue is empty\n");

return;

struct Node* temp = q->front;

while (temp != NULL) {

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


LAB PRACTICAL FILE CP

temp = temp->next;

printf("\n");

int main() {

struct Queue q;

initialize(&q);

enqueue(&q, 10);

enqueue(&q, 20);

enqueue(&q, 30);

enqueue(&q, 40);

printf("Queue after enqueues: ");

display(&q);

printf("Dequeued element: %d\n", dequeue(&q));

printf("Queue after dequeue: ");

display(&q);

enqueue(&q, 50);

printf("Queue after enqueue 50: ");

display(&q);

return 0;

}
LAB PRACTICAL FILE CP
LAB PRACTICAL FILE CP

PROGRAM 11:PRIOTITY QUEUE

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

int priority;

struct Node* next;

};

struct PriorityQueue {

struct Node* front;

};

void initialize(struct PriorityQueue* pq) {

pq->front = NULL;

int isEmpty(struct PriorityQueue* pq) {

return pq->front == NULL;

void enqueue(struct PriorityQueue* pq, int value, int priority) {

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

if (!newNode) {

printf("Memory allocation failed\n");

return;

newNode->data = value;

newNode->priority = priority;

newNode->next = NULL;
LAB PRACTICAL FILE CP

if (isEmpty(pq) || pq->front->priority > priority) {

newNode->next = pq->front;

pq->front = newNode;

} else {

struct Node* temp = pq->front;

while (temp->next != NULL && temp->next->priority <= priority) {

temp = temp->next;

newNode->next = temp->next;

temp->next = newNode;

int dequeue(struct PriorityQueue* pq) {

if (isEmpty(pq)) {

printf("Priority Queue Underflow\n");

return -1;

struct Node* temp = pq->front;

int value = temp->data;

pq->front = pq->front->next;

free(temp);

return value;

void display(struct PriorityQueue* pq) {

if (isEmpty(pq)) {

printf("Priority Queue is empty\n");

return;

}
LAB PRACTICAL FILE CP

struct Node* temp = pq->front;

printf("Priority Queue:\n");

while (temp != NULL) {

printf("Value: %d, Priority: %d\n", temp->data, temp->priority);

temp = temp->next;

int main() {

struct PriorityQueue pq;

initialize(&pq);

enqueue(&pq, 10, 2);

enqueue(&pq, 20, 1);

enqueue(&pq, 30, 3);

enqueue(&pq, 40, 0);

printf("Priority Queue after enqueues:\n");

display(&pq);

printf("Dequeued element: %d\n", dequeue(&pq));

printf("Priority Queue after dequeue:\n");

display(&pq);

enqueue(&pq, 50, 1);

printf("Priority Queue after enqueue 50 with priority 1:\n");

display(&pq);

return 0;

}
LAB PRACTICAL FILE CP
LAB PRACTICAL FILE CP

PROGRAM 12:JOSEPHUSN PROBLEM USING CLL

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* createNode(int data) {

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

if (!newNode) {

printf("Memory allocation failed\n");

exit(1);

newNode->data = data;

newNode->next = NULL;

return newNode;

struct Node* createCircularLinkedList(int n) {

struct Node* head = NULL;

struct Node* temp = NULL;

struct Node* newNode;

for (int i = 1; i <= n; i++) {

newNode = createNode(i);

if (head == NULL) {

head = newNode;

head->next = head;

} else {
LAB PRACTICAL FILE CP

temp = head;

while (temp->next != head) {

temp = temp->next;

temp->next = newNode;

newNode->next = head;

return head;

int josephus(struct Node** head, int k) {

struct Node* prev = *head;

struct Node* curr = *head;

while (prev->next != curr) {

prev = prev->next;

while (curr->next != curr) {

for (int count = 1; count < k; count++) {

prev = curr;

curr = curr->next;

prev->next = curr->next;

printf("Person %d is eliminated\n", curr->data);

free(curr);

curr = prev->next;

int survivor = curr->data;


LAB PRACTICAL FILE CP

free(curr);

return survivor;

int main() {

int n, k;

printf("Enter the number of people: ");

scanf("%d", &n);

printf("Enter the step count (k): ");

scanf("%d", &k);

struct Node* head = createCircularLinkedList(n);

int survivor = josephus(&head, k);

printf("The survivor is person %d\n", survivor);

return 0;

}
LAB PRACTICAL FILE CP
LAB PRACTICAL FILE CP

PROGRAM 13: BINARY TREE TRAVERSAL

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* left;

struct Node* right;

};

struct Node* createNode(int data) {

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

if (!newNode) {

printf("Memory allocation failed\n");

exit(1);

newNode->data = data;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

void inOrder(struct Node* root) {

if (root != NULL) {

inOrder(root->left);

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

inOrder(root->right);

void preOrder(struct Node* root) {


LAB PRACTICAL FILE CP

if (root != NULL) {

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

preOrder(root->left);

preOrder(root->right);

void postOrder(struct Node* root) {

if (root != NULL) {

postOrder(root->left);

postOrder(root->right);

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

int main() {

struct Node* root = createNode(1);

root->left = createNode(2);

root->right = createNode(3);

root->left->left = createNode(4);

root->left->right = createNode(5);

printf("In-order traversal: ");

inOrder(root);

printf("\n");

printf("Pre-order traversal: ");

preOrder(root);

printf("\n");

printf("Post-order traversal: ");


LAB PRACTICAL FILE CP

postOrder(root);

printf("\n");

return 0;

}
LAB PRACTICAL FILE CP

PROGRAM 14: BINARY SEARCH TREE USING LINKED LIST

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* left;

struct Node* right;

};

struct Node* createNode(int data) {

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

if (!newNode) {

printf("Memory allocation failed\n");

exit(1);

newNode->data = data;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

struct Node* insert(struct Node* root, int data) {

if (root == NULL) {

return createNode(data);

if (data < root->data) {

root->left = insert(root->left, data);

} else if (data > root->data) {

root->right = insert(root->right, data);

}
LAB PRACTICAL FILE CP

return root;

int search(struct Node* root, int key) {

if (root == NULL) {

return 0;

if (root->data == key) {

return 1;

if (key < root->data) {

return search(root->left, key);

return search(root->right, key);

void inOrder(struct Node* root) {

if (root != NULL) {

inOrder(root->left);

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

inOrder(root->right);

void preOrder(struct Node* root) {

if (root != NULL) {

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

preOrder(root->left);

preOrder(root->right);

}
LAB PRACTICAL FILE CP

void postOrder(struct Node* root) {

if (root != NULL) {

postOrder(root->left);

postOrder(root->right);

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

int main() {

struct Node* root = NULL;

int choice, value, key;

while (1) {

printf("\n1. Insert\n2. Search\n3. In-order Traversal\n4. Pre-order Traversal\n5. Post-order


Traversal\n6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Enter value to search: ");

scanf("%d", &key);

if (search(root, key)) {

printf("Value %d found in the BST.\n", key);


LAB PRACTICAL FILE CP

} else {

printf("Value %d not found in the BST.\n", key);

break;

case 3:

printf("In-order Traversal: ");

inOrder(root);

printf("\n");

break;

case 4:

printf("Pre-order Traversal: ");

preOrder(root);

printf("\n");

break;

case 5:

printf("Post-order Traversal: ");

postOrder(root);

printf("\n");

break;

case 6:

exit(0);

default:

printf("Invalid choice!\n");

return 0;
LAB PRACTICAL FILE CP

OUTPUT:
LAB PRACTICAL FILE CP

PROGRAM 15: BST WIYH INFO ABOUT AUTOMOBILE COMPANY

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct Automobile {

char type[50];

int year;

};

struct Node {

struct Automobile info;

struct Node* left;

struct Node* right;

};

struct Node* createNode(char* type, int year) {

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

if (!newNode) {

printf("Memory allocation failed\n");

exit(1);

strcpy(newNode->info.type, type);

newNode->info.year = year;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

struct Node* insert(struct Node* root, char* type, int year) {

if (root == NULL) {
LAB PRACTICAL FILE CP

return createNode(type, year);

if (year < root->info.year) {

root->left = insert(root->left, type, year);

} else if (year > root->info.year) {

root->right = insert(root->right, type, year);

return root;

struct Node* findMin(struct Node* root) {

while (root && root->left != NULL) {

root = root->left;

return root;

struct Node* deleteNode(struct Node* root, int year) {

if (root == NULL) {

printf("Node with year %d not found\n", year);

return root;

if (year < root->info.year) {

root->left = deleteNode(root->left, year);

} else if (year > root->info.year) {

root->right = deleteNode(root->right, year);

} else {

if (root->left == NULL) {

struct Node* temp = root->right;

free(root);

return temp;
LAB PRACTICAL FILE CP

} else if (root->right == NULL) {

struct Node* temp = root->left;

free(root);

return temp;

struct Node* temp = findMin(root->right);

root->info = temp->info;

root->right = deleteNode(root->right, temp->info.year);

return root;

void inOrder(struct Node* root) {

if (root != NULL) {

inOrder(root->left);

printf("Automobile Type: %s, Year: %d\n", root->info.type, root->info.year);

inOrder(root->right);

int main() {

struct Node* root = NULL;

int choice, year;

char type[50];

while (1) {

printf("\n1. Insert Automobile\n2. Delete Automobile\n3. Display Automobiles (In-order)\n4.


Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);
LAB PRACTICAL FILE CP

switch (choice) {

case 1:

printf("Enter automobile type: ");

scanf(" %[^\n]s", type);

printf("Enter year of manufacture: ");

scanf("%d", &year);

root = insert(root, type, year);

break;

case 2:

printf("Enter year of manufacture to delete: ");

scanf("%d", &year);

root = deleteNode(root, year);

break;

case 3:

printf("Automobiles in the BST:\n");

inOrder(root);

break;

case 4:

exit(0);

default:

printf("Invalid choice!\n");

return 0;

}
LAB PRACTICAL FILE CP

You might also like