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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

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