
Concept explainers
C++ CODING PROBLEM WHERE I NEED CODE TO CREATE A LINKED LIST
(I also put a screen shot of the problem below.
Two strings, code1 and code2, are read from input as two states' codes.
headObj has the default value of "code".
Create a new node firstState with string code1 and insert firstState after headObj.
Then, create a second node secondState with string code2 and insert secondState after firstState.
Ex: If the input is MA IA, then the output is:
code
MA
IA
#include <iostream>
using namespace std;
class StateNode {
public:
StateNode(string codeInit = "", StateNode* nextLoc = nullptr);
void InsertAfter(StateNode* nodeLoc);
StateNode* GetNext();
void PrintNodeData();
private:
string codeVal;
StateNode* nextNodePtr;
};
StateNode::StateNode(string codeInit, StateNode* nextLoc) {
this->codeVal = codeInit;
this->nextNodePtr = nextLoc;
}
void StateNode::InsertAfter(StateNode* nodeLoc) {
StateNode* tmpNext = nullptr;
tmpNext = this->nextNodePtr;
this->nextNodePtr = nodeLoc;
nodeLoc->nextNodePtr = tmpNext;
}
StateNode* StateNode::GetNext() {
return this->nextNodePtr;
}
void StateNode::PrintNodeData() {
cout << this->codeVal << endl;
}
int main() {
StateNode* headObj = nullptr;
StateNode* firstState = nullptr;
StateNode* secondState = nullptr;
StateNode* currState = nullptr;
string code1;
string code2;
cin >> code1;
cin >> code2;
headObj = new StateNode("code");
/* THE NEEDED CODE GOES HERE. THE REST OF THE CODE IS FINE. */
currState = headObj;
while (currState != nullptr) {
currState->PrintNodeData();
currState = currState->GetNext();
}
return 0;


Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 4 images

- Linked List, create your own code. (Do not use the build in function or classes of Java or from the textbook). Create a LinkedList class: Call the class MyLinkedList, (hint) Create a second class called Node.java and use it, remember in the class I put the Node class inside the LinkedList Class, but you should do it outside. This class should haveo Variables you may need for a Node,o (optional) Constructor Your linked list is of an int type. (you may do it as General type as <E>) For this Linked List you need to have the following methods: add, addAfter, remove, size, contain, toString, compare, addInOrder. This is just a suggestion, if you use Generic type, you must modify this Write a main function or Main class to test all the methods,o Create a 2 linked list and test all your methods. (Including the compare)arrow_forwardSingleLinkedList head- Node next - data- String value = "Tom" Node next- data- String value = "Dick" Node nodeRef = nodeRef.next; nodeRef.next = new Node("Peter"); next- data- String value="Harry" What change will be made to the list after the following codes are run? Node nodeRef = head; While (nodeRef.next!=null) Node next = null data- String value="Sam"arrow_forwardWith Head node or not? statement is: if(first->llink == first) output << "List is empty"; O a. It is a Doubly linked list without Head node. O b. It is a Doubly linked list with Head node.arrow_forward
- Write a program in three parts. The first part should create a linked list of 26 nodes. The second part should fill the list with the letters of the alphabet. The third part should print the contents of the linked list. NOT USE malloc, only use <iostream>arrow_forwardJava Given main() in the ShoppingList class, define an insertAtEnd() method 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 Second image is ItemNodearrow_forwardJava language Write a method to insert an array of elements at index in a single linked list and then display this list. The method receives this array by parametersarrow_forward
- JAVA please Given main() in the ShoppingList class, define an insertAtEnd() method 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 Code provided in the assignment ItemNode.java:arrow_forwardPYTHON LAB: Inserting an integer in descending order (doubly-linked list) Given main.py and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insert_in_descending_order() 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 the output is: 9 8 7 6 5 4 3 2 1 _____________________________________________________________________________________ Main.py: from IntNode import IntNode from IntList import IntList if __name__ == "__main__": int_list = IntList() input_line = input() input_strings = input_line.split(' ') for num_string in input_strings: # Convert from string to integer num = int(num_string) # Insert into linked list in descending order new_node = IntNode(num) int_list.insert_in_descending_order(new_node) int_list.print_int_list() IntNode.py class IntNode: def __init__(self, initial_data, next = None,…arrow_forwardJava - Elements in a Rangearrow_forward
- Linked List, create your own code. (Do not use the build in function or classes of Java or from the textbook). Create a LinkedList class: Call the class MyLinkedList, (hint) Create a second class called Node.java and use it, remember in the class I put the Node class inside the LinkedList Class, but you should do it outside. This class should haveo Variables you may need for a Node,o (optional) Constructor Your linked list is of an int type. (you may do it as General type as <E>) For this Linked List you need to have the following methods: add, addAfter, remove, size, contain, toString, compare, addInOrder. This is just a suggestion, if you use Generic type, you must modify this Write a main function or Main class to test all the methods,o Create a 2 linked list and test all your methods. (Including the compare)arrow_forwardCreate class Test in a file named Test.java. This class contains a main program that performs the following actions: Instantiate a doubly linked list. Insert strings “a”, “b”, and “c” at the head of the list using three Insert() operations. The state of the list is now [“c”, “b”, “a”]. Set the current element to the second-to-last element with a call to Tail() followed by a call to Previous()Then insert string “d”. The state of the list is now [“c”, “d”, “b”, “a”]. Set the current element to past-the-end with a call to Tail() followed by a call to Next(). Then insert string “e”. The state of the list is now [“c”, “d”, “b”, “a”, “e”] . Print the list with a call to Print() and verify that the state of the list is correct.arrow_forwardTrue or False For each statement below, indicate whether you think it is True or False. If you like, you can provide a description of your answer for partial credit in case you are incorrect. Use the standard linked list below to answer True/False statements 9-12: 8 7 null 4 10 The “head” pointer of this list is pointing to Node 4 If we called “insert(5)”, the new node’s “next” pointer will point to Node 8 If we called “delete(10)”, Node 7’s “next” pointer will point to Node 8 If we called “search(20)”, the “head” pointer will be at Node 4 after the search function endsarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





