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?...
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...
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.
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...
We can build a heap by repeatedly calling the insert function to insert the elements into...
We can build a heap by repeatedly calling the insert function to insert the elements into the heap. Here is pseudocode: buildHeap(A) h = new empty heap   for each element e in A       h.insert(e)             What is the Big-O runtime of this version of buildHeap? Justify your answer.
QBO What steps need to be followed to add a new product or service? What steps...
QBO What steps need to be followed to add a new product or service? What steps need to be followed to record a new sales receipt? What steps need to be followed to record a new invoice? What steps need to be followed to record a new payment from a customer? What steps need to be followed to record a new deposit to the bank? What steps need to be followed to record a new product and adding a new...
c++ Binary Search Tree: find parent of a node please insert comment to explain what the...
c++ Binary Search Tree: find parent of a node please insert comment to explain what the code do. Use struct Node{ int data; Node* left; Node* right};
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT