My SingleLinkList: Coding My First SLL

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

The discussion focuses on coding a Single Linked List (SLL) in C++ and addresses issues with the deleteMiddle function. The user successfully implemented the SLL but encountered problems when deleting the middle node. Key issues identified include not updating the previous node's next pointer after deletion, failing to set the next pointer for the last node, and not updating the counter pointer post-deletion. These corrections are essential for preventing memory leaks and ensuring proper list traversal.

PREREQUISITES
  • Understanding of C++ programming language
  • Knowledge of data structures, specifically linked lists
  • Familiarity with memory management in C++
  • Experience with debugging C++ code
NEXT STEPS
  • Review C++ pointers and memory management techniques
  • Learn about linked list operations, focusing on insertion and deletion
  • Study C++ debugging tools to identify and fix runtime errors
  • Explore advanced data structures, such as doubly linked lists and circular linked lists
USEFUL FOR

Students learning data structures, C++ developers implementing linked lists, and programmers seeking to improve their debugging skills in C++ applications.

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) {
			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 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);
   count << endl;
   list.print(f);
   
   
   // part F
   list.deleteMiddle(f);
   count << 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: 116
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!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
8K