Data Structures Lab ALL EXPERIMENTS FILE
Data Structures Lab ALL EXPERIMENTS FILE
Data Structures Lab ALL EXPERIMENTS FILE
Batch: 34
Roll.no: - R2142211010
Experiment 1
Program 1: Find sum of arrays using recursion.
Code:
#include <iostream>
if (n <= 0)
return 0;
int main()
return 0;
}
Program 2: Create an array ‘a1’ with ‘n’ elements. Insert an element in ith position of ‘a1’
and also delete an element from jth position of ‘a1’
#include<iostream>
cout<<a[i]<<"\t"; }}
int main(){
int n , a1[100] , i , j;
cin>>n;
cin>>a1[i]; }
cout<<"Array : ";
display(a1 , n);
cout<<"\n\nEnter position (form 1 to "<<n<<") at which you want to insert the element : ";
cin>>i;
if(i > n) {
cout<<"Invalid position";}
else {
a1[x+1] = a1[x]; }
cout<<"Enter the element to be inserted : ";
cin>>a1[i-1];
n = n+1;
display(a1 , n); }
cout<<"\n\nEnter position (form 1 to "<<n<<") from where you want to delete the
element : ";
cin>>j;
cout<<"Invalid position";}
else{
a1[x] = a1[x+1]; }
n = n-1;
display(a1 , n); }
return 0;
#include <cstring>
int main()
{ char s[20];
int i;
cin>>s;
for(i=0;i<=strlen(s);i++) {
{ s[i]=s[i]+32; } }
return 0;
Program 4: Find the sum of rows and columns of matrix of given order
#include <iostream>
int main()
{ int size1;
size1 = 3;
int Arr[size1][size1];
int a, b, c;
cin>>Arr[a][b]; } }
c = 0;
c += Arr[a][b]; }
c = 0;
c += Arr[b][a]; }
return 0;
}
Program 5: Find the product of matrix using pointers
#include <stdio.h>
#define ROW 3
#define COL 3
int main()
{ int mat1[ROW][COL];
int mat2[ROW][COL];
int product[ROW][COL];
matrixInput(mat1);
matrixInput(mat2);
matrixMultiply(mat1, mat2, product);
matrixPrint(product);
return 0; }
printf("\n"); }}
int sum;
{ sum = 0;
#include <stdio.h>
int i;
if (arr[i] == x)
return i;
return -1;
int main()
int x;
scanf("%d",&x);
(result == -1);
return 0;
#include <stdio.h>
int Search(int arr[], int l, int r, int a)
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == a)
return mid;
if (arr[mid] > a)
return -1;
int main(void)
int a ;
scanf("%d", &a);
if (res == -1)
return 0;
}
#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
char course[10]
};
int main(){
int i;
for(i=0;i<10;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
printf("\nEnter Course:");
scanf("%s",&st[i].course);
}
printf("\nStudent Information List:");
for(i=0;i<10;i++){
return 0;
}
Program 9: Merging of Two Arrays
#include<iostream>
void mergeArrays(int arr1[], int arr2[], int n1,int n2, int arr3[])
{ int i = 0, j = 0, k = 0;
arr3[k++] = arr1[i++];
else
arr3[k++] = arr2[j++];
arr3[k++] = arr1[i++];
arr3[k++] = arr2[j++]; }
int main(){
int arr3[n1+n2];
return 0;
Experiment 2
Write C program, compile, execute and test the code using Linux C compiler with suitable
test cases.
1. Design a union ‘product’ to store the details of the product purchased like product
name, price per unit, number of quantities purchased, and amount spent. Get the
name, price per unit, and number of quantities of the product purchased. Calculate
the amount spent on the product and then display all the details of the procured
product.
#include <stdio.h>
#include <string.h>
union product
int qnt;
char n[10];
float price;
};
int main()
union product t;
int i, n, q;
float m, p;
char a[20];
scanf("%d", &n);
scanf("%s", t.n);
strcpy(a, t.n);
scanf("%f", &t.price);
p=t.price;
scanf("%d", &t.qnt);
q=t.qnt;
m=m+p*q;
};
printf("Your total amount is %f",m);
Experiment 3
List of Lab Activities:
Write algorithm and C program, compile, execute and test the code using Linux C
i) Implement single Linked List data structure and its operations like insert and delete in
the beginning/end and nth position of the list, and display the items stored in the linked
list.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
if (prev_node == NULL) {
return;
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
last->next = new_node;
return;
*head_ref = temp->next;
free(temp);
return;
prev = temp;
temp = temp->next;
prev->next = temp->next;
free(temp);
node = node->next;
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 1);
printList(head);
printf("\n");
insertAtBeginning(&head, 2);
printList(head);
printf("\n");
insertAtBeginning(&head, 3);
insertAtEnd(&head, 4);
printList(head);
printf("\n");
insertAfter(head->next, 5);
printList(head);
printf("\n");
deleteNode(&head, 3);
printList(head);
printf("\n");
}
ii)Using single linked list and functions implement Stack and its operations like insert,
#include <iostream>
struct Node
int data;
Node* link;
};
Node* top;
if (!temp)
exit(1);
temp->data = data;
temp->link = top;
top = temp;
int isEmpty()
int peek(){
if (!isEmpty())
return top->data;
else
exit(1);
void pop()
Node* temp;
if (top == NULL)
exit(1);
else
temp = top;
top = top->link;
free(temp);
void display()
Node* temp;
if (top == NULL)
exit(1);
else
temp = top;
temp = temp->link;
int main()
{
push(11);
display();
printf("\n");
push(22);
display();
printf("\n");
push(33);
push(44);
display();
pop();
pop();
display();
return 0;
}
Experiment 4
1) Write C program, compile, execute and test the code using Linux
• Using array and functions implement Stack and its operations like insert,
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int STACK[MAX],TOP;
if(TOP==MAX-1)
printf("\nFull stack\n");
return;
TOP++;
stack[TOP]=item;
int i=0;
if(TOP==-1)
{
return;
else{
for(i=TOP-1;i >=0;i--)
printf("\n%d",stack[i]);
printf("\n");
int deletedItem;
if(TOP==-1)
return;
else{
deletedItem=stack[TOP];
TOP--;
printf("%d successfully deleted \n",deletedItem);
return;
void main()
int ITEM=0;
int choice=0;
TOP=-1;
while(1)
printf("\n 1: display");
printf("\n 2: insert");
printf("\n 3: remove");
printf("\n 4: Exit");
scanf("%d",&choice);
switch(choice)
{ case 1:
display(STACK);
break;
case 2:
push(STACK,ITEM);
break;
case 3:
pop(STACK);
break;
case 4:
exit(0);
default:
break;
}}}
2) Reverse a String using stack
#include <stdio.h>
#include <string.h>
int top=-1;
int item;
char stack_string[MAX];
char popChar(void);
char fitem;
int isEmpty(void);
int isFull(void);
int main()
char str[MAX];
int i;
scanf("%[^\n]s",str);
fitem=str[0];
for(i=0;i<strlen(str);i++)
if(top==MAX-1){
else
top=top+1;
stack_string[top]=str[i];
top=top+1;
for(i=strlen(str);i>=0;i--)
if(top==-1)
stack_string[top];
top=top-1;
printf("%c",stack_string[i]);
return 0;
Experiment 5
Write C program, compile, execute and test the code using Linux
1) Using array and functions implement Queue data structure and its 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("4.Quit \n");
scanf("%d", &choice);
switch (choice)
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
void insert()
int add_item;
if (rear == MAX - 1)
else
if (front == - 1)
front = 0;
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
void delete()
return ;
else
front = front + 1;
void display()
int i;
if (front == - 1)
else
printf("Queue is : \n");
printf("\n");
}
2) Check whether the string is palindrome or not using array and Queue.
#include<iostream>
class node{
public:
char v;
node *next;
node *prev;
};
class queue{
node *front,*rear;
int size;
public:
queue()
front=new node();
rear=NULL;
size=0;
void enqueue(char a)
newnode->v=a;
if(rear!=NULL)
rear->next=newnode;
newnode->prev=rear;
newnode->next=NULL;
rear=newnode;
else
front->next=newnode;
front->prev=NULL;
newnode->next=NULL;
newnode->prev=front;
rear=newnode;
size++;
int si()
return size;
int check()
int count;
p=front->next;
if(p->v==rear->v)
count++;
p=p->next;
rear=rear->prev;
}
return count;
};
int main()
queue q1;
int s,num;
cin>>s;
char a[s],n;
cin>>a;
for(int i=0;i<s;i++)
n=a[i];
q1.enqueue(n);
num=q1.check();
int ns=s%2;
if(ns==1)
if(num==(s-1))
{
cout<<"Palindrome"<<endl;
else
cout<<"not a Palindrome";
else
if(num==s)
cout<<"Palindrome"<<endl;
else
cout<<"not a Palindrome";
return 0;
}
Experiment 6
Write C program, compile, execute and test the code using Linux
#include <stdio.h>
#include<stdlib.h>
#include <assert.h>
struct Node
int data;
};
dummy.next = NULL;
while (1)
if (a == NULL)
tail->next = b;
break;
else if (b == NULL)
tail->next = a;
break;
MoveNode(&(tail->next), &a);
else
MoveNode(&(tail->next), &b);
tail = tail->next;
return(dummy.next);
assert(newNode != NULL);
*sourceRef = newNode->next;
newNode->next = *destRef;
*destRef = newNode;
}
void push(struct Node** head_ref, int new_data){
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
while (node!=NULL)
node = node->next;
int main()
push(&a, 10);
push(&a, 5);
push(&b, 20);
push(&b, 3);
push(&b, 2);
printList(res);
return 0;
2) Read the numbers from the user into an array and sort them using the insertion Sort
algorithm
#include <math.h>
#include <stdio.h>
int i, key, j;
key = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int i;
printf("\n");
int main()
{ int q;
int arr[q];
scanf("%d",&q);
for(int w = 0;w<q;w++){
scanf("%d",&arr[w]);
};
insertionSort(arr, q);
printArray(arr, q);
return 0;
}
Experiment 7
Write algorithm and C program, compile, execute and test the code using Linux
#include <stdio.h>
#include <stdlib.h>
int main()
int array[100],search_key,i,j,n,low,high,location,choice;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&array[i]);
scanf("%d",&search_key);
printf("___________________\n");
printf("1.LINEAR SEARCH\n");
printf("2.BINARY SEARCH\n");
printf("___________________\n");
scanf("%d",&choice);
switch(choice)
case 1:
linear_search(search_key,array,n);
break;
case 2:
binary_search(search_key,array,n);
break;
default:
exit(0);
return 0;
}
void linear_search(int search_key,int array[100],int n)
int i,location;
for(i=1;i<=n;i++)
if(search_key == array[i])
location = i;
printf("______________________________________\n");
printf("______________________________________\n");
int mid,i,low,high;
low = 1;
high = n;
i=1;
while(search_key != array[mid])
low = 1;
high = mid+1;
mid = (low+high)/2;
else
low = mid+1;
high = n;
mid = (low+high)/2;
printf("__________________________________\n");
printf("location=%d\t",mid);
printf("Search_Key=%d Found!\n",search_key);
printf("__________________________________\n");
}
Experiment 8
Write C program, compile, execute and test the code using Linux
10 families based on the last three digits. Example: Student with SAP-ID
#include <stdio.h>
#include <stdlib.h>
struct set
int SAPID;
int MARKS;
};
int size = 0;
int n,sum=0,m;
n = SAPID;
while(n>0)
m=n%10;
sum=sum+m;
n=n/10;
}
sum = sum-5;
return 0;
};
int checkPrime(int n)
int i;
if (n == 1 || n == 0)
return 0;
if (n % i == 0)
return 0;
return 1;
int getPrime(int n)
if (n % 2 == 0)
n++;
}
while (!checkPrime(n))
n += 2;
return n;
void init_array()
capacity = getPrime(capacity);
array[i].SAPID = 0;
array[i].MARKS = 0;
if (array[index].MARKS == 0)
array[index].SAPID = SAPID;
array[index].MARKS = MARKS;
size++;
array[index].MARKS = MARKS;
else
if (array[index].MARKS == 0)
else
array[index].SAPID = 0;
array[index].MARKS = 0;
size--;
printf("\n Key (%d) has been removed \n", SAPID);
void display()
int i;
if (array[i].MARKS == 0)
else
int size_of_hashtable()
return size;
int main()
{
int choice, SAPID, MARKS, n;
int c = 0;
init_array();
do
scanf("%d", &choice);
switch (choice)
case 1:
scanf("%d", &SAPID);
scanf("%d", &MARKS);
insert(SAPID, MARKS);
break;
case 2:
scanf("%d", &SAPID);
remove_element(SAPID);
break;
case 3:
n = size_of_hashtable();
break;
case 4:
display();
break;
default:
printf("Invalid Input\n");
scanf("%d", &c);
} while (c == 1);
}
2. Implement a Hash table using arrays. Perform Insert, Delete and Search
operations on the hash table using the above Hash function (S.No.1). Adopt a
inserting data.
#include<stdio.h>
#define size 7
int arr[size];
void init()
int i;
arr[i] = -1;
if(arr[key] == -1)
arr[key] = value;
else
if(arr[key] == value)
arr[key] = -1;
else
if(arr[key] == value)
printf("Search Found\n");
else
void print()
int i;
printf("arr[%d] = %d\n",i,arr[i]);
}
int main()
init();
insert(10);
insert(4);
insert(2);
insert(3);
printf("Hash table\n");
print();
printf("\n");
del(10);
print();
printf("\n");
del(5);
print();
printf("\n");
search(4);
return 0;
}
3. Implement a Hash table using arrays. Perform Insert, Delete and Search
operations on the hash table using the above Hash function (S.No.1) and with
#include<stdio.h>
#include<stdlib.h>
struct item
int key;
int value;
};
struct hashtable_item
{
int flag;
};
int size = 0;
void init_array()
int i;
array[i].flag = 0;
array[i].data = NULL;
int i = index;
struct item *new_item = (struct item*) malloc(sizeof(struct item));
new_item->key = key;
new_item->value = value;
while (array[i].flag == 1)
if (array[i].data->key == key)
array[i].data->value = value;
return;
i = (i + 1) % max;
if (i == index)
printf("\n Hash table is full, cannot insert any more item \n");
return;
array[i].flag = 1;
array[i].data = new_item;
size++;
int i = index;
while (array[i].flag != 0)
array[i].flag = 2;
array[i].data = NULL;
size--;
return;
i = (i + 1) % max;
if (i == index)
break;
void display()
int i;
for (i = 0; i < max; i++)
if (current == NULL)
else
printf("\n Array[%d] has elements -: \n %d (key) and %d(value) ", i, current->key, current-
>value);
int size_of_hashtable()
return size;
void main()
clrscr();
init_array();
do {
"\n4.Display Hashtable"
scanf("%d", &choice);
switch(choice)
case 1:
insert(key, value);
break;
case 2:
scanf("%d", &key);
remove_element(key);
break;
case 3:
n = size_of_hashtable();
case 4:
display();
break;
default:
printf("Wrong Input\n");
scanf("%d", &c);
}while(c == 1);
}
Experiment 9
Write C program, compile, execute and test the code using Linux
#include <iostream>
#include <string>
#include <queue>
struct ListNode
int data;
ListNode* next;
};
struct BinaryTreeNode
int data;
};
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
temp->data = data;
return temp;
queue<BinaryTreeNode *> q;
if (head == NULL)
root = NULL;
return;
root = newBinaryTreeNode(head->data);
q.push(root);
head = head->next;
while (head)
leftChild = newBinaryTreeNode(head->data);
q.push(leftChild);
head = head->next;
if (head)
rightChild = newBinaryTreeNode(head->data);
q.push(rightChild);
head = head->next;
parent->left = leftChild;
parent->right = rightChild;
if (root)
inorderTraversal( root->left );
inorderTraversal( root->right );
}
int main()
push(&head, 36);
push(&head, 30);
push(&head, 25);
push(&head, 15);
push(&head, 12);
push(&head, 10);
BinaryTreeNode *root;
convertList2Binary(head, root);
cout << "Inorder Traversal of the constructed Binary Tree is: \n";
inorderTraversal(root);
return 0;
2. Construct a Binary Tree and perform Inorder, Preorder and Postorder Traversal.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
};
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
if (node == NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
if (node == NULL)
return;
printInorder(node->left);
printInorder(node->right);
if (node == NULL)
return;
printPreorder(node->left);
printPreorder(node->right);
int main()
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printPreorder(root);
printInorder(root);
getchar();
return 0;
#include <iostream>
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]);
heapify(arr, n, largest);
}
}
heapify(arr, n, i);
swap(arr[0], arr[i]);
heapify(arr, i, 0);
int main()
heapSort(arr, n);
printArray(arr, n);
}
Experiment 10
Write C program, compile, execute and test the code using Linux
1. Accept the vertices and edges for a graph and stores it as an adjacency matrix.
Implement functions to print in-degree and out-degree of any vertex 'i'. Also
#include <stdio.h>
int N, M;
int arr[][2])
Adj[i][j] = 0;
}
}
int x = arr[i][0];
int y = arr[i][1];
Adj[x][y] = 1;
Adj[y][x] = 1;
printf("\n");
int main()
N = 5;
int arr[][2]
= { { 1, 2 }, { 2, 3 },
{ 4, 5 }, { 1, 5 } };
M = sizeof(arr) / sizeof(arr[0]);
int Adj[N + 1][N + 1];
createAdjMatrix(Adj, arr);
printAdjMatrix(Adj);
return 0;
2.Accept the graph as an adjacency matrix and check if the graph is undirected
#include <stdio.h>
#include <stdlib.h>
struct AdjListNode {
int dest;
};
struct AdjList {
};
struct Graph {
int V;
};
struct AdjListNode* newAdjListNode(int dest)
= (struct AdjListNode*)malloc(
sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
graph->V = V;
V * sizeof(struct AdjList));
int i;
graph->array[i].head = NULL;
return graph;}
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
else {
check = graph->array[src].head;
check = check->next;
check->next = newNode;
newNode = newAdjListNode(src);
if (graph->array[dest].head == NULL) {
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
else {
check = graph->array[dest].head;
check = check->next;
check->next = newNode;
}
void printGraph(struct Graph* graph)
int v;
while (pCrawl) {
pCrawl = pCrawl->next;
printf("\n");
int main()
int V = 5;
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
printGraph(graph);
return 0;