0% found this document useful (0 votes)
6 views75 pages

Ds Programs Sybcs

The document provides a series of programming exercises related to searching and sorting algorithms, including linear search, sentinel search, binary search, bubble sort, insertion sort, selection sort, counting sort, merge sort, and quick sort. It also includes a dynamic implementation of a singly linked list with operations such as append, insert, search, delete, and display. Each section contains code examples in C, along with sample outputs demonstrating the functionality.

Uploaded by

mansishirke08
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)
6 views75 pages

Ds Programs Sybcs

The document provides a series of programming exercises related to searching and sorting algorithms, including linear search, sentinel search, binary search, bubble sort, insertion sort, selection sort, counting sort, merge sort, and quick sort. It also includes a dynamic implementation of a singly linked list with operations such as append, insert, search, delete, and display. Each section contains code examples in C, along with sample outputs demonstrating the functionality.

Uploaded by

mansishirke08
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/ 75

Searching algorithms :-

Q. Create a random array of n integers. Accept a value x from user and use linear search algorithm to
check whether the number is present in the array or not and output the position if the number is
present.

#include<stdio.h>

int main()

int a[50],i,size,data;

printf("enter the size of an array:");

scanf("%d",&size);

printf("enter the elements of an array:");

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

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

printf("enter the data you want to search:");

scanf("%d",&data);

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

if(a[i]==data)

printf("data is found at index %d",i);

break;

} }

if(i==size)

printf("data is not found");

} }
OUTPUT :-

enter the size of an array:5

enter the elements of an array:3 4 6 2 1

enter the data you want to search:6

data is found at index 2 .

Q. Accept n values in array from user. Accept a value x from user and use sentinel linear search
algorithm to check whether the number is present in the array or not and output the position if the
number is present.

#include <stdio.h>

int main() {

int n, x, arr[100];

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

scanf("%d", &n);

printf("Enter the elements of the array:\n");

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

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

printf("Enter the element to be searched: ");

scanf("%d", &x);

arr[n] = x;

int i = 0;

while (arr[i] != x) {

i++;

if (i == n) {

printf("The element is not present in the array.\n");

} else {

printf("The element is present in the array at position %d.\n", i);

} return 0;

}
OUTPUT :-

Enter the number of elements in the array: 4

Enter the elements of the array:

Enter the element to be searched: 2

The element is present in the array at position 3.

Q. Accept n sorted values in array from user. Accept a value x from user and use binary search
algorithm to check whether the number is present in sorted array or not and output the position if
the number is present.

#include<stdio.h>

int main()

int i,size,data,lb,ub,mid;

int a[50];

printf("enter the size of an array:");

scanf("%d",&size);

printf("enter the elements of an array:");

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

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

printf("enter the data you want to search:");

scanf("%d",&data);

lb=0;

ub=size-1;
mid=(lb+ub)%2;

if(data==a[mid])

printf("data is found at index %d",mid);

else if(data<a[mid])

ub=mid-1;

else

lb=mid+1;

if(lb>ub)

printf("data is not found");

return -1;

OUTPUT :-

enter the size of an array:4

enter the elements of an array:3 5 6 2

enter the data you want to search:5

data is found at index 1.


Sorting algorithms : Bubble Sort, Insertion Sort, Selection Sort :-

Q. Sort a random array of n integers (accept the value of n from user) in ascending order by using
bubble sort algorithm.

#include<stdio.h>

int main()

int a[50];

int i,j,n,temp;

printf("enter the size of an array:");

scanf("%d",&n);

printf("enter the elements of an array:");

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

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

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

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

if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

printf("sorted array is:\n");

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

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

OUTPUT :-

enter the size of an array:5

enter the elements of an array:3 4 5 2 1

sorted array is:

Q. Sort a random array of n integers (create a random array of n integers) in ascending order by using
insertion sort algorithm.

#include<stdio.h>

int main()

int a[20],i,j,key,n;

printf("enter the size of an array:");

scanf("%d",&n);

printf("enter an elements of an array:");

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

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

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

{ key=a[i];
j=i-1;

while(j>=0 && a[j]>key)

a[j+1]=a[j];

j=j-1;

a[j+1]=key;

} }

printf("the sorted array is ");

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

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

} }

OUTPUT :-

enter the size of an array:5

enter an elements of an array:3 5 6 2 1

the sorted array is 1

Q. Sort a random array of n integers (accept the value of n from user) in ascending order by using
selection sort algorithm.

#include<stdio.h>

int main()

int a[20],i,j,n,min,temp;

printf("enter an size of an array:");

scanf("%d",&n);
printf("enter an elements of an array");

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

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

min=i;

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

if(a[j]<a[min])

min=j;

} }

if(min!=i)

temp=a[i];

a[i]=a[min];

a[min]=temp;

} }

printf("the sorted array is :",a[i]);

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

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

return 0;

OUTPUT :-

enter an size of an array:5

enter an elements of an array3 4 5 2 1

the sorted array is:1 2 3 4 5


Sorting Algorithms : Counting Sort, Merge Sort, Quick Sort :-

Q. Sort a random array of n integers (accept the value of n from user) in ascending order by using
recursive counting sort algorithm.

#include <stdio.h>

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

int arr1[10];

int x = arr[0];

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

if (arr[i] > x)

x = arr[i];

int count_arr[10];

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

count_arr[i] = 0;

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

count_arr[arr[i]]++;

for (int i = 1; i <= x; i++) {

count_arr[i] += count_arr[i - 1];

for (int i = n - 1; i >= 0; i--) {

arr1[count_arr[arr[i]] - 1] = arr[i];

count_arr[arr[i]]--;

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

arr[i] = arr1[i];

} }

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


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

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

printf("\n");

int main() {

int arr[50],n,i;

printf("enter the size of array : ",n);

scanf("%d",&n);

printf("enter the elements of array :\n ");

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

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

countingSort(arr, n);

display(arr, n);

OUTPUT :-

enter the size of array : 4

enter the elements of array :

1 2 3 5.

Q. Sort a random array of n integers (accept the value of n from user) in ascending order by using
recursive merge sort algorithm.
#include<stdio.h>

int main()

int a[20],i,j,n,min,temp;

printf("enetr an size of an array:");

scanf("%d",&n);

printf("enter an elements of an array");

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

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

min=i;

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

if(a[j]<a[min])

min=j;

if(min!=i)

temp=a[i];

a[i]=a[min];

a[min]=temp;

printf("the sorted array is:",a[i]);

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

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

} return 0;

}
OUTPUT :-

List before sorting

10 14 19 26 27 31 33 35 42 44 0

List after sorting

0 10 14 19 26 27 31 33 35 42 44 .

Q. Sort a random array of n integers (create a random array of n integers) in ascending order by using
recursive quick sort algorithm.

#include <stdio.h>

int partition(int ar[], int low, int high)

int pivot = ar[high];

int i = low - 1;

for (int j = low; j < high; j++)

if (ar[j] <= pivot)

i++;

int temp = ar[i];

ar[i] = ar[j];

ar[j] = temp;

int temp = ar[i + 1];

ar[i + 1] = ar[high];

ar[high] = temp;

return i + 1;

}
void quickSort(int arr[], int low, int high)

if (low < high)

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

int main(void)

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

int n;

scanf("%d", &n);

int arr[n];

puts("Enter the elements of the array: ");

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

printf("%d: ", i);

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

quickSort(arr, 0, n - 1);

puts("The sorted array is: ");

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

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

}
OUTPUT :-

Enter the number of elements in the array:

Enter the elements of the array:

0: 2

1: 4

2: 1

3: 3

The sorted array is:

1234

Singly Linked List – Dynamic Implementation :-

Q. Implement a list library (singlylist.h) for a singly linked list with the operations like
append,insert,search, delete,display. Write menu driven program to call the operations.

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* head = NULL;

void append(int data) {

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

newNode->data = data;

newNode->next = NULL;

if (head == NULL) {

head = newNode;
} else {

struct Node* current = head;

while (current->next != NULL) {

current = current->next;

current->next = newNode;

void insert(int data, int pos) {

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

newNode->data = data;

newNode->next = NULL;

if (pos == 0) {

newNode->next = head;

head = newNode;

} else {

struct Node* current = head;

int i;

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

if (current == NULL) {

printf("Position out of range.\n");

return;

current = current->next;

if (current == NULL) {

printf("Position out of range.\n");

} else {

newNode->next = current->next;

current->next = newNode;

}
}}

int search(int data) {

struct Node* current = head;

int pos = 0;

while (current != NULL) {

if (current->data == data) {

return pos;

current = current->next;

pos++;

return -1;

void deleteByValue(int data) {

struct Node* current = head;

struct Node* prev = NULL;

while (current != NULL) {

if (current->data == data) {

if (prev == NULL) {

head = current->next;

} else {

prev->next = current->next;

free(current);

return;

prev = current;

current = current->next;

printf("Element not found.\n");

}
void deleteByPosition(int pos) {

if (pos == 0) {

if (head == NULL) {

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

} else {

struct Node* temp = head;

head = head->next;

free(temp);

} else {

struct Node* current = head;

struct Node* prev = NULL;

int i;

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

if (current == NULL) {

printf("Position out of range.\n");

return;

prev = current;

current = current->next;

if (current == NULL) {

printf("Position out of range.\n");

} else {

prev->next = current->next;

free(current);

} }

void display() {

struct Node* current = head;

while (current != NULL) {


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

current = current->next;

printf("NULL\n");

int main() {

int choice, data, pos, result;

while (1) {

printf("\nSingly Linked List Operations:\n");

printf("1. Append\n");

printf("2. Insert\n");

printf("3. Search\n");

printf("4. Delete by Value\n");

printf("5. Delete by Position\n");

printf("6. Display\n");

printf("7. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter data to append: ");

scanf("%d", &data);

append(data);

break;

case 2:

printf("Enter data to insert: ");

scanf("%d", &data);

printf("Enter position to insert: ");

scanf("%d", &pos);

insert(data, pos);

break;
case 3:

printf("Enter data to search: ");

scanf("%d", &data);

result = search(data);

if (result != -1) {

printf("Data found at position %d.\n", result);

} else {

printf("Data not found.\n");

break;

case 4:

printf("Enter data to delete: ");

scanf("%d", &data);

deleteByValue(data);

break;

case 5:

printf("Enter position to delete: ");

scanf("%d", &pos);

deleteByPosition(pos);

break;

case 6:

display();

break;

case 7:

exit(0);

default:

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

return 0;

}
OUTPUT :-

Singly Linked List Operations:

1. Append

2. Insert

3. Search

4. Delete by Value

5. Delete by Position

6. Display

7. Exit

Enter your choice: 2

Enter data to insert: 10

Enter position to insert: 0

Singly Linked List Operations:

1. Append

2. Insert

3. Search

4. Delete by Value

5. Delete by Position

6. Display

7. Exit

Enter your choice: 2

Enter data to insert: 12

Enter position to insert: 1

Singly Linked List Operations:

1. Append

2. Insert

3. Search

4. Delete by Value

5. Delete by Position
6. Display

7. Exit

Enter your choice: 2

Enter data to insert: 14

Enter position to insert: 2

Singly Linked List Operations:

1. Append

2. Insert

3. Search

4. Delete by Value

5. Delete by Position

6. Display

7. Exit

Enter your choice: 1

Enter data to append: 16

Singly Linked List Operations:

1. Append

2. Insert

3. Search

4. Delete by Value

5. Delete by Position

6. Display

7. Exit

Enter your choice: 3

Enter data to search: 12

Data found at position 1.

Singly Linked List Operations:

1. Append
2. Insert

3. Search

4. Delete by Value

5. Delete by Position

6. Display

7. Exit

Enter your choice: 4

Enter data to delete: 12

Singly Linked List Operations:

1. Append

2. Insert

3. Search

4. Delete by Value

5. Delete by Position

6. Display

7. Exit

Enter your choice: 6

10 -> 14 -> 16 -> NULL

Singly Linked List Operations:

1. Append

2. Insert

3. Search

4. Delete by Value

5. Delete by Position

6. Display

7. Exit

Enter your choice: 5

Enter position to delete: 0


Singly Linked List Operations:

1. Append

2. Insert

3. Search

4. Delete by Value

5. Delete by Position

6. Display

7. Exit

Enter your choice: 6

14 -> 16 -> NULL

Singly Linked List Operations:

1. Append

2. Insert

3. Search

4. Delete by Value

5. Delete by Position

6. Display

7. Exit

Enter your choice: 7

Q. . Implement a list library (singlylist.h) for a singly linked list. Create a linked list, reverse it and
display reversed linked list.

#include <stdio.h>

#include <stdlib.h>

struct node {

int data; //Data part

struct node *next; //Address part

}*head;

void createList(int n);


void reverseList();

void displayList();

int main()

int n, choice;

printf("Enter the total number of nodes: ");

scanf("%d", &n);

createList(n);

printf("\nData in the list \n");

displayList();

printf("\nPress 1 to reverse the order of singly linked list\n");

scanf("%d", &choice);

if(choice == 1)

reverseList();

printf("\nData in the list\n");

displayList();

return 0;

void createList(int n)

struct node *newNode, *temp;

int data, i;

if(n <= 0)

printf("List size must be greater than zero.\n");

return;
}

head = (struct node *)malloc(sizeof(struct node));

if(head == NULL)

printf("Unable to allocate memory.");

else

printf("Enter the data of node 1: ");

scanf("%d", &data);

head->data = data; // Link the data field with data

head->next = NULL; // Link the address field to NULL

temp = head;

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

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

if(newNode == NULL)

printf("Unable to allocate memory.");

break;

else

printf("Enter the data of node %d: ", i);

scanf("%d", &data);

newNode->data = data; // Link the data field of newNode with data

newNode->next = NULL; // Link the address field of newNode with NULL

temp->next = newNode; // Link previous node i.e. temp to the newNode

temp = temp->next; }

}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");

void reverseList()

struct node *prevNode, *curNode;

if(head != NULL)

prevNode = head;

curNode = head->next;

head = head->next;

prevNode->next = NULL; // Make first node as last node

while(head != NULL)

head = head->next;

curNode->next = prevNode;

prevNode = curNode;

curNode = head;

head = prevNode; // Make last node as head

printf("SUCCESSFULLY REVERSED LIST\n");

void displayList()

{
struct node *temp;

if(head == NULL)

printf("List is empty.");

else

temp = head;

while(temp != NULL)

printf("Data = %d\n", temp->data); // Print the data of current node

temp = temp->next; // Move to next node

} }

OUTPUT:-

Enter the total number of nodes: 5

Enter the data of node 1: 10

Enter the data of node 2: 20

Enter the data of node 3: 30

Enter the data of node 4: 40

Enter the data of node 5: 50

SINGLY LINKED LIST CREATED SUCCESSFULLY

Data in the list

Data = 10

Data = 20

Data = 30

Data = 40

Data = 50
Press 1 to reverse the order of singly linked list

SUCCESSFULLY REVERSED LIST

Data in the list

Data = 50

Data = 40

Data = 30

Data = 20

Data = 10

Doubly Linked List : Dynamic Implementation :-

Q. Implement a list library (doublylist.h) for a doubly linked list. Write a menu driven program to call
operations like append, insert, delete specific node, delete at position and display.

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

struct Node* createNode(int data) {

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

newNode->data = data;

newNode->prev = NULL;

newNode->next = NULL;

return newNode;

}
struct DoublyLinkedList {

struct Node* head;

};

void append(struct DoublyLinkedList* dll, int data) {

struct Node* newNode = createNode(data);

if (dll->head == NULL) {

dll->head = newNode;

} else {

struct Node* current = dll->head;

while (current->next != NULL) {

current = current->next;

current->next = newNode;

newNode->prev = current;

void insertAtPosition(struct DoublyLinkedList* dll, int position, int data) {

struct Node* newNode = createNode(data);

if (position == 1) {

newNode->next = dll->head;

if (dll->head != NULL) {

dll->head->prev = newNode;

dll->head = newNode;

} else {

struct Node* current = dll->head;

int count = 1;

while (count < position - 1 && current != NULL) {

current = current->next;

count++;

}
if (current != NULL) {

newNode->next = current->next;

newNode->prev = current;

if (current->next != NULL) {

current->next->prev = newNode;

current->next = newNode;

} else {

printf("Invalid position\n");

void deleteNode(struct DoublyLinkedList* dll, int key) {

struct Node* current = dll->head;

while (current != NULL) {

if (current->data == key) {

if (current->prev != NULL) {

current->prev->next = current->next;

if (current->next != NULL) {

current->next->prev = current->prev;

if (current == dll->head) {

dll->head = current->next;

free(current);

return;

current = current->next;

printf("Node not found.\n");


}

void display(struct DoublyLinkedList* dll) {

struct Node* current = dll->head;

while (current != NULL) {

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

current = current->next;

printf("NULL\n");

int main() {

struct DoublyLinkedList dll;

dll.head = NULL;

int choice, data, position, key;

while (1) {

printf("\nDoubly Linked List Operations:\n");

printf("1. Append\n");

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

printf("3. Delete specific node\n");

printf("4. Display\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the data to append: ");

scanf("%d", &data);

append(&dll, data);

break;

case 2:

printf("Enter the position: ");

scanf("%d", &position);
printf("Enter the data to insert: ");

scanf("%d", &data);

insertAtPosition(&dll, position, data);

break;

case 3:

printf("Enter the node data to delete: ");

scanf("%d", &key);

deleteNode(&dll, key);

break;

case 4:

printf("Doubly Linked List: ");

display(&dll);

break;

case 5:

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

exit(0);

default:

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

return 0;

OUTPUT :-

Doubly Linked List Operations:

1. Append

2. Insert at position

3. Delete specific node

4. Display

5. Exit

Enter your choice: 2


Enter the position: 1

Enter the data to insert: 12

Doubly Linked List Operations:

1. Append

2. Insert at position

3. Delete specific node

4. Display

5. Exit

Enter your choice: 1

Enter the data to append: 21

Doubly Linked List Operations:

1. Append

2. Insert at position

3. Delete specific node

4. Display

5. Exit

Enter your choice: 4

Doubly Linked List: 12 <-> 21 <-> NULL

Doubly Linked List Operations:

1. Append

2. Insert at position

3. Delete specific node

4. Display

5. Exit

Enter your choice: 3

Enter the node data to delete: 12

Doubly Linked List Operations:


1. Append

2. Insert at position

3. Delete specific node

4. Display

5. Exit

Enter your choice: 4

Doubly Linked List: 21 <-> NULL

Doubly Linked List Operations:

1. Append

2. Insert at position

3. Delete specific node

4. Display

5. Exit

Enter your choice: 5

Exiting program. Goodbye!

Q. Implement a list library (doublylist.h) for a doubly linked list. Create a linked list and display
reversed linked list.

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

struct DoublyLinkedList {

struct Node* head;

struct Node* tail;

};
struct Node* createNode(int data) {

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

if (newNode == NULL) {

printf("Memory allocation error\n");

exit(1);

newNode->data = data;

newNode->prev = NULL;

newNode->next = NULL;

return newNode;

struct DoublyLinkedList* createLinkedList() {

struct DoublyLinkedList* dll = (struct DoublyLinkedList*)malloc(sizeof(struct DoublyLinkedList));

if (dll == NULL) {

printf("Memory allocation error\n");

exit(1);

dll->head = NULL;

dll->tail = NULL;

return dll;

void append(struct DoublyLinkedList* dll, int data) {

struct Node* newNode = createNode(data);

if (dll->head == NULL) {

dll->head = newNode;

dll->tail = newNode;

} else {

dll->tail->next = newNode;

newNode->prev = dll->tail;

dll->tail = newNode;

}
}

void reverse(struct DoublyLinkedList* dll) {

struct Node* temp = NULL;

struct Node* current = dll->head;

while (current != NULL) {

temp = current->prev;

current->prev = current->next;

current->next = temp;

current = current->prev;

if (temp != NULL) {

dll->head = temp->prev;

void display(struct DoublyLinkedList* dll) {

struct Node* current = dll->head;

while (current != NULL) {

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

current = current->next;

printf("NULL\n");

int main() {

struct DoublyLinkedList* dll = createLinkedList();

append(dll, 1);

append(dll, 2);

append(dll, 3);

append(dll, 4);

append(dll, 5);
printf("Original Doubly Linked List: ");

display(dll);

reverse(dll);

printf("Reversed Doubly Linked List: ");

display(dll);

struct Node* current = dll->head;

while (current != NULL) {

struct Node* temp = current;

current = current->next;

free(temp);

free(dll);

return 0;

OUTPUT :-

Original Doubly Linked List: 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> NULL

Reversed Doubly Linked List: 5 <-> 4 <-> 3 <-> 2 <-> 1 <-> NULL

Linked Lists Applications :-

Q. Write a program that merges two ordered linked lists into third new list. When two lists are
merged the data in the resulting list are also ordered. The two original lists should be left unchanged.
That is merged list should be new one. Use linked implementation.

#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;

struct Node* mergeLists(struct Node* list1, struct Node* list2) {

struct Node* mergedList = NULL;

struct Node* current = NULL;

while (list1 != NULL && list2 != NULL) {

if (list1->data < list2->data) {

if (mergedList == NULL) {

mergedList = createNode(list1->data);

current = mergedList;

} else {

current->next = createNode(list1->data);

current = current->next;

list1 = list1->next;

} else {

if (mergedList == NULL) {

mergedList = createNode(list2->data);

current = mergedList;

} else {

current->next = createNode(list2->data);
current = current->next;

list2 = list2->next;

while (list1 != NULL) {

if (mergedList == NULL) {

mergedList = createNode(list1->data);

current = mergedList;

} else {

current->next = createNode(list1->data);

current = current->next;

list1 = list1->next;

while (list2 != NULL) {

if (mergedList == NULL) {

mergedList = createNode(list2->data);

current = mergedList;

} else {

current->next = createNode(list2->data);

current = current->next;

list2 = list2->next;

return mergedList;

void printList(struct Node* list) {

while (list != NULL) {

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

list = list->next;
}

printf("NULL\n");

int main() {

struct Node* list1 = createNode(1);

list1->next = createNode(3);

list1->next->next = createNode(5);

struct Node* list2 = createNode(2);

list2->next = createNode(4);

list2->next->next = createNode(6);

struct Node* mergedList = mergeLists(list1, list2);

printf("List 1: ");

printList(list1);

printf("List 2: ");

printList(list2);

printf("Merged List: ");

printList(mergedList);

return 0;

OUTPUT :-

List 1: 1 -> 3 -> 5 -> NULL

List 2: 2 -> 4 -> 6 -> NULL

Merged List: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL

Q. Write a program that adds two single variable polynomials. Each polynomial should be
represented as a list with linked list implementation.
#include <stdio.h>

#include <stdlib.h>

struct Term {

int coefficient;

int exponent;

struct Term* next;

};

struct Term* createTerm(int coeff, int exp) {

struct Term* newTerm = (struct Term*)malloc(sizeof(struct Term));

if (newTerm == NULL) {

printf("Memory allocation failed\n");

exit(1);

newTerm->coefficient = coeff;

newTerm->exponent = exp;

newTerm->next = NULL;

return newTerm;

void insertTerm(struct Term** polynomial, int coeff, int exp) {

struct Term* newTerm = createTerm(coeff, exp);

if (*polynomial == NULL) {

*polynomial = newTerm;

} else {

struct Term* current = *polynomial;

while (current->next != NULL) {

current = current->next;

current->next = newTerm;

}
}

struct Term* addPolynomials(struct Term* poly1, struct Term* poly2) {

struct Term* result = NULL;

while (poly1 != NULL || poly2 != NULL) {

int coeff1 = (poly1 != NULL) ? poly1->coefficient : 0;

int exp1 = (poly1 != NULL) ? poly1->exponent : 0;

int coeff2 = (poly2 != NULL) ? poly2->coefficient : 0;

int exp2 = (poly2 != NULL) ? poly2->exponent : 0;

if (exp1 == exp2) {

int sum = coeff1 + coeff2;

if (sum != 0) {

insertTerm(&result, sum, exp1);

poly1 = poly1->next;

poly2 = poly2->next;

} else if (exp1 > exp2) {

insertTerm(&result, coeff1, exp1);

poly1 = poly1->next;

} else {

insertTerm(&result, coeff2, exp2);

poly2 = poly2->next;

return result;

void printPolynomial(struct Term* polynomial) {

while (polynomial != NULL) {

printf("%dX^%d", polynomial->coefficient, polynomial->exponent);


polynomial = polynomial->next;

if (polynomial != NULL) {

printf(" + ");

printf("\n");

int main() {

struct Term* poly1 = NULL;

struct Term* poly2 = NULL;

insertTerm(&poly1, 3, 3);

insertTerm(&poly1, 2, 2);

insertTerm(&poly1, 1, 1);

insertTerm(&poly2, 2, 2);

insertTerm(&poly2, 5, 5);

insertTerm(&poly2, 4, 4);

struct Term* result = addPolynomials(poly1, poly2);

printf("Polynomial 1: ");

printPolynomial(poly1);

printf("Polynomial 2: ");

printPolynomial(poly2);

printf("Result: ");

printPolynomial(result);
while (poly1 != NULL) {

struct Term* temp = poly1;

poly1 = poly1->next;

free(temp);

while (poly2 != NULL) {

struct Term* temp = poly2;

poly2 = poly2->next;

free(temp);

while (result != NULL) {

struct Term* temp = result;

result = result->next;

free(temp);

return 0;

OUTPUT :-

Polynomial 1: 3X^3 + 2X^2 + 1X^1

Polynomial 2: 2X^2 + 5X^5 + 4X^4

Result: 3X^3 + 4X^2 + 5X^5 + 4X^4 + 1X^1


Stacks :-

Implement a stack library (ststack.h) of integers using a static implementation of

the stack and implementing the operations like init(S), S=push(S) and S=pop(S). Write

a driver program that includes stack library and calls different stack operations.

Header file save :- stack.h

#include <stdio.h>

#define MAXSIZE 10

typedef struct

int data[MAXSIZE];

int top;

} STACK;

void initstack(STACK *ps)

ps->top = -1;

void push(STACK *ps, int num)

ps->data[++ps->top] = num;

int pop(STACK *ps)

return (ps->data[ps->top--]);

int peek(STACK *ps)

return (ps->data[ps->top]);

}
int isempty(STACK *ps)

return (ps->top == -1);

int isfull(STACK *ps)

return (ps->top == MAXSIZE - 1);

file save :- Stack.c

#include <stdio.h>

#include "stack.h"

void main()

int n, choice;

STACK s;

initstack(&s);

do

printf("\n1:PUSH \n2:POP \n3:PEEK");

printf("\nEnter the choice:");

scanf("%d", &choice);

switch (choice)

case 1: /* PUSH */

if (isfull(&s))

printf("\n Stack overflow");

else

printf("Enter the element to be pushed");


scanf("%d", &n);

push(&s, n);

break;

case 2: /*POP*/

if (isempty(&s))

printf("\nStack underflow");

else

printf("The popped element is :%d", pop(&s));

break;

case 3: /*peek*/

if (isempty(&s))

printf("\nStack underflow");

else

printf("\nThe topmost element is %d", peek(&s));

} while (choice != 4);

OUTPUT :-

1:PUSH

2:POP

3:PEEK

Enter the choice:1

Enter the element to be pushed5

1:PUSH

2:POP
3:PEEK

Enter the choice:1

Enter the element to be pushed9

1:PUSH

2:POP

3:PEEK

Enter the choice:1

Enter the element to be pushed3

1:PUSH

2:POP

3:PEEK

Enter the choice:2

The popped element is :3

1:PUSH

2:POP

3:PEEK

Enter the choice:3

The topmost element is 9

1:PUSH

2:POP

3:PEEK

Enter the choice:

Circular Queue

Q 22 Implement a circular queue library (circularqueue.h) of integers using a dynamic (circular

linked list) implementation of the queue and implementing init(Q), AddQueue(Q) and

DeleteQueue(Q) operations. Write a menu driven program that includes queue library and
calls different queue operations.

Header file save :- circularqueue.h

#include <stdio.h>

#include <malloc.h>

typedef struct node

int info;

struct node *next;

} NODE;

NODE *front, *rear;

void initq()

rear = NULL;

int isempty()

return rear == NULL;

void addq(int num)

NODE *newnode;

newnode = (NODE *)malloc(sizeof(NODE));

newnode->info = num;

if (rear == NULL)

rear = newnode;

rear->next = rear;

else
{

rear->next = rear->next ;

rear->next = newnode;

rear = newnode;

int removeq()

NODE *front= rear->next;

int num = front->info;

if(rear->next == rear) //only one node

free(rear);

else

rear->next= front->next;

free(front);

return (num);

int peek()

return rear->next->info;

file save :- circularQ.c

#include<stdio.h>

#include "circularqueue.h"

void main()

int choice, num;


initq();

do

printf("\n1.ADD\n2.REMOVE\n3.PEEK\n4.EXIT");

printf("\nEnter your choice");

scanf("%d", &choice);

switch (choice)

case 1:

printf("\nEnter the element to be added");

scanf("%d", &num);

addq(num);

break;

case 2:

if (isempty())

printf("\nQueue underflow");

else

printf("\nThe removed element is %d", removeq());

break;

case 3:

peek();

break;

} while (choice != 4);

OUTPUT :-

1.ADD

2.REMOVE
3.PEEK

4.EXIT

Enter your choice1

Enter the element to be added5

1.ADD

2.REMOVE

3.PEEK

4.EXIT

Enter your choice1

Enter the element to be added8

1.ADD

2.REMOVE

3.PEEK

4.EXIT

Enter your choice3

1.ADD

2.REMOVE

3.PEEK

4.EXIT

Enter your choice

Linear Queue

Implement a linear queue library (st_queue.h) of integers using a static implementation


of the queue and implementing the init(Q), add(Q) and peek(Q) operations. Write a program

that includes queue library and calls different queue operations.

Header file save:- Linearqueue.h

#include<stdio.h>

#define MAXSIZE 20

typedef struct

int data[MAXSIZE];

int front,rear;

}QUEUE;

void initqueue(QUEUE *pq)

pq->front=pq->rear=-1;

void add(QUEUE *pq,int num)

pq->rear++;

pq->data[pq->rear]=num;

int removeq(QUEUE *pq)

int num;

pq->front++;

num=pq->data[pq->front];

return num;

int isempty(QUEUE *pq)

return(pq->front==pq->rear);
}

int isfull(QUEUE *pq)

return(pq->rear==MAXSIZE-1);

void display(QUEUE *pq)

int i;

printf("The Queue is: ");

if(pq->front==pq->rear)

printf("\nQueue is Empty");

else

for(i=pq->front+1;i<=pq->rear;i++)

printf("\n%d",pq->data[i]);

printf("\n");

file save:- linearQ.c

#include<stdio.h>

#include "Linearqueue.h"

void main()

int n,choice;

QUEUE q;

initqueue(&q);
do

printf("\n1:ADD\n2:REMOVE\n3:DISPLAY\n4:EXIT");

printf("\nEnter Your Choice: ");

scanf("%d",&choice);

switch(choice)

case 1:if(isfull(&q))

printf("\nQueue is Overflow");

else

printf("\nEnter the number to be added: ");

scanf("%d",&n);

add(&q,n);

display(&q);

break;

case 2:if(isempty(&q))

printf("\nQueue is Underflow");

else

printf("The removed element is %d ",removeq(&q));

break;

case 3:display(&q);

break;

while(choice!=4);

OUTPUT :-
1:ADD

2:REMOVE

3:DISPLAY

4:EXIT

Enter Your Choice: 1

Enter the number to be added: 2

The Queue is:

1:ADD

2:REMOVE

3:DISPLAY

4:EXIT

Enter Your Choice: 1

Enter the number to be added: 5

The Queue is:

1:ADD

2:REMOVE

3:DISPLAY

4:EXIT

Enter Your Choice: 1

Enter the number to be added: 6

The Queue is:

2
5

1:ADD

2:REMOVE

3:DISPLAY

4:EXIT

Enter Your Choice: 2

The removed element is 2

1:ADD

2:REMOVE

3:DISPLAY

4:EXIT

Enter Your Choice: 3

The Queue is:

1:ADD

2:REMOVE

3:DISPLAY

4:EXIT

Enter Your Choice:

Doubly Ended Queue

Implement a queue library (dyqueue.h) of integers using a dynamic (linked list)

implementation of the queue and implement init, enqueue, dequeue, isempty, peek operations.

Header file save :- dyqueue.h


#include<stdio.h>

#include<malloc.h>

typedef struct node

int info;

struct node *next;

}NODE;

NODE *front,*rear;

void initq()

front=rear=NULL;

int isempty()

return(front==NULL);

void addq(int num)

NODE *newnode;

newnode=(NODE*)malloc(sizeof(NODE));

newnode->info=num;

newnode->next=NULL;

if(front==NULL)

rear=front=newnode;

else

rear->next=newnode;

rear=newnode;

}
int removeq()

int num;

NODE *temp=front;

num=front->info;

front=front->next;

free(temp);

if(front==NULL)

rear=NULL;

return(num);

void display()

NODE *temp=front;

if(front==NULL)

printf("\nQueue is empty");

else

for(temp=front;temp!=NULL;temp=temp->next)

printf("\t%d",temp->info);

printf("\n");

file save :- dyQ.c

#include<stdio.h>

#include "dyqueue.h"

void main()

{
int choice,num;

initq();

do

printf("\n1.ADD\n2.REMOVE\n3.DISPLAY\n4.EXIT");

printf("\nEnter your choice");

scanf("%d",&choice);

switch(choice)

case 1:printf("\nEnter the element to be added");

scanf("%d",&num);

addq(num);

break;

case 2:if(isempty())

printf("\nQueue underflow");

else

printf("\nThe removed element is %d",removeq());

break;

case 3:display();

break;

while(choice!=4);

OUTPUT :-

1.ADD

2.REMOVE
3.DISPLAY

4.EXIT

Enter your choice1

Enter the element to be added1

1.ADD

2.REMOVE

3.DISPLAY

4.EXIT

Enter your choice1

Enter the element to be added2

1.ADD

2.REMOVE

3.DISPLAY

4.EXIT

Enter your choice3

1 2

1.ADD

2.REMOVE

3.DISPLAY

4.EXIT

Enter your choice

Priority Queue

Implement a priority queue library (PriorityQ.h) of integers using a static

implementation of the queue and implement the below two operations.


1) Add an element with its priority into the queue.

2) Delete an element from queue according to its priority.

Header file :- priorityQ.h

#include <stdio.h>

#define MAXSIZE 100

typedef struct

int data[MAXSIZE];

int front, rear;

} QUEUE;

void initqueue(QUEUE *pq)

pq->front = pq->rear = -1;

void addq(QUEUE *pq, int num)

int i;

for(i=pq->rear; i>pq->front; i--) //loop for comparison

if(num>pq->data[i])

pq->data[i+1]=pq->data[i]; //shift element

else

break;

pq->data[i+1]=num;

pq->rear++;

int removeq(QUEUE *pq)

int num;

pq->front++;

num = pq->data[pq->front];
return (num);

int isempty(QUEUE *pq)

return (pq->front == pq->rear);

int isfull(QUEUE *pq)

return (pq->rear == MAXSIZE - 1);

void display(QUEUE *pq)

int i;

printf("The Queue is: ");

if (pq->front == pq->rear)

printf("\nQueue is Empty");

else

for (i = pq->front + 1; i <= pq->rear; i++)

printf("\n%d", pq->data[i]);

printf("\n");

File save :- priorityQ.c

#include<stdio.h>

#include "priorityQ.h"

void main()
{

int n,choice;

QUEUE q;

initqueue(&q);

printf("---Priority queue---\n Elements are added according to descending order ");

do

printf("\n1:ADD\n2:REMOVE\n3:DISPLAY\n4:EXIT");

printf("\nEnter Your Choice: ");

scanf("%d",&choice);

switch(choice)

case 1:if(isfull(&q))

printf("\nQueue is Overflow");

else

printf("\nEnter the number to be added: ");

scanf("%d",&n);

addq(&q,n);

display(&q);

break;

case 2:if(isempty(&q))

printf("\nQueue is Underflow");

else

printf("The removed element is %d ",removeq(&q));

break;

case 3:display(&q);

break;

}
while(choice!=4);

OUTPUT :-

---Priority queue---

Elements are added according to descending order

1:ADD

2:REMOVE

3:DISPLAY

4:EXIT

Enter Your Choice: 1

Enter the number to be added: 22

The Queue is:

22

1:ADD

2:REMOVE

3:DISPLAY

4:EXIT

Enter Your Choice: 1

Enter the number to be added: 25

The Queue is:

25

22

1:ADD
2:REMOVE

3:DISPLAY

4:EXIT

Enter Your Choice: 1

Enter the number to be added: 33

The Queue is:

33

25

22

1:ADD

2:REMOVE

3:DISPLAY

4:EXIT

Enter Your Choice: 2

The removed element is 33

1:ADD

2:REMOVE

3:DISPLAY

4:EXIT

Enter Your Choice: 3

The Queue is:

25

22

1:ADD

2:REMOVE

3:DISPLAY

4:EXIT

Enter Your Choice:


Singly Linked List

) Implement a list library (singlylist.h) for a singly linked list of integer

with the operations create, display. Write a menu driven program to call

these operations.

Header file save :- singlylist.h

#include <stdio.h>

#include <stdlib.h>

typedef struct node

int info;

struct node *next;

} NODE;

void createlist(NODE *head)

int n, i;

NODE *last, *newnode;

printf("Enter how many nodes you want to create:");

scanf("%d", &n);

last = head;

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

newnode = (NODE *)malloc(sizeof(NODE));

newnode->next = NULL;

printf("\nEnter the info:");

scanf("%d", &newnode->info);

last->next = newnode;
last = newnode;

void display(NODE *head)

NODE *temp;

for (temp = head->next; temp != NULL; temp = temp->next)

printf("%d\t", temp->info);

File Save :- Singlylist1.c

#include <stdio.h>

#include <stdlib.h>

#include "singlylist.h"

void main()

NODE *head;

int choice;

head = (NODE *)malloc(sizeof(NODE));

head->next = NULL;

do

printf("\n1:CREATE:");

printf("\n2:DISPLAY:");

printf("\n3:EXIT:");

printf("\n\nEnter your choice:");

scanf("%d", &choice);

switch (choice)

case 1:
createlist(head);

break;

case 2:

display(head);

break;

} while (choice != 3);

OUTPUT :-

1:CREATE:

2:DISPLAY:

3:EXIT:

Enter your choice:1

Enter how many nodes you want to create:2

Enter the info:1

Enter the info:2

1:CREATE:

2:DISPLAY:

3:EXIT:

Enter your choice:2

1 2

1:CREATE:

2:DISPLAY:

3:EXIT:
Enter your choice:

Doubly Linked list

a) Implement a list library (doublylist.h) for a doubly linked list with the above four

operations. Write a menu driven driver program to call the operationsappend, insert, delete

specific node, delete at position, search, display.

Header file save :- doublylist.h

#include<stdio.h>

#include<stdlib.h>

struct node

char data;

struct node *next,*prev;

}*start;

void create()

struct node *temp,*q;

temp=(struct node *)malloc(sizeof(struct node));

printf("\nEnter a character:");

scanf(" %c",&temp->data);

temp->next=NULL;

temp->prev=NULL;

if(start==NULL)

start=temp;

else

{
q=start;

while(q->next!=NULL)

q=q->next;

q->next=temp;

temp->prev=q;

void display()

struct node *temp;

if(start==NULL)

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

return;

printf("The Linked List:\n");

temp=start;

while(temp!=NULL)

printf("%d<-%c->%d->|",temp->prev,temp->data,temp->next);

temp=temp->next;

void delete()

struct node *temp,*p;

int pos,t;
printf("\nEnter the position:");

scanf("%d",&pos);

t=1;

temp=start;

if(start==NULL)

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

return;

if(start->next==NULL)

free(start);

start=NULL;

printf("\nNode Deleted Successfully\n");

return;

if(pos==1)

start=start->next;

start->prev=NULL;

free(temp);

printf("\nNode Deleted Successfully\n");

return;

while(t< pos)

temp=temp->next;

t++;

temp->prev->next=temp->next;

if(temp->next!=NULL)
{

temp->next->prev=temp->prev;

free(temp);

printf("\nNode deleted successfully\n");

file save :- doublylist1.c

#include<stdio.h>

#include "doublylist.h"

void main()

int c;

start=NULL;

while(1)

printf("\n1.Create\n");

printf("2.Delete\n");

printf("3.Display\n");

printf("4.Exit\n");

printf("Enter your choice: ");

scanf("%d",&c);

switch(c)

case 1:create(); break;

case 2:delete(); break;

case 3:display(); break;

case 4:exit(0);
}

OUTPUT :-

1.Create

2.Delete

3.Display

4.Exit

Enter your choice: 1

Enter a character:h

1.Create

2.Delete

3.Display

4.Exit

Enter your choice: 1

Enter a character:w

1.Create

2.Delete

3.Display

4.Exit

Enter your choice: 2

Enter the position:1

Node Deleted Successfully


1.Create

2.Delete

3.Display

4.Exit

Enter your choice: 3

The Linked List:

0<-w->0->|

1.Create

2.Delete

3.Display

4.Exit

Enter your choice:

You might also like