MHB Deleting Middle Node in Linked List

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    List
Click For Summary
The discussion revolves around deleting the middle node from a linked list that contains seven items. The user initially confuses the function name "insertMiddle" with its intended purpose of deleting the middle node. The provided code includes a method that traverses the list to find and delete the middle node, but there are questions about its clarity and functionality. After clarification, the user confirms that the function is indeed meant to delete the middle node. The conversation emphasizes the importance of accurate naming in code to reflect its functionality.
needOfHelpCMath
Messages
70
Reaction score
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
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?
 
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:

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K