0% found this document useful (0 votes)
3 views32 pages

C Programming: Search and Queue Algorithms

Uploaded by

mmohamedniyasm
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views32 pages

C Programming: Search and Queue Algorithms

Uploaded by

mmohamedniyasm
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

/* Ex :1 Linear Search */

#include<stdio.h>

#include<conio.h>

void main()

int x,a[20],i,n;

clrscr();

printf("Enter No of Elements : ");

scanf("%d",&n);

printf("Enter %d values : \n",n);

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

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

printf("\nEnter Search Element : ");

scanf("%d",&x);

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

if(x==a[i])

printf("\nElement exist in Location %d",i);

break;

if(i==n+1)

printf("\nElement Not Exist");

Output :
Enter No of Elements : 5

Enter 5 values :

10

20

30

40

50

Enter Search Element : 30

Element exist in Location 3


/* Ex: 2 Binary Search */
#include <stdio.h>

#include <conio.h>

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

int low = 0, high = size - 1, mid;

while (low <= high) {

mid = (low + high) / 2;

if (arr[mid] == key)

return mid; // Key found at index 'mid'

else if (arr[mid] < key)

low = mid + 1; // Key is in the right half

else

high = mid - 1; // Key is in the left half

return -1; // Key not found

void main() {

int arr[100], n, key, result, i;

clrscr();

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

scanf("%d", &n);

printf("Enter %d sorted elements:\n", n);

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

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

printf("Enter the key to search: ");

scanf("%d", &key);

result = binarySearch(arr, n, key);

if (result != -1)

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

else

printf("Element not found\n");

getch(); }
Output :
Enter the number of elements in the array: 5

Enter 5 sorted elements:

10

20

30

40

50

Enter the key to search: 40

Element found at index 3


/* Ex: 3 Stack Operations */
#include <stdio.h>

#include <stdlib.h>

#define MAX 5

int stack[MAX];

int top = -1;

int i;

void push(int value) {

if (top == MAX - 1) {

printf("Stack overflow! Cannot push %d onto the stack.\n", value);

} else {

top++;

stack[top] = value;

printf("%d pushed onto the stack.\n", value);

int pop() {

if (top == -1) {

printf("Stack underflow! No elements to pop.\n");

return -1;

} else {

int poppedValue = stack[top];

top--;

printf("%d popped from the stack.\n", poppedValue);

return poppedValue;

void display() {

if (top == -1) {

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

} else {

printf("Stack elements are:\n");

for (i = top; i >= 0; i--) {

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

void main() {

int choice, value;

while (1) {

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

printf("1. 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 value to push: ");

scanf("%d", &value);

push(value);

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

exit(0);

default:

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

}
Output :
Stack Operations:

1. Push

2. Pop

3. Display

4. Exit

Enter your choice: 1

Enter value to push: 10

10 pushed onto the stack.

Stack Operations:

1. Push

2. Pop

3. Display

4. Exit

Enter your choice: 1

Enter value to push: 20

20 pushed onto the stack.

Stack Operations:

1. Push

2. Pop

3. Display

4. Exit

Enter your choice: 1

Enter value to push: 30

30 pushed onto the stack.

Stack Operations:

1. Push

2. Pop

3. Display

4. Exit

Enter your choice: 3


Stack elements are:

30

20

10

Stack Operations:

1. Push

2. Pop

3. Display

4. Exit

Enter your choice: 2

30 popped from the stack.

Stack Operations:

1. Push

2. Pop

3. Display

4. Exit

Enter your choice: 3

Stack elements are:

20

10

Stack Operations:

1. Push

2. Pop

3. Display

4. Exit

Enter your choice: 4


/* Ex: 4 Queue Operations */
#include <stdio.h>

#define MAX 50

void insert();

void delete();

void display();

int queue_array[MAX];

int rear = - 1;

int front = - 1;

main()

int choice;

while (1)

printf("[Link] element to queue \n");

printf("[Link] element from queue \n");

printf("[Link] all elements of queue \n");

printf("[Link] \n");

printf("Enter your choice : ");

scanf("%d", &choice);

switch (choice)

case 1: insert(); break;

case 2: delete(); break;

case 3: display(); break;

case 4: exit(1);

default:

printf("Wrong choice \n");

void insert()

int add_item;
if (rear == MAX - 1)

printf("Queue Overflow \n");

else

if (front == - 1)

front = 0;

printf("Inset the element in queue : ");

scanf("%d", &add_item);

rear = rear + 1;

queue_array[rear] = add_item;

void delete()

if (front == - 1 || front > rear)

printf("Queue Underflow \n");

return ;

else

printf("Element deleted from queue is : %d\n", queue_array[front]);

front = front + 1;

void display()

int i;

if (front == - 1)

printf("Queue is empty \n");

else

printf("Queue is : \n");

for (i = front; i <= rear; i++)


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

printf("\n");

Output :
[Link] element to queue

[Link] element from queue

[Link] all elements of queue

[Link]

Enter your choice : 1

Inset the element in queue : 10

[Link] element to queue

[Link] element from queue

[Link] all elements of queue

[Link]

Enter your choice : 1

Inset the element in queue : 20

[Link] element to queue

[Link] element from queue

[Link] all elements of queue

[Link]

Enter your choice : 1

Inset the element in queue : 30

[Link] element to queue

[Link] element from queue

[Link] all elements of queue

[Link]

Enter your choice : 3

Queue is :

10 20 30

[Link] element to queue

[Link] element from queue


[Link] all elements of queue

[Link]

Enter your choice : 2

Element deleted from queue is : 10

[Link] element to queue

[Link] element from queue

[Link] all elements of queue

[Link]

Enter your choice : 3

Queue is :

20 30

[Link] element to queue

[Link] element from queue

[Link] all elements of queue

[Link]

Enter your choice : 4


/* Ex: 5 Circular Queue Operations */
#include <stdio.h>

#include <conio.h>

#define SIZE 5 // Maximum size of the Circular Queue

int queue[SIZE];

int front = -1, rear = -1;

int isFull()

return (front == (rear + 1) % SIZE);

int isEmpty()

return (front == -1);

void enqueue(int value) { if (isFull())

printf("Queue is full. Insertion not possible.\n");

else

if (isEmpty()) { front = rear = 0;

} else {

rear = (rear + 1) % SIZE;

queue[rear] = value;

printf("Inserted %d into the queue.\n", value);

void dequeue() { if (isEmpty()) {

printf("Queue is empty. Deletion not possible.\n");

} else {

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


if (front == rear) {

// Queue has only one element front = rear = -1;

} else {

front = (front + 1) % SIZE;

// Function to display the elements of the circular queue

void display()

if (isEmpty()) {

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

} else {

int i = front;

printf("Queue elements are: "); while (1) {

printf("%d ", queue[i]); if (i == rear) {

break;

i = (i + 1) % SIZE;

printf("\n");

// Main function

int main() {

int choice, value;

while (1) {

printf("\nCircular Queue Operations:\n"); printf("1. Enqueue (Insert)\n");

printf("2. Dequeue (Delete)\n"); printf("3. Display Queue\n"); printf("4. Exit\n");

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

switch (choice) { case 1:

printf("Enter the value to insert: "); scanf("%d", &value); enqueue(value);

break;

case 2:
dequeue(); break;

case 3:

display(); break;

case 4:

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

return 0; default:

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

Output :
Circular Queue Operations:

1. Enqueue (Insert)

2. Dequeue (Delete)

3. Display Queue

4. Exit

Enter your choice: 3

Queue elements are: 10 20 30 40 50

Circular Queue Operations:

1. Enqueue (Insert)

2. Dequeue (Delete)

3. Display Queue

4. Exit

Enter your choice: 2

Deleted 10 from the queue.

Circular Queue Operations:

1. Enqueue (Insert)

2. Dequeue (Delete)

3. Display Queue

4. Exit

Enter your choice: 3

Queue elements are: 20 30 40 50

Circular Queue Operations:

1. Enqueue (Insert)
2. Dequeue (Delete)

3. Display Queue

4. Exit

Enter your choice: 1

Enter the value to insert: 60

Inserted 60 into the queue.

Circular Queue Operations:

1. Enqueue (Insert)

2. Dequeue (Delete)

3. Display Queue

4. Exit

Enter your choice: 3

Queue elements are: 20 30 40 50 60

Circular Queue Operations:

1. Enqueue (Insert)

2. Dequeue (Delete)

3. Display Queue

4. Exit

Enter your choice: 4

Exiting program.
/* Ex :6 Singly Linked List */
#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* head = NULL;

// Function to insert a node at the beginning

void insertAtBeginning(int value) {

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

newNode->data = value;

newNode->next = head;

head = newNode;

// Function to insert a node at the end

void insertAtEnd(int value) {

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

newNode->data = value;

newNode->next = NULL;

if (head == NULL) {

head = newNode;

} else {

struct Node* temp = head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

// Function to delete a node from the beginning

void deleteFromBeginning() {

if (head == NULL) {
printf("List is empty.\n");

} else {

struct Node* temp = head;

head = head->next;

free(temp);

printf("Node deleted from the beginning.\n");

// Function to delete a node from the end

void deleteFromEnd() {

if (head == NULL) {

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

} else if (head->next == NULL) {

free(head);

head = NULL;

printf("Node deleted from the end.\n");

} else {

struct Node* temp = head;

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

temp = temp->next;

free(temp->next);

temp->next = NULL;

printf("Node deleted from the end.\n");

// Function to display the linked list

void display() {

if (head == NULL) {

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

} else {

struct Node* temp = head;

printf("Linked List: ");

while (temp != NULL) {


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

temp = temp->next;

printf("NULL\n");

// Main function

void main() {

int choice, value;

while (1) {

printf("\nMenu:\n");

printf("1. Insert at Beginning\n");

printf("2. Insert at End\n");

printf("3. Delete from Beginning\n");

printf("4. Delete from End\n");

printf("5. Display\n");

printf("6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert at beginning: ");

scanf("%d", &value);

insertAtBeginning(value);

break;

case 2:

printf("Enter value to insert at end: ");

scanf("%d", &value);

insertAtEnd(value);

break;

case 3:

deleteFromBeginning();

break;

case 4:
deleteFromEnd();

break;

case 5:

display();

break;

case 6:

exit(0);

default:

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

} } }

Output :
Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display

6. Exit

Enter your choice: 1

Enter value to insert at beginning: 10

Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display

6. Exit

Enter your choice: 2

Enter value to insert at end: 20

Menu:

1. Insert at Beginning

2. Insert at End

4. Delete from End

5. Display
6. Exit

Enter your choice: 5

Linked List: 10 -> 20 -> NULL

Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display

6. Exit

Enter your choice: 1

Enter value to insert at beginning: 30

4. Delete from End

5. Display

6. Exit

Enter your choice: 5

Linked List: 30 -> 10 -> 20 -> NULL

Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display

6. Exit

Enter your choice: 2

Enter value to insert at end: 40

4. Delete from End

5. Display

6. Exit

Enter your choice: 5

Linked List: 30 -> 10 -> 20 -> 40 -> NULL

Menu:

1. Insert at Beginning

2. Insert at End
3. Delete from Beginning

4. Delete from End

5. Display

6. Exit

Enter your choice: 3

Node deleted from the beginning.

Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display

6. Exit

Enter your choice: 5

Linked List: 10 -> 20 -> 40 -> NULL

Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display

6. Exit

Enter your choice: 4

Node deleted from the end.

Menu:

1. Insert at Beginning

2. Insert at End

4. Delete from End

5. Display

6. Exit

Enter your choice: 5

Linked List: 10 -> 20 -> NULL


/* Ex :7 Doubly Linked List */
#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

struct Node* head = NULL;

// Function to insert a node at the beginning

void insertAtBeginning(int value) {

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

newNode->data = value;

newNode->prev = NULL;

newNode->next = head;

if (head != NULL) {

head->prev = newNode;

head = newNode;

// Function to insert a node at the end

void insertAtEnd(int value) {

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

newNode->data = value;

newNode->next = NULL;

if (head == NULL) {

newNode->prev = NULL;

head = newNode;

} else {

struct Node* temp = head;

while (temp->next != NULL) {

temp = temp->next;

}
temp->next = newNode;

newNode->prev = temp;

// Function to delete a node from the beginning

void deleteFromBeginning() {

if (head == NULL) {

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

} else {

struct Node* temp = head;

head = head->next;

if (head != NULL) {

head->prev = NULL;

free(temp);

printf("Node deleted from the beginning.\n");

// Function to delete a node from the end

void deleteFromEnd() {

if (head == NULL) {

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

} else if (head->next == NULL) {

free(head);

head = NULL;

printf("Node deleted from the end.\n");

} else {

struct Node* temp = head;

while (temp->next != NULL) {

temp = temp->next;

temp->prev->next = NULL;

free(temp);

printf("Node deleted from the end.\n");


}

// Function to display the linked list in forward direction

void displayForward() {

if (head == NULL) {

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

} else {

struct Node* temp = head;

printf("Doubly Linked List (Forward): ");

while (temp != NULL) {

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

temp = temp->next;

printf("NULL\n");

// Function to display the linked list in reverse direction

void displayReverse() {

if (head == NULL) {

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

} else {

struct Node* temp = head;

while (temp->next != NULL) {

temp = temp->next;

printf("Doubly Linked List (Reverse): ");

while (temp != NULL) {

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

temp = temp->prev;

printf("NULL\n");

// Main function
void main() {

int choice, value;

while (1) {

printf("\nMenu:\n");

printf("1. Insert at Beginning\n");

printf("2. Insert at End\n");

printf("3. Delete from Beginning\n");

printf("4. Delete from End\n");

printf("5. Display Forward\n");

printf("6. Display Reverse\n");

printf("7. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert at beginning: ");

scanf("%d", &value);

insertAtBeginning(value);

break;

case 2:

printf("Enter value to insert at end: ");

scanf("%d", &value);

insertAtEnd(value);

break;

case 3:

deleteFromBeginning();

break;

case 4:

deleteFromEnd();

break;

case 5:

displayForward();

break;

case 6:
displayReverse();

break;

case 7:

exit(0);

default:

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

Output :
Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display Forward

6. Display Reverse

7. Exit

Enter your choice: 1

Enter value to insert at end: 10

Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display Forward

6. Display Reverse

7. Exit

Enter your choice: 2

Enter value to insert at end: 20

Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning


4. Delete from End

5. Display Forward

6. Display Reverse

7. Exit

Enter your choice: 1

Enter value to insert at end: 30

Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display Forward

6. Display Reverse

7. Exit

Enter your choice: 2

Enter value to insert at end: 40

Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display Forward

6. Display Reverse

7. Exit

Enter your choice: 5

Doubly Linked List (Forward): 30 <-> 10 <-> 20 <-> 40 <-> NULL

Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display Forward

6. Display Reverse

7. Exit
Enter your choice: 6

Doubly Linked List (Reverse): 40 <-> 20 <-> 10 <-> 30 <-> NULL

Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display Forward

6. Display Reverse

7. Exit

Enter your choice: 3

Node deleted from the beginning.

Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display Forward

6. Display Reverse

7. Exit

Enter your choice: 4

Node deleted from the end.

Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display Forward

6. Display Reverse

7. Exit

Enter your choice: 5

Doubly Linked List (Forward): 10 <-> 20 <-> NULL

Menu:

1. Insert at Beginning
2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display Forward

6. Display Reverse

7. Exit

Enter your choice: 6

Doubly Linked List (Reverse): 20 <-> 10 <-> NULL

Menu:

1. Insert at Beginning

2. Insert at End

3. Delete from Beginning

4. Delete from End

5. Display Forward

6. Display Reverse

7. Exit

Enter your choice: 7


/* Ex :8 Binary Tree Traversal */
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* left;

struct Node* right;

};

// Function to create a new node

struct Node* createNode(int data) {

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

newNode->data = data;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

// In-order traversal (Left, Root, Right)

void inorderTraversal(struct Node* node) {

if (node == NULL) {

return;

inorderTraversal(node->left);

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

inorderTraversal(node->right);

// Pre-order traversal (Root, Left, Right)

void preorderTraversal(struct Node* node) {

if (node == NULL) {

return;

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

preorderTraversal(node->left);

preorderTraversal(node->right);

}
// Post-order traversal (Left, Right, Root)

void postorderTraversal(struct Node* node) {

if (node == NULL) {

return;

postorderTraversal(node->left);

postorderTraversal(node->right);

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

// Main function to test the traversals

void main() {

/* Creating the following binary tree

/\

2 3

/\

4 5

*/

struct Node* root = createNode(1);

root->left = createNode(2);

root->right = createNode(3);

root->left->left = createNode(4);

root->left->right = createNode(5);

printf("In-order traversal: ");

inorderTraversal(root);

printf("\n");

printf("Pre-order traversal: ");

preorderTraversal(root);

printf("\n");

printf("Post-order traversal: ");

postorderTraversal(root);

printf("\n");

}
Output :
In-order traversal: 4 2 5 1 3

Pre-order traversal: 1 2 4 5 3

Post-order traversal: 4 5 2 3 1

You might also like