need help with special case, VERY CLOSE

"In this program we will write a small game using link list:

- Your program should ask the user to enter one alphabet letter at a time, and each letter should be stored in a node. Choose a terminate character if the user want to stop.

- Next, you will ask the user for an integer n as your count number.

- The game will go like this, starting from the first letter, you program will count and eliminate e very “nth” letter. For example, if n = 3; starting from the first letter, you will count 3 letter down the line and eliminate it. Then continue, if we reach the last letter we would continue counting from the beginning.

- Announce the last surviving letter.

An example flow of the program as follow, say if the user enter 6 letter and n = 3



ABCDEF - starting from A.

ABDEF - count 3 to C, C is eliminated. Continue from D.

ABDE - count 3 to F, F is eliminated. Continue from A. (since A is the next letter if you imagine this is a circular pattern).

ABE - count 3 to D, D is eliminated. Continue from E.

AE - count 3 to B, B is eliminated. Continue from E.

A - count 3 to E, E is eliminated. A is the last surviving letter"


Well, my program works as is intended, until there are 3 links left in the link list, or only one, at which point the program aborts. I believe I have tracked the program down to the line of code in my remove_node function "here = here->link;" When there is only one node in the link list, here->link = NULL, but I'm having trouble coming up with special case statement. I know that this error happens whenever the last link is the one that is to be removed.
Any help would be very much appreciated. This is my first post, so I'm trying to include all relevant information. Here is my code:

#include <iostream>
using namespace std;

struct Node
{
char letter;
Node* link;
};
typedef Node* NodePtr;

void insert(NodePtr& after, char some_letter);

void remove_node(NodePtr& here);

void remove_head(NodePtr& head);

void output(NodePtr head);

int num_nodes = 0;
NodePtr head = new Node;
int main()
{

char input_letter;
head->link = NULL;
cout << "Enter the first letter in your link list: " << endl;
cin >> head->letter;
NodePtr after = head;

cout << "Enter the letters in your list. Enter 0 to stop:" << endl;
do
{
cin >> input_letter;
insert(after, input_letter);
num_nodes++;
}while(input_letter != '0');

output(head);

NodePtr here = head;
remove_node(here);

while(num_nodes >= 1)
{
output(head);
remove_node(here);
}
}

void insert(NodePtr& after, char some_letter)
{
if(some_letter != '0')
{
NodePtr temp = new Node;
temp->letter = some_letter;
temp->link = NULL;
after->link = temp;
after = temp;
}
}

void remove_node(NodePtr& here)
{
here = here->link;
NodePtr discard;
if(here->link == NULL)
{
discard = head;
NodePtr temp = discard;
temp = temp->link;
head = temp;
here = temp;
}
else
{
discard = here->link;
NodePtr temp = discard;
if(temp->link == NULL)
{
here->link = discard->link;
here = head;//Should point to B
}
else
{
temp = temp->link;
here->link = discard->link;
here = temp;
}
delete discard;
num_nodes--;
}
}

void remove_head(NodePtr& head)
{
NodePtr new_head = head->link;
head = new_head;
}

void output(NodePtr head)
{
NodePtr iter;
for(iter = head; iter != NULL; iter = iter->link)
if(iter->letter != '0')
cout << (iter->letter);
cout << "\n";
}
Last edited on
Topic archived. No new replies allowed.