My SingleLinkList: Coding My First SLL

  • MHB
  • Thread starter needOfHelpCMath
  • Start date
In summary, make sure to update the previous node's next pointer, set the next pointer for the last node to point to NULL, and update the counter pointer in your deleteMiddle function.
  • #1
needOfHelpCMath
72
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: 73
Technology news on Phys.org
  • #2
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!
 

FAQ: My SingleLinkList: Coding My First SLL

What is a SingleLinkList (SLL)?

A SingleLinkList (SLL) is a type of data structure commonly used in computer science to store and organize a collection of data. It is a linear data structure that consists of nodes connected by pointers, with each node containing a data value and a pointer to the next node in the list.

How do I code a SingleLinkList in my first program?

Coding a SingleLinkList in your first program involves creating a class for the nodes, defining the necessary methods for adding and removing nodes, and linking the nodes together using pointers. This process may vary depending on the programming language you are using. It is important to understand the basic principles of the SLL data structure before attempting to code it.

What are the advantages of using a SingleLinkList?

SingleLinkLists have several advantages over other data structures. They have a dynamic size, meaning that nodes can be easily added or removed without needing to allocate a large amount of memory. They also have efficient insertion and deletion operations, making them ideal for applications that require frequent data manipulation. Additionally, SLLs can be easily traversed in a forward direction, allowing for easy access to specific elements in the list.

What are some common operations performed on a SingleLinkList?

The most common operations performed on a SingleLinkList include adding a new node to the beginning or end of the list, removing a node from the list, and searching for a specific element in the list. Other common operations include inserting a node at a specific position, deleting a node at a specific position, and checking if the list is empty.

Are there any limitations to using a SingleLinkList?

SingleLinkLists have a few limitations that should be considered when choosing a data structure for a particular application. For example, unlike arrays, SLLs do not provide direct access to a specific element based on its index. This means that searching for a specific element can be time-consuming in large lists. Additionally, SLLs do not allow for efficient backward traversal, which may be needed for certain operations. Overall, the limitations of SLLs should be carefully considered when deciding whether to use this data structure for a particular application.

Similar threads

Replies
2
Views
1K
Replies
7
Views
2K
Replies
5
Views
2K
Replies
1
Views
9K
Replies
3
Views
8K
Replies
1
Views
2K
Back
Top