2016-11-26 138 views
-4

我想解决这个指针/类的问题,我坚持创建一个函数调用add(string t)。编写add(string t)函数的最佳方法是什么?我不能在main()内部修改。这只是一个愚蠢的代码,你移动你的火车。这里是一个输出的例子:指针/类C++

Current train: Engine 

Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit? 
a 

Which train is this? 
4 

Current train: Engine 

Previous train: 4 

Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit? 
a 

Which train is this? 
1 
Current train: Engine 

Previous train: 1 

Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit? 
p 

任何提示是非常感谢。谢谢!节日快乐。

#include <iostream> 

    using namespace std; 

    class train 
    { 
    private: 
    string s; 
    train* next_train; 
    train* previous_train; 
    train* has_next; 
    train* has_previous; 

    public: 
    train(string name); 
    string getName(); 
    train* nextTrain(); 
    train* previousTrain(); 
    train* hasNext(); 
    train* hasPrevious(); 
    void add(train *t); 
    }; 

    int main() 
    { 
    train engine = train("Engine"); 
    train* current = &engine; 
    string choice; 
    do 
    { 
     if(current -> hasNext()) 
     { 
      cout << "Next train: " << current -> nextTrain() -> getName() << 
             endl; 
     } 
     cout << "Current train: " << current -> getName() << endl; 

     if(current -> hasPrevious()) 
     { 
     cout << "Previous train: " << current -> previousTrain() -> getName() 
     << endl; 
     } 

     cout << "Do you wish to go to the (n)ext train, (p)revious train, 
     (a)dd a train, or (q)uit?\n"; 
     getline(cin,choice); 

     if(tolower(choice[0]) == 'n' && current -> hasNext()) 
     { 
     current = current -> nextTrain(); 
     } 
     else if(tolower(choice[0]) == 'p' && current -> hasPrevious()) 
     { 
     current = current -> previousTrain(); 
     } 
     else if(tolower(choice[0]) == 'a') 
     { 
     cout << "Which train is this?\n"; 
     string name; 
     getline(cin, name); 
     current->add(name); 
     }   

    }while(tolower(choice[0]) != 'q'); 
} 

    train::train(string name) 
    { 
    s = name; 
    } 

    string train::getName() 
    { 
    return s; 
    } 

    train* train::nextTrain() 
    { 
    return next_train; 
    } 

    train* train::previousTrain() 
    { 
    return previous_train; 
    } 

    train* train::hasNext() 
    { 
    return has_next; 
    } 

train* train::hasPrevious() 
{ 
    return has_previous; 
} 

void train::add(string *t) 
{ 
    train* current; 
    current = &t; 
} 
+1

什么是你的问题?你只希望有人为你写这个功能? –

+0

我不喜欢'has_next'或'has_previous'。你要去哪里? – user4581301

+0

@CareyGregory如果我只是想让别人为我写这个函数,那你很高兴问我这个问题。如果是这样,我可以支付某人这样做,而不是在这里。你明白吗?有时,我认为当人们发布问题时,并不一定是问题类型。在这种情况下,如果你认为我需要发布一个问题,那么这将是:在我的代码中使用add函数的最佳方式是什么?不是“你能为我写入添加功能吗?”谢谢! – Hugh

回答

0

你需要一个双向链表。该列表使用节点类,在这种情况下,您可以将其称为train。每个节点有一个name以及nextprevious节点。列表类将存储节点。

下面的例子是一个简单的版本,其中所有的成员都是public。您可以通过将next,prev,head,tail作为private,然后提供public方法来访问它们来改善它。

清单本身也应该自行清理。在add方法中,打电话给train *node = new train(s)。这个内存应该使用delete在析构函数中释放。您必须遍历列表并删除每个节点的内存。

参见这些引用:
https://en.wikipedia.org/wiki/Doubly_linked_list
http://www.dailyfreecode.com/Code/double-linked-list-cpp-3422.aspx

class train 
{ 
public: 
    string name; 
    train *next, *prev; 
    train(const string &s) {name = s; next = prev = nullptr;} 
}; 

class linked_list 
{ 
public: 
    train *head, *tail; 
    linked_list() { head = tail = nullptr; } 
    linked_list() { /*add cleanup routine*/ } 
    train* add(const string &s) 
    { 
     train *node = new train(s); 
     if(!head) { 
      head = tail = node; 
     } 
     else { 
      node->prev = tail; 
      tail->next = node; 
      tail = node; 
     } 
     return node; 
    } 
}; 

int main() 
{ 
    linked_list list; 
    train *walk, *node = nullptr; 
    string name; 
    while(true) { 
     char choice = 'a'; 
     if(node) { 
      cout << "(A)dd, (L)ist, (N)ext, (P)revious, (Q)uit ?\n"; 
      cin >> choice; 
      choice = tolower(choice); 
     } 

     switch(choice) { 
     case 'a': 
      cout << "enter name:\n"; 
      cin >> name; 
      node = list.add(name); 
      break; 
     case 'l': 
      cout << "list:\n"; 
      walk = list.head; 
      while(walk) { 
       cout << walk->name << "\n"; 
       walk = walk->next; 
      } 
      break; 
     case 'n': 
      if(node->next) { 
       node = node->next; 
       cout << "next: " << node->name << "\n"; 
      } 
      break; 
     case 'p': 
      if(node->prev) { 
       node = node->prev; 
       cout << "previous: " << node->name << "\n"; 
      } 
      break; 
     default: break; 
     } 
     if(choice == 'q') 
      break; 
    } 
    return 0; 
} 
+0

非常感谢。 – Hugh

0

看看train::add函数。它接受一个train *作为参数:

void train::add(train *t) 

您正试图在这里将它传递一个std::string

current->add(name);

你需要以某种方式创建一个新的train对象和一个指针传递给它train::add

+0

哦,所以你的意思就像void train :: add(string * t)?因为main()中的add函数将字符串名称作为参数。 – Hugh