Linked list node deletion troubles

I was having problems with this program before but pushed through most of them. However, I keep encountering a run-time error when my "Kill" function is called.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void LinkedList::Kill(int order)
{
	nodePtr cursor = First, temp;
	while(Count() > 1)
	{
		for(int i = 1; i < order; i++)
		{
			temp = cursor;
			temp = temp->next;
			cursor->next = temp->next;
			delete temp;
		}
		 cursor = cursor->next;
	}
	cout << "Person #" << cursor->info << " survived!" << endl;
}

It's supposed to take the order that the nodes should be deleted as a parameter. (For example if the order is 2, every second node should be deleted until only one is left.) I have the list linked circularly so the last node's next node is the beginning node. Is there something I'm missing here?
It appears that my problem is with my constructor, not the Kill function.
1
2
3
4
5
6
7
8
9
10
11
12
LinkedList::LinkedList(int size)
{
	nodePtr cursor, preCursor;
	for(int i = 0; i < size; i++)
	{
		cursor = new Node;
		cursor->info = (i + 1);
		preCursor = cursor;
		cursor = cursor->next;
	}
	preCursor->next = First;
}

I'm getting a bit frustrated with this as it's due by midnight.
closed account (zwA4jE8b)
nodePtr cursor = First, temp;

this line seems a little wierd, are you trying to declare first and temp as nodeptr's?

you cannot declare cursor and set it equal to two variables
closed account (zwA4jE8b)
or are you trying to declare 3 nodeptr's nodePtr cursor, First, temp;

in your constructor you cannot have cursor = cursor->next; because you have not declared what next points. You tell it what the info should be but not next. if cursor is a new node then how can cursor = the node it points to if it just came into existence
Last edited on
Maybe I should include the whole program to weed out any discrepancies.
This is the header file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
using namespace std;

class LinkedList
{ private:
  class Node
  {
   public:
    int info;
    Node *next;
  };
  typedef Node *nodePtr;
 
 public:		
	LinkedList(int size);
	~LinkedList();
	void Kill(int order);
	int Count();
	bool Empty();
  private:
      nodePtr First;
};
LinkedList::LinkedList(int size)
{
	nodePtr cursor, preCursor;
	for(int i = 0; i < size; i++)
	{
		cursor = new Node;
		cursor->info = (i + 1);
		preCursor = cursor;
		cursor = cursor->next;
	}
	preCursor->next = First;
}
LinkedList::~LinkedList()
{
	nodePtr cursor = First;
	while(cursor !=0)
	{
		First = First->next;
		delete cursor;
		cursor = First;
	}
}
void LinkedList::Kill(int order)
{
	nodePtr cursor = First, temp;
	while(cursor->next != cursor)
	{
		for(int i = 1; i < order; i++)
		{
			temp = cursor;
			temp = temp->next;
			cursor->next = temp->next;
			delete temp;
		}
		 cursor = cursor->next;
	}
	cout << "Person #" << cursor->info << " survived!" << endl;
}
int LinkedList::Count()
{
	nodePtr cursor = First;
	int sum;
	if(!Empty())
	{
		for(sum = 1; cursor != 0; sum++)
			cursor = cursor->next;
	}
	else
		sum = 0;
	return sum;
}
bool LinkedList::Empty()
{
	return (First == 0);
}

And this is the driver.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "LinkedList.h"
using namespace std;

int main()
{
	int captives, killOrder;
	cout << "Enter the number of captives: ";
	cin >> captives;
	LinkedList circle(captives);
	cout << endl << "Now enter the order that they will be killed: ";
	cin >> killOrder;
	circle.Kill(killOrder);
	return 0;
}
You should step through the whole thing with a debugger, I dont think your ctor works.
So I'm thinking it would be beneficial to remove preCursor completely from the ctor, as well as the cursor = cursor->next line, then write a separate function to put the nodes into order. I suppose I'll give that a try.
The problem in your ctor is you need cursor->next = new Node;
before cusor = cursor->next;
Topic archived. No new replies allowed.