我從頭開始在C++中創建一個列表,而不是使用STL列表函數。我的程序專爲用戶在列表中插入一個新項目(字符串)而設計,一次一個。這些項目應該在插入時進行排序,並且部分工作。用戶應該能夠向前和向後打印它們。在雙向鏈表中插入排序
實例:
- 我插入:1,3,2 - 結果前方:1,2,3 - 向後:3,2,1
- 我插入1,3,2,4 - 結果向前:1,4,2,3 - 向後:3,2,4,1
- 我插入4,2,3 - 結果向前:2,3,4 - 向後:4,3,2
- 我插入4,2,3,1 - 結果向前:1,4向後:1,3重複直到程序崩潰
這些是幾個例子,我可以包括更多。我不能爲我的生活看到我的代碼有什麼問題。我從我認識的人那裏得到了很多偉大編程人員的幫助,但是他們看了幾個小時後卻看不到問題。我已經堅持了好幾天了,我只是看不到它。
這裏是我的代碼(註釋是在挪威,對不起!):
的main.cpp
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
//definerer noden
struct node
{
string data; //det noden inneholder
node* next; //nestepeker
node* prev; //forrigepeker
};
//definerer funksjonene
bool isEmpty(node* first);
char menu();
void insertAsFirstElement(node* &first, node* &end, string data);
void insert(node* &first, node* &end, string data);
void remove(node* &first, node* &end, string data);
void showList(node* first);
void printBackwards(node* end);
//er noden tom?
bool isEmpty(node* first)
{
if(first == NULL)
return true;
else
return false;
}
//sender valgskjermen til konsollen
char menu()
{
char choice;
cout << "Welcome to LISTOMANIA 3000x! \n" << endl;
cout << "Press <1> to add an item" << endl;
cout << "Press <2> to remove item on top" << endl;
cout << "Press <3> to display the list forward" << endl;
cout << "Press <4> to display the list backwards" << endl;
cout << "Press <5> to exit" << endl;
cout << "\nInput: ";
cin >> choice;
return choice;
}
//hvis lista er tom, sett inn node som første element:
void insertAsFirstElement(node* &first, node* &end, string data)
{
cout << "temp is first\n";
node* temp = new node;
temp->data = data;
temp->next = NULL;
temp->prev = NULL;
first = temp;
end = temp;
}
//hvis lista ikke er tom, sett inn noden sortert:
void insert(node* &first, node* &end, string data)
{
if(isEmpty(first))
{
insertAsFirstElement(first, end, data);
}
else
{
node* temp = new node;
temp->data = data;
node* n = first;
while(n)
{
if(n->data > temp->data)
{
cout << "temp BEFORE n" << endl;
temp->prev = n->prev;
temp->next = n;
n->prev = temp;
if(temp->prev)
{
temp->prev->next = temp;
}
else
{
first = temp;
}
}
else if(n->data <= temp->data)
{
cout << "temp AFTER n" << endl;
//temp = new node;
//temp->data = data;
temp->prev = n;
temp->next = n->next;
n->next = temp;
if(temp->next)
{
temp->next->prev = temp;
}
else
{
end = temp;
}
break;
}
n = n->next;
}
}
}
//sletter valgt node
void remove(node* &first, node* &end, string data)
{
string delItem;
node* temp;
if(isEmpty(first))
cout << "\nNothing to delete, the list is empty!\n------------------------------------------------\n------------------------------------------------\n";
else if(first == end)
{
cout << "\nYou removed <" << first->data << ">!" << endl;
delete first;
first = NULL;
end = NULL;
cout <<"------------------------------------------------\n------------------------------------------------\n";
}
else
{
node* temp = first;
cout << "You removed <" << temp->data << ">!" << endl;
first = first->next;
delete temp;
cout <<"------------------------------------------------\n------------------------------------------------\n";
}
}
//skriver ut listen alfabetisk
void showList(node* first)
{
node * temp = first;
if(isEmpty(first))
{
cout << "\nThe list is empty!\n";
}
else
{
cout << "\nThe list contains: \n\n";
while(temp != NULL)
{
cout << temp->data << endl;
temp = temp->next;
}
}
cout << "------------------------------------------------\n";
cout << "------------------------------------------------\n";
}
//skriver ut listen omvendt alfabetisk
void printBackwards(node* end)
{
node * temp = end;
if(isEmpty(end))
{
cout << "\nThe list is empty!\n";
}
else
{
cout << "\nThe list contains: \n\n";
while(temp != NULL)
{
cout << temp->data << endl;
temp = temp->prev;
}
}
cout << "------------------------------------------------\n";
cout << "------------------------------------------------\n";
}
//mainfunksjon
int main()
{
node* first = NULL;
node* end = NULL;
char choice;
string data;
do
{
choice = menu();
switch(choice)
{
case '1': cout <<"\nPlease add something to the list: ";
cin >> data;
insert(first, end, data);
cout << "------------------------------------------------\n------------------------------------------------\n";
break;
case '2': remove(first, end, data);
break;
case '3': showList(first);
break;
case '4': printBackwards(end);
break;
case '5': cout << "\nSystem exit...\n";
break;
default: cout << "\nInvalid input!\n------------------------------------------------\n------------------------------------------------\n";
}
}while(choice != '5');
return 0;
}
只是忽略連字符的線條,它們只是爲了使程序看起來更漂亮時,它運行。
請注意,我是初學者!除了我的問題之外,還可能有很多其他的錯誤。也忽略了remove
功能,我還沒有得到這一問題尚未...我添加了註釋//1.this line
和//2.this line
拳頭一個被稱爲當temp<n
所以你必須有一個節點比較temp
刪除'break;'給我'cout <<「temp AFTER n」<< endl;'重複,直到程序崩潰。與cout <<「temp BEFORE n」<< endl;'相同,具體取決於我放入的內容以及順序。分類也不是很固定,但有所改善,這也取決於我放入的內容和順序。 – maruudn 2014-09-27 21:26:42