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: 99
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!
 
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 have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top