0% found this document useful (0 votes)
2 views19 pages

Practical 1 Dsa

The document contains code implementations for a circular linked list and a circular queue in C, including functions for insertion, deletion, and display. It also includes a bubble sort algorithm to sort an array. The main function provides a menu for user interaction with the circular queue operations.

Uploaded by

ketkirane692
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)
2 views19 pages

Practical 1 Dsa

The document contains code implementations for a circular linked list and a circular queue in C, including functions for insertion, deletion, and display. It also includes a bubble sort algorithm to sort an array. The main function provides a menu for user interaction with the circular queue operations.

Uploaded by

ketkirane692
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/ 19

PRN NO : 2401202060 , NAME: KETKI RANE

SKILL 1 :

#include <stdio.h>

#include <stdlib.h>

// Node structure

struct Node {

int data;

struct Node* next;

};

struct Node* head = NULL;

// Function to insert at the end

void insert(int value) {

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

newNode->data = value;

if (head == NULL) {

head = newNode;

newNode->next = head; // Circular link

} else {

struct Node* temp = head;

while (temp->next != head) {

temp = temp->next;

temp->next = newNode;

newNode->next = head;

printf("Inserted %d\n", value);

}
// Function to delete a node by value

void delete(int value) {

if (head == NULL) {

printf("List is empty (Underflow)\n");

return;

struct Node* curr = head;

struct Node* prev = NULL;

// Case 1: Single node

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

free(head);

head = NULL;

printf("Deleted %d\n", value);

return;

// Traverse to find the node

do {

if (curr->data == value)

break;

prev = curr;

curr = curr->next;

} while (curr != head);

// Element not found

if (curr->data != value) {

printf("Element %d not found\n", value);

return;
}

// Case 2: Deleting head

if (curr == head) {

struct Node* temp = head;

while (temp->next != head)

temp = temp->next;

temp->next = head->next;

head = head->next;

free(curr);

} else {

prev->next = curr->next;

free(curr);

printf("Deleted %d\n", value);

// Function to display the list

void display() {

if
PRACTICAL 1 :

#include <stdio.h>

#include <stdlib.h>

#define SIZE 5 // Define the size of the circular queue

int queue[SIZE];

int front = -1, rear = -1;

// Function to insert element into circular queue

void insert(int value) {

if ((rear + 1) % SIZE == front) {

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

return;

} else if (front == -1) { // First element

front = rear = 0;

} else {

rear = (rear + 1) % SIZE;


}

queue[rear] = value;

printf("Inserted %d\n", value);

// Function to delete element from front

void delete() {

if (front == -1) {

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

return;

printf("Deleted %d\n", queue[front]);

if (front == rear) { // Queue has only one element

front = rear = -1;

} else {

front = (front + 1) % SIZE;

// Function to display elements in the circular queue

void display() {

if (front == -1) {

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 with menu

int main() {

int choice, value;

while (1) {

printf("\nCircular Queue Operations:\n");

printf("1. Insert\n");

printf("2. Delete\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

insert(value);

break;

case 2:

delete();

break;
case 3:

display();

break;

case 4:

exit(0);

default:

printf("Invalid choice! Please try again.\n");

return 0;

PRACTICAL 1 :
PRACTICAL 2 : #include <stdio.h>

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

int i, j, temp;

int swapped;

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

swapped = 0;

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

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

// Swap arr[j] and arr[j + 1]

temp = arr[j];

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

arr[j + 1] = temp;

swapped = 1;

// If no two elements were swapped in the inner loop, the array is already sorted

if (swapped == 0)

break;

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

int i;

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

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

printf("\n");
}

int main() {

int arr[] = {64, 25, 12, 22, 11};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array:\n");

printArray(arr, n);

bubbleSort(arr, n);

printf("Sorted array:\n");

printArray(arr, n);

return 0;

}
P

You might also like