Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

2020-2021 20CA2013 DATA STRUCTURES LAB

Karunya Nagar, Coimbatore – 641 114, Tamil Nadu, India.

SCHOOL OF SCIENCES, ARTS, MEDIA AND MANAGEMENT


DEPARTMENT OF DIGITAL SCIENCES
LAB MANUAL

20CA2013 DATA STRUCTURES LAB

Name Signature
Prepared by Dr. C. Beulah Christalin Latha

Approved by Dr. P. Ranjit Jeba Thangaiah

Department of Digital Sciences 1


2020-2021 20CA2013 DATA STRUCTURES LAB

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

Department of Digital Sciences 2


2020-2021 20CA2013 DATA STRUCTURES LAB
Ex. No. 1
Date:
ARRAY OPERATIONS
Aim: To implement array operations such as traversal, insertion and deletion.
Algorithm:
Step 1: Start
Step 2: Define a function void read(int a[10],int n) to read the elements of the array.

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.

ii. Insert ‘t’ at ‘pos’


Step 5: Define a function void del(int a[10], int n, int t) to delete an element ‘t’ from the
array.
i. Search for ‘t’ in the array using a loop.
ii. If the element is found, push all the elements after ‘t’ to the previous position.
Step 6: In main() function, create an array.
Step 7: Read the number of elements.
Step 8: Print the number of elements.
Step 9: Read the number to be inserted and the position and insert it by calling the
insert() function.
Step 10: Print the array with the inserted element.

Department of Digital Sciences 3


2020-2021 20CA2013 DATA STRUCTURES LAB
Step 11: Read the number to be deleted and call the del() function to delete the array.
Step 12: Print the array with the element removed.
Step 13: Stop.

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

void del(int a[10], int n, int k)


{
Department of Digital Sciences 4
2020-2021 20CA2013 DATA STRUCTURES LAB
int i;
for (i=0;i<n;i++)
if (a[i]==k)
break;
for (i;i<n;i++)
a[i]=a[i+1];

}
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:

Enter the number of elements in the array : 5


Enter the elements one by one 5 10 15 20 25

The array elements are 5 10 15 20 25


Enter the element to be inserted : 12

Department of Digital Sciences 5


2020-2021 20CA2013 DATA STRUCTURES LAB
Enter the position : 2

The array elements are 5 10 12 15 20 25


Enter the element to be deleted : 15

The array elements are 5 10 12 20 25

Result:
The above program was executed and the output was verified for a sample set of input
values.

Department of Digital Sciences 6


2020-2021 20CA2013 DATA STRUCTURES LAB
Ex. No. 2
Date :
SEARCHING AN ARRAY USING LINEAR SEARCH
Aim: To write a program to search an element in an array using linear search.
Algorithm:
Step 1: Start
Step 2: Define a function void read(int a[10],int n) to read the elements of the array.

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

Department of Digital Sciences 7


2020-2021 20CA2013 DATA STRUCTURES LAB
}
void print(int a[10],int n)
{
int i;
cout<<"\nThe array elements are ";
for (i=0;i<n;i++)
cout<<a[i]<<'\t';
}
int search(int a[10], int n, int k)
{
int i;
for (i=0;i<n;i++)
if (a[i]==k)
break;
if (i<n)
return 1;
else
return 0;
}

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";
}

Department of Digital Sciences 8


2020-2021 20CA2013 DATA STRUCTURES LAB

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

Enter the number of elements in the array : 5


Enter the elements one by one 5 10 12 15 7
Enter the element to search : 12
12 is found in the array

Result:
The above program was executed and the output was verified for a sample set of input
values.

Department of Digital Sciences 9


2020-2021 20CA2013 DATA STRUCTURES LAB
Ex. No. 3
Date:
CHECKING A PALINDROME USING A STACK
Aim :To write a program to check whether the given string is a palindrome or not
using a stack.
Algorithm:
1. Start
2. Define a character stack using a structure.
3. Define the operations init(), isempty(), isfull(), push(), pop() as structure member
functions.
4. Read a string as input
5. Push the string into the stack character by character till the length of the string is
reached.
6. Pop out the characters from the stack and store it in a string variable (s2)
7. Compare s2 with the input string
8. If both strings are equal, print that the input string is a palindrome
9. Otherwise, print that the input string is not a palindrome.
10. Stop
Source Code:
#include<iostream>
using namespace std;
#include<string.h>
#define MAX 100
struct stack
{
char a[MAX];
int top;
void init()
{
top=0;
}
int isfull()
{
if(top==MAX-1) return 1; else
return 0;
}

Department of Digital Sciences 10


2020-2021 20CA2013 DATA STRUCTURES LAB

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.

Department of Digital Sciences 11


2020-2021 20CA2013 DATA STRUCTURES LAB

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;

Department of Digital Sciences 13


2020-2021 20CA2013 DATA STRUCTURES LAB
q.insert(t);
break;
case 2: t=q.del(); if (t!=-1)
cout<<t<<" is deleted\n"; break; default:
coût<<"\Invalid option\n";
}
}while (op<3);
};
Sample Input/Output:
1.Insert
2.Delete
3.Quit
Enter your option : 1
Enter the element to be inserted : 34
1. Insert
2. Delete
3. Quit
Enter your option : 1
Enter the element to be inserted : 67
1. Insert
2. Delete
3. Quit
Enter your option : 1
Enter the element to be inserted : 9
1. Insert
2. Delete
3. Quit
Enter your option : 2
34 is deleted
1. Insert
2. Delete
3. Quit
Enter your option : 2
67 is deleted
1. Insert
2. Delete
3. Quit
Enter your option : 2
9 is deleted
1. Insert
2. Delete
3. Quit

Department of Digital Sciences 14


2020-2021 20CA2013 DATA STRUCTURES LAB
Enter your option : 2
Queue is empty
1. Insert
2. Delete
3. Quit
Enter your option : 3
Result:
The above program was executed and the output was verified for a sample set of input
values.

Department of Digital Sciences 15


2020-2021 20CA2013 DATA STRUCTURES LAB
Ex. No. 5
Date:
SINGLY LINKED LIST
Aim:
To create a singly 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.
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.
Source Code:
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};

Department of Digital Sciences 16


2020-2021 20CA2013 DATA STRUCTURES LAB
node* createNew(node *start, int n)
{
node *node1, *ptr;
node1 = new node();
node1->data = n;
if (start == NULL)
{
node1->next=NULL;
start = node1;
}
else
{
ptr=start;
while (ptr->next!=NULL)
ptr = ptr->next;
ptr->next=node1;
node1->next=NULL;
}
return start;
}

void display(node *start)


{
node *ptr;
ptr=start;
while (ptr!=NULL)
{
cout<<ptr->data<<'\t';
ptr=ptr->next;
}
}
int main()
{
node *start=NULL;
char more;
int data;
do
{
cout<<"\nEnter the data : ";

Department of Digital Sciences 17


2020-2021 20CA2013 DATA STRUCTURES LAB
cin>>data;
start=createNew(start,data);
cout<<"Any more data?";
cin>>more;
}while (more=='y');
cout<<”The elements are \n”;
display(start);
}
Sample Input/Output:
Enter the data :45
Any more data? :y
Enter the data :67
Any more data? : y
Enter the data : 9
Any more data? : y
Enter the data : 34
Any more data? : y
Enter the data : 7
Any more data? : n
The elements are
45 67 9 34 7
Result:
The above program was executed and the output was verified for a sample set of input
values.

Department of Digital Sciences 18


2020-2021 20CA2013 DATA STRUCTURES LAB
Ex. No. 6
Date:
CIRCULAR LINKED LIST
Aim:
To create a circular 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 the
start node to the next 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.
Source Code:
#include<iostream>
using namespace std;
struct node
{
int data;
node*next;
};
node *createNew(node *start, int n)
{

Department of Digital Sciences 19


2020-2021 20CA2013 DATA STRUCTURES LAB
node *node1=new node();
node1->data=n;
if (start==NULL)
start=node1;
else
{
node *ptr=start;
while (ptr>next!=start)
ptr=ptr->next;
ptr->next=node1;
}
node1->next=start;
return start;
}
void display(node* start)
{
node *ptr=start;
while (ptr->next != start)
{
cout<<ptr->data<<'\t';
ptr=ptr->next;
}
cout<<ptr->data<<endl;
}
int main()
{
node *start=NULL;
char more;
int data;
do
{
cout<<"Enter the data :";
cin>>data;
start=createNew(start,data);
cout<<"Any more data? :";
cin>>more;
}
while(more=='y');
cout<<"The elements are :\n ";
display(start);
}

Department of Digital Sciences 20


2020-2021 20CA2013 DATA STRUCTURES LAB

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.

Department of Digital Sciences 21


2020-2021 20CA2013 DATA STRUCTURES LAB
Ex. No. 7
Date:
DOUBLY LINKED LIST

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.

Department of Digital Sciences 22


2020-2021 20CA2013 DATA STRUCTURES LAB
Source Code:
#include<iostream>
using namespace std;
struct node
{
int data;
node *prev, *next;
};
node* createNew(node *start, int n)
{
node *ptr;
node *node1=new node();
node1->data=n;
if (start==NULL) //case 1
{
node1->prev=NULL;
node1->next=NULL;
start=node1;
}
else //case 2
{
ptr=start;
while (ptr->next!=NULL)
ptr=ptr->next;
ptr->next=node1;
node1->prev=ptr;
node1->next=NULL;
}
return start;
}
void display(node *start)
{
node *ptr;
ptr=start;
while (ptr!=NULL)
{
cout<<ptr->data<<'\t'; ptr=ptr->next;
}
}
int main()
{
Department of Digital Sciences 23
2020-2021 20CA2013 DATA STRUCTURES LAB
node *start=NULL;
char more;
int data;
do
{
cout<<"Enter the data :";
cin>>data;
start=createNew(start,data);
cout<<"Any more data? :";
cin>>more;
}
while(more=='y');
cout<<"Start element: ";
display(start);
}
Sample Input/Output:
Enter the data :10
Any more data? :y
Enter the data :20
Any more data? :y
Enter the data :30
Any more data? :y
Enter the data :40
Any more data? :y
Enter the data :50
Any more data? :n
Start element: 10 20 30 40 50
Result:
The above program was executed and the output was verified for a sample set of input
values.

Department of Digital Sciences 24


2020-2021 20CA2013 DATA STRUCTURES LAB
Ex. No. 8
Date:
SELECTION SORT
Aim:
To sort an array using selection sort.
Algorithm:
Step 1: Start
Step 2: Define a function read() to read the elements of the array.
Step 3: Define a function print() to print the elements of the array.
Step 4: Define a function to sort the elements using selection sort.
Step 5: Take the first element as the reference element and compare it with the rest of
the elements. Swap the elements which are not in ascending order.
Step 6. Repeat Step 5 by taking the subsequent elements as the reference elements.
Step 7: In main () function, call all the functions.
Step 8: Get values to be sorted from the user.
Step 9: Call the sort function to sort the elements using selection sort.
Step 8: Print the sorted elements.
Step 9: Stop.
Source Code:
#include <iostream>
using namespace std;
void read(int a[10], int n)
{ int i;
cout<<"Enter the elements : ";
for (i=0;i<n;i++)
cin>>a[i];
}
void print(int a[10], int n)
{
int i;

Department of Digital Sciences 25


2020-2021 20CA2013 DATA STRUCTURES LAB
for (i=0;i<n;i++)
cout<<a[i]<<'\t';
}
void sort(int a[10],int n)
{
int i,j,temp;
for (i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if (a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
int main()
{
int a[10],n;
cout<<"Enter the number of elements : ";
cin>>n;
read(a,n);
cout<<"\nThe array elements are\n";
print(a,n);
sort(a,n);
cout<<"\nThe sorted array elements are\n"; print(a,n);
}
Sample Input/Output:
1. Enter the number of elements : 5
Enter the elements : 34 67 23 89 13
The array elements are
34 67 23 89 13
The sorted array elements are
13 23 34 67 89
2. Enter the number of elements : 4
Enter the elements : -5 -9 0 4

Department of Digital Sciences 26


2020-2021 20CA2013 DATA STRUCTURES LAB
The array elements are
-5 -9 0 4
The sorted array elements are
-9 -5 0 4
Result:
The above program was executed and the output was verified for a sample set of input
values.

Department of Digital Sciences 27


2020-2021 20CA2013 DATA STRUCTURES LAB
Ex. No. 9
Date:
BUBBLE SORT
Aim:
To sort an array using bubble sort.
Algorithm:
Step 1: Start
Step 2: Define a function read() to read the elements of the array.
Step 3: Define a function print() to print the elements of the array.
Step 4: Define a function to sort the elements using selection sort.
Step 5: Sort the array by comparing adjacent elements. Swap the elements that are
not in ascending order.
Step 6: In main () function, call all the functions.
Step 7: Get values to be sorted from the user.
Step 8: Call the sort function to sort the elements using selection sort.
Step 9: Print the sorted elements.
Step 10: Stop.
Source Code:
#include <iostream>
using namespace std;
void read(int a[10], int n)
{
int i;
for(i=0;i<n;i++)
cin>>a[i];
}
void print(int a[10], int n)
{
int i;
for(i=0;i<n;i++)
cout<<a[i]<<'\t';
Department of Digital Sciences 28
2020-2021 20CA2013 DATA STRUCTURES LAB
}
void bubble(int a[10], int n)
{ int i,j,t;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
int main()
{
int a[10], n;
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Enter the elements of the array: ";
read(a,n);
cout<<"The array elements are: ";
print(a,n);
bubble(a,n);
cout<<"\nThe sorted elements are:\n";
print(a,n);
}
Sample Input/Output:
1. Enter the number of elements: 5
Enter the elements of the array: 23 12 89 65 32
The array elements are: 23 12 89 65 32
The sorted elements are:
12 23 32 65 89
2. Enter the number of elements: 5
Enter the elements of the array: -56 -3 0 78 -1
The array elements are: -56 -3 0 78 -1
The sorted elements are:

Department of Digital Sciences 29


2020-2021 20CA2013 DATA STRUCTURES LAB
-56 -3 -1 0 78
Result:
The above program was executed and the output was verified for a sample set of input
values.

Department of Digital Sciences 30


2020-2021 20CA2013 DATA STRUCTURES LAB
Ex. No. 10
Date:
INSERTION SORT
Aim:
To sort the elements of an array using insertion sort.
Algorithm:
Step 1: Start
Step 2: Define a function read() to read the elements of the array.
Step 3: Define a function print() to print the elements of the array.
Step 4: Define a function to sort the elements using selection sort.
Step 5: Sort the array by shifting the elements one by one and inserting each element
at the right position and it sorts from index 1.
Step 6: In main () function, call all the functions.
Step 7: Get values to be sorted from the user.
Step 8: Call the sort function to sort the elements using selection sort.
Step 9: Print the sorted elements.
Step 10: Stop.
Source Code:
#include <iostream>
using namespace std;
void read(int a[10], int n)
{
int i;
for(i=0;i<n;i++)
cin>>a[i];
}
void print(int a[10],int n)
{
int i;
for(i=0;i<n;i++)
cout<<a[i]<<'\t';

Department of Digital Sciences 31


2020-2021 20CA2013 DATA STRUCTURES LAB
}
void insert(int a[], int n)
{ int i,j,index;
for(i=1;i<n;i++)
{
index=a[i];
j=i-1;
while ((index<a[j]) && (j>=0))
{
a[j+1]=a[j]; j--;
}
a[j+1]=index;
}
}
int main()
{
int a[10], n;
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Enter the elements of the array: ";
read(a,n);
cout<<"\nThe array elements are\n";
print(a,n);
insert(a,n);
cout<<"\nThe sorted elements of are:\n";
print(a,n);
}
Sample Input/Output:
1. Enter the number of elements: 5
Enter the elements of the array: 34 89 2 65 13 The array elements are:
34 89 2 65 13
The sorted elements are:
2 13 34 65 89
2. Enter the number of elements: 5
Enter the elements of the array: -4 0 -1 6 2 The array elements are:
-4 0 -1 6 2 The sorted elements are:

Department of Digital Sciences 32


2020-2021 20CA2013 DATA STRUCTURES LAB
-4 -1 0 2 6
Result:
The above program was executed and the output was verified for a sample set of input
values.

Department of Digital Sciences 33


2020-2021 20CA2013 DATA STRUCTURES LAB
Ex. No. 11
Date:
SORTING A LINKED LIST
Aim:
To sort a singly linked list
Algorithm:
Step 1: Start
Step 2: Create a singly linked list and assign data to the nodes and link the nodes.
Step 3: Define a sort function to sort the elements using selection sort.
Step 4: Display the sorted elements using a loop.
Step 5: In main() function call the functions.
Step 6: Stop.
Source Code:
#include<iostream>
using namespace std;
struct node
{
int data;
node*next;
};
node* createNew(node *start, int n )
{
node *node1, *ptr;
node1 = new node();
node1->data = n;
if (start == NULL)
{
node1->next=NULL;
start = node1;
}
else
{
ptr=start;
while(ptr->next!=NULL)

Department of Digital Sciences 34


2020-2021 20CA2013 DATA STRUCTURES LAB
ptr = ptr->next;
ptr->next=node1;
node1->next=NULL;
}
return start;
}
node *sort(node *start)
{
node *i, *j; int t;
for (i=start;i->next != NULL; i=i->next)
for (j=i->next;j!=NULL;j=j->next)
if (i->data>j->data)
{
t=i->data;
i->data=j->data;
j->data=t;
}
return start;
};
void display(node *start)
{
node *ptr; ptr=start;
while (ptr!=NULL)
{
cout<<ptr->data<<'\t';
ptr=ptr->next;
}
}
int main()
{
node *start=NULL;
char more;
int data;
do
{
cout<<"Enter the data :";
cin>>data;
start=createNew(start,data);
cout<<"Any more data? :";
cin>>more;
}

Department of Digital Sciences 35


2020-2021 20CA2013 DATA STRUCTURES LAB
while(more=='y');
cout<<"\nList: \t";
display(start);
cout<<"\nSorted list: \t";
start=sort(start);
display(start);
}
Sample Input/Output:
Enter the data :6
Any more data? :y
Enter the data :-1
Any more data? :y
Enter the data :0
Any more data? :y
Enter the data :4
Any more data? :y
Enter the data :9
Any more data? :y
Enter the data :5
Any more data? :n
List: 6 -1 0 4 9 5
Sorted list: -1 0 4 5 6 9
Result:
The above program was executed and the output was verified for a sample set of input
values.

Department of Digital Sciences 36


2020-2021 20CA2013 DATA STRUCTURES LAB
Ex. No. 12
Date:
SOLVING JOSEPHUS PROBLEM

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

Department of Digital Sciences 38


2020-2021 20CA2013 DATA STRUCTURES LAB
8 is eliminated counting 1 counting 2 counting 3
5 is eliminated counting 6 counting 7 counting 1
2 is eliminated counting 3 counting 6 counting 7
1 is eliminated counting 3 counting 6 counting 7
3 is eliminated counting 6 counting 7 counting 6
7 is eliminated
Winner is: 6
Result:
The above program was executed and the output was verified for a sample set of input
values.

Department of Digital Sciences 39

You might also like