Question

C++ linked list insertion at the end assistance. I know that this works and everything, but...

C++ linked list insertion at the end assistance. I know that this works and everything, but I can't seem to understand some pieces of the code. I wanted to ask if someone could go through a simple linked list like 6, 7, and 8 step by step using this code. I have tried working it on paper, but I keep messing up somewhere. What is most confusing me is:

{ while(p->next != NULL)
p = p->next;
p->next = newNode;
}
return head;

Thank you.

Node* insertEnd(Node * head, int data){

Node * p = head;
Node * newNode = new Node;

newNode->data = data;
newNode->next = NULL;

if(head == NULL)
head = newNode;

else
{
while(p->next != NULL)
p = p->next;
p->next = newNode;
}
return head;
}

Homework Answers

Know the answer?
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
Similar Questions
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read code and all comments carefully): Student.h StudentList.h StudentList.cpp main.cpp This program: Creates a sorted linked list (student name and gpa) . The list is sorted in ascending order by name. Displays the list Read and understand this program, then do the following: finish writing Student.h and other files fix errors in StudentList.cpp Student.h: #ifndef STUDENT_H #define STUDENT_H //using namespace std; //<==== This statement //...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
C++ questions QUESTION 1 What kind of linked list begins with a pointer to the first...
C++ questions QUESTION 1 What kind of linked list begins with a pointer to the first node, and each node contains a pointer to the next node, and the pointer in the last node points back to the first node? A. Circular, singly-linked list. B. Circular, doubly-linked list. C. Singly-linked list. D. Doubly-linked list. E. None of the above.    QUESTION 2 _________ is not an advantage of linked lists when compared to arrays. A. Dynamic memory allocation. B. Efficient...
Using the given SList as a template to implement a doubly linked list with the following...
Using the given SList as a template to implement a doubly linked list with the following functionalities Add first: add an element to the head of the list Add last: add an element to the end of the list Print: print the content of the list starting from the head Find: a private method that will return a reference to a node if it contains the same value of a given argument Insert after: a method that takes a key...
C++ Assumption and Terminology: Forward Linked List FLL has "head" Doubly Linked List DLL has "head"...
C++ Assumption and Terminology: Forward Linked List FLL has "head" Doubly Linked List DLL has "head" and "tail" Each node in FLL has "next" Each node in DLL has "next" and "prev" Q1: Complete the following constructor code for FLL FLL::FLL() { Q2: Complete the FLL insert-front code (which insert node new at the front of FLL) FLL::insert-front( node * new) { Q3: Complete the DLL insert-after code (which insert node new after node p) DLL::insert-after(node *p, node *new) {
Adding large numbers with linked list Requirement - in C++ - use file for the input...
Adding large numbers with linked list Requirement - in C++ - use file for the input (nums.txt) - (recommended) use only one linked list to hold intermediate answer and final answer. You may use another one to reverse the answer. - store the num reversely in the linked list. For example, the num 123 is stored as 3 (at first node), 2 (at second node) and 1 (at third node) in the linked list. - write a function that performs...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • In Java a function can call itself(we may explore more about this later in the course).  This...
    asked 9 minutes ago
  • Write a short program,Java, that implements the Caesar substitution cipher with k=3. That is, each letter...
    asked 11 minutes ago
  • Design a function named findMax that accepts two integer values as arguments and returns the value...
    asked 17 minutes ago
  • In MIPS, I am trying to create an array of 7 words that can either be...
    asked 18 minutes ago
  • Cash, $2,000 Accounts Receivable, $1,250 Professional Equipment, $10,200 Office Equipment, $5,500 Accounts Payable, $3,500 P. Palmer,...
    asked 19 minutes ago
  • Add two more statements to main() to test inputs 3 and -1. Use print statements similar...
    asked 20 minutes ago
  • C++ program called that reads a string and check if it’s well-formed or not. ex== The...
    asked 31 minutes ago
  • 1: Describe five functions of nonverbal communication. (Creating and maintaining relationships, Regulating Interaction, Influencing others, Concealing/deceiving,...
    asked 44 minutes ago
  • Trucks that travel on highways have to stop at various locations to be weighed and inspected...
    asked 46 minutes ago
  • Define and describe the PERT technique. For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac).
    asked 58 minutes ago
  • Are the following languages regular languages or not ? Justify your answer with a proof. If...
    asked 58 minutes ago
  • How does dehydration affect osmosis? Please include a diagram & identify whether the cell us hypotonic...
    asked 1 hour ago