MHB Help Me Fix My Program: What Is Wrong?

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    Program
AI Thread Summary
The discussion centers on troubleshooting a linked list implementation in C++. Key issues identified include the lack of updates to the `prev` pointers in the `Insert` function, which is crucial for maintaining a doubly linked list structure. The `Find` method is noted to have unnecessary print statements, which should be removed as it should only return a pointer to the node without outputting its key. The `Delete` method is also highlighted as needing implementation, with suggestions provided for its structure, including handling cases for deleting the head node and updating pointers correctly. It is recommended that the `Delete` function return a boolean value to indicate success or failure and consider whether to deallocate the memory for the node being deleted. Overall, the conversation emphasizes refining the linked list operations to ensure proper functionality and adherence to best coding practices.
needOfHelpCMath
Messages
70
Reaction score
0
What is wrong with my program? I am so close to finish it. May anyone tell me what is wrong and how to fix it.

HTML:
#include <iostream>
#include <cstdlib>
using namespace std;

class Node {
   public:
   
   Node(); 
   
   Node* prev;
   
   string key;
   
   Node* next;
};

Node::Node(){
   prev = 0;
   next = 0;
}

class LinkedList {
   public:
   LinkedList();
   void Insert(string key);
   void Print();
   Node* Find(string key);
   void Delete(Node* x);
   
   Node* head;
};
LinkedList::LinkedList(){
   head=0;
}
void LinkedList::Insert(string key) {
   Node* n;
   
    n=new Node;
     n->key= key;
      
      n->next = head;
      
      head=n;
      
      return;
}
void LinkedList::Print() {
   
 Node* n;
 cout <<"Print"<<endl;
 n=head;
 
 
 while (n!=0){
      cout << n->key << endl;
      n=n->next;
   }
   return;
   }
   
/*void LinkedList::Find() {
   
 Node* n;
 cout <<"Print"<<endl;
 n=head;
 
 
 while (n!=0){
      cout << n->key << endl;
      n=n->next;
   }
   return;
   }
   */
   
int main(){
  
   string line;
   LinkedList l;
   
while (true){
   
   getline(cin, line);
   if (line.empty()){
      break;
   }
    l.Insert(line);
   
}
l.Print();
/*
   
   
   n = l.Find("test3");
   cout << "found key "<< n->key << endl; 
*/

return 0;
}
HTML:
*What I got correct*
1. Unit test
1/1
Code Compiles

2. Compare output:
1/1
Input
100
200

Your output	:
Print
200
100

3. Unit test
1/1

Test Node Class
4. Unit test
1/1

Test Linked List Insert Function
5. Unit test
1/1

Insert Twice
6. Unit test
0/1
Find Node Test
 
Technology news on Phys.org
1. Your Node class has prev and next pointers. So it seems like your linked list would be a doubly linked list. However, in your insert function, you do not update the prev pointers.

2. Your find method should return a pointer to a node. Here's one possibility for the body of find:
Code:
Node *n = head;
while (n) {
  if (n->key == key) {
    return(n);
  }
  n = n->next;
}
return(NULL);  // key not in list has return of 0

3. Presumably, you are to also write method delete. Post again if you need help for this method.
 
Okay thank you! ill post again here if i need help. Thank you very much
 
okay now i need help for the "Delete" method

HTML:
#include <iostream>
#include <cstdlib>
using namespace std;

class Node {
   public:
   
   Node(); 
   
   Node* prev;
   
   string key;
   
   Node* next;
};
Node:: Node(){
   prev=0;
   next=0;

}
class LinkedList {
   public:
   LinkedList();
   void Insert(string key);
   void Print();
   Node* Find(string key);
   void Delete(Node* x);
   
   Node* head;
};
LinkedList::LinkedList(){
   head=0;
}
void LinkedList::Insert(string key) {
   Node* n;
   
    n=new Node;
     n->key= key;
      
      n->next = head;
      
      head=n;
      
      return;
}
void LinkedList::Print() {
   
 Node* n;
 cout <<"List:"<<endl;
 n=head;
 
 
 while (n!=0){
      cout << n->key << endl;
      n=n->next;
   }
   return;
   }
   
Node* LinkedList::Find(string key){
   Node* n;
 cout <<""<<endl;
 n=head;
 while (n!=0){
     if(n->key==key){
         cout << n->key << endl;
         return n;
     }
      n=n->next;
      
   }
   return 0;
}
   

   
int main(){
   
  
   LinkedList l;
 //  Node* n;
   
   
   string line;
   while(true){
      
      getline(cin, line);
      if(line.empty()){
         break;
         
      }
      
      l.Insert(line);
      
      
   
   }
   l.Print();
/*
n=l.Find("Test 1");
if(n==0){
   cout << "ERROR";
}
*/
return 0;

}

what i got right so far:
Code:
1. Unit test
1/1
Code Compiles
2. Compare output
2/2
Input
Input Test 1
Input Test 2
Input Test 3
Your output	
List:
Input Test 3
Input Test 2
Input Test 1
3. Unit test
2/2
Test Node Class
4. Unit test
2/2
Test Linked List Insert Function
5. Unit test
2/2
Insert Twice
6. Unit test
2/2
Find Node Test
Your output

test2

test1

7. Unit test
0/1
 Delete Node For a bonus question you can add the LinkedList member function Delete to delete a node: ``` void LinkedList::Delete(Node* n); ```
 
First comment. In your implementation of Find, you print the key of the sought node. Normally, you don't want to do this. Unless the specification of a method calls for output (printing), don't have print statements in the method. I wouldn't think Find should have any printing.

Here's most of the body of a Delete method:

Code:
if (x==NULL || head==NULL) {
  return; // do nothing for empty list or removal of NULL pointer
}
if (x==head) {
  // you write the updates of the pointers
  return;
}
Node *p=head->next, *q=head;
while (p != NULL) {
  if (p==x) {
    // you update the pointers
    return;
  }
  q=p;
  p=p->next;
}

Comments:
Assuming x!=NULL and head!=NULL, for x==head, you just need to change head appropriately; otherwise you need to find the node *x in the list and also the node that precedes x in the list. For x!=head, you do this by sending two pointers p and q down the list with always q==p->prev. Then one assignment removes x from the list. Finally, it's a good idea to set x->next to NULL before a return.

Rather than Delete a void function, I would probably make it a boolean method, returning true for a successful removal and false otherwise. There is also the question of whether Delete should deallocate the storage for node x. If the specification says to do this, you need to include a call delete(x) before you return. I would think a reasonable Delete would leave this deallocation to the caller.

Finally, Delete is much "easier" for a doubly linked list. You might try to implement a doubly linked list and modify all the methods.
 
johng said:
First comment. In your implementation of Find, you print the key of the sought node. Normally, you don't want to do this. Unless the specification of a method calls for output (printing), don't have print statements in the method. I wouldn't think Find should have any printing.

Here's most of the body of a Delete method:

Code:
if (x==NULL || head==NULL) {
  return; // do nothing for empty list or removal of NULL pointer
}
if (x==head) {
  // you write the updates of the pointers
  return;
}
Node *p=head->next, *q=head;
while (p != NULL) {
  if (p==x) {
    // you update the pointers
    return;
  }
  q=p;
  p=p->next;
}

Comments:
Assuming x!=NULL and head!=NULL, for x==head, you just need to change head appropriately; otherwise you need to find the node *x in the list and also the node that precedes x in the list. For x!=head, you do this by sending two pointers p and q down the list with always q==p->prev. Then one assignment removes x from the list. Finally, it's a good idea to set x->next to NULL before a return.

Rather than Delete a void function, I would probably make it a boolean method, returning true for a successful removal and false otherwise. There is also the question of whether Delete should deallocate the storage for node x. If the specification says to do this, you need to include a call delete(x) before you return. I would think a reasonable Delete would leave this deallocation to the caller.

Finally, Delete is much "easier" for a doubly linked list. You might try to implement a doubly linked list and modify all the methods.

Thank you very much for your guidance appreciate it! Thank you.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
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

Replies
1
Views
1K
Replies
9
Views
3K
Replies
89
Views
6K
Replies
2
Views
2K
Replies
3
Views
8K
Replies
23
Views
2K
Replies
5
Views
3K
Back
Top