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.
 
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 had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

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