All my other functions of my doubly linked list which consist of Account class and Node class are all working except this last function.
The function bool deleteAcc(string name) will take as it’s argument, the string name (that the user inputs) and returns a bool result. It will then find the name in the list and delete the corresponding Account (and Node) from the list, then return True. If the Account was not in the list, it will return False.
This is my code as shown below:
#include <iostream>
#include "Account.h"
#include <string>
#include "Node.h"
using namespace std;
bool deleteAcc(const string name1);
int main(){
string name;
int k;
cout << "How many accounts do you want to enter? ";
cin >> k;
Node* head = NULL;
//Node* tail = NULL;
for (int i = 0; i < k; i++) {
string name;
double balance;
cout << "Enter account name: ";
cin >> name;
cout << "Enter account balance: ";
cin >> balance;
Account account(name, balance);
Node* newNode = new Node(account);
if (head == NULL) {
// The list is empty, so set both head and tail to the
// new node
head = newNode;
// tail = newNode;
} else {
// The list is not empty, so add the new node to the
// tail
newNode->setNext(head);
//newNode->setPrevious(head); // Update previous pointer
// of newNode to point to the previous last node (i.e.,
// tail)
head = newNode;
}
}
// Print the list
cout << "Account balances:" << endl;
cout << endl;
Node* currentNode = head;
while (currentNode != NULL) {
cout << currentNode->getData() << endl;
currentNode = currentNode->getNext();
}
cout << "Enter the account name you want to delete: ";
cin >> name;
deleteAcc(name);
// Deallocate memory
currentNode = head;
while (currentNode != NULL) {
Node* nextNode = currentNode->getNext();
delete currentNode;
currentNode = nextNode;
}
return 0;
}
bool deleteAcc(const string name1)
{
Node* currentNode = head;
Node* previousNode = NULL;
while (currentNode != NULL){
if (currentNode->getData().getName() == name1) {
if (previousNode == NULL) {
// The node to be deleted is the head node
head = currentNode->getNext();
}
else {
// The node to be deleted is in the middle of the list
previousNode->setNext(currentNode->getNext());
}
delete currentNode;
return true;
}
// Update the previous node and move to the next node
previousNode = currentNode;
currentNode = currentNode->getNext();
}
// The account was not found in the list
return false;
}
The problem is that head inside the deleteAcc(name) function is outside of the scope so is it even possible to answer this question with just a single argument string inside the bool deleteAccount(string name)????
Please help. Thanks
I have tried many times to find how to solve this problem like declaring head at the top of the file before int main() like this:
Node* head = NULL;
int main(){
// functions and variables here
}
but it does not produce the desired result of showing an output.
In most cases, in addition to having a
Nodeclass/struct, you'd also have another class/struct to represent the doubly linked list, and that class would hold theheadnode, as well as any other functions relevant to the list. See this example:Then, your
deleteAccfunction (as well as any other function defined as part ofDllist) would have access tohead.As you've written the code now, I'm inferring from this comment that you've set up the list entirely within
main:// other functions and variables written here. If that's the case, then the only way that you'd have access toheadwithout adding an additional parameter to thedeleteAccfunction would be to makeheada global variable, which I'd strongly discourage.On another node, your title has the phrase "doubly linked list", but it looks like your
deleteAccfunction is only modifying the forward link (i.e. calling->setNext. For the list to maintain its status as a doubly linked list, you need to make sure that both the forward and backwards links are set appropriately.Update: I'm responding to your comment here because the formatting is better:
Your
tailmember is absolutely needed for the structure to be doubly linked. However,newNode->setPrevious(head)won't work, as that'll result in bothgetNext()andgetPrevious()returning the sameNode. You'll wanthead->setPrevious(newNode)after the call tonewNode->setNext(head). The reason has to do with how you're constructing your list.Consider that we want to add three
Accounts, and they'll be named "Account 1", "Account 2", and "Account 3". Here is how your list will look after the addition of each account.After adding Account 1
After adding Account 2
After adding Account 3
Given the above, if you uncomment the line
newNode->setPrevious(head), then your final list will look like this, with each node having two pointers pointing to the same node, which is not how a doubly linked list should be set up:With
head->setPrevious(newNode), then you ultimately get:This is because, for instance, when you set the
nextpointer for "Account 2",headis still "Account 1", and adding apreviouspointer to that node is the inverse ofnewNode->setNext(head).