Deleting Middle Node in Linked List

  • Context: MHB 
  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    List
Click For Summary
SUMMARY

The discussion focuses on deleting the middle node from a singly linked list in C++. The provided code demonstrates the implementation of a function named insertMiddle within a class SLL that traverses the list to identify and delete the middle node. The linked list is initialized with seven nodes, and the middle node, which is the fourth node, is successfully removed. The code also includes functions for printing the list and deleting the last node.

PREREQUISITES
  • Understanding of C++ programming language
  • Knowledge of data structures, specifically linked lists
  • Familiarity with class and object-oriented programming concepts
  • Basic understanding of memory management in C++ (e.g., dynamic allocation)
NEXT STEPS
  • Learn about different methods for deleting nodes in linked lists
  • Explore the implementation of doubly linked lists in C++
  • Study the use of smart pointers in C++ for better memory management
  • Investigate algorithms for finding the middle node in a linked list efficiently
USEFUL FOR

Software developers, computer science students, and anyone interested in mastering linked list operations in C++.

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);
   count << 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) {
			count << 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);
   count << endl;
   list.print(f);
   
   // part F
   list.insertMiddle(f);
   count << 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
2K
  • · Replies 2 ·
Replies
2
Views
2K