C++ 将节点添加到双链接列表时出现问题

C++ 将节点添加到双链接列表时出现问题,c++,doubly-linked-list,C++,Doubly Linked List,嗨,我正在尝试制作一个双链接列表,将单个数字存储为双链接列表的节点,然后将它们添加到一起并打印出来作为家庭作业。我在让它工作时遇到了很多麻烦,并将问题追溯到我的add节点函数,因为它们不能正确更新指针。例如,在AddToFront()函数中,我无法理解如何使prev指针工作并指向它后面的节点 我不能使用STL,必须自己实现LL,以防有人怀疑。谢谢 #include <stdio.h> #include <string.h> #include <stdlib.h>

嗨,我正在尝试制作一个双链接列表,将单个数字存储为双链接列表的节点,然后将它们添加到一起并打印出来作为家庭作业。我在让它工作时遇到了很多麻烦,并将问题追溯到我的add节点函数,因为它们不能正确更新指针。例如,在AddToFront()函数中,我无法理解如何使prev指针工作并指向它后面的节点

我不能使用STL,必须自己实现LL,以防有人怀疑。谢谢

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>

using namespace std;

/////// PART A
template <class T>
class List {
private:
    struct Node {
        T data;
        Node *next;
        Node *prev;
    };
    Node *front, *current, *rear;

public:
    List();
    ~List();
    void AddtoFront (T newthing);
    void AddtoRear (T newthing);
    bool FirstItem (T & item);
    bool LastItem (T & item);
    bool NextItem (T & item);
    bool PrevItem (T & item);
};

template <class T>
List<T>::List() {
  front = NULL;  current = NULL; rear = NULL;
}
template <class T>
List<T>::~List() {

}

template <class T>
void List<T>::AddtoFront (T newthing) {
    if (front == NULL) {
        Node *temp;
        temp = new Node;
        temp->data = newthing;
        temp->next = front;
        temp->prev = NULL;
        front = temp;
    } else {
        Node *temp;
        temp = new Node;
            front->prev = temp;
        temp->data = newthing;
        temp->next = front;
        temp->prev = NULL;
        front = temp; 
    }
}

template <class T>
void List<T>::AddtoRear (T newthing) {
    if (rear == NULL) {
        Node *temp;
        temp = new Node;
        temp->data = newthing;
        temp->prev = rear;
        temp->next = NULL;
        rear = temp;
    } else {
        Node *temp;
        temp = new Node;
            rear->next = temp;
        temp->data = newthing;
        temp->prev = rear;
        temp->next = NULL;
        rear = temp;
    }
}

template <class T>
bool List<T>::FirstItem (T & item) {
    if (front == NULL) { return false; }
    current = front;
    item = front->data;
    return true;
}

template <class T>
bool List<T>::LastItem (T & item) {
    if (rear == NULL) { return false; }
    current = rear;
    item = rear->data;
    return true;
}

template <class T>
bool List<T>::NextItem (T & item) {
    if (current != NULL) current = current->next;
    if (current == NULL) { return false; }
    item = current->data;
    return true;
}

template <class T>
bool List<T>::PrevItem (T & item) {
    if (current == NULL) { return false; }
    if (current->prev != NULL) current = current->prev;
    item = current->data;
    return true;
}

/////// PART B
class BigNumber {
private:
//complete here...
//include here a List of integers, or shorts etc
    List<int>L;

public:
//complete here...
//what methods do you need?
//e.g., ReadFromString, PrintBigNumber, AddBigNumbers
    BigNumber();
    ~BigNumber();
    void ReadFromString(char * decstring);
    void PrintBigNumber();
    void AddBigNumbers(BigNumber B1, BigNumber B2);
};

BigNumber::BigNumber(){
// anything here?
}

BigNumber::~BigNumber(){
//you can keep that empty
}

void BigNumber::ReadFromString (char * decstring ) {
    //read a string, adding a new node per digit of the decimal string
    // To translate 'digits' to integers: myinteger=decstring[index]-48
    //You need to use the AddtoFront()
    int temp;
    for (unsigned i=0; i < strlen(decstring); ++i) {
        //cin >> decstring[i];
        temp = decstring[i]-48;
        //L.AddtoFront(temp);
        L.AddtoRear(temp);
        //cout <<"Number added!" <<endl;
    }
}

void BigNumber::PrintBigNumber () {
//complete here, print the list (i.e., use FirstItem() and NextItem() )
    int val;
    if (L.FirstItem(val)) {
        cout << val;
    } else {
        cout << "print failed";
    }
    //if (L.FirstItem(val)) { cout << "true-first";} else { cout <<"false-first";};
    //if (L.LastItem(val)) { cout << "true";} else { cout <<"false";};
    //L.FirstItem(val);
    //cout << val;
    /*while (L.PrevItem(val)){

            cout << val;
            //cout <<"Print error!Value not here.";
    }*/
}

void BigNumber::AddBigNumbers(BigNumber B1,BigNumber B2){
//complete here.
//use FirstItem(), NextItem() and AddNode()
//to add two big numbers, what do you have to do? Be careful about the carry
//Remember to add the last carry, the resulting number can have one more digit than B1 or B2
}

/////// PART C

BigNumber B1, B2, RES;

int main (int argc, char ** argv) {
  //use command line arguments
  if(argc!=3){printf("usage: executable number1 number2\n");exit(0);}
  B1.ReadFromString(argv[1]);
  B2.ReadFromString(argv[2]);
  //print
  cout << endl<< "Add the following numbers " << endl;
  B1.PrintBigNumber();
  cout << " + ";
  B2.PrintBigNumber();
  cout << " = " << endl;
  //compute the addition
  RES.AddBigNumbers(B1,B2);
  //print the result
  RES.PrintBigNumber();
  cout << endl;
  return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
///////A部分
模板
班级名单{
私人:
结构节点{
T数据;
节点*下一步;
节点*prev;
};
节点*前,*当前,*后;
公众:
List();
~List();
无效添加到前端(T newthing);
无效的额外费用(T newthing);
bool第一项(T和项目);
bool LastItem(T&item);
bool NextItem(T和项目);
布尔项目(T和项目);
};
模板
列表::列表(){
前=零;当前=零;后=零;
}
模板
列表::~List(){
}
模板
无效列表::AddtoFront(T newthing){
if(front==NULL){
节点*温度;
temp=新节点;
温度->数据=newthing;
温度->下一步=前;
temp->prev=NULL;
前=温度;
}否则{
节点*温度;
temp=新节点;
前->前=温度;
温度->数据=newthing;
温度->下一步=前;
temp->prev=NULL;
前=温度;
}
}
模板
作废列表::AddtoRear(T newthing){
如果(后==NULL){
节点*温度;
temp=新节点;
温度->数据=newthing;
温度->前=后;
temp->next=NULL;
后部=温度;
}否则{
节点*温度;
temp=新节点;
后->下一步=温度;
温度->数据=newthing;
温度->前=后;
temp->next=NULL;
后部=温度;
}
}
模板
布尔列表::第一项(T和项){
如果(front==NULL){return false;}
电流=前沿;
项目=前端->数据;
返回true;
}
模板
bool List::LastItem(T&item){
如果(后==NULL){return false;}
电流=后部;
项目=后方->数据;
返回true;
}
模板
bool列表::NextItem(T&item){
如果(当前!=NULL)当前=当前->下一步;
如果(current==NULL){return false;}
项目=当前->数据;
返回true;
}
模板
布尔列表::PreviItem(T和item){
如果(current==NULL){return false;}
如果(当前->上一个!=NULL)当前=当前->上一个;
项目=当前->数据;
返回true;
}
///////乙部
类大数{
私人:
//在这里完成。。。
//在这里包括一个整数列表,或短字符列表等
李斯特;
公众:
//在这里完成。。。
//你需要什么方法?
//e、 例如,ReadFromString、PrintBigNumber、AddBigNumber
BigNumber();
~BigNumber();
void ReadFromString(char*decstring);
void PrintBigNumber();
void addBigNumber(BigNumber B1、BigNumber B2);
};
BigNumber::BigNumber(){
//这里有什么吗?
}
BigNumber::~BigNumber(){
//你可以让它空着
}
void BigNumber::ReadFromString(char*decstring){
//读取字符串,为十进制字符串的每一位添加一个新节点
//将“数字”转换为整数:myinteger=decstring[index]-48
//您需要使用AddtoFront()命令
内部温度;
for(无符号i=0;i>decstring[i];
temp=decstring[i]-48;
//L.AddtoFront(温度);
L.AddtoRear(温度);

//cout
AddtoFront
还需要更新
front
prev
指针

temp -> front <-> rest_of_list
temp->列表的前座
它应该在哪里

temp <-> front <-> rest_of_list.
temp前支架(在列表中)。

另外,您可能会注意到,
AddtoFront
中的
if
语句的两个分支是相同的…)

AddtoFront
还需要更新
front
prev
指针。也就是说,您当前正在执行的操作

temp -> front <-> rest_of_list
temp->列表的前座
它应该在哪里

temp <-> front <-> rest_of_list.
temp前支架(在列表中)。

也可以注意到,<<代码>的两个分支> <代码> > AddoTrrt>/Cuth>中的语句是相同的……:(<)/P>你应该使用<代码> const t和item < /c>参数到你的添加函数中。你可以考虑在你的类中有一个实际的<代码>节点< /代码>,用来实现循环列表。如果那是<代码>ODE头,空列表有<代码> -> Next=Health-Prv= &首;,并且只有当前指针分开。这可以减少处理特殊情况的数量。您应该使用<代码> const t和item < /C> >参数到您的添加函数中。您可以考虑在您的类中有一个实际的<代码>节点< /代码>。它用于实现循环列表。如果是

节点头
,则空列表有
头->下一个=头->上一个=&head;
,并且您只需要将当前指针分开。这可以减少您必须处理的特殊情况的数量。