将节点添加到双向链表时出现问题
Trouble adding nodes to a doubly Linked List
嗨,我正在尝试制作一个双向链表,将单个数字存储为双向链表的节点,然后将它们加在一起并打印出来用于家庭作业。我在让它工作时遇到了很多麻烦,并且已经将我的问题追溯到我的添加节点函数,因为它们没有正确更新指针。例如,在 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 number2n");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;
}
编辑:我在AddToFront()和AddToRear()中分别添加了一行。这是否走在正确的轨道上?
AddtoFront
还需要更新front
的prev
指针。也就是说,您当前正在做
temp -> front <-> rest_of_list
需要的地方
temp <-> front <-> rest_of_list.
此外,您可能会注意到 if
语句的两个分支在AddtoFront
是相同的...... :)
相关文章:
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- 如何实现容纳整数和无效指针的双向链表?
- 为什么我的双向链表删除函数会删除多个节点?
- 我对数据结构、双向链表有一些问题
- C++ 双向链表:使用矩阵类成员创建和填充列表
- 在 c++ 中具有向量的双向链表构造函数
- 如何在双向链表上实现复制赋值?
- 在C++中删除双向链表的头节点后出现访问冲突异常
- 在双向链表中实现 Popback
- 在C++中反转双向链表
- 双向链表 - 无法删除第一个节点
- 我当前实现的双向链表类是否需要重构迭代器 end() 功能?
- 双向链表 std::unique_ptr 类在节点删除时无法按预期工作
- 双向链表问题 - 包含项目但不显示
- 双向链表问题(特别是复制构造函数和删除函数)
- 将项目添加到双向链表的背面时遇到问题
- 添加到双向链表的背面时遇到问题
- 双向链表节点交换问题
- 双向链表插入问题
- 将节点添加到双向链表时出现问题