MHB Deleting Middle Node in Linked List

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    List
AI Thread 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:
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...

Similar threads

Replies
1
Views
1K
Replies
5
Views
2K
Replies
1
Views
3K
Replies
3
Views
8K
Replies
1
Views
10K
Replies
6
Views
13K
Replies
2
Views
2K
Back
Top