Note: I am coding in C++ Here is list.cpp: #define _LIST_CPP #include \"list.h\"
ID: 3824915 • Letter: N
Question
Note: I am coding in C++
Here is list.cpp:
#define _LIST_CPP
#include "list.h"
#include "person.h"
#include <cstddef>
//
// In the `List` class, complete the `reorganizeList` member function in
// `list.cpp`. The `reorganizeList` function must place all leaders at the
// front of the list followed by all non-leaders, while otherwise maintain
// the same order.
//
template <class T>
void List<T>::reorganizeList() {
//your code here
}
template <class T>
List<T>::List() {
this->head = NULL;
}
template <class T>
void List<T>::insertBack(const T& ndata) {
static int ct = 0;
ListNode* node = new ListNode(ndata);
node->leader = (ct++ % 2 == 0);
if (!head) {
head = node;
} else {
ListNode *thru = head;
while (thru->next != NULL) { thru = thru->next; }
thru->next = node;
}
}
template <class T>
const T *List<T>::get(int index) const {
ListNode *thru = head;
while (thru && index-- > 0) { thru = thru->next; }
if (thru) { return &(thru->data); }
else { return NULL; }
}
Consider a game night for students, where each student is added to a end of a linked list in the order they arrive to the game night. To help organize the games, every other 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 ... [Leader] [Leader] [Leader] In the List class, complete the reorganizeList member function in list-p2.cpp. The reorganizeList function must place all leaders at the front of the list followed by all non-leaders, while otherwise maintaining the same order. Using the example above, the new list must be the following: 1st rightarrow 3rd rightarrow 5th rightarrow 2nd rightarrow 4th rightarrow 6th rightarrow ... Alice rightarrow Carol rightarrow Erin rightarrow Boh rightarrow Don rightarrow Faythe rightarrow ... [Leader] [Leader] [Leader] 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-testExplanation / Answer
template <class T>
void List<T>::reorganizeList() {
ListNode *ptr = head;
int count =0 ;
int leaderPos =1;
while(ptr != NULL){
if(ptr->next != NULL && ptr->next-leader){
ListNode *node = ptr-next;
ptr->next = ptr-next-next;
insertAt(node,leaderPos)
leaderPos++;
}
ptr = ptr->next;
}
}
template <class T>
void List<T>::insertAt(ListNode *node , int n)
{
if (n == 1){
node->next = head;
head = node;
return;
}
Node* temp2 = head;
for (int i = 0; i < n-2; i++){
temp2 = temp2->next;
}
node->next = temp2->next;
temp2->next = temp2;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.