-2
編輯:
好的,我已經修復並完成了我所有的原始問題/問題。但是,現在我在List.cpp的第54行gotoLast上遇到了分段錯誤,我似乎無法弄清楚它爲什麼會出現分段錯誤。圓形,雙鏈表 - 分割錯誤
List.h:
#ifndef _list_h
#define _list_h
struct ListNode
{
int data;
int rank;
ListNode *prev, *next;
// ListNode (int x = 0, ListNode *n, ListNode *p);
};
class List {
private:
ListNode *current;
ListNode *head;
ListNode *temp;
public:
List();
int size();
int currentValue();
int currentRank();
void gotoFirst();
void gotoLast();
void retreat();
void advance();
void gotoRank(int r);
bool isNull();
bool isFirst();
bool isLast();
void insertFirst(int x);
void insertLast(int x);
void insertBefore(int x);
void insertAfter(int x);
void replace(int x);
int removeFirst();
int removeLast();
int remove();
int removeAndRetreat();
int removeAndAdvance();
void display();
};
#endif
和我list.cpp:
#include <iostream>
using namespace std;
#include "List.h"
int ncounter = 0;
int rank = 0;
/*ListNode (int x = 0, ListNode *n, ListNode *p)
{
data = x;
next = n;
prev = p;
}
*/
List::List()
{
//ListNode *head;
//ListNode *temp;
//ListNode *current;
head = NULL;
current = NULL;
temp = NULL;
return;
}
int List::size()
{
return ncounter;
}
int List::currentValue()
{
int v;
if(current == NULL)
{
return 0;
}
else
{
v = current -> data;
return v;
}
}
int List::currentRank()
{
return rank;
}
void List::gotoFirst()
{
current = head;
rank = 0;
}
void List::gotoLast()
{
current = head -> prev;
rank = ncounter-1;
}
void List::retreat()
{
current = current -> prev;
}
void List::advance()
{
current = current -> next;
}
void List::gotoRank(int r)
{
current = head;
for (int i=0; i != r; i++)
{
current = current -> next;
}
rank = r;
}
bool List::isNull()
{
if (current == NULL)
return true;
else
return false;
}
bool List::isFirst()
{
if (current == head)
{
return true;
}
else
{
return false;
}
}
bool List::isLast()
{
if (current == head -> prev)
{
return true;
}
else
{
return false;
}
}
void List::insertFirst(int x)
{
if (head == NULL)
{
head = new ListNode;
head -> data = x;
head -> next = head;
head -> prev = head;
ncounter++;
rank = 0;
}
else
{
ListNode *temp = new ListNode;
temp -> data = x;
temp -> next = head;
temp -> prev = head -> prev;
head -> prev -> next= temp;
head -> prev = temp;
head = temp;
rank++;
ncounter++;
}
}
void List::insertLast(int x)
{
if (head == NULL)
{
head = new ListNode;
head -> data = x;
head -> next = head;
head -> prev = head;
}
else
{
ListNode *temp = new ListNode;
temp -> data = x;
temp -> next = head;
temp -> prev = head -> prev;
head -> prev -> next = temp;
head -> prev = temp;
ncounter++;
}
}
void List::insertBefore(int x)
{
ListNode *temp = new ListNode;
temp -> data = x;
temp -> next = current;
temp -> prev = current -> prev;
current -> prev -> next = temp;
current -> prev = temp;
rank++;
ncounter++;
}
void List::insertAfter(int x)
{
ListNode *temp = new ListNode;
temp -> data = x;
temp -> next = current -> next;
current -> next -> prev = temp;
current ->next = temp;
temp -> prev = current;
ncounter++;
}
void List::replace(int x)
{
current -> data = x;
}
int List::removeFirst()
{
int tempx;
if(current == head)
{
current = NULL;
}
if(head->next == head)
{ tempx = head ->data;
head = NULL;
return tempx;
}
else
{
ListNode *temp = head;
tempx = temp -> data;
head = head ->next;
temp -> prev -> next = head;
head -> prev = temp -> prev;
delete temp;
ncounter--;
rank--;
return tempx;
}
}
int List::removeLast()
{
int tempx;
if (current == head -> prev)
{
current = NULL;
}
if(head->prev == head)
{ tempx = head ->data;
head = NULL;
return tempx;
}
else
{
ListNode *temp = head -> prev;
tempx = temp -> data;
temp -> prev -> next = head;
head -> prev = temp -> prev;
delete temp;
ncounter--;
return tempx;
}
}
int List::remove()
{
int tempx;
if(head->next == head)
{
tempx = head -> data;
head = NULL;
return tempx;
}
else
{
ListNode *temp = current;
tempx = current -> data;
temp -> prev -> next = temp -> next;
temp -> next -> prev = temp -> prev;
current = NULL;
delete temp;
ncounter--;
rank=0;
return tempx;
}
}
int List::removeAndRetreat()
{
ListNode *temp;
int tempx;
temp = current;
current -> prev -> next = current -> next;
current -> next -> prev = current -> prev;
tempx = current -> data;
current = current -> prev;
delete temp;
ncounter--;
rank--;
return tempx;
}
int List::removeAndAdvance()
{
ListNode *temp;
int tempx;
temp = current;
current -> prev -> next = current -> next;
current -> next -> prev = current -> prev;
tempx = current -> data;
current = current -> next;
delete temp;
ncounter--;
return tempx;
}
void List::display()
{
if(head == NULL)
cout <<"[ ]" << endl;
else
{
ListNode *temp = head;
cout << "[ ";
for (int i = 0; i < ncounter; i++)
{
if (temp == current)
{
cout << "(" << current -> data << ") ";
}
else
{
cout << temp -> data << " ";
temp = temp -> next;
}
}
cout << " ]";
}
'Circular'鏈表應該表示你的頭部和尾部節點*是同一個節點* –