Question

Modify the code to append the additional numbers: 30, -30, 90, -1, 100, 40, and 6...

Modify the code to append the additional numbers: 30, -30, 90, -1, 100, 40, and 6

Implement the Prepend function (hint: think of how you can modify the append function)

Prepend 5 to the linked list

code must be done in c++

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

 

class IntNode {

  public:

     IntNode(int dataInit = 0, IntNode* nextLoc = nullptr);

     void InsertAfter(IntNode* nodePtr);

     IntNode* GetNext();

     void PrintNodeData();

     IntNode* Append(int dataVal);

  private:

     int dataVal;

     IntNode* nextNodePtr;

};

 

// Constructor

IntNode::IntNode(int dataInit, IntNode* nextLoc) {

   this->dataVal = dataInit;

   this->nextNodePtr = nextLoc;

  

   return;

}

 

 

void IntNode::InsertAfter(IntNode* nodeLoc) {

   IntNode* tmpNext = nullptr;

  

   tmpNext = this->nextNodePtr;    // Remember next

   this->nextNodePtr = nodeLoc;    // this -- node -- ?

   nodeLoc->nextNodePtr = tmpNext; // this -- node -- next

  

   return;

}

 

// Print dataVal

void IntNode::PrintNodeData() {

   cout << this->dataVal << endl;

  

   return;

}

 

// Grab location pointed by nextNodePtr

IntNode* IntNode::GetNext() {

   return this->nextNodePtr;

}

 

 

IntNode* IntNode::Append(int dataVal)
//COMMENT EACH LINE BELOW
{

  IntNode* head;

    IntNode *last = new IntNode;

    last->dataVal = dataVal;

    last->nextNodePtr = NULL;

 
    if (head == NULL) {

        head = last;

    } else {

        IntNode *temp = new IntNode;

        temp = head;


        while (temp->nextNodePtr != NULL) {

            temp = temp->nextNodePtr;

        }

 
        temp->nextNodePtr = last;

    }

 
    return head;

}

 

 

int main() {

  srand(time(0));

  IntNode* headObj  = nullptr; // Create intNode objects
  IntNode* nodeObj1 = nullptr;
  IntNode* nodeObj2 = nullptr;
  IntNode* nodeObj3 = nullptr;
  IntNode* nodeObj4 = nullptr;
  IntNode* nodeObj5 = nullptr;
  IntNode* nodeObj6 = nullptr;
  IntNode* nodeObj7 = nullptr;
  IntNode* nodeObj8 = nullptr;
  IntNode* nodeObj9 = nullptr;
  IntNode* nodeObj10 = nullptr;

  IntNode* currObj  = nullptr;

  int i;

  IntNode test;

 

  // Front of nodes list

  headObj = new IntNode((rand() % 51) + 50);

  for(i = 0; i < 9; i++){

    nodeObj1 = new IntNode((rand() % 51) + 50);

    headObj->InsertAfter(nodeObj1);

  }

  test.Append(16);

 

  // Print linked list

  currObj = headObj;

  while (currObj != nullptr) {

    currObj->PrintNodeData();

    currObj = currObj->GetNext();

  }

 

  return 0;

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

class IntNode {

public:

IntNode(int dataInit = 0, IntNode* nextLoc = nullptr);

void InsertAfter(IntNode* nodePtr);

IntNode* GetNext();

void PrintNodeData();

IntNode* Append(int dataVal);
IntNode* Prepend(int dataVal);

private:

int dataVal;

IntNode* nextNodePtr;

};

// Constructor

IntNode::IntNode(int dataInit, IntNode* nextLoc) {

this->dataVal = dataInit;

this->nextNodePtr = nextLoc;

  

return;

}

void IntNode::InsertAfter(IntNode* nodeLoc) {

IntNode* tmpNext = nullptr;

  

tmpNext = this->nextNodePtr; // Remember next

this->nextNodePtr = nodeLoc; // this -- node -- ?

nodeLoc->nextNodePtr = tmpNext; // this -- node -- next

  

return;

}

// Print dataVal

void IntNode::PrintNodeData() {

cout << this->dataVal << endl;

  

return;

}

// Grab location pointed by nextNodePtr

IntNode* IntNode::GetNext() {

return this->nextNodePtr;

}

IntNode* IntNode::Append(int dataVal)
//COMMENT EACH LINE BELOW
{

IntNode* head;

IntNode *last = new IntNode;

last->dataVal = dataVal;

last->nextNodePtr = NULL;


if (head == NULL) {

head = last;

} else {

IntNode *temp = new IntNode;

temp = head;


while (temp->nextNodePtr != NULL) {

temp = temp->nextNodePtr;

}


temp->nextNodePtr = last;

}


return head;

}

IntNode* IntNode::Prepend(int dataVal)
//COMMENT EACH LINE BELOW
{

IntNode* head;

IntNode *last = new IntNode;

last->dataVal = dataVal;

last->nextNodePtr = NULL;


if (head == NULL) {

head = last;

} else {

IntNode *temp = new IntNode;

temp = head;
last->nextNodePtr = head;
head = last;
}

return head;

}

int main() {

srand(time(0));

IntNode* headObj = nullptr; // Create intNode objects
IntNode* nodeObj1 = nullptr;
IntNode* nodeObj2 = nullptr;
IntNode* nodeObj3 = nullptr;
IntNode* nodeObj4 = nullptr;
IntNode* nodeObj5 = nullptr;
IntNode* nodeObj6 = nullptr;
IntNode* nodeObj7 = nullptr;
IntNode* nodeObj8 = nullptr;
IntNode* nodeObj9 = nullptr;
IntNode* nodeObj10 = nullptr;

IntNode* currObj = nullptr;

int i;

IntNode test;

// Front of nodes list

headObj = new IntNode((rand() % 51) + 50);

for(i = 0; i < 9; i++){

nodeObj1 = new IntNode((rand() % 51) + 50);

headObj->InsertAfter(nodeObj1);

}

test.Append(16);
headObj = test.Prepend(5);

// Print linked list

currObj = headObj;

while (currObj != nullptr) {

currObj->PrintNodeData();

currObj = currObj->GetNext();

}

return 0;

}

=============================
See Output

95 75 64 main.cpp 123 124 125 し26 127 67 57 76 68 86 63 16 temp->nextNodePtr last; = 128 129 130 131 132 33 134 135 36 137 38


Thanks, PLEASE RATE

Add a comment
Know the answer?
Add Answer to:
Modify the code to append the additional numbers: 30, -30, 90, -1, 100, 40, and 6...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 10.16 LAB: Grocery shopping list (linked list: inserting at the end of a list)

    10.16 LAB: Grocery shopping list (linked list: inserting at the end of a list)Hello, I searched for similar problems, but their InsertAtEnd() have two parameters, while my question asks for one.Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list.DO NOT print the dummy head node.Ex. if the input is:4 Kale  Lettuce  Carrots  Peanutswhere 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are...

  • Grocery shopping list (linked list: inserting at the end of a list)

    import java.util.Scanner;public class ShoppingList {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);      ItemNode headNode;  // Create intNode objects                                                         ItemNode currNode;      ItemNode lastNode;      String item;      int i;      // Front of nodes list           ...

  • Given the following class definition in the file Classroom.h, which XXX and YYY complete the corresponding.cpp...

    Given the following class definition in the file Classroom.h, which XXX and YYY complete the corresponding.cpp file? #ifndef CLASSROOM_H #define CLASSROOM_H class Classroom public: void SetNumber(int n); void Printo const; private: int num; }; Tendif #include <iostream using namespace std; XXX void Class Room:: Set Number(int n) { nun: cout << "Number of Class Rooms: << num << endl; include "Classroom.hr vold Classroom: Print( const sinclude "Classroom.cpp" vold Classroom. Print() const #include "Classroom.h" void Class Room::Print) const #include "Classroom.cpp" void...

  • THE ENTIRE CODE SHOULD BE IN JAVA Playlist (output linked list) Given main(), complete the SongNode...

    THE ENTIRE CODE SHOULD BE IN JAVA Playlist (output linked list) Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers...

  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

  • I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I...

    I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I have right now. ************city.h**************** #ifndef city_h #define city_h #include <string> using namespace std; class City{ public: City () { name = "N/A"; population = 0; } City (string nm, unsigned int pop){ name = nm; population = pop; } void setName (string name) { this -> name = name; } void setPopulation (unsigned int population){ this -> population = population; } string getName() const...

  • JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the...

    JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers Johnson Title: The Dude Length: 337 Artist: Quincy Jones...

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

  • Inventory (linked lists: insert at the front of a list)

    import java.util.Scanner;public class Inventory {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);             InventoryNode headNode;                                                    InventoryNode currNode;      InventoryNode lastNode;      String item;      int numberOfItems;      int i;      // Front of nodes list           ...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT