Linkedlist

Guys if you plz help me i'm stuck here ....
This is linked list and when i try to add second time to list it throws exception ..........

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  // LinkeListDetailed.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>


using namespace std;

struct Contact{

	Contact *next;
	string name;
	string phone;

};


ostream& operator <<(ostream &os,const Contact &c){

	os << "Name :" << c.name << "and Contact is :" << c.phone << endl;
	return os;
}

Contact* insert(Contact* head, Contact *last){
	Contact *newNode;
	Contact *hd = head;

	string name;
	string number;
	cout << "Enter Name :";
	cin >> name;
	cout << endl;
	cout << "Enter Number :";
	cin >> number;
	cout << endl;

	newNode = new Contact;
	newNode->name = name;
	newNode->phone = number;
	newNode->next = NULL;

	
	if (head == NULL)
	{
		head = newNode;
		last = newNode;

	}
	else
	{
		last->next = newNode;
		last = newNode;
		
	}
	hd = head;
	return hd;
}

void print_List(Contact *node){
	
	while (node != NULL)
	{
		cout << *node;
		node = node->next;
	}

}


char Menu(){

	char choice;
	cout << "1:- Enter Data :";
	cout << endl;
	cout << "2:-Show Data :";
	cout << endl;
	cout << "3:-)Exit :";
	cout << endl;
	cin >> choice;
	return choice;
}

int main(int argc, char* argv[])
{
	Contact *head = NULL;
	Contact *last = NULL;
	char choice;

	do
	{
		choice = Menu();
		switch (choice)
		{
		case '1':
			

			head = insert(head, last);
			break;
		case '2':
			print_List(head);
			break;
		case '3':
			break;

		default:
			break;
		}
		

	} while (choice != '3');

	system("pause");
	return 0;
}

The parameters to insert() are both by value. Main updates its head with the return value of insert, but nothing changes the last variable of main.
should i pass by reference???do you mean??
Thanks keskiverto ,you realy helped me solving my problem ....
i have a question if you plz tell me how to make my code exception safe i'll be realy very thankfull to you...


Any suggestions to make this code even better....???plz tell me must...
must me nothing


If a function has to modify a variable of the caller, then it has three options: return value, reference parameter, and a pointer. Reference is a feasible option here.

You do have other issues too.
* You main() keeps track of both the head and tail of your list. It would be better, if you had a genuine list type that tracks the details.
* Your insert() function does two tasks: ask for input and insert node. Follow the mantra: one function, one task.
* You don't deallocate memory appropriately.
* stdafx.h and system() have standard languages alternatives.
Thanks very much bro....
Topic archived. No new replies allowed.