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 Peanuts
where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added at the end of the list.
The output is:
Kale Lettuce Carrots Peanuts
//ItemNode.h
---------------------------------------------------------------------------------
#include
#include
using namespace std;
class ItemNode {
private:
string item;
ItemNode* nextNodeRef;
public:
// Constructor
ItemNode() {
item = "";
nextNodeRef = NULL;
}
// Constructor
ItemNode(string itemInit) {
this->item = itemInit;
this->nextNodeRef = NULL;
}
// Constructor
ItemNode(string itemInit, ItemNode *nextLoc) {
this->item = itemInit;
this->nextNodeRef = nextLoc;
}
// Insert node after this node.
void InsertAfter(ItemNode &nodeLoc) {
ItemNode* tmpNext;
tmpNext = this->nextNodeRef;
this->nextNodeRef = &nodeLoc;
nodeLoc.nextNodeRef = tmpNext;
}
// TODO: Define insertAtEnd() function that inserts a
node
// to the end of the linked list
void InsertAtEnd(ItemNode *currNode){
ItemNode * headNode;
if(currNode == NULL){ // check if it is the first Node
headNode = currNode; // add curr to head
}else{ // find last node
while(headNode->nextNodeRef!=NULL){
headNode = headNode->nextNodeRef;
}// add currnode to last node
headNode->nextNodeRef = currNode;
}
}
// Get location pointed by nextNodeRef
ItemNode* GetNext() {
return this->nextNodeRef;
}
void PrintNodeData() {
cout << this->item
<< endl;
}
};
---------------------------------------------------------------------------------
#include "ItemNode.h"
int main() {
ItemNode *headNode; // Create intNode objects
ItemNode *currNode;
ItemNode *lastNode;
string item;
int i;
int input;
// Front of nodes list
headNode = new ItemNode();
lastNode = headNode;
cin >> input;
for (i = 0; i < input; i++) {
cin >> item;
currNode = new
ItemNode(item);
lastNode->InsertAtEnd(currNode);
lastNode = currNode;
}
// Print linked list
currNode = headNode->GetNext();
while (currNode != NULL) {
currNode->PrintNodeData();
currNode =
currNode->GetNext();
}
}
To meet your requirment I have changed in two place.
1. In main method.
2. In InsertAtEnd method
#include
#include
using namespace std;
class ItemNode
{
private:
string item;
ItemNode *nextNodeRef;
public:
// Constructor
ItemNode()
{
item = "";
nextNodeRef = NULL;
}
// Constructor
ItemNode(string itemInit)
{
this->item = itemInit;
this->nextNodeRef = NULL;
}
// Constructor
ItemNode(string itemInit, ItemNode *nextLoc)
{
this->item = itemInit;
this->nextNodeRef = nextLoc;
}
// Insert node after this node.
void InsertAfter(ItemNode &nodeLoc)
{
ItemNode *tmpNext;
tmpNext = this->nextNodeRef;
this->nextNodeRef = &nodeLoc;
nodeLoc.nextNodeRef = tmpNext;
}
// TODO: Define insertAtEnd() function that inserts a node
// to the end of the linked list
void InsertAtEnd(ItemNode *lastNode)
{
if (nextNodeRef == NULL)
{
nextNodeRef = lastNode;
}
else
{
ItemNode *secondLast = nextNodeRef;
while (secondLast->nextNodeRef != NULL)
{
secondLast->PrintNodeData();
secondLast = secondLast->nextNodeRef;
} // add currnode to last node
secondLast->nextNodeRef = lastNode;
}
}
// Get location pointed by nextNodeRef
ItemNode *GetNext()
{
return this->nextNodeRef;
}
void PrintNodeData()
{
cout << this->item << endl;
}
};
int main()
{
ItemNode *headNode; // Create intNode objects
ItemNode *currNode;
string item;
int i;
int input;
cout << "Enter Number of item :";
cin >> input;
for (i = 0; i < input; i++)
{
cout << "Enter Item : ";
cin >> item;
currNode = new ItemNode(item);
if (i == 0)
{
headNode = currNode;
}
else
{
headNode->InsertAtEnd(currNode);
}
}
// Print linked list
currNode = headNode;
while (currNode != NULL)
{
currNode->PrintNodeData();
currNode = currNode->GetNext();
}
}
past this code in single file and complie and run.
If you find this answer, useful give thumbs up.
if you have any query, ask in comment.
Regards.
10.16 LAB: 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 ...
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 ...
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...
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...
Given a ListItem class, complete main() using the built-in LinkedList type to create a linked list called shoppingList. The program should read items from input (ending with -1), adding each item to shoppingList, and output each item in shoppingList using the printNodeData() method.Ex. If the input is:the output is:--ShoppingList.java:import java.util.Scanner;import java.util.LinkedList;public class ShoppingList { public static void main (String[] args) { Scanner scnr = new Scanner(System.in); // TODO: Declare a LinkedList called shoppingList of type ListItem String...
Given a ListItem class, complete main() using the built-in LinkedList type to create a linked list called shoppingList. The program should read items from input (ending with -1), adding each item to shoppingList, and output each item in shoppingList using the printNodeData() method.Ex. If the input is:the output is:--ShoppingList.java:import java.util.Scanner;import java.util.LinkedList;public class ShoppingList { public static void main (String[] args) { Scanner scnr = new Scanner(System.in); // TODO: Declare a LinkedList called shoppingList of type ListItem String...
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; }; //...
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...
9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and a PeopleNode class, complete the PeopleList class by writing findFirst() and findLast() methods. The findFirst() method should find the first occurrence of an age value in the linked list and return the corresponding node. Similarly, the findLast() method should find the last occurrence of the age value in the linked list and return the corresponding node. For both methods, if the age value is...
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...