DS Complete File, SEM-3
DS Complete File, SEM-3
(CIC-255)
Bachelor of Technology in
Information Technology
Assistant Professor
Submitted by:
Kshitij Kumar
09415003121
10
11
12
13
14
15
16
17
18
19
20
1. WAP to search an element in an array using Linear Search
#include<stdio.h>
void linearsearch(int* arr,int target){
int main(){
int arr[]={1,2,3,4,5,6,7,8,9};
linearsearch(arr,9);
return 0;
}
Output
2. WAP to search an element in an array using Binary Search
#include <stdio.h>
void binarySearch(int *arr, int start, int end, int target)
{
if (end >= start) {
int mid = start + (end - start) / 2;
if (arr[mid] == target)
{
printf("Element found at index %d",mid);
return ;
}
if (arr[mid] > target)
return binarySearch(arr, start, mid - 1, target);
int main()
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
binarySearch(arr, 0, n - 1, 10);
return 0;
}
Output
3. WAP to check whether a given matrix is sparse matrix or not
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ( )
{
int n, m;
printf ( "Enter rows and columns of a matrix : " );
scanf ( "%d %d", &n, &m );
int* arr[n];
for ( int i = 0; i < n; i++ )
{
arr[i] = malloc ( m * sizeof ( int ) );
}
int count = 0;
printf ( "Enter elements of matrix\n" );
for ( int i = 0; i < n; i++ )
{
for ( int j = 0; j < m; j++ )
{
printf ( "Enter a[%d][%d] element : ", i + 1, j + 1 );
scanf ( "%d", &arr[i][j] );
if ( arr[i][j] == 0 )
{
count++;
}
}
}
return 0;
}
Output
4. WAP to perform following operations on a Singly Linked List
a) Insert a new node at specified position
b) Deletion of a node from a specified position
c) Reversal of that linked list
#include<stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *get_Input();
void printLL(struct Node *head);
struct Node *get_Ati(struct Node *head, int data, int i);
struct Node *del_NodeAti(struct Node *head, int i);
struct Node *reverseLL(struct Node *head);
int main()
{
struct Node *head = get_Input();
printLL(head);
printf("\n");
int data, position;
printf("Enter data you want to insert : ");
scanf("%d", &data);
printf("Enter position : ");
scanf("%d", &position);
head = get_Ati(head, data, position);
printLL(head);
printf("\n");
int del_Pos;
printf("Enter position you want to delete : ");
scanf("%d", &del_Pos);
head = del_NodeAti(head, del_Pos);
printLL(head);
printf("\n");
printf("Reversed Linked List is\n");
head = reverseLL(head);
printLL(head);
return 0;
}
struct Node *get_Input()
{
struct Node *head = NULL;
struct Node *tail = NULL;
int num;
printf("Enter number of nodes you want to enter : ");
scanf("%d", &num);
int i = 0;
while (i < num)
{
int data;
printf("Enter data of %d nodes : ", i + 1);
scanf("%d", &data);
if (head == NULL)
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
head = newNode;
tail = newNode;
}
else
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
tail->next = newNode;
tail = newNode;
}
i++;
}
return head;
}
void printLL(struct Node *head)
{
struct Node *temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
struct Node *get_Ati(struct Node *head, int data, int i)
{
if (head == NULL)
return NULL;
struct Node *temp = head;
if (i == 0)
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->next = head;
newNode->data = data;
head = newNode;
return head;
}
for (int position = 0; position < i - 1 && temp != NULL; position++)
temp = temp->next;
if (temp != NULL)
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = temp->next;
temp->next = newNode;
}
return head;
}
struct Node *del_NodeAti(struct Node *head, int i)
{
if (head == NULL)
return NULL;
if (i == 0)
{
struct Node *temp = head;
head = head->next;
temp->next = NULL;
free(temp);
return head;
}
struct Node *temp = head;
for (int position = 0; position < i - 1 && temp != NULL; position++)
temp = temp->next;
if (temp != NULL && temp->next != NULL)
{
struct Node *deletingNode = temp->next;
temp->next = temp->next->next;
deletingNode->next = NULL;
free(deletingNode);
}
return head;
}
struct Node *reverseLL(struct Node *head)
{
if (head == NULL || head->next == NULL)
return head;
struct Node *temp = reverseLL(head->next);
head->next->next = head;
head->next = NULL;
head = temp;
return head;
}
Output
5. WAP to perform following operations on a Doubly Linked List
a) Insert a new node at beginning of linked list
b) Deletion of a node from end of the linked list
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
struct Node* previous;
};
struct Node* takeInput()
{
struct Node* head = NULL;
struct Node* tail = NULL;
struct Node* pre = NULL;
int n;
printf ( "Enter number of nodes you want to enter : " );
scanf ( "%d", &n );
int i = 0;
while ( i < n )
{
int nodeData;
printf ( "Enter data of %d node : ", i+1 );
scanf ( "%d", &nodeData );
struct Node* newNode = ( struct Node* ) malloc( sizeof ( struct Node ) );
newNode -> data = nodeData;
newNode -> next = NULL;
newNode -> previous = NULL;
else
{
tail -> next = newNode;
tail = newNode;
tail -> previous = pre;
pre = newNode;
}
i++;
}
return head;
}
if ( head == NULL )
{
newNode -> next = NULL;
newNode -> previous = NULL;
head = newNode;
return head;
}
newNode -> next = head;
newNode -> previous = NULL;
head -> previous = newNode;
head = newNode;
return head;
}
struct Node* deleteAtEnd ( struct Node* head )
{
if ( head == NULL )
{
printf ( "Doubly Linked List is empty\n" );
return NULL;
}
int main()
{
struct Node* head = takeInput();
printDLL ( head );
printf ( "Inserting a node at beginning\n" );
head = insertAtStart ( head );
printDLL ( head );
printf ( "Deleting node from end\n" );
head = deleteAtEnd ( head );
printDLL ( head );
return 0;
}
Output
6. Implement two stacks using a single array.
#include <stdio.h>
#define SIZE 10
int ar[SIZE];
ar[++top1] = data;
else
ar[--top2] = data;
else
void pop_stack1()
if (top1 >= 0)
else
void pop_stack2()
else
void print_stack1()
int i;
printf("\n");
void print_stack2()
{
int i;
printf("\n");
int main()
int ar[SIZE];
int i;
int num_of_ele;
push_stack1(i);
push_stack2(i);
print_stack1();
print_stack2();
num_of_ele = top1 + 1;
while (num_of_ele)
pop_stack1();
--num_of_ele;
pop_stack1();
return 0;
Output
struct listNode {
int eno;
};
node* n;
n = (node*)malloc(sizeof(node));
if (n){
n->eno = info;
if (!head){
n->next = n;
head = n;
return;
do {
temp = temp->next;
temp->next = n;
n->next = head;
head = n;
else {
printf("Overflow\n");
}
return;
void deletionAtEnd(){
if (!head){
printf("List Empty\n");
return;
if (head->next == head){
free(temp);
head = NULL;
return;
do {
temp = temp->next;
temp->next = head;
free(temp2);
return;
void print(){
if (!head){
printf("List Empty\n");
return;
}
node* temp = head;
do {
temp = temp->next;
printf("\n");
return;
int main(){
int c,e;
while (1){
scanf("%d",&c);
if (c==4){
printf("Fin!\n");
break;
switch (c){
case 1:
scanf("%d",&e);
insertionAtFront(e);
printf("Element Inserted!\n");
break;
case 2:
deletionAtEnd();
printf("Element Deleted!\n");
break;
case 3:
print();
break;
return 0;
Output:
8. Create a linear queue using linked list and implement different different
operations such as insert ,delete and display the queue element
#include <stdio.h>
#include <stdlib.h>
struct queue{
int data;
};
ptr = ptr->next;
temp->data = value;
temp->next = NULL;
ptr->next = temp;
return top;
top = top->next;
return top;
printf("%d\n", top->data);
top = top->next;
int main(){
struct queue *top;
top->data = 14;
top->next = NULL;
tail = top;
print(top);
top = dequeue(top);
print(top);
Output:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *top = NULL;
void linkedListTraversal(struct Node *ptr)
{
while (ptr != NULL)
{
printf("Element: %d\n", ptr->data);
ptr = ptr->next;
}
}
int isEmpty(struct Node *top)
{
if (top == NULL)
return 1;
else
return 0;
}
int isFull(struct Node *top)
{
struct Node *p = (struct Node *)malloc(sizeof(struct Node));
if (p == NULL)
return 1;
else
return 0;
}
struct Node *push(struct Node *top, int x)
{
if (isFull(top)){
printf("Stack Overflow\n");
}
else{
struct Node *n = (struct Node *)malloc(sizeof(struct Node));
n->data = x;
n->next = top;
top = n;
return top;
}
}
int pop(struct Node *tp)
{
if (isEmpty(tp)){
printf("Stack Underflow\n");
}
else{
struct Node *n = tp;
top = (tp)->next;
int x = n->data;
free(n);
return x;
}
}
int peek(int pos)
{
struct Node *ptr = top;
for (int i = 0; (i < pos - 1 && ptr != NULL); i++){
ptr = ptr->next;
}
if (ptr != NULL)
return ptr->data;
else
return -1;
}
int main()
{
int choice, a, b;
printf("1 : To push\n");
printf("2 : To peek\n");
printf("3 : To traverse\n");
printf("4 : To pop\n");
printf("5 : To Exit\n");
do
{
printf("Enter the choice (1/2/3/4/5): ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
printf("Enter the element to push : ");
scanf("%d", &a);
top = push(top, a);
break;
}
case 2:
{
printf("Enter the index whose element you want to find : ");
scanf("%d", &b);
printf("Value at position %d is : %d\n", b, peek(b));
break;
}
case 3:
{
linkedListTraversal(top);
break;
}
case 4:
{
pop(top);
break;
}
case 5:
{
printf(" EXIT POINT ");
break;
}
default:
{
printf("Please Enter a Valid Choice(1/2/3/4)");
}
}
} while (choice != 5);
return 0;
}
Output:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
// creating node
struct node *create(int data)
{
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
p->data = data;
p->left = NULL;
p->right = NULL;
return p;
}
void preorder(struct node *p)
{
if (p != NULL)
{
printf("%d\n", p->data);
preorder(p->left);
preorder(p->right);
}
}
void postorder(struct node *p)
{
if (p != NULL)
{
postorder(p->left);
postorder(p->right);
printf("%d\n", p->data);
}
}
void inorder(struct node *p)
{
if (p != NULL)
{
inorder(p->left);
printf("%d\n", p->data);
inorder(p->right);
}
}
int main()
{
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
struct node *p1 = create(38);
struct node *p2 = create(67);
struct node *p3 = create(2);
struct node *p4 = create(48);
struct node *p5 = create(59);
p1->left = p2;
p1->right = p3;
p2->left = p4;
p2->right = p5;
printf("Preorder\n");
preorder(p1);
printf("Inorder\n");
inorder(p1);
printf("Postorder\n");
postorder(p1);
return 0;
}
Output:
11. Implement Selection sort , Insertion sort , Bubble sort , Merge sort ,
Quick sort and Heap sort using array as a data structure .
#include <stdio.h>
*a = *b;
*b = temp;
int i, j;
int i, element, j;
element = array[i];
j = i - 1;
array[j + 1] = array[j];
j = j - 1;
array[j + 1] = element;
// Selection Sort
void selectionSort(int array[], int n){
int i, j, min_element;
min_element = i;
min_element = j;
swap(&array[min_element], &array[i]);
// quick sort
i++;
swap(&arr[i], &arr[j]);
return (i + 1);
// QuickSort Function
// MergeSort Function
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
i = 0;
j = 0;
k = l;
arr[k] = L[i];
i++;
else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
// printing an array
int i;
for (i = 0; i < size; i++)
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
largest = l;
largest = r;
if (largest != i) {
swap(&arr[i], &arr[largest]);
solveHeap(arr, N, largest);
solveHeap(arr, N, i);
swap(&arr[0], &arr[i]);
solveHeap(arr, i, 0);
int main() {
bubbleSort(array, size);
printArray(array, size);
printf("\n");
insertionSort(array1, n);
printArray(array1, n);
printf("\n");
selectionSort(array2, size);
printArray(array2, size);
printf("\n");
quickSort(arr, 0, n - 1);
printArray(arr, n);
printf("\n");
int arr1[] = {85, 24, 63, 45, 17, 31, 96, 50};
printArray(arr1, arr_size);
printf("\n");
printf("HEAP SORT\n ");
heapSort(arr2, N);
printArray(arr2, N);
return 0;
Output:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct DataItem
int data;
int key;
};
if (hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
++hashIndex;
hashIndex %= 20;
return NULL;
{
struct DataItem *item = (struct DataItem *)malloc(sizeof(struct DataItem));
item->data = data;
item->key = key;
++hashIndex;
hashIndex %= 20;
hashArray[hashIndex] = item;
void display()
int i = 0;
if (hashArray[i] != NULL)
printf("\n");
int main()
dummyItem->data = -1;
dummyItem->key = -1;
insert(1, 44);
insert(2, 7);
insert(42, 49);
insert(4, 86);
insert(12, 65);
insert(14, 18);
insert(17, 11);
insert(13, 8);
insert(37, 16);
display();
item = search(17);
if (item != NULL)
else
return 0;
Output:
#include <stdio.h>
int q[20], top = -1, front = -1, rear = -1, a[20][20], vis[20], stack[20];
int del(){
int k;
return (0);
else {
k = q[front++];
return (k);
if (top == 19)
else
stack[++top] = item;
int pop(){
int k;
if (top == -1)
return (0);
else {
k = stack[top--];
return (k);
if (rear == 19)
printf("QUEUE FULL");
else {
if (rear == -1) {
q[++rear] = item;
front++;
else
q[++rear] = item;
int p, i;
add(s);
vis[s] = 1;
p = del();
if (p != 0)
while (p != 0) {
add(i);
vis[i] = 1;
p = del ();
if (p != 0)
if (vis[i] == 0)
bfs(i, n);
int i, k;
push(s);
vis[s] = 1;
k = pop();
if (k != 0)
while (k != 0) {
push(i);
vis[i] = 1;
k = pop();
if (k != 0)
if (vis[i] == 0)
dfs(i, n);
int main() {
int n, i, s, ch, j;
char c, dummy;
printf("ENTER THE NUMBER VERTICES ");
scanf("%d", &n);
scanf("%d", &a[i][j]);
printf("\n");
do {
vis[i] = 0;
printf("\n1.B.F.S");
printf("\n2.D.F.S");
scanf("%d", &ch);
scanf("%d", &s);
switch (ch) {
case 1:
bfs(s, n);
break;
case 2:
dfs(s, n);
break;
scanf("%c", &dummy);
scanf("%c", &c);
Output: