Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Data Structures & Algorithms: Implementing Stack, Linked List, and Sorting in C, Study Guides, Projects, Research of Data Structures and Algorithms

A comprehensive collection of c code examples demonstrating fundamental data structures and algorithms. It covers the implementation of a stack, linked list, and various sorting algorithms, including bubble sort, insertion sort, selection sort, and quick sort. Each code snippet is accompanied by clear explanations and comments, making it an excellent resource for learning and understanding these core concepts in computer science.

Typology: Study Guides, Projects, Research

2023/2024

Uploaded on 12/20/2024

agam-pathak
agam-pathak 🇮🇳

1 document

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
SIR CHHOTU RAM INSTITUTE OF
ENGINEERING & TECHNOLOGY
Practical File
on
Data Structures
BranchComputer Science / 3rd Semester
Session: 2023 - 2024
––––––
Submited By:
Students Name
Roll no. 100200300
Submited To:
Teachers Name
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Data Structures & Algorithms: Implementing Stack, Linked List, and Sorting in C and more Study Guides, Projects, Research Data Structures and Algorithms in PDF only on Docsity!

SIR CHHOTU RAM INSTITUTE OF

ENGINEERING & TECHNOLOGY

Practical File

on

Data Structures

Branch – Computer Science / 3

rd

Semester

Session: 2023 - 2024

––––––

Submited By:

Student’s Name

Roll no. 100200300

Submited To:

Teacher’s Name

Table of Contents

S.no Content

  1. Array Operations
  2. Stack Operations
  3. Queue Operations
  4. Linked List Insertion
  5. Linked List Deletion
  6. Searching (Linear & Binary)
  7. Bubble Sort
  8. Quick Sort
  9. Insertion Sort
  10. Selection Sort

int deletedElement = arr[position];

for (int i = position; i < size - 1; i++) { arr[i] = arr[i + 1]; }

return size - 1; }

int main() { int arr[MAX_SIZE]; int size = 0; int choice, element, position;

traverseArray(arr, size);

printf("Array Operations:\n"); printf("1. Traverse\n"); printf("2. Insert\n"); printf("3. Delete\n"); printf("4. Exit\n");

do { printf("Enter your choice: "); scanf("%d", &choice);

switch (choice) { case 1: traverseArray(arr, size); break; case 2: printf("Enter the element to insert: "); scanf("%d", &element); printf("Enter the position to insert: scanf("%d", &position); size = insertElement(arr, size, element, position); break; case 3: printf("Enter the position to delete: "); scanf("%d", &position); size = deleteElement(arr, size, position); break; case 4: printf("Bye Bye ���������\n"); break; default: printf("That option Don't Exist ��������\n"); } if (choice!=1 && choice!= 4){ traverseArray(arr, size); }

} while (choice != 4);

return 0; }

Output:

Array elements:

Array Operations:

  1. Traverse
  2. Insert
  3. Delete
  4. Exit Enter your choice: 2 Enter the element to insert: 12 Enter the position to insert: 0 Array elements: 12

Enter your choice: 2 Enter the element to insert: 31 Enter the position to insert: 1 Array elements: 12 31

Enter your choice: 2 Enter the element to insert: 2 Enter the position to insert: 2 Array elements: 12 31 2

Enter your choice: 3 Enter the position to delete: 0 Array elements: 31 2

Enter your choice: 1 Array elements: 31 2

Enter your choice: 4

Bye Bye ���������

case 2: pop(); break; case 3: printf("Bye Bye ���������\n"); break; default: printf("That option Don't Exist ��������\n"); } } while (choice != 3);

return 0; }

Output:

Stack Operations

  1. Push
  2. Pop
  3. Exit Enter your choice: 1 Enter the value to push: 1 Pushed 1 onto the stack

Enter your choice: 1 Enter the value to push: 2 Pushed 2 onto the stack

Enter your choice: 1 Enter the value to push: 4 Pushed 4 onto the stack

Enter your choice: 2 Popped 4 from the stack

Enter your choice: 2 Popped 2 from the stack

Enter your choice: 2 Popped 1 from the stack

Enter your choice: 2 Stack Underflow Enter your choice: 3 Bye Bye ���������

Queue Operations

3. WAP to perform the following operations on Queue.

a. Enqueue (Insertion) b. Dequeue (Deletion)

Program Source Code:

#include <stdio.h> #define MAX_SIZE 4

int queue[MAX_SIZE]; int front = -1; int rear = -1;

void enqueue(int value) { if ((front == 0 && rear == MAX_SIZE - 1) || (rear == (front - 1) % (MAX_SIZE - 1))) { printf("Queue Overflow\n"); } else if (front == -1) { front = rear = 0; queue[rear] = value; printf("Enqueued %d into the queue\n\n", value); } else if (rear == MAX_SIZE - 1 && front != 0) { rear = 0; queue[rear] = value; printf("Enqueued %d into the queue\n\n", value); } else { rear++; queue[rear] = value; printf("Enqueued %d into the queue\n\n", value); } }

void dequeue() { if (front == -1) { printf("Queue Underflow\n"); } else if (front == rear) { int value = queue[front]; front = rear = -1; printf("Dequeued %d from the queue\n\n", value); } else if (front == MAX_SIZE - 1) { int value = queue[front]; front = 0; printf("Dequeued %d from the queue\n\n", value); } else { int value = queue[front]; front++; printf("Dequeued %d from the queue\n\n", value); } }

Enter the value to enqueue: 7 Queue Overflow Enter your choice: 2 Dequeued 2 from the queue

Enter your choice: 2 Dequeued 3 from the queue

Enter your choice: 1 Enter the value to enqueue: 4 Enqueued 4 into the queue

Enter your choice: 3

Bye Bye ���������

Linked List Insertion Operation

4. WAP to perform the Insertion operation on Linked List.

a. At the Beginning b. At the End c. After a specified node (value)

Program Source Code:

#include <stdio.h> #include <stdio.h> #include <stdlib.h>

struct Node { int data; struct Node* next; };

struct Node* createNode(int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (newNode == NULL) { printf("Memory allocation failed\n"); exit(1); } newNode->data = value; newNode->next = NULL; return newNode; }

void insertAtBeginning(struct Node** head, int value) { struct Node* newNode = createNode(value); newNode->next = *head; *head = newNode; }

void insertAtEnd(struct Node** head, int value) { struct Node* newNode = createNode(value); if (*head == NULL) { head = newNode; } else { struct Node temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } }

void insertAfterNode(struct Node* prevNode, int value) {

if (temp == NULL) { printf("Node with value %d not found\n", prevValue); } else { insertAfterNode(temp, value); } break; case 4: printf("Bye Bye ���������\n"); break; default: printf("That option doesn't exist ��������\n");

} if (choice!= 4){ printLinkedList(head); } } while (choice != 4); return 0; }

Output:

Linked List Insertion Operations

  1. Insert at Beginning
  2. Insert at End
  3. Insert after a specified node
  4. Exit Enter your choice: 1 Enter the value to insert at the beginning: 23 Linked List Elements: 23 Enter your choice: 1 Enter the value to insert at the beginning: 34 Linked List Elements: 34 23 Enter your choice: 2 Enter the value to insert at the end: 45 Linked List Elements: 34 23 45 Enter your choice: 3 Enter the value to insert: 12 Enter the value of the node after which to insert: 18 Node with value 18 not found Linked List Elements: 34 23 45 Enter your choice: 3 Enter the value to insert: 22 Enter the value of the node after which to insert: 23 Linked List Elements: 34 23 22 45 Enter your choice: 4

Bye Bye ���������

Linked List Deletion Operation

5. WAP to perform the Deletion operation on Linked List.

a. At the Beginning b. At the End c. After a specified node (value)

Program Source Code:

#include <stdio.h> #include <stdlib.h>

struct Node { int data; struct Node* next; };

struct Node* createNode(int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (newNode == NULL) { printf("Memory allocation failed\n"); exit(1); } newNode->data = value; newNode->next = NULL; return newNode; }

void insertAtBeginning(struct Node** head, int value) { struct Node* newNode = createNode(value); newNode->next = *head; *head = newNode; }

void deleteAtBeginning(struct Node** head) { if (head == NULL) { printf("Linked list is empty\n"); return; } struct Node temp = *head; head = (head)->next; free(temp); }

void deleteAtEnd(struct Node** head) { if (*head == NULL) { printf("Linked list is empty\n"); return; }

insertAtBeginning(&head, 30); insertAtBeginning(&head, 40); insertAtBeginning(&head, 40); insertAtBeginning(&head, 40);

printf("Linked List Deletion Operations\n"); printf("1. Delete at Beginning\n"); printf("2. Delete at End\n"); printf("3. Delete a specified node\n"); printf("4. Exit\n");

do { printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: deleteAtBeginning(&head); break; case 2: deleteAtEnd(&head); break; case 3: printf("Enter the value of the node to delete: "); scanf("%d", &value); deleteNode(&head, value); break; case 4: printf("Bye Bye ���������\n"); break; default: printf("That option doesn't exist ��������\n"); }

if (choice != 4) { printLinkedList(head); } } while (choice != 5);

return 0; }

Output:

Linked List Deletion Operations

  1. Delete at Beginning
  2. Delete at End
  3. Delete a specified node
  4. Exit Enter your choice: 1 Linked List Elements: 40 40 30 20 10 Enter your choice: 1

Node with value 45 not found

  • Linked List Elements:
  • Enter your choice:
  • Enter the value of the node to delete:
  • Linked List Elements:
  • Enter your choice:
  • Linked List Elements:
  • Enter your choice:
  • Enter the value of the node to delete:
  • Linked List Elements:
  • Enter your choice:

traverseArray(arr, size);

printf("Array Operations:\n"); printf("1. Linear Search\n"); printf("2. Binary Search\n"); printf("3. Exit\n");

do { printf("Enter your choice: "); scanf("%d", &choice);

switch (choice) { case 1: printf("Enter the key to search: "); scanf("%d", &element); int linearSearchResult = linearSearch(arr, size, element); if (linearSearchResult == -1) { printf("Element not found.\n"); } else { printf("Element found at index %d.\n", linearSearchResult); } break; case 2: printf("Enter the key to search: "); scanf("%d", &element); int binarySearchResult = binarySearch(arr, size, element); if (binarySearchResult == -1) { printf("Element not found.\n"); } else { printf("Element found at index %d.\n", binarySearchResult); } break; case 3: printf("Bye Bye ���������\n"); break; default: printf("That option Don't Exist ��������\n"); }

} while (choice != 3);

return 0; }

Output:

Array elements: 1 2 3 4 5 6 7 8 9 10

Array Operations:

  1. Linear Search
  2. Binary Search
  3. Exit Enter your choice: 1 Enter the key to search: 5 Element found at index 4. Enter your choice: 2 Enter the key to search: 9 Element found at index 8. Enter your choice: 1 Enter the key to search: 11 Element not found. Enter your choice: 2 Enter the key to search: 34 Element not found. Enter your choice: 3

Bye Bye ���������