Deleting Middle Node in Linked List

  • MHB
  • Thread starter needOfHelpCMath
  • Start date
  • Tags
    List
In summary, the code creates a Node object, assigns it the value 1, and stores it in a pointer variable called a. Next, it creates a Node object and assigns it the value 2. It stores the pointer to the first Node in another variable called b. Then, it creates a Node object and assigns it the value 4. It stores the pointer to the second Node in another variable called c. Finally, it creates a Node object and assigns it the value 6.
  • #1
needOfHelpCMath
72
0
how would be able to delete the middle D: so far I deleted the last two, which i do not want.

These are my values in the nodes: 9->1->2->4->10->6->8

HTML:
//part F will tranverse in the middle and delete the middle
 void insertMiddle(Node *nn) {
	while(nn->next->next->next != NULL) {
		nn = nn->next;	
	}
		delete nn->next;
		nn->next = NULL;
        }
    
    int main() {
    // part A
   Node *a; 
   a = new Node(1,NULL);
   
   
   Node *b;
   b = new Node(2,NULL);
   
   Node *c;
   c = new Node(4,NULL);
  
   
   Node *d;
   d = new Node(6,NULL);
   
   
   Node *e;
   e = new Node(8,NULL);
  
   Node *f;
   f = new Node(9,NULL); // insert at the beginning of Node *a;
   
   Node *g;
   g = new Node(10,NULL); // insert in the middle
   
   f->next = a;
   a->next = b;
   b->next = c;
   c->next = d;
   d->next = e;
   
   c->next = g;
   g->next = d;

/ /part F
   list.insertMiddle(f);
   cout << endl;
   list.print(f);

   return 0;
	}
 
Technology news on Phys.org
  • #2
I am not sure what you are saying here. Are you given that the "linked list" includes exactly 7 items and you want to remove the middle, i.e. fourth, item?

Also you have a comment that says "part F will tranverse (I think you mean "traverse") in the middle and delete the middle" but you have named the subroutine "insertMiddle"/ Are you inserting or deleting?
 
  • #3
HallsofIvy said:
I am not sure what you are saying here. Are you given that the "linked list" includes exactly 7 items and you want to remove the middle, i.e. fourth, item?

Also you have a comment that says "part F will tranverse (I think you mean "traverse") in the middle and delete the middle" but you have named the subroutine "insertMiddle"/ Are you inserting or deleting?

sorry i wasnt clear..sorry ill upload the whole source code and sorry for mis-information; the insertToMiddle is my function to find the middle and delete the middle

- - - Updated - - -

This is the source code:

HTML:
#include<iostream>

using namespace std;

class Node {
      
      public:
             int  Nodedata;
             Node *next;
             Node() {next =  NULL; Nodedata = 0;}
             
             Node(int data, Node *nextNode) { //overloaded constructor
             Nodedata = data;
             next = nextNode;
          }
      
      
};
 // traverse printing all the elements
class SLL {
	
	public:
		Node *front;
		// part D Traverse the SLL and print all the elements out
	void print(Node *nn) {
		while (nn !=  NULL) {
			cout << nn->Nodedata << " ";
			nn = nn->next;
		}
		
	}
	 // part E delete last node
	 void deleteback(Node *nn) {
	 	while (nn->next->next != NULL) {
	 		nn = nn->next;
		 }
		 
		 delete nn->next;
		 nn->next = NULL;
		 
	}
		//part F will tranverse in the middle and delete the middle
	 void insertMiddle(Node *nn) {
		while(nn->next->next->next != NULL) {
		nn = nn->next;	
	}
		delete nn->next;
		nn->next = NULL;
	}
	
};

int main() {
    // part A
   Node *a; 
   a = new Node(1,NULL);
   
   
   Node *b;
   b = new Node(2,NULL);
   
   Node *c;
   c = new Node(4,NULL);
  
   
   Node *d;
   d = new Node(6,NULL);
   
   
   Node *e;
   e = new Node(8,NULL);
  
   Node *f;
   f = new Node(9,NULL); // insert at the beginning of Node *a;
   
   Node *g;
   g = new Node(10,NULL); // insert in the middle
   
   f->next = a;
   a->next = b;
   b->next = c;
   c->next = d;
   d->next = e;
   
   c->next = g;
   g->next = d;
   
   SLL list; // object of the class SLL and part D
   list.print(f);
   
   // part E deleting the last element
   list.deleteback(f);
   cout << endl;
   list.print(f);
   
   // part F
   list.insertMiddle(f);
   cout << endl;
   list.print(f);
   
   return 0;
}
 
Last edited:

Related to Deleting Middle Node in Linked List

1. How do you delete a middle node in a linked list?

To delete a middle node in a linked list, you need to first find the node that you want to delete. Then, you can simply remove the node by changing the pointer of the previous node to point to the next node. This effectively removes the middle node from the linked list.

2. What is the time complexity of deleting a middle node in a linked list?

The time complexity of deleting a middle node in a linked list is O(1). This is because finding the middle node and removing it can be done in constant time, regardless of the size of the linked list.

3. Can you delete the first or last node in a linked list using the same method?

Yes, the same method can be used to delete the first or last node in a linked list. However, you will need to handle special cases where the first or last node is the one being deleted. This may involve updating the head or tail pointer of the linked list.

4. Is it possible to delete multiple middle nodes in a single operation?

Yes, it is possible to delete multiple middle nodes in a single operation. This can be achieved by traversing the linked list and keeping track of the middle nodes that need to be deleted. Then, you can perform a single operation to remove all of the middle nodes at once.

5. What happens to the data stored in the middle node when it is deleted?

The data stored in the middle node is lost when it is deleted. This is because the node is removed from the linked list and the data is no longer accessible. Therefore, it is important to make sure that the data is no longer needed before deleting a middle node in a linked list.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
6
Views
12K
Back
Top