循环,双向链表 - 分段故障

时间:2016-07-22 22:00:44

标签: c++ list doubly-linked-list

编辑:
好的,我已修复并完成了所有原始问题/问题。然而,现在,我在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 << " ]";

     }

1 个答案:

答案 0 :(得分:1)

insertLast代码在CDDL中实际上要简单得多。您只需在第一个节点之前插入,因为那也是最后一个节点。