Question

In: Computer Science

In C++, write a member method delete() that deletes a node from a linked list at...

In C++, write a member method delete() that deletes a node from a linked list at a random position. (It should first randomly generate that position. and then delete that node).

Solutions

Expert Solution

The required method is highlighted as bold in the below source code:

#include <iostream>

using namespace std;

//structure declaration
struct node
{
int data;
struct node *next;
};

struct node *head = NULL;

//function to count the size of the linked list
int size()
{
//declare a structure type variable and initialize with head
struct node *ptr = head;
int count = 0;
while(ptr != NULL)
{
count++;
ptr = ptr->next;
}
return count;
}

//function to check if list is empty or not
bool isEmpty()
{
//declare a structure type variable and initialize with head
struct node *ptr = head;
int count = 0;
while(ptr != NULL)
{
count++;
ptr = ptr->next;
}
if(count==0)
return true;
return false;
}

//function to print the node value with address
void printList()
{
//declare a structure type variable and initialize with head
struct node *ptr = head;

while(ptr != NULL)
{
//display the node address, value stored in node and the address of the next node
cout<<ptr->data<<" ";
  
//assign the pointer to the next node
ptr = ptr->next;
}
}

//function to add a new node at the end of the linked list
void addLast(int data)
{
//declare a structure type variable and initialize with head
struct node *ptr = head;
struct node *link = (struct node*) malloc(sizeof(struct node));
  
while(ptr != NULL)
{
//assign the pointer to the next node
ptr = ptr->next;
}
  
//assign the new node data field with the paramener value
link->data = data;
//assigne the new node address field with the head pointer
link->next = NULL;
  
//assign the head node to the new node
ptr->next = link;
}

//function to add a new node at the beginning of the linked list
void addFirst(int data)
{
struct node *link = (struct node*) malloc(sizeof(struct node));
  
//assign the new node data field with the paramener value
link->data = data;
//assigne the new node address field with the head pointer
link->next = head;
  
//assign the head node to the new node
head = link;
}

//function to delete a node
void delete(int pos)
{
struct node *headNode = head;
if (headNode == NULL)
return;
struct node* temp = headNode;
  
if (pos == 0)
{
headNode = temp->next;   
free(temp);
return;
}
  
for (int i=0; temp!=NULL && i<pos-1; i++)
temp = temp->next;
if (temp == NULL || temp->next == NULL)
return;
  
struct node *next = temp->next->next;

free(temp->next);
  
temp->next = next;
}

int main()
{
//insert value
addFirst(15);
addFirst(10);
addFirst(20);
addFirst(30);
  
int sz = size();
  
int loc = rand() % sz;
  
delete(loc);
  
//display the linked list by calling printList() function
cout<<"The linked list is: "<<endl;
printList();

return 0;

}

OUTPUT:

The linked list is:
30 20 10


Related Solutions

1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list....
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list. So if the item is 7 and the list is [1,3,7,4,7,3,7,2], the result is [1,3,7,4,7,3,2] 2- Use LinkList. Write removeAll(int n). Deletes all occurrences of an item n from a linked list. So if the item is 7 and the list1 is [1,3,7,4,7,3,2] , then list1.removeAll(7) then list1 becomes [1,3,4,3,2]. Demonstrate by displaying the list contents before and after calling the above methods. Eg:...
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list....
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list. So if the item is 7 and the list is [1,3,7,4,7,3,7,2], the result is [1,3,7,4,7,3,2] 2- Use LinkList. Write removeAll(int n). Deletes all occurrences of an item n from a linked list. So if the item is 7 and the list1 is [1,3,7,4,7,3,2] , then list1.removeAll(7) then list1 becomes [1,3,4,3,2]. Demonstrate by displaying the list contents before and after calling the above methods. Eg:...
Write a member function that deletes all repetitions from the bag. In your implementation, assume that...
Write a member function that deletes all repetitions from the bag. In your implementation, assume that items can be compared for equality using ==. Use the following header for the function: void remove_repetitions() Here is a brief outline of an algorithm: - A node pointer p steps through the bag - For each Item, define a new pointer q equal to p - While the q is not the last Item in the bag ◼ If the next Item has...
You are asked to delete the last occurrence of an item from a linked list. So,...
You are asked to delete the last occurrence of an item from a linked list. So, for instance: Input: 2 -> 3 -> 2 -> 4 Delete last occurrence of 2, result: 2 -> 3 -> 4 Implement the following method to do the deletion. You may NOT use or implement helper methods - all your code must be implemented inside the given method. You may NOT use recursion. public class Node { public int data; public Node next; }...
C++ Write a C++ program that implements a tree using a linked representation Each node will...
C++ Write a C++ program that implements a tree using a linked representation Each node will contain a single integer data element. Initialize the tree to contain 10 nodes. The program should allow for the insertion and deletion of data. The program should allow the user to output data in Preorder, Inorder and Postorder.
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_);...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_); shared_ptr<node> temp(new node); if(cursor == NULL) { temp = cursor-> next; cursor= temp; if (temp = NULL) { temp->next = NULL; } } else if (cursor-> next != NULL) { temp = cursor->next->next; cursor-> next = temp; if (temp != NULL) { temp->next = cursor; } } }
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with the higher value data. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head; while (tempPtr != null) { tempPtr = tempPtr.nextNode; result =...
Question 3: Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
Question 3: Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT