DSC Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 59

GURUGRAM UNIVERSITY , GURUGRAM

PRACTICAL FILE
Data Structure using C
CSE – 102 P

Submitted To:- Submitted By:-


Dr. Manvi Breja ANKIT
Assistant Professor In B.TECH [2nd Sem]
Engineering & Technology Roll no. : 231031150009
Department
Program-1: Program in C to implement bubble sort.

#include<stdio.h>

void main()

int arr[30],n,i,j,temp;

printf("enter the size of an array = ");

scanf("%d",&n);

printf("enter the elements of an array\n");

for(i=0;i<n;i++) {

scanf("%d",&arr[i]);

printf("the elements of an array are:-\n");

for(i=0;i<n;i++) {

printf("%d\n",arr[i]);

temp=0;

printf("The sorted array are:-\n");

for(i=0;i<n;i++) {

for(j=0;j<n-i;j++) {

if(arr[j]>arr[j+1]) {

temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

for(i=0;i<n;i++) {

printf("%d\n",arr[i]);

}
Output :-
Program-2: Program in C to implement insertion sort.

#include<stdio.h>

void main()

int arr[30],i,j,temp,n;

temp=0;

printf("Enter the size of an array = ");

scanf("%d",&n);

printf("enter the elements of an array:-\n");

for(i=0;i<n;i++) {

scanf("%d",&arr[i]);

printf("The elements of the array are:-\n");

for(i=0;i<n;i++) {

printf("%d\n",arr[i]);

printf("the sorted array is:-\n");

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

temp=arr[i];

j=i-1;

while(temp<=arr[j]) {

arr[j+1]=arr[j];

j=j-1;

arr[j+1]=temp;

for(i=0;i<n;i++) {

printf("%d\n",arr[i]);

}
Output :-
Program-3: Program in C to implement selection sort.

#include<stdio.h>

void selection_sort(int arr[], int n) {

int i,j,min_index;

for(i=0;i<n-1;i++) {

min_index=i;

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

if(arr[j]<arr[min_index]) {

min_index=j; }

int temp=arr[i];

arr[i]=arr[min_index];

arr[min_index]=temp; }

void main() {

int i;

int my_array[]={37,21,49,11,52,5};

int n=6;

printf("The given array is:-\n");

for(i=0;i<n;i++) {

printf("%d\n",my_array[i]);

selection_sort(my_array,n);

printf("sorted array:\n");

for(i=0;i<n;i++) {

printf("%d\n",my_array[i]);

printf("\n");

}
Output :-
Program-4: Program in C to implement quick sort.

#include <stdio.h>

#define size 100

int partition(int a[], int beg, int end);

void quick_sort(int a[], int beg, int end);

int main() {

int arr[size], i, n;

printf("\n Enter the number of elements in the array: ");

scanf("%d", &n);

printf("\n Enter the elements of the array: ");

for(i=0;i<n;i++) {

scanf("%d", &arr[i]);

quick_sort(arr, 0, n-1);

printf("\n The sorted array is: \n");

for(i=0;i<n;i++) {

printf(" %d\t", arr[i]);

int partition(int a[], int beg, int end) {

int left, right, temp, loc, flag;

loc = left = beg;

right = end;

flag = 0;

while(flag != 1) {

while((a[loc] <= a[right]) && (loc!=right)) {

right--;

if(loc==right) {

flag =1;

}
else if(a[loc]>a[right]) {

temp = a[loc];

a[loc] = a[right];

a[right] = temp;

loc = right;

if(flag!=1) {

while((a[loc] >= a[left]) && (loc!=left)) {

left++;

if(loc==left) {

flag =1; }

else if(a[loc] <a[left]) {

temp = a[loc];

a[loc] = a[left];

a[left] = temp;

loc = left; }

} }

return loc;

void quick_sort(int a[], int beg, int end) {

int loc;

if(beg<end) {

loc = partition(a, beg, end);

quick_sort(a, beg, loc-1);

quick_sort(a, loc+1, end);

}
Output :-
Program-5: Program in C to implement merge sort.

#include <stdio.h>

#define size 100

void merge(int a[], int ARR, int BEG, int END);

void merge_sort(int a[],int, int);

int main() {

int arr[size], i, n;

printf("\n Enter the number of elements in the array : ");

scanf("%d", &n);

printf("\n Enter the elements of the array: ");

for(i=0;i<n;i++) {

scanf("%d", &arr[i]); }

merge_sort(arr, 0, n-1);

printf("\n The sorted array is: \n");

for(i=0;i<n;i++) {

printf(" %d\t", arr[i]); }

void merge(int arr[], int beg, int mid, int end) {

int i=beg, j=mid+1, index=beg, temp[size], k;

while((i<=mid) && (j<=end)) {

if(arr[i] < arr[j]) {

temp[index] = arr[i];

i++; }

else {

temp[index] = arr[j];

j++; }

index++;

if(i>mid) {

while(j<=end) {
int MERGE_SORT ;

int ARR ; int BEG ; int END ; int MID;

MERGE_SORT;

if (BEG < END) {

MID = (BEG + END)/2;

MERGE_SORT;

temp[index] = arr[j];

j++; }

index++;

else {

while(i<=mid) {

temp[index] = arr[i];

i++; }

index++;

for(k=beg;k<index;k++) {

arr[k] = temp[k];

void merge_sort(int arr[], int beg, int end) {

int mid;

if(beg<end) {

mid = (beg+end)/2;

merge_sort(arr, beg, mid);

merge_sort(arr, mid+1, end);

merge(arr, beg, mid, end);

}
Output :-
Program-1: To demonstrate the concept of one dimensional array finding the sum of array
elements .

#include <stdio.h>

#define MAX_SIZE 100

int main()

int arr[MAX_SIZE];

int i, n, sum=0;

printf("Enter size of the array: ");

scanf("%d", &n);

printf("Enter %d elements in the array: ", n);

for(i=0; i<n; i++)

scanf("%d", &arr[i]);

for(i=0; i<n; i++)

sum = sum + arr[i];

printf("Sum of all elements of array = %d", sum);

return 0;

}
Output :-
Program-2: To insert an element in an array .

#include <stdio.h>

int main()

int i, n, num, pos, arr[10];

printf("\n Enter the number of elements in the array : ");

scanf("%d", &n);

for(i=0;i<n;i++)

printf("\n arr[%d] = ", i);

scanf("%d", &arr[i]);

printf("\n Enter the number to be inserted : ");

scanf("%d", &num);

printf("\n Enter the position at which the number has to be added :");

scanf("%d", &pos);

for(i=n -1;i>=pos;i--)

arr[i+1] = arr[i];

arr[pos] = num;

n = n+1;

printf("\n The array after insertion of %d is : ", num);

for(i=0;i<n;i++)

printf("\n arr[%d] = %d", i, arr[i]);

return 0;

}
Output :-
Program-3: To perform addition and multiplication of two matrices.

#include <stdio.h>

void add_matrices (int rows, int cols, int matrix1[][cols], int matrix2[][cols], int result[][cols]) {

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

for (int j = 0; j < cols; j++) {

result[i][j] = matrix1[i][j] + matrix2[i][j];

void multiply_matrices(int rows1, int cols1, int cols2, int matrix1[][cols1], int matrix2[][cols2], int
result[][cols2]) {

int sum;

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

for (int j = 0; j < cols2; j++) {

sum = 0;

for (int k = 0; k < cols1; k++) {

sum += matrix1[i][k] * matrix2[k][j]; }

result[i][j] = sum; }

void print_matrix(int rows, int cols, int matrix[][cols]) {

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

for (int j = 0; j < cols; j++) {

printf("%d\t", matrix[i][j]); }

printf("\n");

int main() {

int rows_a, cols_a, rows_b, cols_b;

printf("Enter the number of rows and columns for matrix A: ");


scanf("%d %d", &rows_a, &cols_a);

printf("Enter the number of rows and columns for matrix B: ");

scanf("%d %d", &rows_b, &cols_b);

if (cols_a != rows_b) {

printf("Number of columns in matrix A must be equal to the number of rows in matrix B for
multiplication.");

return 1;

int matrix_a[rows_a][cols_a], matrix_b[rows_b][cols_b], result_addition[rows_a][cols_a],


result_multiplication[rows_a][cols_b];

printf("Enter elements of matrix A:\n");

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

for (int j = 0; j < cols_a; j++) {

scanf("%d", &matrix_a[i][j]);

printf("Enter elements of matrix B:\n");

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

for (int j = 0; j < cols_b; j++) {

scanf("%d", &matrix_b[i][j]); }

// Addition

printf("Matrix Addition:\n");

add_matrices(rows_a, cols_a, matrix_a, matrix_b, result_addition);

print_matrix(rows_a, cols_a, result_addition);

// Multiplication

printf("\nMatrix Multiplication:\n");

multiply_matrices(rows_a, cols_a, cols_b, matrix_a, matrix_b, result_multiplication);

print_matrix(rows_a, cols_b, result_multiplication);

return 0;

}
Output :-
Program-4: To perform transpose of a matrices.

#include <stdio.h>

void transpose(int rows, int cols, int matrix[][cols], int result[][rows]) {

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

for (int j = 0; j < cols; j++) {

result[j][i] = matrix[i][j]; }

void print_matrix(int rows, int cols, int matrix[][cols]) {

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

for (int j = 0; j < cols; j++) {

printf("%d\t", matrix[i][j]); }

printf("\n"); }

int main() {

int rows, cols;

printf("Enter the number of rows and columns of the matrix: ");

scanf("%d %d", &rows, &cols);

int matrix[rows][cols], result[cols][rows];

printf("Enter the elements of the matrix:\n");

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

for (int j = 0; j < cols; j++) {

scanf("%d", &matrix[i][j]); } }

printf("Original Matrix:\n");

print_matrix(rows, cols, matrix);

transpose(rows, cols, matrix, result);

printf("\nTranspose Matrix:\n");

print_matrix(cols, rows, result);

return 0;

}
Output :-
Program-1: Program to implement stack operations using array.

#include <stdio.h>

#include <stdbool.h>

#define MAX_SIZE 100

// Structure to represent a stack

struct Stack {

int items[MAX_SIZE];

int top; };

// Initialize the stack

void initialize(struct Stack *s) {

s->top = -1; }

// Check if the stack is empty

bool isEmpty(struct Stack *s) {

return s->top == -1;

// Check if the stack is full

bool isFull(struct Stack *s) {

return s->top == MAX_SIZE - 1; }

// Push an element onto the stack

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

if (isFull(s)) {

printf("Stack Overflow! Cannot push element.\n");

return; }

s->items[++s->top] = value;

printf("%d pushed to stack.\n", value); }

// Pop an element from the stack

int pop(struct Stack *s) {

if (isEmpty(s)) {

printf("Stack Underflow! Cannot pop element.\n");

return -1; }
return s->items[s->top--];

int main() {

struct Stack stack;

initialize(&stack);

int choice, value;

while (1) {

printf("\nStack Operations:\n");

printf("1. Push\n");

printf("2. Pop\n");

printf("3. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the value to push: ");

scanf("%d", &value);

push(&stack, value);

break;

case 2:

printf("Popped element: %d\n", pop(&stack));

break;

case 3:

printf("Exiting program.\n");

return 0;

default:

printf("Invalid choice! Please enter a valid option.\n"); }

return 0;

}
Output :-
Program-2: Program to evaluate postfix expression using stack.

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#define MAX_SIZE 100

// Structure to represent a stack

struct Stack {

int items[MAX_SIZE];

int top; };

void initialize(struct Stack *s) {

s->top = -1; }

int isEmpty(struct Stack *s) {

return s->top == -1; }

int isFull(struct Stack *s) {

return s->top == MAX_SIZE - 1; }

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

if (isFull(s)) {

printf("Stack Overflow! Cannot push element.\n");

exit(EXIT_FAILURE); }

s->items[++s->top] = value; }

int pop(struct Stack *s) {

if (isEmpty(s)) {

printf("Stack Underflow! Cannot pop element.\n");

exit(EXIT_FAILURE); }

return s->items[s->top--]; }

// Evaluate postfix expression

int evaluatePostfix(char *exp) {

struct Stack stack;

initialize(&stack);

for (int i = 0; exp[i]; ++i) {


if (isdigit(exp[i])) {

push(&stack, exp[i] - '0'); }

else {

int val1 = pop(&stack);

int val2 = pop(&stack);

switch (exp[i]) {

case '+':

push(&stack, val2 + val1);

break;

case '-':

push(&stack, val2 - val1);

break;

case '*':

push(&stack, val2 * val1);

break;

case '/':

push(&stack, val2 / val1);

break; }

return pop(&stack);

int main() {

char exp[MAX_SIZE];

printf("Enter postfix expression: ");

fgets(exp, sizeof(exp), stdin);

int result = evaluatePostfix(exp);

printf("Result: %d\n", result);

return 0;

}
Output :-
Program-3: Program to perform infix to postfix conversion using stack.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#define MAX_SIZE 100

char stack[MAX_SIZE];

int top = -1;

void push(char item) {

if (top == MAX_SIZE - 1) {

printf("Stack Overflow\n");

return; }

stack[++top] = item; }

char pop() {

if (top == -1) {

printf("Stack Underflow\n");

exit(1); }

return stack[top--]; }

int isOperator(char symbol) {

if (symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol == '-') {

return 1; }

return 0; }

int precedence(char symbol) {

if (symbol == '^') {

return 3; }

else if (symbol == '*' || symbol == '/') {

return 2; }

else if (symbol == '+' || symbol == '-') {

return 1; }

else {
return 0; } }

void infixToPostfix(char infix[], char postfix[]) {

int i, j = 0; char symbol;

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

symbol = infix[i];

if (isalnum(symbol)) {

postfix[j++] = symbol;}

else if (symbol == '(') {

push(symbol); }

else if (symbol == ')') {

while (top != -1 && stack[top] != '(') {

postfix[j++] = pop(); }

if (top == -1) {

printf("Invalid expression\n");

exit(1); }

pop(); }

else if (isOperator(symbol)) {

while (top != -1 && precedence(stack[top]) >= precedence(symbol)) {

postfix[j++] = pop(); }

push(symbol); } }

while (top != -1) {

if (stack[top] == '(') {

printf("Invalid expression\n");

exit(1); }

postfix[j++] = pop(); }

postfix[j] = '\0'; }

int main() {

char infix[MAX_SIZE], postfix[MAX_SIZE];

printf("Enter an infix expression: ");

fgets(infix, MAX_SIZE, stdin);


infix[strlen(infix) - 1] = '\0'; // removing newline character

infixToPostfix(infix, postfix);

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

return 0; }
Output :-
Program-1: Program to implement queue operations using array.

#include <stdio.h>

#define MAX_SIZE 100

struct Queue {

int items[MAX_SIZE];

int front;

int rear;};

void createQueue(struct Queue* queue) {

queue->front = -1;

queue->rear = -1;}

int isFull(struct Queue* queue) {

return queue->rear == MAX_SIZE - 1;}

int isEmpty(struct Queue* queue) {

return queue->front == -1;}

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

if (isFull(queue)) {

printf("Queue is full!\n");

return; }

if (isEmpty(queue)) {

queue->front = 0;}

queue->rear++;

queue->items[queue->rear] = value;}

int dequeue(struct Queue* queue) {

if (isEmpty(queue)) {

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

return -1;}

int removedItem = queue->items[queue->front];

if (queue->front >= queue->rear) {

queue->front = -1;

queue->rear = -1; }
else {

queue->front++;}

return removedItem;}

void display(struct 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->items[i]);}

printf("\n");}

int main() {

struct Queue queue;

createQueue(&queue);

int choice, element;

do {printf("1. Enqueue\n");

printf("2. Dequeue\n");

printf("3. Display\n");

printf("4. 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:

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

break;
case 3:

display(&queue);

break;

case 4:

printf("Exiting...\n");

break;

default:

printf("Invalid choice!\n");}

while (choice != 4);

return 0;

}
Output :-
Program-2: Program to implement circular queue using array.

#include <stdio.h>

#define MAX_SIZE 5

struct CircularQueue {

int items[MAX_SIZE];

int front;

int rear;};

void createCircularQueue(struct CircularQueue* queue) {

queue->front = -1;

queue->rear = -1;}

int isFull(struct CircularQueue* queue) {

return (queue->front == 0 && queue->rear == MAX_SIZE - 1) || (queue->rear == (queue->front - 1) %


(MAX_SIZE - 1));}

int isEmpty(struct CircularQueue* queue) {

return queue->front == -1;}

void enqueue(struct CircularQueue* queue, int value) {

if (isFull(queue)) {

printf("Queue is full!\n");

return;}

if (queue->front == -1) {

queue->front = 0;}

queue->rear = (queue->rear + 1) % MAX_SIZE;

queue->items[queue->rear] = value;

int dequeue(struct CircularQueue* queue) {

if (isEmpty(queue)) {

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

return -1;}

int removedItem = queue->items[queue->front];

if (queue->front == queue->rear) {

queue->front = -1;
queue->rear = -1;}

else {

queue->front = (queue->front + 1) % MAX_SIZE;}

return removedItem;}

void display(struct CircularQueue* queue) {

if (isEmpty(queue)) {

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

return; }

printf("Queue elements: ");

if (queue->rear >= queue->front) {

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

printf("%d ", queue->items[i]); } }

else {

for (int i = queue->front; i < MAX_SIZE; i++) {

printf("%d ", queue->items[i]);

for (int i = 0; i <= queue->rear; i++) {

printf("%d ", queue->items[i]);} }

printf("\n");}

int main() {

struct CircularQueue queue;

createCircularQueue(&queue);

int choice, element;

do {printf("\n1. Enqueue\n");

printf("2. Dequeue\n");

printf("3. Display\n");

printf("4. 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:

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

break;

case 3:

display(&queue);

break;

case 4:

printf("Exiting...\n");

break;

default:

printf("Invalid choice!\n");}

} while (choice != 4);

return 0;}
Output :-
Program-1: Program to perform following operations on singly linked list :

(a) Create a singly linked list comprising four nodes.

(b) Insert a node at the end of the linked list.

(c) Insert a node at the start of the linked list.

(d) Delete the last node of the linked list.

(e) Search the 3rd node of the linked list and print the value of that node.

#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 == NULL) {

printf("Memory allocation failed\n");

exit(1);}

newNode->data = data;

newNode->next = NULL;

return newNode;}

void insertAtEnd(struct Node** headRef, int newData) {

struct Node* newNode = createNode(newData);

if (*headRef == NULL) {

*headRef = newNode;

return;}

struct Node* last = *headRef;

while (last->next != NULL) {

last = last->next;}

last->next = newNode;}

void insertAtBeginning(struct Node** headRef, int newData) {


struct Node* newNode = createNode(newData);

newNode->next = *headRef;

*headRef = newNode;}

void deleteLastNode(struct Node** headRef) {

if (*headRef == NULL) {

printf("List is empty\n");

return;}

if ((*headRef)->next == NULL) {

free(*headRef);

*headRef = NULL;

return;}

struct Node* secondLast = *headRef;

while (secondLast->next->next != NULL) {

secondLast = secondLast->next;}

free(secondLast->next);

secondLast->next = NULL;}

void searchAndPrint(struct Node* head, int position) {

struct Node* current = head;

int count = 0;

while (current != NULL) {

if (count == position) {

printf("Value of node at position %d: %d\n", position, current->data);

return;}

current = current->next;

count++;}

printf("Node at position %d not found\n", position);}

void displayList(struct Node* head) {

struct Node* current = head;

if (current == NULL) {

printf("List is empty\n");
return;}

printf("Linked list elements: ");

while (current != NULL) {

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

current = current->next;}

printf("\n");}

int main() {

struct Node* head = NULL;

int data;

printf("Enter data for 4 nodes to create the linked list:\n");

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

printf("Enter data for node %d: ", i + 1);

scanf("%d", &data);

insertAtEnd(&head, data);}

displayList(head);

printf("Enter data to insert at the beginning of the linked list: ");

scanf("%d", &data);

insertAtBeginning(&head, data);

displayList(head);

char choice;

printf("Do you want to delete the last node of the linked list? (y/n): ");

scanf(" %c", &choice);

if (choice == 'y' || choice == 'Y') {

deleteLastNode(&head);

displayList(head);}

int position;

printf("Enter the position of the node to search (0-based indexing): ");

scanf("%d", &position);

searchAndPrint(head, position);

return 0; }
Output :-
Program-2: Program to implement stack using 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));

if (newNode == NULL) {

printf("Memory allocation failed\n");

exit(1); }

newNode->data = data;

newNode->next = NULL;

return newNode;}

struct Stack {

struct Node* top;};

void initializeStack(struct Stack* stack) {

stack->top = NULL;}

int isEmpty(struct Stack* stack) {

return stack->top == NULL;}

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

struct Node* newNode = createNode(newData);

newNode->next = stack->top;

stack->top = newNode;}

int pop(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack underflow\n");

return -1; }

struct Node* temp = stack->top;

int poppedData = temp->data;


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

free(temp);

return poppedData;}

void displayStack(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack is empty\n");

return;}

struct Node* current = stack->top;

printf("Stack elements: ");

while (current != NULL) {

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

current = current->next;}

printf("\n");}

int main() {

struct Stack stack;

initializeStack(&stack);

int choice, data;

do { printf("\n1. Push\n");

printf("2. Pop\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter data to push: ");

scanf("%d", &data);

push(&stack, data);

break;

case 2:
printf("Popped element: %d\n", pop(&stack));

break;

case 3:

displayStack(&stack);

break;

case 4:

printf("Exiting...\n");

break;

default:

printf("Invalid choice!\n");}

} while (choice != 4);

return 0;

}
Output :-
Program-3: Program to implement queue using 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));

if (newNode == NULL) {

printf("Memory allocation failed\n");

exit(1);}

newNode->data = data;

newNode->next = NULL;

return newNode;}

struct Queue {

struct Node* front;

struct Node* rear;};

void initializeQueue(struct Queue* queue) {

queue->front = NULL;

queue->rear = NULL;}

int isEmpty(struct Queue* queue) {

return queue->front == NULL;}

void enqueue(struct Queue* queue, int newData) {

struct Node* newNode = createNode(newData);

if (isEmpty(queue)) {

queue->front = newNode;

queue->rear = newNode;

return;}

queue->rear->next = newNode;

queue->rear = newNode;}
int dequeue(struct Queue* queue) {

if (isEmpty(queue)) {

printf("Queue underflow\n");

return -1;}

struct Node* temp = queue->front;

int dequeuedData = temp->data;

queue->front = queue->front->next;

if (queue->front == NULL) {

queue->rear = NULL;}]

free(temp);

return dequeuedData;}

void displayQueue(struct Queue* queue) {

if (isEmpty(queue)) {

printf("Queue is empty\n");

return;}

struct Node* current = queue->front;

printf("Queue elements: ");

while (current != NULL) {

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

current = current->next;}

printf("\n");}

int main() {

struct Queue queue;

initializeQueue(&queue);

int choice, data;

do { printf("\n1. Enqueue\n");

printf("2. Dequeue\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");


scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter data to enqueue: ");

scanf("%d", &data);

enqueue(&queue, data);

break;

case 2:

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

break;

case 3:

displayQueue(&queue);

break;

case 4:

printf("Exiting...\n");

break;

default:

printf("Invalid choice!\n");}

} while (choice != 4);

return 0;

}
Output :-
Lab – 1
(SEARCHING)

 Program in C to implement linear search.


 Program in C to implement binary search.
Lab – 2
(SORTING)

 Program in C to implement bubble sort.


 Program in C to implement insertion sort.
 Program in C to implement selection sort.
 Program in C to implement quick sort.
 Program in C to implement merge sort.
Lab – 3
(ARRAYS)

 To demonstrate the concept of one dimensional


array finding the sum of array elements .
 To insert an element in an array.
 To perform addition and multiplication of two
matrices.
 To perform transpose of a matrix.
Lab – 4
(STACKS)

 Program to implement stack operations using


array .
 Program to evaluate postfix expression using
stack.
 Program to perform infix to postfix conversion
using stack.
Lab – 5
(QUEUES)

 Program to implement queue operations using


array.
 Program to implement circular queue using
array.
Lab – 6
(LINKED LIST)

 Program to perform following operations on


singly linked list :
(a) Create a singly linked list comprising four
nodes.
(b) Insert a node at the end of the linked list.
(c) Insert a node at the start of the linked list.
(d) Delete the last node of the linked list.
(e) Search the 3rd node of the linked list and
print the value of that node.
 Program to implement stack using linked list.
 Program to implement queue using linked list.
Index

S.no Aim Date Signature

1 Searching Feb 15,2024

2 Sorting Feb 29,2024

3 Arrays Mar 14,2024

4 Stacks Mar 21,2024

5 Queues Apr 04,2024

6 Linked List Apr 25,2024

You might also like