


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 26
This page cannot be seen from the preview
Don't miss anything!
––––––
Table of Contents
S.no Content
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:
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
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
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
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:
Bye Bye ���������