Q1. 125 pt) write the following classes that implement a single List of integers
ID: 3845782 • Letter: Q
Question
Q1. 125 pt) write the following classes that implement a single List of integers: List of integers: Linked 1) Class Node: It includes two instance variables: int number and Node next. It contains the following methods: o A constructor that initializes the instance variable number. o Set and get method for each instance variable. 2) Class SingleLinkedList: It includes two instance variables: Node head and Node tail. It contains the following methods: o public void insert (int n) that creates and adds a Node at the end of the linked List. o public void print 0 that prints the values of all nodes in the linked list, separated by an arrow as shown in the sample output. o public boolean oneDigit 0 that returns true if all nodes in the linked List have a one-digit value and false otherwise. o public void iterativeReverse 0 that reverses the original linked list iteratively o public void recursiveReverse 0 that calls the private method void recursive Reverse(Node head) o private void recursiveReverse (Node current) that reverses the original linked list recursively 3) Class TestSingleLinkedList. In the main method, do the following: Create an object of class SingleLinkedList. Read integer values from the user. The user must enter -1 to stop. Call method print() Call method oneDigit) and print a proper message based on the returning value. Call and then call method print() Call method iterativeReverse() and then call method print()Explanation / Answer
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
class node
{
public:
int number;
struct node *next;
}*start;
/*
* Class Declaration
*/
class singlelinklist
{
public:
node* create_node(int);
void insert();
void print();
void reverse();
singlelinklist()
{
start = NULL;
}
};
main()
{
singlelinklist sl;
int choice, position, inodes, element;
start = NULL;
while (1)
{
cout<<"1.Insert node at last"<<endl;
cout<<"2.print Linked List"<<endl;
cout<<"3.Reverse Linked List "<<endl;
cout<<"4.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Last: "<<endl;
sl.insert();
cout<<endl;
break;
case 2:
cout<<"print elements of link list"<<endl;
sl.print();
cout<<endl;
break;
case 3:
cout<<"Reverse elements of Link List"<<endl;
sl.reverse();
cout<<endl;
break;
case 4:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"invalid choice"<<endl;
}
}
}
/*
* Creating Node
*/
node *singlelinklist::create_node(int value)
{
node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->number = value;
temp->next = NULL;
return temp;
}
}
/*
* Inserting Node at last
*/
void singlelinklist::insert()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
node *temp, *s;
temp = create_node(value);
s = start;
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at last"<<endl;
}
/*
* Reverse Link List
*/
void singlelinklist::reverse()
{
node *ptr1, *ptr2, *ptr3;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
if (start->next == NULL)
{
return;
}
ptr1 = start;
ptr2 = ptr1->next;
ptr3 = ptr2->next;
ptr1->next = NULL;
ptr2->next = ptr1;
while (ptr3 != NULL)
{
ptr1 = ptr2;
ptr2 = ptr3;
ptr3 = ptr3->next;
ptr2->next = ptr1;
}
start = ptr2;
}
/*
* printing Elements of a link list
*/
void singlelinklist::print()
{
node *temp;
if (start == NULL)
{
cout<<" List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->number<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.