大家好,我是新来的,相对于编程也是如此。我试图删除链表中的一系列节点,但无法弄清楚如何这样做。我尝试使用for循环,但是因为我将删除函数传递给一个字符串,所以我无法递增节点以使其删除最终计数。任何帮助将不胜感激!
#include <iostream>
#define nullptr 0
#include <cstdlib>
#include <algorithm>
#include <string>
#include <conio.h>
using namespace std;
int menu();
class ItemList {
private:
struct ListNode{
string IName;
string QQuantity;
string PPrice;
double value;
struct ListNode * next;
};
ListNode *head;
public:
ItemList()
{
head = new ListNode;
head->next=nullptr;
}
~ItemList();
void insertNode(string Item, string Quantity, string Price)
{
ListNode *newNode;
ListNode *nodePtr;
ListNode *previousNode=nullptr;
newNode=new ListNode;
newNode->IName=Item;
newNode->QQuantity=Quantity;
newNode->PPrice=Price;
if(!head)
{
head=newNode;
newNode->next=nullptr;
}
else
{
nodePtr=head;
previousNode=nullptr;
while(nodePtr != nullptr && nodePtr->IName < Item)
{
previousNode=nodePtr;
nodePtr=nodePtr->next;
}
if(previousNode==nullptr)
{
head=newNode;
newNode->next=nodePtr;
}
else
{
previousNode->next=newNode;
newNode->next=nodePtr;
}
}
}
void displayNode()
{
ListNode *nodePtr;
nodePtr=head->next;
while(nodePtr)
{
cout << nodePtr->IName << ", ";
cout << nodePtr->QQuantity << " ";
cout << "$" << nodePtr->PPrice << "\n" << endl;
nodePtr=nodePtr->next;
}
}
void modifyNode(string Item)
{
ListNode *nodePtr;
ListNode *nodePrev;
string newName, newQuantity, newPrice;
int modify;
if (!head)
{
return;
cout << "Store is empty." << endl;
}
else
{
nodePtr = head;
if (head->IName==Item)
nodePtr = head->next;
else
{
while (nodePtr != nullptr && nodePtr->IName != Item)
{
nodePrev = nodePtr;
nodePtr = nodePtr->next;
}
}
if (nodePtr)
{
cout << nodePtr->IName << "\t" << nodePtr->QQuantity << "\t" << nodePtr->PPrice << endl;
cout << "What would you like to change?\n";
cout << "1. Item" << endl;
cout << "2. Quantity" << endl;
cout << "3. Price" << endl;
cout << "4. Whole Entry" << endl;
cin >> modify;
transform(newName.begin(), newName.end(), newName.begin(), ::toupper);
switch (modify)
{
case 1:
cout << "Change to what?\n";
cin >> newName;
nodePtr->IName = newName;
break;
case 2:
cout << "Change to what?\n";
cin >> newQuantity;
nodePtr->QQuantity = newQuantity;
break;
case 3:
cout << "Change to what?\n";
cin >> newPrice;
nodePtr->PPrice = newPrice;
break;
case 4:
cout << "Change to what?\n";
cin >> newName;
nodePtr->IName = newName;
cout << "Change to what?\n";
cin >> newQuantity;
nodePtr->QQuantity = newQuantity;
cout << "Change to what?\n";
cin >> newPrice;
nodePtr->PPrice = newPrice;
}
}
else
cout << "Person not found\n";
}
}
void deleteNode(string Item)
{
ListNode *nodePtr;
ListNode *previousNode;
if(!head)
return;
if(head->IName==Item)
{
nodePtr=head->next;
delete head;
head=nodePtr;
}
else
{
nodePtr=head;
while(nodePtr!=nullptr && nodePtr->IName!=Item)
{
previousNode=nodePtr;
nodePtr=nodePtr->next;
}
if(nodePtr)
{
previousNode->next=nodePtr->next;
delete nodePtr;
}
else
{
cout << "Nothing to delete." << endl;
}
}
}
};
ItemList::~ItemList()
{
ListNode *nodePtr;
ListNode *nextNode;
nodePtr=head;
while(nodePtr!=nullptr)
{
nextNode=nodePtr->next;
delete nodePtr;
nodePtr=nextNode;
}
}
int main()
{
ItemList pro;
int method;
while(method!=0)
{
int method=menu();
system("cls");
string It, Q, P;
switch(method)
{
case 1:
int count;
cout << "How many products would you like to put in?" << endl;
cin >> count;
system("cls");
for(int i=0; i<count; i++)
{
cout << "Product #" << i + 1 << endl;
cout << "Enter the item name: ";
cin.sync();
getline(cin,It);
transform(It.begin(), It.end(), It.begin(), ::toupper);
cout << "Enter the Quantity: ";
cin >> Q;
transform(Q.begin(), Q.end(), Q.begin(), ::toupper);
cout << "Enter the Price: ";
cin >> P;
pro.insertNode(It, Q, P);
cout << "\n";
}
break;
case 2:
pro.displayNode();
break;
case 3:
pro.displayNode();
cout << "What product do you wish to modify? (by item name)" << endl;
cin.sync();
getline(cin, It);
transform(It.begin(), It.end(), It.begin(), ::toupper);
system("cls");
pro.modifyNode(It);
break;
case 4:
int del;
cout << "Do you wish to delete one product or more?" << endl;
cout << "1. One" << endl;
cout << "2. Range of Products" << endl;
cin >> del;
system("cls");
switch(del)
{
case 1:
cout << "What product do you wish to delete? (by item name)" << endl;
pro.displayNode();
cout << "\n";
cin.sync();
getline(cin,It);
transform(It.begin(), It.end(), It.begin(), ::toupper);
pro.deleteNode(It);
cout << "\n";
break;
case 2:
int count;
cout << "How many?";
cin >> count;
pro.displayNode();
cout << "Starting where?" << endl;
cin >> It;
transform(It.begin(), It.end(), It.begin(), ::toupper);
for(int i=0; i<count; i++)
{
pro.deleteNode(It);
It(i);
}
break;
}
break;
case 5:
pro.~ItemList();
cout << "All items deleted." << endl;
break;
case 0:
cout << "Exiting the program." << endl;
return 0;
}
system("pause");
system("cls");
}
return 0;
}
int menu()
{
string space1= " ";
string space2= " ";
int method;
cout << space1 << "What would you like to do to the Phone Book?" << endl;
cout << space2 << "1. Insert" << endl;
cout << space2 << "2. Display" << endl;
cout << space2 << "3. Modify" << endl;
cout << space2 << "4. Delete" << endl;
cout << space2 << "5. Empty" << endl;
cout << space2 << "0. Exit\n" << endl;
cout << space2;
cin >> method;
return(method);
}
答案 0 :(得分:0)
您可以创建一个检索给定名称的节点指针,然后更改“删除节点”的函数,而不是按节点名称(IName)删除。函数用于通过指针而不是名称删除的函数。此外,此函数应返回下一个节点指针(如果列表中没有其他元素,则返回null),可用于删除后续节点。
编辑:非常基本的代码示例:
char * get_pointer_by_name(string Name)
{
ListNode * curr_node = this->head;
while(curr_node != NULL)
{
if(curr_node->IName == Name)
{
return (char *) curr_node;
}
else
{
curr_node = curr_node->next;
}
}
// node not found
}
char * delete_node(char * pointer)
{
char * tmp = (char *) (ListNode *) pointer->next; // check this, it may be wrong
// this is a very simple and illustrative way to delete your node, not the right one
delete pointer;
// return the next node pointer
return tmp;
}
完成此操作后,您所要做的就是使用get_pointer_by_name()获取要删除的节点,并迭代几次以删除其余节点,如下所示:
char * tmp = get_pointer_by_name("name");
while(tmp != NULL && count > 0)
{
tmp = deleteNode(tmp);
count--;
}