从节点中删除数据
Remove a data from a node
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;
class MysticalBag
{
private:
int useCount;
int content;
string itemName;
public:
MysticalBag *next_ptr;
void setAttributes()
{
//Called for showing the items without any placement of item
useCount = 1;
content = 5;
itemName = "Healing Potion";
}
int takeUseCount()
{
return useCount;
}
int takeContent()
{
return content;
}
string takeItemName()
{
return itemName;
}
void setAttributes(int userCount, int content, string itemName)
{
this->useCount = useCount;
this->content = content;
this->itemName = itemName;
}
void itemAdder()
{
cout << "Enter use count (1-3) " <<endl;
cin >> useCount;
cout << "Enter content (1.0 - 100.0) " <<endl;
cin >> content;
cout << "Enter item name as text" << endl;
cin >> itemName;
cout<< itemName <<" is added in the bag."<< endl;
}
void showBag()
{
cout << "Showing bag contents" << endl << endl;
cout << itemName << endl;
cout << "U - "<< useCount <<", C - "<< content << endl;
}
};
int main()
{
char choice1,choice2;
choice1 = 0;
MysticalBag *head, *tail, *navigator;
navigator = new MysticalBag();
navigator->setAttributes();
head = navigator;
tail = navigator;
tail->next_ptr = NULL;
while(choice1 !='x')
{
cout << "What do you want to do with the bag?" << endl;
cout << "(a)dd item" << endl;
cout << "(r)emove item" << endl;
cout << "(s)how items" <<endl;
cout << "e(x)it" <<endl;
cin >> choice1;
if(choice1 == 'a')
{
navigator = new MysticalBag();
if(head==NULL)
{
head=navigator;
tail=navigator;
tail->next_ptr=NULL;
}
navigator->itemAdder();
tail->next_ptr = navigator;
tail = navigator;
tail->next_ptr = NULL;
}
else if(choice1 == 'r')
{
navigator = head;
tail = head;
while(navigator->next_ptr != NULL)
{
navigator = navigator->next_ptr;
tail = navigator;
}
cout << "Do you want to remove "<< navigator->takeItemName() <<" from your bag? (y/n)"<< endl;
cin >> choice2;
if(choice2 == 'y')
{
navigator = head;
if(navigator == head)
//I am stuck at this point!
navigator = NULL;
cout << "Item removed." << endl;
tail = head;
while(tail->next_ptr != NULL)
{
tail = tail->next_ptr;
}
}
else
{
cout<< "No item removed" <<endl;
}
navigator = head;
if(navigator == head && navigator == tail)
{
navigator = NULL;
head = NULL;
tail = NULL;
}
else
{
navigator = tail;
navigator = NULL;
tail = NULL;
tail = head;
while(tail->next_ptr != NULL)
{
tail = tail->next_ptr;
}
}
}
else if(choice1 != 'x')
{
navigator = head;
while(navigator != NULL)
{
navigator->showBag();
navigator = navigator->next_ptr;
}
}
}
getch();
}
我的目标是从节点中删除数据。用户可以将数据放入节点,即导航器。
我想的是将导航器指向头部和使头部回忆 setAttributes((。这将显示"治疗药水"。但是,如果用户添加到项目。如何一次只删除一个项目?
您可以只取出最后添加到袋子中的物品,还是可以取出任何物品?更一般的情况是询问从链表中删除。
要从链表(包(中删除节点(项目(,您需要知道要删除的项目的父级。节点的父节点是节点,next_ptr节点是节点。所以在循环中,你需要跟踪导航器的父级:
while(navigator->next_ptr != NULL)
{
/* a sample list */
/* "parent" --> "navigator" --> "navigator->next_ptr" */
parent = navigator; /* now we know who points to navigator */
navigator = navigator->next_ptr;
tail = navigator;
}
一旦我们知道了导航器的父级,删除导航器所需要做的就是:
parent->next_ptr = navigator->next_ptr;
/* now the list looks like */
/* "parent" --> "navigator->next_ptr" */
现在导航器已从袋子中取出。
我认为您的许多 while 循环仍然存在问题,但修复它们需要更多地了解您的意图。
这是一个列表吗?为什么不在自定义类中使用 std::list?
好吧,简而言之,你要问的是"如果我有一个指向类成员的指针,我如何访问类函数? 至少我是这么看的。
#include "Items.h" //let me assume you have some sort of struct or class for items in your bag
class Bag : public player { //player would give access to attributes like HP/MP/Who Owns the bag, etc..
protected:
vector<Item*> Contents;
int MaxItems;
public:
Bag();
Store(Item* It);
Remove(Item* It);
UseItem(Item* It);
};
Bag::Store(Item* It) {
if(Contents.size() >= MaxItems) return;
for( int i = 0; i < Contents.size(); i++ ) {
if(It == Contents[i] ) {
return;
}
}
Contents.push_back(It);
}
Bag::Remove(Item* It) {
for(int i = 0; i < Contents.size(); i++) {
if( It == Contents[i] ) {
Contents.erase(Contents.begin() + i);
break;
}
}
Bag::UseItem(Item* It) {
if(!(It->Usable())) { return };
It->Use();
}
class Item {
protected:
int ItemType; //suggest you use some enum for this
public:
Item();
virtual bool Usable();
virtual void Use();
};
Item::Item() { };
bool Item::Usable() {
//Purely Virtual
}
void Item::Use() {
//Same
}
class Potion : public Item {
protected:
int PotionType; //Again, I'd enum this
public:
Potion();
virtual bool Useable();
virtual void Use();
}
Potion::Potion() { ItemType = POTION; }
bool Potion::Useable() { return true; }
void Potion::Use() { //Purely Virtual }
class HealPotion : public Potion {
protected:
int Strength; //who knows, # of hp to heal
public:
HealPotion();
virtual void Use();
};
HealPotion::HealPotion() { PotionType = Heal; Strength = 45; }
void HealPotion::Use() { Owner->ChangeHits(Strength);
this->RemoveItem(this); } //Now you might also want to destroy the item to free up memory
相关文章:
- 从 Typedef 数据类型中删除常量
- 从输入中删除重复项,而不使用任何数据结构
- 删除使数据处于"错误状态"的默认构造函数的模式?
- 从自定义数据类型向量中删除重复元素
- 如何使用C++删除/更新txt文件中的特定数据
- 如何使用C++删除/更新txt文件中的特定数据?
- 从文本文件中读取数据并删除所有换行符空格,并在 C++ 控制台中显示
- C++ - 按自定义数据类型向量的值删除元素
- 如何从 txt 文件中删除数据/内容
- 如何通过其持有的数据从链表中删除某个节点?
- 基于给定字符串数据类型的链表删除节点
- 如何在 C++ 中使用删除运算符删除单个数据
- 如何在每次循环迭代期间生成向量,存储数据,然后删除该向量?
- 如何从C ++的输出中删除垃圾数据(垃圾数据)
- 如何从 HTTP 响应中删除垃圾数据
- C++ 类析构函数删除部分但不是全部成员数据
- 在向量上调用 clear() 实际上不会删除 data() 中的数据?
- SOCAT,将TCP套接字转发到Unix套接字,将recvmsg()更改为read(),删除辅助数据
- 如何删除字符串中的所有空格并将数据存储在两者之间
- 筛选器重复列表-列出重复数据删除