Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Note: I am coding in C++ Consider a game night for students, where each student

ID: 3824909 • Letter: N

Question

Note: I am coding in C++

Consider a game night for students, where each student is added to a end of a linked list in the order they arrive the night to game night. To help organize the games, every 4th student is a leader. For example: 1st rightarrow 2nd rightarrow 3rd rightarrow 4th rightarrow 5th rightarrow 6th rightarrow ... Alice rightarrow Bob rightarrow Carol rightarrow Don rightarrow Erin rightarrow Faythe rightarrow .... In the List class, complete the reorganizeList member function in list-p2.cpp. The reorganizeList function must place all non-leaders at the front of the list followed by a leaders, while otherwise maintaining the same order. Using the example above, the new list must be the following: 2nd rightarrow 3rd rightarrow 4th rightarrow 6th rightarrow 1st rightarrow 5th rightarrow ... Bob rightarrow Carol rightarrow Dorn rightarrow Faythe rightarrow Alice rightarrow Erin rightarrow ... The List class (list-p2.cpp list.h) stores a singly linked list of ListNode nodes, using the same structure and variable names as MP3. The ListNode class contains a bool leader that will be set to true when that entry contains a leader. A complete Makefile and tester code is provided for you. To compile and test, run: make ./gameNight -test.

Explanation / Answer

/*
* C++ Program to Implement Singly Linked List
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
char info[20];
int leader=0;
struct node *next;
}*start;

/*
* Class Declaration
*/
class single_llist
{
public:
node* create_node();
int size();
void insert();
void displayReorganizeList();
single_llist()
{
start = NULL;
}
};

/*
* Main :contains menu
*/
main()
{
int choice, nodes, element, position, i;
single_llist sl;
start = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Operations "<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert node"<<endl;
cout<<"2.display"<<endl;
cout<<"3.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node "<<endl;
sl.insert();
cout<<endl;
break;
case 2:
cout<<"Display elements of link list"<<endl;
sl.displayReorganizeList();
cout<<endl;
break;
case 3:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}

/*
* Creating Node
*/
node *single_llist::create_node()
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{ cout<<"Enter the value to be inserted: ";
cin>>temp->info ;
//setting leader
if(size()%4==0){
temp->leader=1;
}

temp->next = NULL;
return temp;
}
}

/*
* Inserting Node at last
*/
void single_llist::insert()
{
struct node *temp, *p;
temp = create_node();
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}

}


/*
* Display Elements of a link list
*/
int single_llist::size(){

int size=0;
struct node *temp;
if (start == NULL)
{
return 0;
}
temp = start;

while (temp != NULL)
{
size++;
temp = temp->next;
}
return size;
}
void single_llist::displayReorganizeList()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
//Display non leader elements
while (temp != NULL)
{ if(temp->leader != 1){
cout<<temp->info<<"--"<<temp->leader<<"->";
}
temp = temp->next;

}
temp = start;
//Display leader elements
while (temp != NULL)
{ if(temp->leader == 1){
cout<<temp->info<<"--"<<temp->leader<<"->";

}
temp = temp->next;

}
cout<<"NULL"<<endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote