Question

C++ How would you delete a node in a linked list? from beginning, end, and a...

C++ How would you delete a node in a linked list? from beginning, end, and a node somewhere in the middle without breaking the links to the next or previous nodes

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
  
/* structure of a linked list node */
class Node
{
public:
int data;
Node *next;
};

/*function to insert a node at the beginning */
void push(Node **head_ref, int new_data)
{
Node *new_node = new Node();
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}

void deleteNode(Node *head, Node *n)
{
// When node to be deleted is beginning node
if(head == n)
{
if(head->next == NULL)
{
cout << "There is only one node." <<
" The list can't be made empty ";
return;
}
  
/* Copy the data of next node to head */
head->data = head->next->data;
  
// store address of next node
n = head->next;
  
// Remove the link of next node
head->next = head->next->next;
  
// free memory
free(n);
  
return;
}
  
// When node to be deleted is somewhere in the middle
Node *prev = head;
while(prev->next != NULL && prev->next != n)
prev = prev->next;
  
// Check if node really exists in Linked List
if(prev->next == NULL)
{
cout << "\nGiven node is not present in Linked List";
return;
}
  
// Remove node from Linked List
prev->next = prev->next->next;
  
// Free memory
free(n);
  
return;
}

// When node to be deleted is end node
Node* removeLastNode(struct Node* head)
{
if (head == NULL)
return NULL;
  
if (head->next == NULL) {
delete head;
return NULL;
}
  
// Find the second last node
Node* second_last = head;
while (second_last->next->next != NULL)
second_last = second_last->next;
  
// Delete last node
delete (second_last->next);
  
// Change next of second last
second_last->next = NULL;
  
return head;
}

/* function to print a linked list */
void printList(Node *head)
{
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
cout<<endl;
}

int main()
{
Node *head = NULL;
  
/* Create following linked list
1->2->3->4->5->6->7->8 */
push(&head,8);
push(&head,7);
push(&head,6);
push(&head,5);
push(&head,4);
push(&head,3);
push(&head,2);
push(&head,1);
  
cout<<"Given Linked List: ";
printList(head);   
  
/* Let us delete the beginning node */
cout<<"\nDeleting the beginning node ";
deleteNode(head, head);
  
cout<<"\nModified Linked List: ";
printList(head);
  
/* Let us delete the node which is somewhere in the middle */
cout<<"\nDeleting node "<< head->next->next->data<<" ";
deleteNode(head, head->next->next);
  
cout<<"\nModified Linked List: ";
printList(head);
  
/* Let us delete the end node */
cout<<"\nDeleting the end node ";
removeLastNode(head);
  
cout<<"\nModified Linked List: ";
printList(head);
  
return 0;
}

SOURCE CODE IMAGES:

OUTPUT IMAGE:

Add a comment
Know the answer?
Add Answer to:
C++ How would you delete a node in a linked list? from beginning, end, and a...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...

    In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class (50 pts): Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation. Part 2 Add Method (30 pts): Create an add method in your linked list...

  • Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list -...

    Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student records that should not be sorted Create a liked list of 15 student record nodes. Each node is a node of one student record from the above unsorted array. The list of student records should be sorted by student ID. (Insert function without sort function to create a linked list.) (If you insert correctly, the...

  • 1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is...

    1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is given below. struct node { int info; struct node *next; }; typedef struct node node; You can assume that all the nodes in the linked list are distinct and each node appears in the list at most once. Prototype of the functions are given below. node *delete(node *head, int k) node *recursivedelete(node *head, int k) • delete deletes the node with info k from...

  • This is a recursive function to delete all even nodes from a linked list. node *deleteEven(node...

    This is a recursive function to delete all even nodes from a linked list. node *deleteEven(node *head) if (head == NULL)    return head;    if (head->data % 2 == 0) { node *temp = head; free(temp); return deleteEven(head->next); } else { head->next = deleteEven(head->next); return head; } } What is the purpose of heaving to set "head->next = deleteEven(head->next);" instead of just having "head = head->next;" in one of the lines above and just calling "deleteEven(head->next);"?

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • implement delete node function in c++ language I just need a basic doubly linked list code for the delete node portion of the code // Delete node containing word from list if it is present void...

    implement delete node function in c++ language I just need a basic doubly linked list code for the delete node portion of the code // Delete node containing word from list if it is present void delNode (DLList list, char *str) ( // Delete node containing word from list if it is present void delNode (DLList list, char *str) (

  • 16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below,...

    16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below, write expressions that do the following, assuming that currPtr is somewhere in the middle of the list: a. Access the component member of the first list element. b. Advance currPtr to point to the next element. c. Access the component member of the next element (the one that follows the current element). d. Access the component member of the element that follows the next...

  • c++ Computational Complexity Create a singly linked list for storing positive integers. Each node will store...

    c++ Computational Complexity Create a singly linked list for storing positive integers. Each node will store one integer. For example, 12->3->5->777-111 is such a list. There are five nodes in this list. 12 is the head node and 111 is the tail node. (111 points to NULL.) Your linked list starts empty. It should support the following three operations: Add x to tail: Create a new node whose data field contains x. Append this node at the end of the...

  • 11.) Suppose you have a linked list of Node objects from the Textbook Collections Framework and...

    11.) Suppose you have a linked list of Node objects from the Textbook Collections Framework and currentNode has been initialized to refer to the head node in the list. Which of these statements needs to appear in a loop that goes through the list's items one-by-one? Select one: a. currentNode++ b. currentNode = currentNode.next c. currentNode = previous() d. currentNode += 1 e. currentNode = next() 12.) A singly linked node contains which of these fields? Select one: a. data...

  • a. Using C++, define a node structure of the linked list (e.g. value is an integer,...

    a. Using C++, define a node structure of the linked list (e.g. value is an integer, next is a node type pointer), construct a linked list of 10 nodes and assign random numbers as the nodes’ values. Use loop to track and print from the first node to the last and output all nodes’ values. Finally, free all memories of the linked list.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT