![](/img/trans.png)
[英]I am trying to use my header files for a doubly linked list but when I call any functions in the main it gives same error code
[英]I am creating a doubly linked list that will alphabetize a list of names, but I am unsure of what to put in the int main( ) function
最終,該程序將按字母順序打印出名稱列表,以及與該名稱關聯的其他屬性。 換句話說,輸出屏幕將顯示如下:
Ares: Greek, fire, sword.
Freia: Norse, water, bow and arrow.
Poseidon: Greek, horses, ocean.
Thor: Norse, chariot, hammer.
Zeus: Greek, cloud, lightning.
同樣,名字在此列表中按字母順序排列,但屬性與它們並列顯示。 關於我的int main(),我不確定應該如何開始對這些名稱進行排序和排序。 我有一個必須排序的未排序列表(使用將這些名稱添加/插入正確順序的函數)。
//
// This is a standard library support code to the chapters of the book
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//
#ifndef STD_LIB_FACILITIES_GUARD
#define STD_LIB_FACILITIES_GUARD 1
#include <cmath>
#include <iostream>
#include <vector>
#include <stdexcept>
#include <string>
using namespace std;
//------------------------------------------------------------------------------
// Helper function to show an error message
inline void error(const string& errormessage)
{
throw runtime_error(errormessage);
}
//------------------------------------------------------------------------------
#endif // STD_LIB_FACILITIES_GUARD
//------------------------------------------------------------------------------
struct Link {
string name;
string mythology;
string vehicle;
string weapon;
Link* prev;
Link* succ;
Link(const string& n, const string& a, const string& b, const string&c,Link* p = 0,
Link* s = 0)
: name(n), mythology(a), vehicle(b), weapon(c), prev(p), succ(s) { }
};
Link* insert(Link* p, Link* n) // insert n before p; return n
{
if (n==0) return p;
if (p==0) return n;
n->succ = p; // p comes after n
if (p->prev) p->prev->succ = n;
n->prev = p->prev; // p's predecessor becomes n's predecessor
p->prev = n; // n becomes p's predecessor
return n;
}
void print_all(Link *p)
{
Link *current;
current = p;
while(current)
{
cout<<"For this link we have: \n";
cout<<"Name: "<<current->name<<".\n";
cout<<"Info1: "<<current->mythology<<".\n";
cout<<"Info2: "<<current->vehicle<<".\n";
cout<<"Info3: "<<current->weapon<<".\n";
current = current->succ;
}
}
Link * add_after_find(Link *p, Link *n,const string& s )
{ Link *current = 0;
current = p;
/* empty list */
if(p == 0)
{ cout<<"List is empty so string not found so not added after it. \n";
return 0;
}
/* DO WE NEED ONE LINK ONLY */
else if(p->succ == 0) /* one link only */
{
if(p->name == s)
{
/* add after link with s */
/* p in front */
p->succ = n;
n->prev = p;
p->prev = 0;
n->succ = 0;
return p;
} /* end of if names = */
else {
cout<<"String not found in link listed so not added. \n";
return p;
}
} /* end of one link */
else /* two or more links */
{
current = p;
while(current->succ)
{
if (s == current->name)
{
/* then n goes AFTER this link */
n->prev = current;
n->succ = current->succ;
current->succ = n;
return p;
} /* end of name matches */
else
{
current = current->succ;
}
}// end of while
/* if outside of while then we are at last link with a current -> name
so s not found */
cout<<"String is not found so not add after it. \n";
return p;
} // end of else 2 or more
} // end of function
int main()
{
Link*newlist = new Link("Thor","Norse","chariot","hammer");
newlist = add_after_find(newlist,new Link("Hera","Greek", "horse", "arrow"),"Thor");
newlist = add_after_find(newlist,new Link("Poseidon","Greek", "ocean", "trident"),"Freia");
newlist = add_after_find(newlist,new Link("Ares","Greek", "fire", "sword"),"Poseidon");
newlist = add_after_find(newlist,new Link("Zeus","Greek", "cloud", "lightning"),"Ares");
print_all(newlist);
cout<<"Now let's alphabetize these five gods.\n";
system("Pause");
return 0;
}
我假設這是家庭作業,如果不是,簡單的答案是您應該在該容器中使用std::list
和sort
方法。
main
什么? 最有可能在以下代碼行上進行sort_list( newlist )
: sort_list( newlist )
,其中指針通過引用傳遞(因為列表的頭部可能會更改)。 至於如何實現,取決於您要實現的排序算法,最簡單的可能是冒泡排序,列表的次佳選擇是合並排序。 Google,如果您需要有關算法的幫助,請回來詢問。
同時,您可能需要處理我作為對該問題的評論而提出的問題:代碼格式化,內存泄漏(在插入時(如果找不到位置,則在插入時以及在程序末尾)),數據結構的正確性在任何時候……我都沒有做深入的分析,但是我有一種感覺,當您的add_after_find
需要在列表的add_after_find
添加元素時,它可能會失敗。甚至在考慮進行排序之前,您都應該確保輸入正確。 比起開始添加更多代碼,調試當前問題要容易得多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.