編輯:在C++中刪除不起作用
好的,所以這是列表的正確執行。我想很多人都會覺得它很有用。特別感謝你們,特別是在通訊員幫助下我的Agent_L。
正確的鏈表實現
#include <cstdio>
#include <cmath>
#include<iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
class Node{
friend class List;
public:
Node(Node* next, int wrt){
this->next = next;
this->wrt = wrt;
}
Node(const Node& obiekt){
this->wrt = obiekt.wrt;
this->next = obiekt.next;
}
//NIE MA DESTRUKTORA BO NIE ALOKUJE ZADNYCH DANYCH !!!
void show(){
cout<<this->wrt<<endl;
}
private:
Node* next;
int wrt;
};
class List{
public:
List(int wrt){
this->root = new Node(NULL, wrt);
}
List(const List& list)
{
// jesli pusty kopiujemy
if (list.root == NULL)
{
this->root = NULL;
return;
}
//tworzenie nowego korzenia
this->root = new Node(NULL, list.root->wrt);
Node* list_currentNode = list.root;
Node* this_currentNode = this->root;
while (list_currentNode->next != NULL)
{
// tworzenie nastepnika
Node* newNode = new Node(NULL, list_currentNode->next->wrt);
this_currentNode->next = newNode;
this_currentNode = this_currentNode->next;
list_currentNode = list_currentNode->next;
}
}
void add(int wrt){
Node* node = new Node(NULL, wrt);
Node* el = this->root;
while(el->next != NULL){
//el->show();
el = el->next;
}
el->next = node;
}
void remove(int index){
Node* el = this->root;
if(index == 0){
this->root = el->next;
delete el;
}
else{
int i = 0;
while(el != NULL && i < index - 1){
el = el->next;
i++;
}
if(el!=NULL){
Node* toRem = el->next;
Node* newNext = toRem->next;
el->next = newNext;
delete toRem;
}
}
}
void show(){
Node* el = this->root;
while(el != NULL){
el->show();
el = el->next;
}
}
~List(){
Node* currentNode = this->root;
while (currentNode != NULL)
{
Node* nextNode = currentNode->next;
delete currentNode;
currentNode = nextNode;
}
}
private:
Node* root;
};
int main(){
List* l = new List(11);
l->add(22); l->add(33);
l->show();
cout<<endl;
List* lala = new List(*l);
lala->show();
cout<<endl;
lala->add(44);
cout<<"lala before remove"<<endl;
lala->show();
lala->remove(1);
cout<<"l before delete"<<endl;
l->show();
cout<<"lala before delete"<<endl;
lala->show();
delete l;
/* cout<<"l after delete "<<endl;
l->show(); */
cout<<"lala after delete"<<endl;
lala->show();
return 0;
}
我實現了列表和存在的問題。我在列表中的析構函數不工作,因爲它應該:請看看主要,並看到「l刪除後」它向後打印l列表。 更大的問題是刪除方法沒有刪除它裏面的工作,因爲它應該但當我試圖取消註釋刪除el /刪除雷姆我進入無限循環。 我已經嘗試修復它4個小時。請看一眼。
爲什麼當我調用l-> remove(0)時,這些行特別是87(忘記100)會導致程序崩潰?
http://wklej.org/id/761056/線路87和100
remove方法和列表的析構函數是重要
void remove(int index){
Node* el = this->root;
if(index == 0){
this->root = el->next;
// delete el;
}
else{
int i = 0;
while(el != NULL && i < index - 1){
el = el->next;
i++;
}
if(el!=NULL){
Node* toRem = el->next;
Node* newNext = toRem->next;
el->next = newNext;
// delete toRem;
}
}
}
~List(){
Node* currentNode = this->root;
while (currentNode != NULL)
{
Node* nextNode = currentNode->next;
delete currentNode;
currentNode = nextNode;
}
}
所有代碼
#include <cstdio>
#include <cmath>
#include<iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
class Node{
friend class List;
public:
Node(Node* next, int wrt){
this->next = next;
this->wrt = wrt;
}
Node(const Node& obiekt){
this->wrt = obiekt.wrt;
this->next = obiekt.next;
}
//NIE MA DESTRUKTORA BO NIE ALOKUJE ZADNYCH DANYCH !!!
void show(){
cout<<this->wrt<<endl;
}
private:
Node* next;
int wrt;
};
class List{
public:
List(int wrt){
this->root = new Node(NULL, wrt);
}
List(const List& list)
{
// jesli pusty kopiujemy
if (list.root == NULL)
{
this->root = NULL;
return;
}
//tworzenie nowego korzenia
this->root = new Node(NULL, list.root->wrt);
Node* list_currentNode = list.root;
Node* this_currentNode = this->root;
while (list_currentNode->next != NULL)
{
// tworzenie nastepnika
Node* newNode = new Node(NULL, list_currentNode->next->wrt);
this_currentNode->next = newNode;
this_currentNode = this_currentNode->next;
list_currentNode = list_currentNode->next;
}
}
void add(int wrt){
Node* node = new Node(NULL, wrt);
Node* el = this->root;
while(el->next != NULL){
//el->show();
el = el->next;
}
el->next = node;
}
void remove(int index){
Node* el = this->root;
if(index == 0){
this->root = el->next;
// delete el;
}
else{
int i = 0;
while(el != NULL && i < index - 1){
el = el->next;
i++;
}
if(el!=NULL){
Node* toRem = el->next;
Node* newNext = toRem->next;
el->next = newNext;
// delete toRem;
}
}
}
void show(){
Node* el = this->root;
while(el != NULL){
el->show();
el = el->next;
}
}
~List(){
Node* currentNode = this->root;
while (currentNode != NULL)
{
Node* nextNode = currentNode->next;
delete currentNode;
currentNode = nextNode;
}
}
private:
Node* root;
};
int main(){
List* l = new List(10);
l->add(12); l->add(13);
l->show();
cout<<endl;
List* lala = new List(*l);
lala->show();
cout<<endl;
lala->add(4);
cout<<"lala before remove"<<endl;
lala->show();
lala->remove(0);
cout<<"l before delete"<<endl;
l->show();
cout<<"lala before delete"<<endl;
lala->show();
delete l;
cout<<"l after"<<endl;
l->show();
cout<<"lala after delete"<<endl;
lala->show();
return 0;
}
代碼太長。 http://sscce.org/ – Fanael
要求陌生人通過檢查發現代碼中的錯誤並不是富有成效的。您應該使用調試器或打印語句來識別(或至少隔離)問題,然後回來一個更具體的問題(一旦您將其縮小到10行[測試案例](http:///sscce.org))。 –
另外,我想換個問題。我懷疑C++中的刪除操作是否被破壞,你可能做錯了。如果它被破壞了,我們在它的世界裏遇到了一個棘手的問題。 – Skalli