void CLL::push(char data) { // You write - if the size is 0, add a first node in
ID: 3709044 • Letter: V
Question
void CLL::push(char data) { // You write - if the size is 0, add a first node in the linked list
// by calling addFirst. Otherwise add a new node to the end of the linked list. Note that this
// linked list is circular, so you must make the last node's next pointer point to the first node.
}
void CLL::addFirst(char data) {
// you write - add the very first node to the linked list
}
void CLL::addAtFront(char data) {
// you write - if the size of the linked list is 0, add a first node by calling addFirst.
//Otherwise add a new node to the beginning of the list
//Since this linked list is circular, you must make the last node now point to this new node
//you just added to the front of the linked list
}
void CLL::removeNext(SNode *n) {
// Longest method - given node n, remove the next node in the linked list.
// you have 4 conditions:
//1: there's only one node in the list, in which case you delete that node and set the size to 0
//2: n's next node is a last node, in which case n becomes the new last node and n's next must point // to the first node in the list (remember to decrease the size of the list)
//3: n's next node is the first node in the list (i.e., n is the last node in the list), in which case
// n's next becomes first's next, first is deleted, and first becomes n's next (again, remember
// to decrease the size)
//4: n is just some node in the middle of the list, in which case you should create a tmp node to point
// to n's next, then set n's next to n's next's next, and then delete the tmp node (again, decrease
// size).
}
void CLL::printList(){
// print out the data in each node in the linked list, starting at the first node.
}
You will also be modifying the file DuckDuckGoose.cpp to fill in the methods as defined below:
void DuckDuckGoose::MakeListEnd() {
// create a linked list by pushing every character in arr1 to the end of linked list clist (the field in
// DuckDuckGoose
}
void DuckDuckGoose::MakeListBoth() {
// create a linked list by alternately pushing and adding at front every character in arr2 onto the linked list // clist (so if i%0 == 0, push arr2[i], and otherwise addAtFront arr2[i])
}
//Duck duck goose CPP
DuckDuckGoose::DuckDuckGoose(bool b, int x) {
clist = new CLL();
if (b) {
MakeListEnd();
}
else {
MakeListBoth();
}
clist->printList();
PlayDuck(x);
cout << "***************************************************" << endl;
clist->printList();
cout << "***************************************************" << endl;
}
void DuckDuckGoose::MakeListEnd() {
// create a linked list by pushing every character in arr1 to the end of linked list clist (the field in
// DuckDuckGoose
}
void DuckDuckGoose::MakeListBoth() {
// create a linked list by alternately pushing and adding at front every character in arr2 onto the linked list
// clist (so if i%0 == 0, push arr2[i], and otherwise addAtFront arr2[i])
}
void DuckDuckGoose::PlayDuck(int x){
SNode *tmp = clist->first;
for (int i = 0; i < x; i++) {
tmp = tmp->next;
clist->removeNext(tmp);
tmp = tmp->next;
//clist->printList();
}
}
//duck duck goose HPP
/*
* DuckDuckGoose.hpp
*
* Created on: Mar 12, 2018
* Author: Debra
*/
class DuckDuckGoose {
char arr1[20] = {'t','w','b','i','s','f','l','d','i','j','e','h','b','n','o','e','e','r','s','t'};
int arr1size = 20;
char arr2[18] = {'y','b','p','d','k','t','s','h','u','a','k','l','s','n','o','l','q','p'};
int arr2size = 18;
CLL *clist;
bool justend;
public:
DuckDuckGoose(bool b, int x);
void MakeListEnd();
void MakeListBoth();
void PlayDuck(int x);
};
Explanation / Answer
void CLL::push(char data) { // You write - if the size is 0, add a first node in the linked list
// by calling addFirst. Otherwise add a new node to the end of the linked list. Note that this
// linked list is circular, so you must make the last node's next pointer point to the first node.
if(size == 0){
addfirst(data);
return;
}
Node *temp = new Node;
temp->data = data;
temp->next = head;
Node *tt = head->next;
while(tt.next != head){
tt = tt->next;
}
tt->next = temp;
size++
}
void CLL::addFirst(char data) {
// you write - add the very first node to the linked list
Node *temp = new Node;
temp->data = data;
head = temp;
temp->next = head;
size++;
}
void CLL::addAtFront(char data) {
// you write - if the size of the linked list is 0, add a first node by calling addFirst.
//Otherwise add a new node to the beginning of the list
//Since this linked list is circular, you must make the last node now point to this new node
//you just added to the front of the linked list
Node *temp = new Node;
temp->data = data;
temp->next = head;
Node *tt = head->next;
while(tt->next != head){
tt = tt->next;
}
tt->next = temp;
head = temp;
size++;
}
void CLL :: removeNext(node* n) {
//1:only one node
if(n->next == NULL) {
size=0;
delete n;
}
//2: n's next node is a last node, in which case n becomes the new last node and n's next must point
// to the first node in the list
else if(n->next->next == first){
delete n->next;
n->next = first;
size--;
}
//3: n's next node is the first node in the list (i.e., n is the last node in the list), in which case
// n's next becomes first's next, first is deleted, and first becomes n's next (again, remember
// to decrease the size)
else if(n->next == first)
{
n->next = first->next;
delete first;
first=n->next;
size--;
}
//4: n is just some node in the middle of the list, in which case you should create a tmp node to point
// to n's next, then set n's next to n's next's next, and then delete the tmp node (again, decrease
// size).
else{
node *tmp = n->next;
n->next=tmp->next;
delete tmp;
size--;
}
}
void CLL ::printList()
{
if (first != NULL)
{
node *temp = first;
do
{
cout<<temp->data<<"->";
temp = temp->next;
}
while (temp != first);
}
cout<<"NULL ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.