while loop for linked list not working

Hi guys , i am writing a project that asks us to create a linked list with numbers, print the numbers, and reverse the linked test and print the numbers again. However, my while loop does not work and it terminates the program after printing the numbers (Thus didnt execute the rest of the program). I am wondering if anyone can help me spot if something i should change in my code.

Thank you very much in advance.

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
#include <iostream>
using namespace std;

struct Node
{
	int data;
	Node *link;
};

typedef Node* Nodeptr;

void swap(Nodeptr& swapH);

int main()
{

	Nodeptr head, temp, show;
	int n, num;
	cout << "How many numbers? \n";
	cin >> n;

	cout << "Type your numbers\n";
	cin >> num;

	head = new Node;
	head->data = num;
	temp = new Node;
	head->link = temp;
	for (int i = 2; i <= n; i++)
	{
		cin >> num;
		temp->data = num;
		temp->link = new Node;
		temp = temp->link;
	}
	temp = NULL;

	show = head;
	while (show != NULL) // this loop is not working and causes the program to terminates
	{
		cout << show->data << endl;
		show = show->link;
	}

	swap(head);

	cout << "in reverse it is \n";
	show = head;
	while (show != NULL)
	{
		cout << show->data << endl;
		show = show->link;
	}
	return 0;
}
void swap(Nodeptr& swapH)
{
	Nodeptr t1, t2;
	t1 = NULL;
	t2 = NULL;
	while (swapH != NULL)
	{
		t2 = swapH;
		swapH = swapH->link;
		t2->link = t1;
		t1 = t2;
	}
	swapH = t2;
}
Last edited on
It compiles and runs fine here.
http://postimg.org/image/jc1ehqxvd/

hmm, this is the screen and error i get from my computer, not sure if it is something wrong with my computer then...
by they way i printed 1 before showing the node to see where the error is , so disregard the 1s at the beginning of the line
Yeah for some reason it's not detecting your pointer as NULL even though you set it to that value. Try nullptr instead, maybe? Don't really know what to tell you.
Topic archived. No new replies allowed.