C++ Templated Class Invalid Use of Incomplete Class - c++

Trying to learn C++. I'm getting an error in my code, in the line:
class LinkedList : public LinkedList<T> {
The error says:
invalid use of incomplete type 'class
LinkedList,
std::allocator > >' declaration of 'class
LinkedList,
std::allocator > >'
Can anyone point out what I'm doing wrong? Here is the entire code.
#ifndef LINKEDLIST_HPP
#define LINKEDLIST_HPP
#include <iostream>
using namespace std;
template <class T>
class LinkedList : public LinkedList<T> {
private:
// a ListNode consists of the item and a pointer to the next ListNode
struct ListNode {
T data;
ListNode *next;
};
int size; //Size of the list
ListNode *head; //the beginning of the list
public:
LinkedList(); // default oonstructor
~LinkedList(); // destructor
virtual bool isEmpty ();
virtual int getLength ();
virtual void insert (int pos, T item);
virtual T remove (int pos);
virtual T retrieve (int pos);
//helper methods
LinkedList(LinkedList &copyList); //copy constructor
ListNode *find (int pos) const; // internal find function
};
//default constructor
template <class T>
LinkedList<T>::LinkedList() {
size = 0;
head = NULL;
}
//destructor
template <class T>
LinkedList<T>::~LinkedList() {
//loop through each node, and delete it
ListNode* current = head;
while (current != NULL) {
ListNode* next = current->next;
delete current;
current = next;
}
head = NULL; // set head node to back to null
}
//copy constructor
template <class T>
LinkedList<T>::LinkedList(LinkedList& copyList) {
size = copyList.size;
// if copyList is empty
if (copyList.head == NULL)
head = NULL;
else {
//create a new head
head = new ListNode;
head->data = copyList.head->data;
//create a new list
ListNode *newPtr = head; // start at the head
// iterate through rest of list to be copied
for (ListNode *copyPtr = copyList.head->next; copyPtr != NULL; copyPtr = copyPtr->next) {
newPtr->next = new ListNode;
newPtr->data = copyPtr->data;
}
//make last ListNode's next point to NULL
newPtr->next = NULL;
}
}
template <class T>
bool LinkedList<T>::isEmpty() {
if (size == 0)
return true;
return false;
}
template <class T>
int LinkedList<T>::getLength() {
return size;
}
// used in other methods to find a given index
template <class T>
LinkedList<T>::ListNode LinkedList<T>::find(int pos) const {
// check that pos is in bound of LinkedList
if ((pos < 1) || pos > getLength()) {
cout << "Find position of out bounds" << endl;
return NULL;
} else { //search through ListNodes
ListNode *temp = head; // start at the head
for (int i = 1; i < pos; ++i)
temp = temp->next;
return temp;
}
}
template <class T>
T LinkedList<T>::retrieve(int pos) {
T tempData; // to hold retrieved data
try {
if ((pos < 1) || (pos > getLength())) {
cout << "Retrieve request outside LinkedList's bounds" << endl;
return NULL;
}
else { //traverse list
ListNode *temp = find(pos);
tempData = temp->data;
return tempData;
}
} catch (int e) {
cout << "Could not retrieve position " << pos << endl;
}
}
template <class T>
void LinkedList<T>::insert(int pos, T item) {
//check bounds
if ((pos < 1) || (pos > getLength() +1))
cout << "Must insert at a position between 1 and getLength() + 1" << endl;
else {
try {
//create new ListNode
ListNode *temp = new ListNode;
temp->data = item;
//if the new item is at the first position
if (pos == 1) {
temp->next = head;
head = temp;
}
else {
ListNode *prev = find(pos - 1);
temp->next = prev->next;
prev->next = temp;
}
//increment size
++size;
} catch (int e) {
cout << "Error inserting " << item << " at position " << pos << endl;
}
}
}
template <class T>
T LinkedList<T>::remove(int pos) {
//check bounds
if ((pos < 1) || (pos > getLength()))
cout << "Must remove a position between 1 and getLength()" << endl;
else {
try {
ListNode *temp; //to hold shifted node
//if node to be deleted is first node
if (pos == 1) {
temp = head;
head = head->next;
}
//for anything but the head
//write over the node before the pos'th index
else {
ListNode *prev = find(pos - 1);
temp = prev->next;
prev->next = temp->next;
}
//destroy temp, to free up memory
temp->next = NULL;
delete temp;
//decrement size
--size;
} catch (int e) {
cout << "Error removing item from position " << pos << endl;
}
}
}
#endif /* LINKEDLIST_HPP */

template <class T>
class LinkedList : public LinkedList<T> {
This doesn't make sense. It is like this:
class A : public A
Does this make sense to you? How exactly?
The class you're deriving from doesn't exist, rather it is being defined while you're deriving from it, so the compiler doesn't know what the base supposed to be. The compiler need to know the full definition of the base class before you derive from it.

Nawaz gave the right answer.
Just to give a few off-topic informations :
make your destructor virtual (even if inheriting from a templated container is not something very c++-ish).
Use const where it belongs :
LinkedList(const LinkedList &copyList); //copy constructor
virtual bool isEmpty () const;
// etc.
Obviously, in real life you would use an stl container like std::list ;)

Related

how can i get the front or top element of a vector queue?

I didn't put the full code because it was very long and i only need help with the small portion which is the **** area. i can't seem to use front() or top() to get the top element of the queue. I tried making top() function List keep getting error : 1) class List has no memeber named 'top' which means i don't have a function top in List, when i make it it says 2) no match for 'operator=' in printer_cpu[i] = SList::top() with T=PCB]()'
template <class T>
class node{
public:
T data;
node *next;
};
template <class T>
class List{
node<T> *head;
node<T> *tail;
public:
List()
{
head = tail = NULL;
}
bool isEmpty()
{
if(head == NULL) return true;
else return false;
}
void enqueue(T new_data){
node<T> *temp = new node<T>;
temp->data = new_data;
temp->next = NULL;
if(isEmpty()){
head = temp;
tail = temp;
}
else{
tail->next = temp;
tail = temp;
}
}
void dequeue(){
if(isEmpty())
{
cout << "The list is already empty" << endl;
}
node<T>* temp;
if(head == tail){
temp->data=head->data;
delete head;
head = tail = NULL;
}
else{
temp->data = head->data;
head = head->next;
delete temp;
}
}
node<T> top() // need help here ****
{
return head;
}
void display(){
node<T> *current = head;
while(current != NULL){
cout << current->data << endl;
current = current->next;
}
}
};
struct PCB
{
int ProcessID;
int ProcessorSize;
int priority;
string name;
};
typedef List<PCB> printing;
typedef List<PCB> disk;
void gen(vector<printing> &printer_queue,string printer_name[], int printers)
{
for(int i = 0; i < printers; i++)
{
int num = i+1;
ostringstream convert;
convert << num;
printer_name[i] = "p" + convert.str();
printer_queue.push_back(printing());
}
int main()
{
int numOfPrinter = 5;
string interrupt;
cin >> interrupt;
PCB cpu;
PCB printer_cpu[numOfPrinter];
string printer_name[numOfPrinter];
vector<printing> PQ;
gen(PQ,printer_name,numOfPrinter);
for(int i = 0; i < numOfPrinter; i++)
{
if(interrupt == printer_name[i])
{
cout << "Enter a name for this printer file: " << endl;
cin >> cpu.name;
PQ[i].enqueue(cpu);
printer_cpu[i] = PQ[i].top(); //need help here ****
}
}
}
It looks like you're missing an asterisk, because you need to return of type pointer, because that's what head is.
You should have
node<T> * top()
{
...
}
You also need to overload the = operator, because you are trying to compare type PCB with type node *.
Well, I compile your code successfully after correcting some mistakes.
I didn't meet class List has no memeber named 'top' problem.
Then your top() function returns the value of head, so you should change it to: node<T>* top() because head is a pointer to node<T>.
And the reason you got no match for 'operator=' error is that printer_cpu[i]'s type is PCB while PQ[i].top()'s type should be node<T>*
I have also found that the code you post lacks a } just before int main().

How to determine size of a Linked List without using a while loop?

I need to print out the number of nodes in a linked list. My teacher said that the linked list keeps track of its data and "knows" how many nodes are in it. So, I should not need a while loop to determine the size of the linked list. I have trouble figuring out a way other than a while loop to print out the size.
this is the linked list:
template <class T>
class LinkedList
{
private:
struct ListNode
{
T data ;
struct ListNode * next;
};
ListNode *head;
public:
LinkedList() { head = nullptr; }
~LinkedList();
// Linked list operations
void insertNode(T);
bool deleteNode(T);
void displayList() const;
};
/////////// Implementation portion of linked list with template //////////////
// displayList: print all list data
template <class T>
void LinkedList<T>::displayList() const
{
ListNode * ptr = head;
while (ptr != nullptr)
{
cout << ptr->data << endl;
ptr = ptr->next;
}
}
// insertNode: add a node in list order
template <class T>
void LinkedList<T>::insertNode(T newValue)
{
ListNode *newNode;
ListNode *pCur;
ListNode *pPre = NULL;
newNode = new ListNode;
newNode->data = newValue;
newNode->next = nullptr;
if (head == nullptr)
{
head = newNode;
}
else
{
pCur = head;
pPre = nullptr;
while (pCur != nullptr && pCur->data < newValue)
{
pPre = pCur;
pCur = pCur->next;
}
if (pPre == nullptr)
{
head = newNode;
newNode->next = pCur;
}
else
{
pPre->next = newNode;
newNode->next = pCur;
}
}
}
// deleteNode: delete a node if found
template <class T>
bool LinkedList<T>::deleteNode(T toBeDeleted)
{
ListNode *pCur;
ListNode *pPre;
if (!head)
return true;
pCur = head;
pPre = NULL;
while (pCur != NULL && pCur->data < toBeDeleted)
{
pPre = pCur;
pCur = pCur->next;
}
if (pCur != NULL && pCur->data == toBeDeleted)
{
if (pPre)
pPre->next = pCur->next;
else
head = pCur->next;
delete pCur;
return true;
}
return false;
}
// destructor, delete all nodes
template <class T>
LinkedList<T>::~LinkedList()
{
ListNode *ptr = head;
while (ptr != NULL)
{
head = head->next;
delete ptr;
ptr = head;
}
}
Using the code you've defined, the size of the list is not stored by the list directly. Further to this, the main advantage of linked list is that each node does not know about the rest of the list, and storing the size would defeat the purpose of this.
However, you may have misunderstood what was asked of you in terms of not using a while loop. Each node knows that it's length is 1+(the length of it's tail), and so the more suitable implementation for getting the length of a linked list is recursion, not iteration.
Here is an example of a very simple LinkedList class, that implements the simple methods using recursion. As you can see, the code uses no iteration, only making a check for it's own data, then calling the same method for the next node. Although recursion in procedural languages is less efficient in most cases, for structures like this there is no doubting it is elegant.
#include <iostream>
template<class T>
class LinkedList
{
private:
T data;
LinkedList* next;
public:
LinkedList()
: LinkedList(T()) {
}
LinkedList(T value)
: data(value), next(nullptr) {
}
~LinkedList() {
delete next;
}
void insertNode(T newValue) {
if (!next) {
next = new LinkedList(newValue);
return;
}
next->insertNode(newValue);
}
void displayList() const {
std::cout << data << std::endl;
if (next) {
next->displayList();
}
}
T& at(int N) {
if (N == 0) {
return this->data;
}
return next->at(N-1);
}
int size() {
if (!next) {
return 1;
}
return 1+next->size();
}
};
int main(int argc, char const *argv[])
{
LinkedList<int>* test = new LinkedList<int>(0);
for (int i = 1; i < 10; ++i) {
test->insertNode(i);
}
std::cout << "List of length: " << test->size() << std::endl;
test->displayList();
return 0;
}
You'll notice I haven't included deleteNode, that's because writing it for the oversimplified class above is not possible for the case where the list only has one node. One possible way to implement this is to have a wrapper class, much like you in the original code, that is a pointer to the start of a linked list. See here.

Inserting two elements in node template in a doubly linked list

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.

C++: Expected Constructor, Destructor or Type Conversion

Trying to learn C++. Was wondering if someone could point me in the right direction on this error (pun intended).
The class is a LinkedList (reproduced in its entirety below). The error occurs on this line:
LinkedList<T>::ListNode LinkedList::find(int pos) const {
and says:
expected constructor, destructor, or type conversion before
'LinkedList'
Any tips would be greatly appreciated.
#ifndef LINKEDLIST_HPP
#define LINKEDLIST_HPP
#include <iostream>
using namespace std;
template <class T>
class LinkedList {
private:
// a ListNode consists of the item and a pointer to the next ListNode
struct ListNode {
T data;
ListNode *next;
};
int size; //Size of the list
ListNode *head; //the beginning of the list
public:
LinkedList(); // default oonstructor
~LinkedList(); // destructor
virtual bool isEmpty ();
virtual int getLength ();
virtual void insert (int pos, T item);
virtual T remove (int pos);
virtual T retrieve (int pos);
//helper methods
LinkedList(LinkedList &copyList); //copy constructor
ListNode *find (int pos) const; // internal find function
};
//default constructor
template <class T>
LinkedList<T>::LinkedList() {
size = 0;
head = NULL;
}
//destructor
template <class T>
LinkedList<T>::~LinkedList() {
//loop through each node, and delete it
ListNode* current = head;
while (current != NULL) {
ListNode* next = current->next;
delete current;
current = next;
}
head = NULL; // set head node to back to null
}
//copy constructor
template <class T>
LinkedList<T>::LinkedList(LinkedList& copyList) {
size = copyList.size;
// if copyList is empty
if (copyList.head == NULL)
head = NULL;
else {
//create a new head
head = new ListNode;
head->data = copyList.head->data;
//create a new list
ListNode *newPtr = head; // start at the head
// iterate through rest of list to be copied
for (ListNode *copyPtr = copyList.head->next; copyPtr != NULL; copyPtr = copyPtr->next) {
newPtr->next = new ListNode;
newPtr->data = copyPtr->data;
}
//make last ListNode's next point to NULL
newPtr->next = NULL;
}
}
template <class T>
bool LinkedList<T>::isEmpty() {
if (size == 0)
return true;
return false;
}
template <class T>
int LinkedList<T>::getLength() {
return size;
}
// used in other methods to find a given index
template <class T>
LinkedList<T>::ListNode LinkedList::find(int pos) const {
// check that pos is in bound of LinkedList
if ((pos < 1) || pos > getLength()) {
cout << "Find position of out bounds" << endl;
return NULL;
} else { //search through ListNodes
ListNode *temp = head; // start at the head
for (int i = 1; i < pos; ++i)
temp = temp->next;
return temp;
}
}
template <class T>
T LinkedList<T>::retrieve(int pos) {
T tempData; // to hold retrieved data
try {
if ((pos < 1) || (pos > getLength())) {
cout << "Retrieve request outside LinkedList's bounds" << endl;
return NULL;
}
else { //traverse list
ListNode *temp = find(pos);
tempData = temp->data;
return tempData;
}
} catch (int e) {
cout << "Could not retrieve position " << pos << endl;
}
}
template <class T>
void LinkedList<T>::insert(int pos, T item) {
//check bounds
if ((pos < 1) || (pos > getLength() +1))
cout << "Must insert at a position between 1 and getLength() + 1" << endl;
else {
try {
//create new ListNode
ListNode *temp = new ListNode;
temp->data = item;
//if the new item is at the first position
if (pos == 1) {
temp->next = head;
head = temp;
}
else {
ListNode *prev = find(pos - 1);
temp->next = prev->next;
prev->next = temp;
}
//increment size
++size;
} catch (int e) {
cout << "Error inserting " << item << " at position " << pos << endl;
}
}
}
template <class T>
T LinkedList<T>::remove(int pos) {
//check bounds
if ((pos < 1) || (pos > getLength()))
cout << "Must remove a position between 1 and getLength()" << endl;
else {
try {
ListNode *temp; //to hold shifted node
//if node to be deleted is first node
if (pos == 1) {
temp = head;
head = head->next;
}
//for anything but the head
//write over the node before the pos'th index
else {
ListNode *prev = find(pos - 1);
temp = prev->next;
prev->next = temp->next;
}
//destroy temp, to free up memory
temp->next = NULL;
delete temp;
//decrement size
--size;
} catch (int e) {
cout << "Error removing item from position " << pos << endl;
}
}
}
#endif /* LINKEDLIST_HPP */
ListNode is a dependant type, so it needs to be qualified with typename. Also, your find function was declared to return a pointer-to-ListNode, but the definition returns a ListNode by value. This seems like a simple typo:
template <class T>
typename LinkedList<T>::ListNode* LinkedList<T>::find(int pos) const {
Qualify the dependant type with typename and add <T> (I'm not sure if this is necessary, might as well do it though):
typename LinkedList<T>::ListNode* LinkedList<T>::find(int pos) const {
See: Where and why do I have to put the "template" and "typename" keywords?
Also, your definition doesn't match. In-class it returns a pointer, but outside it returns a value.
Should be
LinkedList<T>::ListNode LinkedList<T>::find(int pos) const {

Trouble adding nodes to a doubly Linked List

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... :)