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 6 years ago.
Improve this question
Write the definition of the function moveNthFront that takes as a parameter a positive integer, n. The function moves the nth element of the queue to the front. The order of the remaining elements remains unchanged. For example, suppose:
queue = {5, 11, 34, 67, 43, 55} and n = 3.
After a call to the function moveNthFront:
queue = {34, 5, 11, 67, 43, 55}.
Add this function to the class queueType. Also, write a program to test your method.
Here is my header
#ifndef queueType_H
#define queueType_H
#include<iostream>
class queueType
{
private:
class Queue
{
friend class queueType;
int value;
Queue *next;
Queue(int value1, Queue *next1 = NULL)
{
value = value1;
next = next1;
}
};
// These track the front and rear of the queue
Queue *front;
Queue *rear;
Queue *head;
public:
void moveNthFront(int);
Here is the cpp for the header.
#include"queueType.h"
#include<iostream>
using namespace std;
queueType::queueType() {
front = NULL;
rear = NULL;
}
queueType::~queueType() {
clear();
}
void queueType::enqueue(int num)
{
if (isEmpty())
{
front = new Queue(num);
rear = front;
}
else
{
rear->next = new Queue(num);
rear = rear->next;
}
}
void queueType::dequeue(int &num)
{
Queue *temp;
if (isEmpty())
{
cout << "The queue is empty.\n";
exit(1);
}
else
{
num = front->value;
temp = front;
front = front->next;
delete temp;
}
}
bool queueType::isEmpty() const
{
if (front == NULL)
return true;
else
return false;
}
int queueType::search(int x)
{
if (front == NULL)
return -1;
else
{
int count = 0;
Queue *aptr = front;
while (aptr != NULL)
{
if (aptr->value == x)
return count;
aptr = aptr->next;
count++;
}
return -1;
}
}
void queueType::clear()
{
int value; // Dummy variable for dequeue
while (!isEmpty())
dequeue(value);
}
void queueType::remove(int pos)
{
if (front == NULL)
return;
else if (pos == 0)
front = front->next;
else
{
int count = 0;
Queue *now = front, *past;
while (now != NULL && count != pos)
{
past = now;
now = now->next;
count++;
}
if (now)
{
past->next = now->next;
delete now;
}
}
}
void queueType::insert(int x, int pos )
{
Queue *now, *past;
if (front == NULL)
front = new Queue(x);
else if (now != NULL && pos == 0)
{
now = front;
;
for (int i = 0; i < 5; i++)
{
past = now;
now = now->next;
}
past->next = new Queue(x, now);
}
else
{
now = front;
int count = 0;
while (now != NULL && count != pos)
{
past = now;
now = now->next;
count++;
}
past->next = new Queue(x, now);
}
}
Here is my main
#include"queueType.h"
#include<iostream>
using namespace std;
int main() {
queueType intqueue;
int input, temp, x = 0;
for (int i = 0; i < 5; i++) {
intqueue.enqueue(i*i);
}
cout << "The values in the queue were:\n";
while (!intqueue.isEmpty())
{
int value;
intqueue.dequeue(value);
cout << value << " ";
}
for (int i = 0; i < 5; i++) {
intqueue.enqueue(i*i);
}
cout << "\nEnter the value to find:" << endl;
cin >> input;
intqueue.search(input);
temp = intqueue.search(input);
intqueue.remove(temp);
intqueue.insert(input, 0);
cout << "\nThe values after change was made:\n";
while (!intqueue.isEmpty())
{
int value;
intqueue.dequeue(value);
cout << value << " ";
}
return 0;
}
In insert method
else if (now != NULL && pos == 0)
{
this part can never run because now is null as it has never been initialized.
Then control goes to else part
where if pos =0 *past is never initialized.
Initialize *now first.
else you can remove else if part and just initialize *past once outside the while loop i.e.
else
{
now = front;
int count = 0;//here initialize past once past=now;
while (now != NULL && count != pos)
{
past = now;
now = now->next;
count++;
}
past->next = new Queue(x, now);
}
I edited it. It works for all positions except for 0 pos.
I need the element to go to the front of the queue.
void queueType::insert(int x, int pos )
{
Queue *now, *past;
if (front == NULL)
front = new Queue(x);
else
{
now = front;
int count = 0;
while (now != NULL && count != pos)
{
past = now;
now = now->next;
count++;
}
past->next = new Queue(x, now);
}
}
Related
This assignment (I've linked the full description here) asks us to create a multi server queue simulation (i.e. grocery store type scenario). My code seems to be working for the most part however I currently have 2 questions pertaining to my output that I am having a hard time answering.
Why does queue_total not match the actual number of elements displayed in each queue at the end of the program?
Why won't new items be enqueued to the shortest queue even though the shortest_queue function seems to be working?
The main code:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
#include "queue_2.h"
using namespace std;
// Utility function to return shortest queue.
int shortest_queue(Queue line[], int queuecount)
{
int shortest = 0;
// Condition 1: If a queue is empty
// with no active transactions, return it.
for (int i = 0; i < queuecount; i++) {
if (line[i].empty() && line[i].get_trans_time() == 0)
return i;
}
// Condition 2: If a queue is simply empty,
// return it next.
for (int i = 0; i < queuecount; i++) {
if(line[i].empty()) {
return i;
}
}
// Condition 3: (otherwise) If all queues are
// occupied check for shortest queue.
for (int i = 1; i < queuecount; i++) {
if(line[i].size() < line[shortest].size())
shortest = i;
}
return shortest;
}
// Utility function to return total number
// of customers waiting in queues.
int queue_total(Queue q[], int queuecount)
{
int custcount = 0;
for (int i = 0; i < queuecount; ++i)
custcount += q[i].size();
return custcount;
}
int main()
{
// Variable decelrations
int trans_time = 0;
int count = 0;
int entry_time;
int wait_sum = 0;
int wait_time = 0;
int seed = 3;
int ariv_prob = 80;
int MAX_TRANS_TIME = 12;
int DURATION = 120;
int queuecount = 4;
int shortline;
int temp;
// Create a list of queues
// and random number generator
Queue line[queuecount];
srand(seed);
for (int time = 1; time < DURATION + 1; time++)
{
// Display the time
cout << "Time: " << time << endl;
// Check probablity only once per iteration
if ( rand() % 100 < ariv_prob ) {
shortline = shortest_queue(line, queuecount);
line[shortline].enqueue(time);
}
// Update the transaction times for
// the corresponding queues and compute
// summary statistics.
for (int i = 0; i < queuecount; i++) {
if ( line[i].get_trans_time() == 0 ) {
if ( !line[i].empty() ) {
entry_time = line[i].print_front();
line[i].dequeue();
temp = (time - entry_time);
if(temp > wait_time)
wait_time = temp;
wait_sum += (time - entry_time);
++count;
line[i].set_trans_time((rand() % MAX_TRANS_TIME) + 1);
}
}
// Decrement and update transaction time.
else {
trans_time = line[i].get_trans_time() - 1;
line[i].set_trans_time(trans_time);
}
}
// Display status of the queues for the
// the given iteration.
for (int i = 0; i < queuecount; i++) {
cout << setw(4) << line[i].get_trans_time() << " ";
line[i].display();
}
cout << endl;
}
cout << count << " customers waited an average of ";
cout << wait_sum / count << " ticks." << endl;
cout << "The longest time a customer waited was " << wait_time << " ticks." << endl;
cout << queue_total(line, queuecount) << " customers remain in the lines." << endl;
return 0;
}
The queue implementation
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
using namespace std;
class Queue {
private:
struct Node {
int data;
Node* next;
Node(int d) {
data = d;
next = NULL;
}
};
Node* front;
Node* rear;
int count;
int trans_time;
public:
Queue() {
front = rear = NULL;
count = trans_time = 0;
}
void enqueue(int x) {
// Create a new linked Node.
Node* temp = new Node(x);
// If the queue is already empty
// then new node is front and rear
if (empty()) {
front = rear = temp;
return;
}
// Add the new node at the end
// of the queue and change the rear
rear->next = temp;
rear = temp;
++count;
}
void dequeue() {
// If the queue is empty
// then we can return NULL
if (empty())
return;
// Store the previous front and
// move the front one node ahead
Node* temp = front;
front = front->next;
// If front becomes NULL, then
// Change the rear to be NULL as well
if (front == NULL)
rear = NULL;
delete(temp);
--count;
}
// Utility function to check
// if the queue is empty
bool empty() {
return (front == NULL && rear == NULL);
}
int size() {
return count;
}
// Utility function to print front
// element of the queue
int print_front() {
return front->data;
}
void display() {
Node* temp = front;
while (!(temp == NULL)) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void set_trans_time(int i) {
trans_time = i;
}
int get_trans_time() {
return trans_time;
}
};
#endif
Your enqueue function doesn't increment (or set) the count when inserting into an empty queue. This causes the count to be 0 for a queue with 1 element.
The comparisons for count > 0 or count >= 0 before decrementing/incrementing it should be unnecessary. If those conditions are ever false then you have a problem elsewhere that caused the inconsistency.
I'm trying to backtrack through a linkedlist I have created to solve a knight's tour problem, but once I hit a condition requiring me to backtrack, it totally empties the list. I have figured out it is because when I return false from the function findpath() to keep my while loop going, it continually loops back through the goback() function. What I'm having trouble figuring out is why. When i go through the goback function and call the calcmove function, it pushes the correct move to the structure I have declared, so passing false should keep the loop going and restart with the boolean cando and figuring out if that is false or not. But, it should be true because I just tested it and pushed it to the stack. Instead, it's emptying the list after I backtrack for the first time. So calcmove() would just be returning false every time after the first pop. But I don't see why that's happening. I need to return false to keep the loop going. What I need it to do is go back only once, find a new move, and continue on. Why would it be looping back to the function where the value on the list gets popped? If the value worked, and a new move is created (which I did verify it is doing correctly), it should be ready to call with a new (true) boolean. Is this just an inherently bad way of implementing the backtracking?
#include "stdafx.h"
#include "Header.h"
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <iomanip>
llist list;
Move m;
int board[8][8];
int cx[] = { -2, -1, 1, 2, 2, 1, -2, -1 };
int cy[] = { 1, 2, 2, 1, -1, -2, -1, -2 };
int movenum = 1;
Move first;
/*Check to see if move is within the constraints of the board*/
bool constraints(int k, int b) {
if ((k < 0) || (b < 0) || (k > 7) || (b > 7) || (board[k][b] != -1)) {
return true;
}
else {
return false;
}
}
/* Initialization of 8x8 board, fill the array with -1 */
void initboard() {
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
board[x][y] = -1;
}
}
}
/* Output the current board array */
void printboard(int arr[8][8]) {
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
cout << std::setw(2) << arr[x][y] << " ";
}
cout << endl;
}
}
void updateboard(int k, int b) {
board[k][b] = movenum;
movenum++;
}
bool calcmove(int x, int y, int i) {
for (i; i < 9; i++) {
int a0 = x + cx[i];
int a1 = y + cy[i];
/*if(board[a0][a1] == board[first.x][first.y]){
}*/
if (constraints(a0, a1) == false) {
updateboard(a0, a1);
m.x = a0;
m.y = a1;
m.index = i;
list.push(m);
//list.display();
return true;
}
}
return false;
}
void goback(int k, int b) {
board[k][b] = -1;
list.display();
Move m = list.pop();
bool cando = calcmove(m.x, m.y, m.index);
cout << "prev is " << m.x << " , " << m.y << endl;
if (cando) {
return;
}
else {
goback(m.x,m.y);
}
}
bool findpath(){
bool cando = calcmove(m.x, m.y, 0);
if (cando) {
return false;
}
else {
movenum--;
goback(m.x, m.y);
return false; /*------> inserting this here drains the list*/
}
return true;
}
int main()
{
int m1 = 1;
int m2 = 2; //random
first.x = m1;
first.y = m2;
first.index = 0;
list.push(first); //push first node on to stack
initboard();
updateboard(m1, m2); //update board
while (!findpath()) {
;
}
printboard(board);
}
the .h file:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include <ostream>
using std::cout;
using std::endl;
using std::setw;
struct Move {
int x;
int y;
uint32_t index;
};
struct node
{
public:
Move info;
struct node *next;
};
class llist {
struct node *start;
public:
void push(Move coords) {
struct node *p = new node;
p = newnode(coords);
if (start == NULL) {
start = p;
}
else {
p->next = start;
start = p;
//cout << "pushed" << endl;
}
}
Move pop() {
if (start == NULL) {
throw std::runtime_error("The list is empty");
}
else {
struct node *temp = new node;
temp = start;
Move data = start->info;
start = start->next;
delete temp;
//cout << "pop successful" << endl;
return data;
}
}
node *newnode(Move value) {
struct node *temp = new(struct node);
if (temp == NULL) {
return 0;
}
else {
temp->info = value;
temp->next = NULL;
return temp;
}
}
void display()
{
struct node *temp;
if (start == NULL)
{
cout << "The List is Empty" << endl;
return;
}
temp = start;
cout << "Elements of list are: " << endl;
while (temp != NULL)
{
cout << temp->info.x << "->";
temp = temp->next;
}
cout << "empty" << endl;
}
};
The goal of this is to create a linked list, solve the problem using the list for backtracking (push the current move unless no moves are available, then pop the previous values), and return the completed board.
edit, I'll try to give a better exemple:
first I'll tell your about the program:
basicly, there' two phases, the first is inserting the words (works perfectly fine)
struct trieLeaf {
keyType age = 1;
};
class Trie
{
private:
dataType character;
trieLeaf *leaf = nullptr;
class Trie **alphabet = nullptr;
int main()
{
string input;
Trie dictionary('\0');
while (getline(cin, input) && input[0] != '.')
{
dictionary.analyzeText(input);
}
while (getline(cin, input) && input[0] != '.')
{
dictionary.approxFind(input);
}
system("pause>null");
}
second phase is searching for words, if words is found then I need to reduce the number of times it has been added by 1.
if number of times it has been added is 0, then I remove it.
void Trie::approxFind(string &word)
{
int index = 0;
string result;
Trie *curr;
if (curr=find(word))
{
cout << curr->leaf->age << endl;
curr->leaf->age -= 1;
if (curr->leaf->age == 0)
{
remove(word);
}
}
else
{
result = approximate(word);
cout << "Did you mean " << result << "?\n";
}
}
Trie* Trie::find(const string &word)
{
int index = 0;
Trie *curr = this;
while (word[index] != '\0')
{
if (curr->alphabet[word[index] - 'a'] != nullptr)
{
curr = curr->alphabet[word[index++] - 'a'];
}
else
{
return (nullptr);
}
}
if (curr->leaf != nullptr)
{
return (curr);
}
}
void Trie::remove(const string &word)
{
int index = 0;
Trie *curr = this, **tempArr, *temp;
while (word[index] != '\0')
{
tempArr = curr->alphabet;
temp = curr;
curr = curr->alphabet[word[++index] - 'a'];
if (!isArrEmpty(tempArr))
{
delete(tempArr);
}
delete(temp);
temp = nullptr;
}
}
bool Trie::isArrEmpty(Trie **alphabet)
{
for (int index = 0; index < 26; index++)
{
if (!alphabet[index])
{
return (true);
}
}
return(false);
}
TempArr in isArrEmpty is losing the object it's pointing to when passing onto the funcion.
I hope it gives a better idea why
I am currently trying to learn C++ on my own and have been going through some textbooks and trying to do some problems. While learning pointers, I decided to try and implement a linked list on my own. I have written the program, but keep getting an error that says: "segmentation error (core dumped)". I have searched through other similar questions on this website and although there are a lot on the same topic, none have helped me fix my problem. I'm pretty new to programming and pointers, so any help will be appreciated!
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct node
{
int element;
struct node *next;
}*start;
class pointerlist
{
public:
node* CREATE(int num);
void ADD(int num);
int FIRST();
int END();
int RETRIEVE(int pos);
int LOCATE(int num);
int NEXT(int pos);
int PREVIOUS(int pos);
void INSERT(int pos, int num);
void DELETE(int pos);
void MAKENULL();
pointerlist()
{
start = NULL;
}
};
main()
{
pointerlist pl;
start = NULL;
pl.ADD(1);
cout << "Added 1" << endl;
for (int j=1; j<=5; j++)
pl.ADD(j);
cout << "The pointer implemented list is: " << endl;
for (int i=1; i<=5; i++)
{
cout << pl.END() << " " ;
}
cout << endl << endl;
}
void pointerlist::ADD(int num)
{
struct node *temp, *s;
temp = CREATE(num);
s = start;
while (s->next != NULL)
s = s->next;
temp->next = NULL;
s->next = temp;
}
node *pointerlist::CREATE(int num)
{
struct node *temp, *s;
temp = new(struct node);
temp->element = num;
temp->next = NULL;
return temp;
}
int pointerlist::FIRST ()
{
int num;
struct node *s;
s = start;
num = s->element;
return num;
}
int pointerlist::END()
{
struct node *s;
s = start;
int num;
while (s != NULL);
{
num = s->element;
s = s->next;
}
return num;
}
int pointerlist::RETRIEVE(int pos)
{
int counter = 0;
struct node *s;
s = start;
while (s != NULL)
{
counter++;
if (counter == pos)
{
return s->element;
}
s = s->next;
}
}
int pointerlist::LOCATE(int num)
{
int pos = 0;
bool flag = false;
struct node *s;
s = start;
while (s != NULL)
{
pos++;
if (s->element == num)
{
flag == true;
return pos;
}
s = s->next;
}
if (!flag)
return -1;
}
int pointerlist::NEXT(int pos)
{
int next;
int counter = 0;
struct node *s;
s = start;
while (s != NULL)
{
counter++;
if (counter == pos)
break;
s = s->next;
}
s = s->next;
next = s->element;
return next;
}
int pointerlist::PREVIOUS(int pos)
{
int previous;
int counter = 1;
struct node *s;
s = start;
while (s != NULL)
{
previous = s->element;
counter++;
if (counter = pos)
break;
s = s->next;
}
return previous;
}
void pointerlist::INSERT(int pos, int num)
{
struct node *temp, *s, *ptr;
temp = CREATE(num);
int i;
int counter = 0;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start = NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if(pos>1 && pos <= counter)
{
s = start;
for (i=1; i<pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
}
void pointerlist::DELETE(int pos)
{
int counter;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos >0 && pos <= counter)
{
s = start;
for(int i=1; i<pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
free(s);
}
}
void pointerlist::MAKENULL()
{
free(start);
}
The problem occurs in line 39 of my code (inside main where I write pl.ADD(1)). In this line I was trying to start off the list with the vale 1. There maybe be problems with the rest of my code too, but I have not been able to get past this line to check. Please help!
List is empty at the beginning, therefore it will fail on s-> next as s == start ==NULL :
s = start;
while (s->next != NULL)
Thanks for the help! I was able to fix the problem by adding a first function which adds the first element to the list. But now I am having trouble with my END function. It appears to not be entering the loop within the function when it is called. I cannot find a reason for this. Would anyone be able to help me figure out why my END function does not work?
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm trying to write a function that calculates the N-th Fibonacci number using doubly linked lists, but for some reason when I compile and run the linked list does not stop growing, it keeps adding 1 number over and over with no ending.
This should be a SSCCE:
#include <iostream>
using namespace std;
class node {
public:
int value;
node* previous;
node* next;
};//node
class number {
public:
node* start;
node* end;
node* add (int value);
void show (int K);
number ();
void destroy ();
void copy (number gg1);
void addition (number gg1, number gg2, int K);
void fibonacci (int K, int times);
};//number
number::number () {
start = NULL;
end = NULL;
}
int power (int K) {
int L = 1;
for (int i = (K-1); i > 0; i--) {
L = L*10;
}
return L;
}
int checksize (int value) {
int counter = 0;
while (value != 0) {
value = value / 10;
counter += 1;
}
return counter;
}
void number::show (int K) {
node* current;
cout << "\nValue:" << endl;
if (start == NULL) {
cout << "\nNothing\n" << endl;
}
if (start != NULL) {
current = start;
while (current != NULL) {
if (current->value == 0) {
for (int i = 0; i < K; i++) {
cout << "0";
}
cout << "\n";
}
else {
int size = checksize (current->value);
for (int j = size; j < K; j++) {
cout << "0";
}
cout << current->value << endl;
}
current = current->next;
}
}
//cout << "\n";
}
int main () {
number gg1;
number gg2;
number gg3;
const int K = 5;
gg1.fibonacci (K, 10);
}
node* number::add(int value) {
node* currentcode;
if (start == NULL){
currentcode = new node;
start = currentcode;
end = currentcode;
currentcode->next = NULL;
currentcode->previous = NULL;
currentcode->value = value;
return currentcode;
}
if (start != NULL) {
currentcode = new node;
currentcode->next = NULL;
end->next = currentcode;
currentcode->previous = end;
end = currentcode;
currentcode->value = value;
return currentcode;
}
return NULL;
}
void number::addition (number gg1, number gg2, int K) {
int value1, value2, value3;
int carry = 0;
node* current1;
node* current2;
current1 = gg1.start;
current2 = gg2.start;
while (current1 != NULL || current2 != NULL) {
if (current1 != NULL && current2 !=NULL) {
value1 = current1->value;
value2 = current2->value;
value3 = value1 + value2 + carry;
current1 = current1->next;
current2 = current2->next;
}
else if (current1 == NULL && current2 != NULL) {
value3 = current2->value + carry;
current2 = current2->next;
}
else if (current1 != NULL && current2 == NULL) {
value3 = current1->value + carry;
current1 = current1->next;
}
checksize(value3);
if (value3 > power(K)) {
value3 = value3 - 10*(power(K));
carry = 1;
}
else
carry = 0;
add(value3);
if ((current1 == NULL && current2 == NULL) && (carry == 1))
add(1);
}
}
void number::destroy () {
node* current;
node* current2;
if (start != NULL) {
current = start;
current2 = current->next;
while (current2 != NULL) {
delete current;
current = current2;
current2 = current->next;
}
delete current;
}
}
void number::fibonacci (int K, int times) {
number g1;
number g2;
number g3;
destroy ();
g1.add (1);
g2.add (1);
g3.addition (g1, g2, K);
g2.copy(g1);
g1.show(K);
g2.show(K);
//g1.copy(g3);
//g1.show(K);
//g2.show(K);
//g3.show(K);
//g3.addition (g1, g2, K);
//g3.show(K);
//g2.copy(g1);
//g1.copy(g3);
/*for (int i = 0; i < 2; i++) {
g3.addition (g1, g2, K);
g3.show(K);
g2.copy(g1);
g1.copy(g3);
}*/
copy(g3);
}
void number::copy (number gg1) {
int value;
destroy ();
node* current = gg1.start;
while (current != NULL) {
value = current->value;
add(value);
current = current->next;
}
}
Whenever I run the Fibonacci function it gives me endless 1's in the terminal.
The number class is just a basic doubly linked pointer list.
The addition function standalone works just fine, so does the copy. In fact everything was working fine until this. It's easy to finish the function with a for-loop, but this error prevents me from doing so. Does anyone know what my mistake is? Thanks in advance.
Right now, you have invalid memory access, since calling delete on each node in destroy() does not NULL-out the memory but it only marks the memory free.
Suggested correction:
void number::destroy () {
node* current;
node* current2;
if (start != NULL) {
current = start;
current2 = current->next;
while (current2 != NULL) {
delete current;
current = current2;
current2 = current->next;
}
delete current;
}
start = NULL; // so you can't access the now non-existing list anymore.
end = NULL;
}
Remark:
Class names should be capital-first by widely adapted convention.
You should not pass a class by value in your copy and addition function but const-ref.
Better use in this case operator= instead of copy. copy can be copy_from or copy_to, calling a function copy is ambigous, really.
Always better to use for loops when you can.
Node is not a class but a struct, it is better to call it a struct.
The new code can also look like this:
Number& Number::operator=(const Number& n)
{
destroy();
for(Node* current = gg1.start; current; current = current->next)
add(current->value);
}
void Number::destroy()
{
Node* temp;
for(Node* current = start; current; current = current->next, delete temp)
temp = current;
start = NULL;
end = NULL;
}