Convert this program to C++, write comments where needed: import java.util.Scann
ID: 3601398 • Letter: C
Question
Convert this program to C++, write comments where needed:
import java.util.Scanner;
//Create the class list
public class link
{
//Create the list
int info;
link nextlink;
link(int data)
{
info = data;
nextlink = null;
}
}
//create the linklist class
class LinkList
{
//define the list head
link h;
//Method to swap two adjacent nodes of list
public void skiplist(int a, int Aadj)
{
// check if the adjacent element are not same.
if (a == Aadj) return;
// Search for the element.
link previousA = null, currentA = h;
while (currentA != null &&
currentA.info != a)
{
previousA = currentA;
currentA = currentA.nextlink;
}
// Search for adjacent element.
link previousAadj = null, currentAadj = h;
while (currentAadj != null &&
currentAadj.info != Aadj)
{
previousAadj = currentAadj;
currentAadj = currentAadj.nextlink;
}
// check if the element and its adjacent
//element are present
if (currentAadj == null || currentAadj == null)
return;
// check if the element is not the head
//of the list.
if (previousA != null)
previousA.nextlink = currentAadj;
else //make y the new head
h = currentAadj;
// check if adjacent element is not the head
//of the list
if (previousAadj != null)
previousAadj.nextlink = currentA;
else // make x the new head
h = currentA;
// Swap the links.
link t = currentA.nextlink;
currentA.nextlink = currentAadj.nextlink;
currentAadj.nextlink = t;
}
//Method to add the element in the list.
public void push(int new_data)
{
//Add the new link.
link new_Node = new link(new_data);
new_Node.nextlink = h;
h = new_Node;
}
//Method to display the list.
public void DisplayList()
{
link llink = h;
while (llink != null)
{
System.out.print(llink.info+" ");
llink = llink.nextlink;
}
}
//Define the main method.
public static void main(String[] args)
{
int e;
//Create the object of the class linklist
LinkList llist = new LinkList();
//Accept the size of the link list.
System.out.println("Enter the number of "
+ "element in the "
+ "list(At least 4):");
Scanner scobj = new Scanner(System.in);
int n = scobj.nextInt();
//Accept the element of the list.
System.out.println("Enter the elements of"
+ " the list:");
for(int i=0;i<n;i++)
{
e=scobj.nextInt();
llist.push(e);
}
int s1,s2,s3,s4;
//Display the list.
System.out.println(" List before calling "
+ "the skiplist(): ");
llist.DisplayList();
System.out.println();
//Accept the element to be swapped.
System.out.println("Enter the two adjacent "
+" elements to be swapped:");
s1=scobj.nextInt();
s2=scobj.nextInt();
//Accept the element to be swapped with.
System.out.println("Enter the two adjacent "
+"elements to be swapped with:");
s3=scobj.nextInt();
s4=scobj.nextInt();
//Call the skiplist function and swap
//the adjacent elements.
llist.skiplist(s1, s2);
llist.skiplist(s3, s4);
//Display the list.
System.out.print(" List after calling "
+ "the skiplist():");
llist.DisplayList();
}
}
OUTPUT:
Enter the number of element in the list (At least 4): Enter the elements of the list: 2 List before calling the skiplist ) 1 2 3 4 5 Enter the two adjacent elements to be swapped: 1 2 Enter the two adjacent elements to be swapped with: List after calling the skiplist ) :21 43 5Explanation / Answer
#include<iostream>
#include<stdlib.h>
using namespace std;
//Create the class link
class link
{
public:
// To store number
int info;
// Point to next node
link *nextlink;
// Parameterized constructor to assign data
link(int data)
{
info = data;
nextlink = NULL;
}// End of constructor
}; //End of class
//Create the LinkList class
class LinkList
{
// Define the list head
link *h;
public:
//Method to swap two adjacent nodes of list
void skiplist(int a, int Aadj)
{
// Check if the adjacent element are not same.
if (a == Aadj)
return;
// Search for the first number to swap
link *previousA = NULL, *currentA = h;
// Loops till currentA is not null and currentA information in not first value to swap
while (currentA != NULL && currentA->info != a)
{
// Stores the currentA address in previousA
previousA = currentA;
// currentA is pointing to next
currentA = currentA->nextlink;
}// End of while loop
// Search for adjacent element.
link *previousAadj = NULL, *currentAadj = h;
// Loops till currentAadj is not null and currentAadj information in not second value to swap
while (currentAadj != NULL && currentAadj->info != Aadj)
{
// Stores the currentAadj address in previousAadj
previousAadj = currentAadj;
// currentAadj is pointing to next
currentAadj = currentAadj->nextlink;
}// End of while loop
// Check if the element and its adjacent element are present
if (currentAadj == NULL || currentAadj == NULL)
return;
// Check if the element is not the head of the list.
if (previousA != NULL)
previousA->nextlink = currentAadj;
//Otherwise make a new head
else // make y the new head
h = currentAadj;
// Check if adjacent element is not the head of the list
if (previousAadj != NULL)
previousAadj->nextlink = currentA;
//Otherwise make a new head
else // make x the new head
h = currentA;
// Swap the links.
link *t = currentA->nextlink;
currentA->nextlink = currentAadj->nextlink;
currentAadj->nextlink = t;
}// End of function
//Method to add the element in the list.
void push(int new_data)
{
// Creates a new node
link *new_Node = new link(new_data);
// New node next pointing to head
new_Node->nextlink = h;
// Head is pointing to new node
h = new_Node;
}// End of function
//Method to display the list.
void DisplayList()
{
// Creates a temporary pointer stores the head address
link *llink = h;
// Loops till end of linked list
while (llink != NULL)
{
// Displays the information of each node
cout<<llink->info<<" ";
// Pointing to next position
llink = llink->nextlink;
}// End of while loop
}// End of function
};// End of class
//Define the main method.
int main()
{
// To store the number enter by the user
int e;
//Create the object of the class linklist
LinkList *llist = new LinkList();
//Accept the size of the link list.
cout<<"Enter the number of element in the list(At least 4): ";
int n;
cin>>n;
//Accept the element of the list.
cout<<"Enter the elements of the list: ";
for(int i = 0; i < n; i++)
{
cin>>e;
llist->push(e);
}
int s1,s2,s3,s4;
//Display the list.
cout<<" List before calling the skiplist(): ";
llist->DisplayList();
cout<<endl;
//Accept the element to be swapped.
cout<<"Enter the two adjacent elements to be swapped: ";
cin>>s1>>s2;
//Accept the element to be swapped with.
cout<<"Enter the two adjacent elements to be swapped with: ";
cin>>s3>>s4;
//Call the skiplist function and swap the adjacent elements.
llist->skiplist(s1, s2);
llist->skiplist(s3, s4);
//Display the list.
cout<<" List after calling the skiplist(): ";
llist->DisplayList();
}// End of main function
Sample Run 1:
Enter the number of element in the list(At least 4): 5
Enter the elements of the list: 5
4
3
2
1
List before calling the skiplist(): 1 2 3 4 5
Enter the two adjacent elements to be swapped: 1
2
Enter the two adjacent elements to be swapped with: 3
4
List after calling the skiplist(): 2 1 4 3 5
Sample Run 2:
Enter the number of element in the list(At least 4): 6
Enter the elements of the list: 1
2
3
4
5
6
List before calling the skiplist(): 6 5 4 3 2 1
Enter the two adjacent elements to be swapped: 5
3
Enter the two adjacent elements to be swapped with: 3
1
List after calling the skiplist(): 6 1 4 5 2 3