C++ C++;排序双链接列表:在列表中间插入问题
下面是我当前正在进行的代码,它将单链接列表转换为双链接列表。我还没有触及删除功能。我已经在空列表中插入了insert,列表的末尾和开头都工作了 但是,在中间插入的节点似乎无法创建与前一个节点的链接。我插入的调试行似乎同时显示了N-> NEXT和N-> PREV以及正确的内存地址,但是当我回到ReavePress时,中间插入的任何节点都会丢失,链接也就消失了。在这方面我错在哪里 代码如下:C++ C++;排序双链接列表:在列表中间插入问题,c++,linked-list,C++,Linked List,下面是我当前正在进行的代码,它将单链接列表转换为双链接列表。我还没有触及删除功能。我已经在空列表中插入了insert,列表的末尾和开头都工作了 但是,在中间插入的节点似乎无法创建与前一个节点的链接。我插入的调试行似乎同时显示了N-> NEXT和N-> PREV以及正确的内存地址,但是当我回到ReavePress时,中间插入的任何节点都会丢失,链接也就消失了。在这方面我错在哪里 代码如下: #include <iostream> #include <string> usin
#include <iostream>
#include <string>
using namespace std;
// define a node for storage and linking
class node {
public:
string name;
node *next;
node *prev;
};
class linkedList {
public:
linkedList() :top(NULL) {}
bool empty() { return top == NULL; }
node *getTop() { return top; }
node *getEnd() { return end; }
void setTop(node *n) { top = n; }
void setEnd(node *p) { end = p; }
void add(string);
int menu();
void remove(string);
~linkedList();
void reversePrint();
friend ostream& operator << (ostream&, const linkedList&); // default output is in-order print.
private:
node *top;
node *end;
};
void main() {
linkedList l;
cout << l.empty() << endl;
int option = 0;
string s;
bool go = true;
while (go) {
option = l.menu();
switch (option) {
case 1: cout << "enter a name: "; cin >> s; l.add(s); break;
case 2: cout << "enter name to be deleted: "; cin >> s; l.remove(s); break;
case 3: cout << l; break;
//case 4: cout << "can not be done with a singly linked list" << endl;
case 4: l.reversePrint(); break;
case 5: cout << "exiting" << endl; go = false; break;
}
}
system("pause");
}
void linkedList::remove(string s) {
bool found = false;
node *curr = getTop(), *prev = NULL;
while (curr != NULL) {
// match found, delete
if (curr->name == s) {
found = true;
// found at top
if (prev == NULL) {
node *temp = getTop();
setTop(curr->next);
delete(temp);
// found in list - not top
}
else {
prev->next = curr->next;
delete(curr);
}
}
// not found, advance pointers
if (!found) {
prev = curr;
curr = curr->next;
}
// found, exit loop
else curr = NULL;
}
if (found)cout << "Deleted " << s << endl;
else cout << s << " Not Found " << endl;
}
void linkedList::add(string s) {
node *n = new node();
n->name = s;
n->next = NULL;
n->prev = NULL;
// take care of empty list case
if (empty()) {
top = n;
end = n;
// take care of node belongs at beginning case
}
else if (getTop()->name > s) {
n->next = getTop();
n->prev = NULL;
setTop(n);
node *temp;
temp = n->next;
temp->prev = n;
// take care of inorder and end insert
}
else {
// insert in order case
node *curr = getTop(), *prev = curr;
while (curr != NULL) {
if (curr->name > s)break;
prev = curr;
curr = curr->next;
}
if (curr != NULL) { // search found insert point
n->next = curr;
cout << n->name << " " << n << " prev " << prev << " " << prev->name << endl;
n->prev = prev;
prev->next = n;
cout << "n->prev is: " << n->prev << " " << n->prev->name << endl;
cout << "n->next is: " << n->next << " " << n->next->name << endl;
}
// take care of end of list insertion
else if (curr == NULL) {// search did not find insert point
prev->next = n;
n->prev = prev;
cout << "n->prev is: " << n->prev << " " << n->prev->name << endl;
setEnd(n);
}
}
}
ostream& operator << (ostream& os, const linkedList& ll) {
//linkedList x = ll; // put this in and the code blows up - why?
node *n = ll.top;
if (n == NULL)cout << "List is empty." << endl;
else
while (n != NULL) {
os << n->name << endl;
os << n << endl;
if (n->next != NULL) {
os << "next is " << n->next << endl;
}
n = n->next;
}
return os;
}
void linkedList::reversePrint() {
node *n = end;
if (n == NULL)cout << "List is empty." << endl;
else
while (n != NULL) {
//cout << n->name << endl;
cout << "memory address of " << n->name << " is " << n << endl;
if (n->prev != NULL) {
cout << "prev is " << n->prev << endl;
}
n = n->prev;
}
return;
}
// return memory to heap
linkedList::~linkedList() {
cout << "~linkedList called." << endl;
node *curr = getTop(), *del;
while (curr != NULL) {
del = curr;
curr = curr->next;
delete(del);
}
}
int linkedList::menu() {
int choice = 0;
while (choice < 1 || choice > 5) {
cout << "\nEnter your choice" << endl;
cout << " 1. Add a name." << endl;
cout << " 2. Delete a name." << endl;
cout << " 3. Show list." << endl;
cout << " 4. Show reverse list. " << endl;
cout << " 5. EXIT " << endl;
cin >> choice;
}
return choice;
}
#包括
#包括
使用名称空间std;
//定义用于存储和链接的节点
类节点{
公众:
字符串名;
节点*下一步;
节点*prev;
};
类链接列表{
公众:
linkedList():top(NULL){}
bool empty(){return top==NULL;}
node*getTop(){return top;}
node*getEnd(){return end;}
void setTop(node*n){top=n;}
void setEnd(node*p){end=p;}
无效添加(字符串);
int菜单();
脱空(串);
~linkedList();
void reversePrint();
friend-ostream&运算符prev=NULL;
setTop(n);
节点*温度;
temp=n->next;
温度->上一个=n;
//注意顺序和末端插入
}
否则{
//在订单箱中插入
节点*curr=getTop(),*prev=curr;
while(curr!=NULL){
如果(curr->name>s)中断;
上一次=当前;
当前=当前->下一步;
}
如果(curr!=NULL){//搜索找到插入点
n->next=当前;
cout name插入到中间时,您没有设置当前的prev,只需执行以下操作:
n->next = curr;
curr->prev = n; // <-- this
n->next=curr;
curr->prev=n;//不相关:main
必须始终具有返回类型int
,只需在其中添加那一行就行了,其他什么都没有。现在我必须找出原因。。。。