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

cs project

The document presents various fundamental data structures and algorithms implemented in C, including Binary Search, Linear Search, Bubble Sort, stack operations using linked lists, queue operations using linked lists, and linked list creation and display. It concludes with reflections on the learning experience and acknowledgments for guidance and support received during the project. The practical implementations enhance understanding of algorithm efficiency and dynamic memory management.

Uploaded by

xsyam03
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

cs project

The document presents various fundamental data structures and algorithms implemented in C, including Binary Search, Linear Search, Bubble Sort, stack operations using linked lists, queue operations using linked lists, and linked list creation and display. It concludes with reflections on the learning experience and acknowledgments for guidance and support received during the project. The practical implementations enhance understanding of algorithm efficiency and dynamic memory management.

Uploaded by

xsyam03
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

INDEX

➢ Binary Search
➢ Linear Search
➢ Bubble sort
➢ Push and Pop in stack using Link List
➢ Enqueue and Dequeue using Link List
➢ Linked List creation and display
➢ Conclusion
➢ Acknowledgement
Binary Search
Source code
#include <stdio.h>

int binarySearch(int arr[], int size, int target) {

int left = 0;

int right = size - 1;

while (left <= right)

{ int mid = left + (right - left) / 2;

if (arr[mid] == target)

return mid;

if (arr[mid] < target)

left = mid + 1;

else

right = mid - 1; }

return -1; }

int main() {

int target;

int arr[] = {2, 3, 4, 5, 6, 10, 20, 70}; //predefined array

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

printf("Enter search value: ");

scanf("%d",&target);

int result = binarySearch(arr, size, target);

if (result == -1)

printf("Element not present");

else {

printf (“Element found at index %d”, result); return 0; }


Binary Search
Output

If found

If not found
Linear search
Source code
#include <stdio.h>

int linearSearch(int arr[], int size, int key) {

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

if (arr[i] == key) {

return i; } }

return -1;

int main() {

int arr[] = {10, 20, 80, 30, 60, 50, 110, 100};

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

int key;

printf("Enter element to search: ");

scanf("%d", &key);

int result = linearSearch(arr, size, key);

if (result == -1) {

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

} else {

printf("Element %d found at index %d\n", key, result);

return 0;

}
Linear search
Output

If found

If not found
Bubble sort
Source code
#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]) {

temp = arr[j];

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

arr[j + 1] = temp;

swapped = 1; }

if (swapped == 0)

break; }}

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

for (int i = 0; i < size; i++)

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

printf("\n");}

int main() {

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

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

printf("Original array: ");

printArray(arr, n);

bubbleSort(arr, n);

printf("Sorted array: ");

printArray(arr, n);

return 0; }
Bubble sort
Output
Push and Pop in Stack using
Link List
Source code
#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;

int isEmpty(struct Node* top) {

return (top == NULL);

void push(struct Node** top, int data) {

struct Node* newNode = createNode(data);

newNode->next = *top;

*top = newNode;

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

int pop(struct Node** top) {


if (isEmpty(*top)) {

printf("Stack Underflow! Cannot pop from empty stack\n");

return -1; }

struct Node* temp = *top;

int poppedData = temp->data;

*top = (*top)->next;

free(temp);

return poppedData; }

void display(struct Node* top) {

if (isEmpty(top)) {

printf("Stack is empty\n");

return; }

printf("Stack contents (top to bottom): ");

struct Node* current = top;

while (current != NULL) {

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

current = current->next; }

printf("\n"); }

void menu() {

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

printf("1. Push\n");

printf("2. Pop\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice (1-4): ");

int main() {

struct Node* top = NULL;

int choice, data;

while (1) {

menu();
scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to push: ");

scanf("%d", &data);

push(&top, data);

break;

case 2:

if (!isEmpty(top)) {

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

} else {

pop(&top);

break;

case 3:

display(top);

break;

case 4:

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

while (!isEmpty(top)) {

pop(&top);

return 0;

default:

printf("Invalid choice! Please enter a number between 1 and 4\n");

return 0;

}
Push and Pop in Stack using Link
List
Output
Enqueue and Dequeue using Link
List
Source code
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next; };

struct Queue {

struct Node* front;

struct Node* rear;

};

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* createQueue() {

struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));

queue->front = queue->rear = NULL;

return queue;

int isEmpty(struct Queue* queue) {


return (queue->front == NULL);

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

struct Node* newNode = createNode(data);

struct Node *current, *prev;

if (isEmpty(queue) || data < queue->front->data) {

newNode->next = queue->front;

queue->front = newNode;

if (queue->rear == NULL) {

queue->rear = newNode;

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

return;

current = queue->front;

prev = NULL;

while (current != NULL && current->data <= data) {

prev = current;

current = current->next;

prev->next = newNode;

newNode->next = current;

if (current == NULL) {

queue->rear = newNode;

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

int dequeue(struct Queue* queue) {

if (isEmpty(queue)) {
printf("Queue Underflow! Cannot dequeue\n");

return -1;

struct Node* temp = queue->front;

int data = temp->data;

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

if (queue->front == NULL) {

queue->rear = NULL; }

free(temp);

return data; }

void display(struct Queue* queue) {

if (isEmpty(queue)) {

printf("Queue is empty\n");

return; }

printf("Queue contents (front to rear): ");

struct Node* current = queue->front;

while (current != NULL) {

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

current = current->next; }

printf("\n");}

void menu() {

printf("\nQueue Operations Menu:\n");

printf("1. Enqueue\n");

printf("2. Dequeue\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice (1-4): ");}

int main() {

struct Queue* queue = createQueue();


int choice, data;

while (1) {

menu();

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &data);

enqueue(queue, data);

break;

case 2:

if (!isEmpty(queue)) {

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

} else {

dequeue(queue); }

break;

case 3:

display(queue);

break;

case 4:

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

while (!isEmpty(queue)) {

dequeue(queue); }

free(queue);

return 0;

default:

printf("Invalid choice! Please enter a number between 1 and 4\n");

} }

return 0; }
Enqueue and Dequeue using Link
List
Output
Link List Creation and Display
Source code
#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* 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;

}
void displayList(struct Node* head) {

struct Node* temp = head;

if (head == NULL) {

printf("List is empty\n");

return; }

printf("Linked List: ");

while (temp != NULL)

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

temp = temp->next;

printf("NULL\n");

int main()

struct Node* head = NULL;

head = insertAtEnd(head, 10);

head = insertAtEnd(head, 20);

head = insertAtEnd(head, 30);

head = insertAtEnd(head, 40);

displayList(head);

return 0;

}
Link List Creation and Display
Output
Conclusion
In this project, I explored the various aspects of fundamental
data structures and algorithms including Binary Search,
Linear Search, Bubble Sort, stack operations (push and pop)
using a linked list, queue operations (enqueue and dequeue)
using a linked list, and linked list creation and display, all
implemented in C. Through practical implementation and
experimentation, I gained a deeper understanding of how
these algorithms and data structures work, their efficiency,
and their applications. This project helped me enhance my
skills in programming with C and working with dynamic
memory management, which will be beneficial for future
applications in software development and problem-solving.
Overall, this project has been an enriching learning
experience, reinforcing my knowledge of core computer
science concepts and their real-world uses. It has inspired
me to further explore and improve my understanding of
algorithms and data structures in future projects.
Acknowledgement
I would like to express my sincere gratitude to my computer
teacher, Mr./Mrs.,__________________for their valuable
guidance and support throughout this project. Their
encouragement and assistance have been instrumental in
helping me complete my work successfully, especially in
understanding and implementing concepts like Binary
Search, Bubble Sort, and linked list operations in C.A
special thanks to my family and friends, whose constant
encouragement and motivation have been a great source of
support during this project. Their belief in me has helped me
stay focused and complete my work with dedication .Finally,
I appreciate the knowledge and skills I have gained while
working on this project, which has been a valuable learning
experience in deepening my understanding of data
structures and algorithms.

………………………… …………………………
Signature of the student Signature of the teacher

You might also like