Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
How come when I do Queue<listEntry<int> > queue2(intList); the copy constructor is being called twice?
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template <class dataType>
class listEntry
{
private:
dataType data;
public:
listEntry *next;
listEntry *prev;
dataType getData() { return this->data; }
listEntry();
listEntry(dataType data) { this->data = data; }
~listEntry() {};
};
template <class dataType>
class List
{
private:
dataType *head;
dataType *tail;
int count;
public:
dataType *getHead() { return this->head; }
dataType *getTail() { return this->tail; }
void addToTail(dataType *newEntry);
void addToHead(dataType *newEntry);
int getCount() { return count; }
void printListForward();
List();
List(const List<dataType> &li);
~List();
};
template <class dataType>
class Queue: public List<dataType>
{
public:
void enQueue(dataType *newEntry);
Queue():List<dataType>() { return; }
Queue(List<dataType> li):List<dataType>(li) { return; }
};
void addIntegersToList(Queue<listEntry<int> > *queue);
int main()
{
cout << "Queue of Integers: " << endl;
cout << "Default Queue Constructor: " << endl;
//Create an intance of a queue using the default queue constructor
Queue<listEntry<int> > queue;
addIntegersToList(&queue);
queue.printListForward();
cout << endl;
cout << "List parameter Queue Constructor: " << endl;
//Create a new list that will be used to initiate an instance of a queue
List<listEntry<int> > intList;
//Add some data to that list
intList.addToTail(new listEntry<int>(11));
//Now create a new instance of a queue with a list as its creation parameter
Queue<listEntry<int> > queue2(intList);
//addIntegersToList(&queue2);
//queue2.printListForward();
while (true);
}
template <class dataType>
List<dataType>::List()
{
this->head = NULL;
this->tail = NULL;
this->count = 0;
}
template <class dataType>
void List<dataType>::addToTail(dataType *newEntry)
{
newEntry->next = NULL;
newEntry->prev = NULL;
if (this->head == NULL)
{
this->head = newEntry;
this->tail = this->head;
this->count = this->count + 1;
}
else
{
this->tail->next = newEntry;
newEntry->prev = this->tail;
this->tail = newEntry;
this->count = this->count + 1;
}
dataType *temp = this->tail;
}
template <class dataType>
void List<dataType>::addToHead(dataType *newEntry)
{
newEntry->next = NULL;
newEntry->prev = NULL;
if (this->head == NULL)
{
this->head = newEntry;
this->tail = this->head;
}
else
{
this->head->prev = newEntry;
newEntry->next = this->head;
this->head = newEntry;
}
this->count = this->count + 1;
}
template <class dataType>
void List<dataType>::printListForward()
{
cout << "Head: ";
dataType *temp = this->head;
while (temp)
{
cout << temp->getData() << " -> ";
temp = temp->next;
}
cout << " :Tail" << endl;
}
void addIntegersToList(Queue<listEntry<int> > *queue)
{
int i = 0;
for (i = 1; i <= 10; i++)
queue->enQueue(new listEntry<int>(i));
}
template <class dataType>
void Queue<dataType>::enQueue(dataType *newEntry)
{
this->addToTail(newEntry);
}
template <class dataType>
List<dataType>::~List()
{
while (this->head)
{
dataType *next = this->head->next;
delete this->head;
this->count = this->count - 1;
this->head = next;
}
}
template <class dataType>
List<dataType>::List(const List<dataType> &li)
{
tail = new dataType;
*tail = *li.tail;
head = new dataType;
*head = *li.head;
cout << "head: " << head << endl;
cout << "tail: " << tail << endl;
count = li.count;
cout << "count: " << count << endl;
}
template <class dataType>
listEntry<dataType>::listEntry()
{
this->next = NULL;
this->prev = NULL;
this->data = 0;
}
Because you are passing the List by value to the Queue constructor. Try this instead
Queue(const List<dataType>& li):List<dataType>(li) { return; }
Related
So, I am trying to implement a stack using a linked list as the parent, and have written these two classes:
template <class T>
class LL {
public:
struct Node {
Node* next = nullptr;
T data = 0;
};
Node* head;
public:
LL();
~LL();
void insert(T item);
void append(T item);
virtual void disp();
};
template<class T>
LL<T>::LL(){ head = nullptr; }
template<class T>
void LL<T>::insert(T item)
{
Node* newNode = new Node;
newNode->data = item;
if (head == nullptr) head = newNode;
else
{
Node* loop = head;
Node* prev = nullptr;
while (loop != nullptr && loop->data < item) {
prev = loop;
loop = loop->next;
}
if (prev == nullptr) { //head
newNode->next = head;
head = newNode;
}
else { //insert
newNode->next = prev->next;
prev->next = newNode;
}
}
}
template<class T>
void LL<T>::append(T item)
{
Node* newNode = new Node;
newNode->data = item;
if (head == nullptr) head = newNode;
else
{
Node* loop = head;
while(loop->next != nullptr)
loop = loop->next;
loop->next = newNode;
}
}
template<class T>
void LL<T>::disp()
{
cout << "[";
if (head) {
if (head->next == nullptr)
cout << head->data;
else {
Node* loop = head;
while (loop->next != nullptr)
{
cout << loop->data << ",";
loop = loop->next;
}
cout << loop->data;
}
}
cout << "]" << endl;
}
template<class T>
LL<T>::~LL()
{
if (head != nullptr) {
while (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
temp = nullptr;
}
}
}
template<class T>
class stack: public LL<T> {
private:
public:
stack();
~stack();
virtual void push(T item);
void pop(T ret);
bool empty();
void disp();
};
template<class T>
stack<T>::stack():LL<T>()
{
}
template<class T>
stack<T>::~stack()
{
}
template<class T>
void stack<T>::push(T item){ this->append(item); }
template<class T>
void stack<T>::pop(T ret)
{
ret = this->head->data;
this->head = this->head->next;
}
template<class T>
bool stack<T>::empty() { return false;}
template<class T>
void stack<T>::disp()
{
Node<T>* loop = this->head;
cout << "-" << endl;
while (loop) {
cout << loop->data << endl;
loop = loop->next;
}
cout << "-";
}
The trouble comes in the very last function, "disp()", where it does not give me access to the Node struct. It gives me the error "'Node': identifier not found". This prevents me from declaring the needed "loop" variable. As far as I know, the Node struct should be inherited from the LL class and protected in the stack class. Thus, I should be able to access it directly.
Any ideas?
Ok, so I got around it using this post:
Declaring a struct in a template class, undefined for member functions
My disp() function now looks like this:
template<class T>
void stack<T>::disp()
{
typename LL<T>::Node* loop = this->head;
cout << "-" << endl;
while (loop) {
cout << loop->data << endl;
loop = loop->next;
}
cout << "-";
}
my header and implimentation file
#ifndef VIKTOR_H_
#define VIKTOR_H_
#include <iostream>
template <class DataType>
class viktor {
private:
template <class NodeType>
struct Node {
NodeType data;
Node<NodeType> * next;
};
Node<DataType> * backPtr;
int length;
public:
viktor();
~viktor();
DataType &operator [] (int) const;
void push(const DataType &);
friend std::ostream &operator << (std::ostream &strm, const
viktor<DataType> &A){
for(int i = 0; i < A.length; ++i) {
strm << A[i] << " " << std::flush;
}
strm << std::endl;
return strm;
}
};
template <class DataType>
viktor<DataType>::viktor() {
backPtr = nullptr;
length = 0;
}
template <class DataType>
viktor<DataType>::~viktor() {
if ( length == 0 ) return;
Node<DataType>* previousPtr = backPtr;
Node<DataType>* nextPtr = backPtr->next;
while( nextPtr != backPtr )
{
nextPtr = nextPtr->next;
previousPtr->next = nullptr;
delete previousPtr;
length--;
}
backPtr->next = nullptr;
delete backPtr;
}
template <class DataType>
void viktor<DataType>::push(const DataType &item) {
Node<DataType>* newNode = new Node<DataType>;
newNode->data = item;
std::cout << "data: " << newNode->data << std::endl;
if(length != 0) {
newNode->next = backPtr->next;
backPtr->next = newNode;
}
else {
newNode->next = newNode;
}
backPtr = newNode;
length += 1;
//std::cout << "Finished pushing..." << std::endl;
}
template <class DataType>
DataType &viktor<DataType>::operator [] (int i) const {
Node<DataType>* conductor = backPtr;
if (i > length) {
throw "Item is inaccessible";
}
for (int j = 0; j <= i; ++j) {
conductor = conductor->next;
}
return conductor->data;
}
#endif //VIKTOR_H_
i am having problems with this function
template <class DataType>
void viktor<DataType>::push(const DataType &item) {
Node<DataType>* newNode = new Node<DataType>;
newNode->data = item;
std::cout << "data: " << newNode->data << std::endl;
if(length != 0) {
newNode->next = backPtr->next;
backPtr->next = newNode;
}
else {
newNode->next = newNode;
}
backPtr = newNode;
length += 1;
//std::cout << "Finished pushing..." << std::endl;
}
i am using this test driver
int main() {
viktor<int> blah;
blah.push(2);
std::cout << blah << std::endl;
}
now it works if i only push once but if i do
blah.push(1);
blah.push(2);
it runs and prints all of the data that i want but at the end of the program it dumps my core (i can provide the specific error if you want but it's just a memory map and then aborted (core dumped))
i fixed it! thanks Some programmer dude! so the problem was my deconstructor, what i did was changed previousPtr->next = nullptr; to previousPtr = previousPtr->next;
I'm new with data structures in C++, and i'm trying to do a doubly linked list with templates. All the examples that i have seen are only for 1 element of the template node, so i'm trying to put 2 elements in the template node in the list, but i don't know how to do it, anyway, i tried to make the list.
Here's the code:
#include<iostream>
#include<cstring>
using namespace std;
template<class T>
// node class
class node
{
public:
node();
node(T);
~node();
node *next;
T data[2];
void borra_todo();
void print();
};
// by defect
template<typename T>
node<T>::node()
{
data[0] = NULL;
data[1] = NULL;
next = NULL;
}
// by parameter
template<typename T>
node<T>::node(T data_)
{
data[0] = data_[0];
data[1] = data_[1];
next = NULL;
}
// delete nodes
template<typename T>
void node<T>::borra_todo()
{
if (next)
next->borra_todo();
delete this;
}
// node printing
template<typename T>
void node<T>::print()
{
cout << data[0] << " " << data[1] << "->";
}
template<typename T>
node<T>::~node() {}
// list
template <class T>
class list
{
private:
node<T> *m_head;
int m_num_nodes;
public:
list();
~list();
void add_head(T);
void add_end(T);
void add_sort(T);
void fill(char r[30], char n[30]);
void search(T);
void del_by_data(T);
void print();
};
template<typename T>
list<T>::list()
{
m_num_nodes = 0;
m_head = NULL;
}
//add in the beginning
template<typename T>
void list<T>::add_head(T data_)
{
node<T> *new_node = new node<T>(data_);
node<T> *temp = m_head;
if (!m_head)
{
m_head = new_node;
}
else
{
new_node->next = m_head;
m_head = new_node;
while (temp)
{
temp = temp->next;
}
}
m_num_nodes++;
}
// add to the last
template<typename T>
void list<T>::add_end(T data_)
{
node<T> *new_node = new node<T> (data_);
node<T> *temp = m_head;
if (!m_head)
{
m_head = new_node;
}
else
{
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = new_node;
}
m_num_nodes++;
}
// it is supposed that sorts items in the list ...
template<typename T>
void list<T>::add_sort(T data_)
{
node<T> *new_node = new node<T> (data_);
node<T> *temp = m_head;
if (!m_head)
{
m_head = new_node;
}
else
{
for (int i =0; i <= 1; i++)
{
if (m_head->data[0] > data_[i])
{
new_node->next = m_head;
m_head = new_node;
}
else
{
while ((temp->next != NULL) && (temp->next->data[0] < data_[i]))
{
temp = temp->next;
}
new_node->next = temp->next;
temp->next = new_node;
}
}
m_num_nodes++;
}
}
// sort adding ...
template<typename T>
void list<T>::fill(char rfc[30])
{
char temprfc[30];
char tempnombre[30];
temprfc = "DUDE010101R0";
tempnombre = "Dude";
add_sort(temprfc, tempnombre);
temprfc = "AUDE010101R1";
tempnombre = "Commander";
add_sort(temprfc, tempnombre);
}
// print list
template<typename T>
void list<T>::print()
{
node<T> *temp = m_head;
if (!m_head)
{
cout << "List is empty" << endl;
}
else
{
while (temp)
{
temp->print();
if (!temp->next)
cout << "NULL\n";
temp = temp->next;
}
}
cout << endl;
}
// search the list
template<typename T>
void list<T>::search(T data_)
{
node<T> *temp=m_head;
int cont=1;
int cont2=0;
while(temp)
{
if(strcmp(temp->data,data_[0]))
{
cout<<"Element found " << temp->data;
cout << " in position: " << cont << endl;
cont2++;
}
temp=temp->next;
cont++;
}
if(cont2==0)
{
cout << "Element not found"<<endl;
}
}
// ... delete by data
template<typename T>
void list<T>::del_by_data(T data_)
{
node<T> *temp = m_head;
node<T> *temp1 = m_head->next;
int cont =0;
if (m_head->data == data_)
{
m_head = temp->next;
}
else
{
while (temp1)
{
if (temp1->data == data_)
{
node<T> *aux_node = temp1;
temp->next = temp1->next;
delete aux_node;
cont++;
m_num_nodes--;
}
temp = temp->next;
temp1 = temp1->next;
}
}
if (cont == 0)
{
cout << "No data" << endl;
}
}
// destroy the constructor
template<typename T>
list<T>::~list() {}
int main()
{
list<char> list1;
char element1[30];
char element2[30];
int dim, choice, pos;
do{
cout << "Select a choice.\n";
cout << "1. Print list\n";
cout << "2. Delete an element of the list\n";
cout << "3. Search an element of the list\n";
cout << "4. Exit\n";
cin >> choice;
switch(choice)
{
case 1:
{
cout << "Printing list:\n";
list1.fill("1","2");
list1.print();
break;
}
case 2:
{
cout << "Element to delete: ";
cin >> element1;
list1.search(element1);
element1 = "";
break;
}
case 3:
{
cout << "Element to search: ";
cin >> element1;
list1.search(element1);
element1 = "";
break;
}
}
}while(choice != 4);
return 0;
}
The code doesn't compile, it marks errors like:
" error: prototype for ‘void list::fill(char*)’ does not match any in class ‘list’
void list::fill(char rfc[30])
^
t3b.cpp:79:8: error: candidate is: void list::fill(char*, char*)
void fill(char r[30], char n[30]);"
Any ideas on how to fix it? Or any ideas on how to put 2 elements in a node using templates?
Thanks in advance.
Dude, you should really try to isolate the error a little bit before posting it. This is 500 lines of code, I had to copy and paste it all into an editor before I could even look at it.
When you declared fill, it had two arguments, when you defined it, it had one. Also, I would avoid arrays of characters for numerous reasons, instead use std::string.
my data structure teacher gave us the task to implement lists and stacks manually and it worked just fine, but then we had to reimplement it to accept any type of parameters using template. Since then, I've been facing lots of different errors. Right now, I'm having a c2019 error, but I can't see what's wrong with my code.
Tried this:pilha.getTopo<int>();, this: Lista<int> lista;, this: typedef Lista<int> Lista_int;, but no luck.
Now, my code:
######UPDATE: The code bellow is working
Listas.h
#pragma once
#include "stdafx.h"
#include <iostream>
using namespace std;
/*============================Node================================*/
template <typename N>
class Node
{
N value;
Node *next, *prev;
public:
Node(void) {};
Node(N _value){
this->value = _value;
this->prev = NULL;
this->next = NULL;
};
~Node(void) {};
void setValue(int _value) { this->value = _value; }
void setNext(Node *_next) { this->next = _next; }
void setPrev(Node *_prev) { this->previous = _prev; }
int getValue() { return this->value; }
Node *getNext() { return this->next; }
Node *getPrev() { return this->prev; }
};
/*===========================List===============================*/
template <typename T>
class List
{
Node<T> *begin, *end;
int list_size;
public:
List(void){
this->begin = NULL;
this->end = NULL;
this->list_size = 0;
};
~List(void) {};
template <typename T> void insertBegin(Node<T> node){
Node<T> *newNode = new Node<T>;
*newNode = node;
if(this->list_size == 0){
this->begin = this->end = newNode;
this->list_size += 1;
}else{
newNode->setNext(begin);
this->begin = newNode;
this->list_size += 1;
}
};
template <typename T> void removeBegin(Node<T> node){
Node<T> *newNode = new Node<T>;
*newNode = node;
if(this->list_size == 0){
this->begin = this->end = NULL;
this->list_size = 0;
}else{
if(begin == end)
this->end = NULL;
this->begin = newNode->getNext();
newNode = begin;
newNode->setPrev(NULL);
this->list_size -= 1;
}
};
template <typename T> void insertEnd(Node<T> node){
Node<T> *newNode = new Node<T>;
*newNode = node;
if(this->list_size == 0){
this->begin = this->end = newNode;
this->list_size += 1;
}else{
newNode->setPrev(end);
this->end = newNode;
this->list_size += 1;
}
};
template <typename T> void removeEnd(Node<T> node){
Node<T> *newNode = new Node<T>;
*newNode = node;
if(this->list_size == 0){
this->begin = this->end = NULL;
this->list_size = 0;
}else{
if(begin == end)
this->end = NULL;
this->end = newNode->getPrev();
newNode = end;
newNode->setNext(NULL);
this->list_size -= 1;
}
};
template <typename T> void exibeList(){
Node<T> *node;
cout << "Begin: " << begin << endl
<< "End: " << end << endl
<< "Size: " << list_size << endl << endl;
if(begin != NULL){
node = begin;
do{
cout << "Mem. adress: " << &node << endl
<< "Value: " << node->getValue() << endl
<< "Previous: " << node->getPrev() << endl
<< "Next: " << node->getNext() << endl
<< endl;
node = node->getNext();
}while(node != NULL);
}
};
};
/*===========================Stack==============================*/
template <typename T>
class MyStack
//: public List<T>
{
Node<T> *top, *next;
int my_stack_size;
public:
MyStack<T>(void){
this->my_stack_size = 0;
this->top = NULL;
this->next = NULL;
};
~MyStack<T>(void) {};
template <typename T> void insertTop(Node<T> node){
Node<T> *newNode = new Node<T>;
*newNode = node;
if(this->my_stack_size == 0){
this->top = this->next = newNode;
this->my_stack_size += 1;
}else{
newNode->setNext(top);
this->next = top;
this->top = newNode;
this->my_stack_size += 1;
}
};
template <typename T> void removeTop(){
Node<T> *node;
if(this->my_stack_size > 0){
node = top;
this->top = next;
delete node;
this->my_stack_size -= 1;
}else
cout << "Stack underflow." << endl;
};
template <typename T> void getTop(){
Node<T> *node = new Node<T>;
node = top;
if(node->getPrev() == NULL)
cout << node->getValue() << endl;
else
cout << "Error. Node isn't the first." << endl;
};
template <typename T> void show(){
Node<T> *node;
cout << "Top: " << top << endl
<< "Next: " << next << endl
<< "Size: " << my_stack_size << endl << endl;
if(top != NULL){
node = top;
do{
cout << "Mem. adress: " << &node << endl
<< "Value: " << node->getValue() << endl
<< "Previous: " << node->getPrev() << endl
<< "Next: " << node->getNext() << endl
<< endl;
node = node->getNext();
}while(node != NULL);
}
};
};
and finally, Lista.cpp
// Lista.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Listas.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
/* ERROR C2019
//typedef Pilha<int> Pilha_int;
//typedef Nodo<int> Nodo_int;
//
//Lista_int lista;
//Pilha_int pilha;
//Nodo_int nodo(25), nodo2(40), nodo3(55), nodo4(70);
*/
/* C2019
Pilha<int> pilha;
Nodo<int> nodo(25), nodo2(40);
/*error C2955: use of class template requires template argument list
//Pilha pilha;
//Nodo nodo(25), nodo2(40), nodo3(55), nodo4(70);
pilha.insereTopo(nodo);
pilha.getTopo(); //C2783
pilha.insereTopo(nodo2);
pilha.getTopo(); //C2783
pilha.exibe(); //error C2783: could not deduce template argument for 'T'
pilha.exibe<int>(); //C2019
system("pause");
return 0;
}
Thanks in advance.
Hi I am trying to make a doubly linked list to store individual numbers as nodes of a doubly linked list and then add them together and print them out for a homework assignment. I am having a lot of trouble getting this to work and have traced my problem to my add node functions as they don't update the pointers correctly. For example on the AddToFront() function I can't understand how I can get the prev pointer to work and point to the node behind it.
I can't use the STL and have to implement the LL myself in case anyone is wondering. Thanks!
#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;
}
EDIT: I added in a line each in AddToFront() and AddToRear(). Is this on the right track?
AddtoFront needs to also update the prev pointer for front. That is, you're currently doing
temp -> front <-> rest_of_list
where it needs to be
temp <-> front <-> rest_of_list.
Also, you might notice that the two branches of your if statement in AddtoFront are identical... :)