Question

In: Computer Science

a. You have to write the steps that we need to insert a new node as...

a. You have to write the steps that we need to insert a new node as the head of an existing linked list.

b.You have to write the code in c++ programming language of the function that we need to insert a new node in the end of an existing node.

c.Suppose that the below main function is executed correctly and all the functions that are invoked are imported from functions.h file. Explain.

int main(){
Node *head=NULL;
insertEnd(&head,"John");//inserts a new node in the end of the list
insertBeg((&head,"Tom");//inserts a new node in the beginning if the list
insertEnd(&head,"Kim");
insertAfter(head->next->next,"Sarah");//inserts a new node after a node
insertEnd(&head,"Jenna");
insertBeg((&head,"Daniel");
insertAfter(head->next->next,"Damon");
insertAfter(head->next->next->next->next,"Dario");
display(head);//it shows the elements of the list from head to tail
return 0;
}
  

Solutions

Expert Solution

#include <iostream>
#include <string>
using namespace std;

// Defines class Node
class Node
{
public:
// To store data
string data;
// To point to next node
Node *next;
};// End of class

// Function to insert a node at the beginning
void insertBeg(Node **headNode, string insData)
{
// Dynamically creates a new node
Node* newNode = new Node();

// Assigns parameter data to new node data
newNode->data = insData;

// Newly created node next points to head node
newNode->next = *headNode;

// Now head node points to the newly created node
*headNode = newNode;
}// End of function

// Function to insert a node after given parameter currentNode
void insertAfter(Node *currentNode, string insData)
{
// Checks if parameter current Node is NULL then
// cannot insert node at previous location
if(currentNode == NULL)
{
cout<<"\n ERROR: Node is NULL cannot insert after.";
return;
}// End of if condition

// Dynamically allocates memory to create a new node
Node *newNode = new Node();

// Assigns parameter data to newly created node
newNode->data = insData;

// Newly created next points to current node next
newNode->next = currentNode->next;

// Current node next points to new node
currentNode->next = newNode;
}// End of function

// Function to insert a node at the end of the single linked list
void insertEnd(Node **headNode, string insData)
{
// Dynamically allocates memory to create a new node
Node *newNode = new Node();
// Creates a temporary node which points to head node
Node *lastNode = *headNode;

// Assigns parameter data to newly created node
newNode->data = insData;

// Assign NULL to newly created node next (because it is the last node)
newNode->next = NULL;

// Checks if head node is equals to NULL then it is the first node to insert
if(*headNode == NULL)
{
// Assigns new node to head node
*headNode = newNode;
return;
}// End of if condition

// Loops till end of the list
while(lastNode->next != NULL)
// Move to next node
lastNode = lastNode->next;

// Last node next points to new node
lastNode->next = newNode;
return;
}// End of function

// Function to display the single linked list data
void display(Node *headNode)
{
// Checks if head node is equals to NULL then empty list
if(headNode == NULL)
{
cout<<"\n ERROR: Empty Single Linked List.";
return;
}// End of if condition

// Loops till end of the single linked list
while(headNode != NULL)
{
// Displays current node data
cout<<" "<<headNode -> data;
// Move to next node
headNode = headNode->next;
}// End of while loop
}// End of function

// main function definition
int main()
{
// Creates a head node
Node *head = NULL;
insertEnd(&head,"John");//inserts a new node in the end of the list
insertBeg(&head,"Tom");//inserts a new node in the beginning if the list
insertEnd(&head,"Kim");
insertAfter(head->next->next,"Sarah");//inserts a new node after a node
insertEnd(&head,"Jenna");
insertBeg(&head,"Daniel");
insertAfter(head->next->next,"Damon");
insertAfter(head->next->next->next->next,"Dario");
display(head);//it shows the elements of the list from head to tail
return 0;
}// End of main function
Sample Output:

Daniel Tom John Damon Kim Dario Sarah Jenna


Related Solutions

Write PSEUDOCODE to insert a node at position 2 in a doubly-linked list (assume position follows...
Write PSEUDOCODE to insert a node at position 2 in a doubly-linked list (assume position follows classic indexing from 0 to item_count - 1)
Activity 3-2 Write and compare procedural documents. Write an explanation of the steps required to insert...
Activity 3-2 Write and compare procedural documents. Write an explanation of the steps required to insert a USB flash drive into a USB port. Your explanation should be aimed at a person who has never used a computer before. Exchange explanations with a classmate or coworker and critique each other’s draft. Did you include enough information for a user to insert the USB flash drive correctly? Is your explanation complete, or did you leave out important information about the steps?...
Write the steps to use the vi editor command to create myfile. Insert the following lines...
Write the steps to use the vi editor command to create myfile. Insert the following lines of text save and display the result to STDOUT with cat command. line 5 line 6 line 3 line 2
In java please create a method that will insert a value after a specific node for...
In java please create a method that will insert a value after a specific node for a linked list. public void insertAfter(Node a, int newData){ }
Insert: this method takes a value as a parameter and adds a node which contains the...
Insert: this method takes a value as a parameter and adds a node which contains the value to the end of the linked list Delete: This method deletes a node from the linked list. If an index is passed as a parameter, then the method should delete the node at this index. If no index is passed, then delete the first item in the list Find: this method takes a value as a parameter, and returns the index of the...
JAVA Write a class to implement a list with the ability to insert a new item...
JAVA Write a class to implement a list with the ability to insert a new item at any location within the list, remove an item, and search for an item. The list should use a linked list implementation. This implementation should make use of a dummy node at the head of the list and should have explict references head and previous. Add a method to the list that inserts elements in acsending order assuming the list is already sorted before...
In vitro transcription: .If you wanted to ensure that you only synthesized “insert RNA”, what steps...
In vitro transcription: .If you wanted to ensure that you only synthesized “insert RNA”, what steps would have to be taken?
What order would I need to insert the numbers 1 to 7 into a new AVL...
What order would I need to insert the numbers 1 to 7 into a new AVL tree in order to ensure that there was no need to perform a rotation.
There are three steps (open, action, close) required for handling files. You need to write a...
There are three steps (open, action, close) required for handling files. You need to write a Python program what executes each of these three steps. Your program must include:  Extensive comments to ensure explanation of your code (1).  Open a file (file name must include your student number, thus filexxxxxxxx.txt). (1).  Write the details of the five students (from Question 1 above) to a file (3).  Close the file. (1)  Then open the file again...
Write a C++ die roller program. We need you to write a program that will roll...
Write a C++ die roller program. We need you to write a program that will roll dice for them, but not just six-sided dice. Your program needs to be able to take an input in for form nDx or ndx where the d is the letter d or D. n may or not be present. If it is, it represents the number of dice. If not, assume it is a 1. x may or may not be present. If it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT