MHB My SingleLinkList: Coding My First SLL

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
AI Thread Summary
The discussion revolves around troubleshooting a single linked list implementation, specifically focusing on the deleteMiddle function. The user successfully created a single linked list but encountered issues when attempting to delete the middle node. Key points of concern include the failure to update the previous node's next pointer after deletion, which can lead to memory leaks. Additionally, the last node's next pointer was not set to NULL, potentially causing errors during deletion. It was also noted that the counter pointer was not updated post-deletion, which could lead to further complications. Suggestions for resolving these issues emphasize proper pointer management to ensure the integrity of the linked list structure.
needOfHelpCMath
Messages
70
Reaction score
0
Yea! I was able to code my own first SINGLE LINKED LIST! but having a little issues with my deleting middle. As you can see if you take my code run it.

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;
		Node *counter;
		// 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 deleteMiddle (Node *nn) {
	 	Node *y_ptr = nn; // will intialize y and z pointers to reach the middle of the linked list
	 	Node *z_ptr = nn;
	 	
	 	Node *prev;
	 	
	 	if (nn == NULL) {
	 		return;
		}
		if (nn->next == NULL) {
			delete nn;
			return;
		} 
		
		while (z_ptr != NULL && z_ptr->next != NULL) {
			z_ptr = z_ptr->next->next;
			prev = y_ptr;
			y_ptr = y_ptr->next;
		}
		
			prev->next = y_ptr->next;
			delete y_ptr->next;
			
			
		}
};

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.deleteMiddle(f);
   cout << endl;
   list.print(f);
	
   return 0;
}
My output:View attachment 6429
It will give me this:
 

Attachments

  • SLL.png
    SLL.png
    5.9 KB · Views: 101
Technology news on Phys.org
1 2 10 4 6 8

Hi there,

First of all, congratulations on coding your first single linked list! That is a great accomplishment.

I can see that you are having some issues with your deleteMiddle function. From looking at your code, it seems like you are on the right track. However, there are a few things that I noticed that might be causing the issue.

1. In the deleteMiddle function, you are deleting the node that is in the middle, but you are not updating the previous node's next pointer. This means that the previous node will still be pointing to the node that was deleted, causing a potential memory leak. To fix this, you need to update the previous node's next pointer to point to the node after the one you are deleting.

2. In your main function, when you are creating the linked list, you are not setting the next pointer for the last node (node with value 9). This means that the next pointer for this node will be NULL, and when you try to delete the last node, it will cause an error. To fix this, make sure you set the next pointer for the last node to point to NULL.

3. Another issue I noticed is that in your deleteMiddle function, you are deleting the node that is in the middle, but you are not updating the counter pointer. This means that the counter pointer will still be pointing to the old middle node, which is now deleted. This can cause issues if you try to use the counter pointer later on. To fix this, make sure you update the counter pointer to point to the correct node after the middle node is deleted.

I hope this helps. Keep up the good work!
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
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...

Similar threads

Back
Top