gpt4 book ai didi

c++ - c++删除一系列节点

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:32 26 4
gpt4 key购买 nike

大家好,我是这里的新手,也是编程方面的新手。我正在尝试删除链表中的一系列节点,但不知道如何操作。我尝试使用 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);
}

最佳答案

您可以创建一个函数来检索给定名称的节点指针,然后将“deleteNode”函数更改为通过指针而不是名称删除的函数,而不是通过节点名称 (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--;
}

关于c++ - c++删除一系列节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36660418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com