20CA2013 - Data Structures Lab Manual
20CA2013 - Data Structures Lab Manual
Name Signature
Prepared by Dr. C. Beulah Christalin Latha
INDEX OF EXPERIMENTS
S.No
Title of the Program Page.
No
1. Array Operations 3
2. Searching an array using Linear Search 7
3. Checking a Palindrome using a Stack 10
4. Queue Operations 12
5. Singly Linked List 16
6. Circular Linked List 19
7. Doubly Linked List 22
8. Selection Sort 25
9. Bubble Sort 28
10. Insertion Sort 31
11. Sorting a Linked List 34
12. Solving Josephus Problem 37
Step 3: Define a function void print(int a[10], int n) to print the elements of the
array.
Step 4: Define a function void insert(int a[10], int n, int t, int pos) to insert an
element ‘t’ into the array at position ‘pos’.
i. Move the elements from the last element till the element in position ‘pos’ to
the next position using a loop.
Source Code:
#include <iostream>
using namespace std;
void read(int a[10], int n)
{
int i;
cout<<"Enter the elements one by one ";
for (i=0;i<n;i++)
cin>>a[i];
}
void print(int a[10],int n)
{
int i;
cout<<"\nThe array elements are ";
for (i=0;i<n;i++)
cout<<a[i]<<'\t';
}
void insert(int a[10],int n,int k,int pos)
{
int i,t1,t2;
t1=a[pos];
a[pos]=k;
for (i=pos+1;i<n+1;i++)
{
t2=a[i];
a[i]=t1;
t1=t2;
}
}
}
int main()
{
int a[10],n,pos,k;
cout<<"\nEnter the number of elements in the array : ";
cin>>n;
read(a,n);
print(a,n);
cout<<"\nEnter the element to be inserted : ";
cin>>k;
cout<<"\nEnter the position : ";
cin>>pos;
insert(a,n,k,pos);
n=n+1;
print(a,n);
cout<<"\nEnter the element to be deleted : ";
cin>>k;
del(a,n,k);
n=n-1;
print(a,n);
}
Sample Input/Output:
Result:
The above program was executed and the output was verified for a sample set of input
values.
Step 3: Define a function void print(int a[10], int n) to print the elements of the
array.
Step 4: Define a function int search(int a[10], int n, int t) to search an element ‘t’ in the
array.
a. Compare each element of the array with ‘t’ using a loop.
b. If ‘t’ is found, return 1 else return 0.
Step 5: In main() function, declare the required elements.
Step 6: Read the number of elements of the array.
Step 7: Read the array elements using the read() function.
Step 8: Read the element to be searched ‘t’.
Step 9: If the element is found, display that the element is found.
Step 10: Otherwise display that the element is not found.
Step 11: Stop.
#include <iostream>
using namespace std;
void read(int a[10], int n)
{
int i;
cout<<"Enter the elements one by one ";
for (i=0;i<n;i++)
cin>>a[i];
int main()
{
int a[10],n,k;
cout<<"\nEnter the number of elements in the array : ";
cin>>n;
read(a,n);
cout<<"Enter the element to search : ";
cin>>k;
if (search(a,n,k))
cout<<k<<" is found in the array";
else
cout<<k<<" is not found in the array";
}
Sample Input/Output:
Enter the number of elements in the array : 5
Enter the elements one by one 5 6 1 5 1
Enter the element to search : 12
12 is not found in the array
Result:
The above program was executed and the output was verified for a sample set of input
values.
int isempty()
{
if (top==0)
return 1;
else
return 0;
}
void push(char s)
{
if (!isfull()) a[top++]=s;
else
cout<<"\nStack is full" ; } char pop()
{
if (!isempty()) return a[--top];
else
return '\0';
}};
int main()
{
stack k;
string s1, s2=""; char c; int
i; k.init();
cout<<"\nEnter a string : "; cin>>s1; for(i=0;i<s1.length()++) k.push(s1[i]);
do{
c=k.pop(); cout<<c<<'\t'; if(c!='\0') s2=s2+c;
}
while (c!='\0'); cout<<s2; if
(s1==s2)
cout<<s1<<" is a palindrome\n"; else
cout<<s1<<" is not a palindrome\n";
}
Sample Input/Output:
Enter a string : malayalam
malayalam is a palindrome
Result:
The program to check the palindrome using a stack is successfully completed and the
output is verified.
Ex. No. 4
Date:
QUEUE OPERATIONS
Aim : To write a program to implement queue operations.
Algorithm:
1. Start
2. Define a queue using a structure and implement the operations as structure
member functions.
3. Define the operation init() initializing front and rear to zero.
4. Define the isempty() operation. If front=rear, return 1 else return 0.
5. Define the isfull() operation. If rear=MAX-1, return 1 else return 0.
6. Define insert() operation; check for overflow and store the element to be inserted in
the position of rear and increment rear by 1.
7. Define the del() operation; check for underflow and return the element in the
position of front and increment front by 1.
8. In main() function, create a queue variable and call the operations based on the
choice from the user.
9. Stop
Source Code:
#include <iostream>
using namespace std;
#define MAX 100
struct queue
{
int a[MAX];
int rear, front;
void init()
{
front=0;
rear=0;
}
int isFull()
{
if (rear==MAX-1)
return 1;
else
return 0;
Department of Digital Sciences 12
2020-2021 20CA2013 DATA STRUCTURES LAB
}
int isEmpty()
{
if (front==rear)
return 1;
else
return 0;
};
void insert(int s)
{
if (!isFull())
a[rear++]=s;
else cout<<"\nQueue is full" ;
}
int del()
{
if (!isEmpty())
return a[front++];
else
{
cout<<"\nQueue is empty";
return -1;
}
}
};
int main()
{
queue q;
int op,t;
q.init();
do
{
cout<<"\n1. Insert"; cout<<"\n2. Delete"; cout<<"\n3. Quit";
cout<<"\nEnter your option : ";
cin>>op;
switch(op)
{
case 1:
cout<<"\nEnter the element to be inserted : ";
cin>>t;
Sample Input/Output:
Enter the data :1
Any more data? :y
Enter the data :2
Any more data? :y
Enter the data :3
Any more data? :y
Enter the data :4
Any more data? :y
Enter the data :5
Any more data? :n
The elements are :
12345
Result:
The above program was executed and the output was verified for a sample set of input
values.
Aim:
To create a doubly linked list and display the elements.
Algorithm:
Step 1: Start
Step 2: Create new node(node1)
Step 3: Assign n to data
Step 4: If the linked list is empty, make the new node as the start node and assign null
to the next pointer. Connect the previous node through the previous pointer.
Step 5: If the list is already existing, add the new node to the end of the linked list.
Step 6: Create a pointer to the node and point it to the start node. Display its value.
Step 7: Move the pointer to the next node.
Step 8: Repeat steps 6-8 till the pointer reaches the last node.
Step 0: In main() function, create a list.
Step 10: Add elements to the linked list.
Step 11: Display the elements.
Step 12: Stop.
Aim:
To solve Josephus' problem using a circular linked list.
Algorithm:
Step 1: Start
Step 2: Create a new start node and assign a pointer ptr for linking.
Step 3: Create subsequent players and arrange them in a circular linked list.
Step 4: For the elimination, take a variable k which is the number of players to be
skipped.
Step 5: kth element will be eliminated and the ptr moves to the next element.
Step 6: Step 5 is repeated till we get the winner.
Step 8: Stop
Source Code:
#include <iostream>
using namespace std;
struct node
{
int playerID;
node *next;
};
int main()
{
node *start, *ptr, *node1;
int n, k, i, c;
cout<<"\nEnter the number of players : ";
cin>>n;
start=new node();
start->playerID = 1;
ptr=start;
for (i=2;i<=n;i++)
Department of Digital Sciences 37
2020-2021 20CA2013 DATA STRUCTURES LAB
{
node1=new node();
node1->playerID=i;
ptr->next=node1;
node1->next=start;
ptr=node1;
}
//Eliminate players
cout<<"\nEnter the value of k : ";
cin>>k;
for (c=n;c>1;c--)
{
for (i=1;i<k;i++)
{
ptr=ptr->next;
cout<<"counting "<<ptr->playerID<<"\n";
}
cout<<endl<<ptr->next->playerID<<" is eliminated\n";
ptr->next=ptr->next->next;
}
cout<<"Winner is : "<<ptr->playerID<<endl;
}
Sample Input/Output:
1. Enter the number of players : 6
Enter the value of k : 3 counting 1 counting 2
3 is eliminated counting 4 counting 5
6 is eliminated counting 1 counting 2
4 is eliminated counting 5 counting 1
2 is eliminated counting 5 counting 1
5 is eliminated Winner is : 1
2. Enter the number of players : 8
Enter the value of k : 4 counting 1 counting 2 counting 3
4 is eliminated counting 5 counting 6 counting 7