Help with linked list program?

My program needs to be able to read in a file that looks like this:

1
2
3
4
5
2345 Bradley 777-7777 M 197100
3456 Kimberly 161-6161 F 197300
4567 Melanie 121-2121 F 197500
5678 Matthew 222-2222 M 197700
6789 Jonathan 444-4444 M 197900


and perform operations on it, such as adding a member, removing a member, displaying a single member and displaying all members. It's menu-driven. I also know I need to incorporate a search function but I'm not quite sure how.

If anyone could show me the adjustments I need to make to my code so far (to at least make the add, display and input functions work) I would really appreciate it. First time working with linked lists.

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
120
121
122
123
#include <iostream>
#include <fstream>
using namespace std;

struct NodeType
	{
		long Id_Num;
		string Name;
		string Phone;
		char Status;
		float Income;
		NodeType* next;
	};
/////////////////////////////////////
class linklist
{
private: 	
	NodeType* first;
	void searchList();
public:
	linklist()
	{first = NULL;}
	void Input(long Id, string N, string P, char S, float In);
	void addMember(long Id, string N, string P, char S, float In);
	void removeMember();
	void displayMember();
	void displayAll();
};
////////////////////////////////////////
void linklist::Input(long Id, string N, string P, char S, float In) //input file
{

NodeType* nextlink = new NodeType;

nextlink->Id_Num = Id;
nextlink->Name = N;
nextlink->Phone = P;
nextlink->Status = S;
nextlink->Income = In;

nextlink->next=first;

first = nextlink;

ifstream infile("Member.txt");
 
infile >> Id >> N >> P >> S >> In;

}

void linklist::addMember(long Id, string N, string P, char S, float In)
	{
		cout << "New member ID: "; cin >> Id;
		cout << "New member name: "; cin >> N;
		cout << "New member phone number: "; >> P;
		cout << "New member status: "; >> S;
		cout << "New member income: "; >> In;
		
		NodeType* newlink = new NodeType;
		newlink->Id_Num = Id;
		newlink->Name = N;
		newlink->Phone = P;
		newlink->Status = S;
		newlink->Income = In;
		newlink->next = first;
		first = newlink;
	}

////////////////////////////////////////
void linklist::displayAll()
{
	NodeType* current = first;
	while(current != NULL)
	{
		cout <<current->Id_Num;
		cout << current->Name;
		cout << current->Phone;		
		cout << current->Status;
		cout << current->Income;
		current = current-> next;
	}
}
////////////////////////////////////////
int main()
{
	void addMember(long Id, string N, string P, char S, float In);

	cout << "This program allows the user to add or remove members from a club register.\n"
	<<"The user may also display individual members or the entire list.\n\n";

	linklist li;
	char ch;

	//li.Input(long Id, string N, string P, char S, float In);
	//Main menu:

	do{
		cout<<"\n\n1. Add a member\n2. Remove a member \n3. Display a member \n4. Display  all members \n5. Quit";
		cout<<"\nEnter your choice:\t";
		cin>>ch;	//Stores menu choice

	}while(ch != 5);

	if(ch == 1)
		{
			
		}
		else if (ch == 2)
		{

		}
		else if (ch == 3)
		{

		}
		else if (ch == 4)
		{
			li.displayAll();
		}
	
	
	return 0;
}
hi,pastamaker,
i read a little bit , i have a good idea!
This is my turn over your code:
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
 
#include <iostream>
#include <fstream>
using namespace std;

struct NodeType
	{
		long Id_Num;
		string Name;
		string Phone;
		char Status;
		float Income;
		NodeType* next;
	};
/////////////////////////////////////
class linklist
{
private: 	
	NodeType* head;
	void searchList();
public:
	linklist()
	{head = NULL;}
	NodeType *InputList();
	NodeType *AddList(NodeType *head);
	NodeType *RemoveMember(NodeType *head);
	void displayMember(NodeType *head);
	void displayAll(NodeType *head);
};
int count = 0;
////////////////////////////////////////
NodeType *linklist::InputList() //input file
{
char s[50];
NodeType* head,*link,*list;
head =NULL;
ifstream ifile;
ifile.open("Member.txt",ios::in)
link = list = new NodeType;
ifile.getline(s,50);
while(ifile>>link->IdNum>>link->Name>>link->Phone>>link->Status>>link->Income)
{

if(count++ ==1)
head = link;
else
{
list->next = link;
list = link;
}
link = new NodeType;
}
list->next = 0;
delete link;
return head;
}

NodeType *linklist::AddList(NodeType *head)
	{
                NodeType *add,*app;
                add = head;
                while(add->next !=0)
               {add=add->next}
                app = new NodeType;
		cout << "New member ID: "; cin >> app->IdNum;
		cout << "New member name: "; cin >> app->Name;
		cout << "New member phone number: "; >>app->Phone;
		cout << "New member status: "; >>app->Status;
		cout << "New member income: "; >> app->Income;
		add->next=app;
                app->next=0;
                ofstream ofile;
                ofile.open("Member.txt",ios::app)
                ofile<<app->IdNum<<app->Name<<app->Phone<<app->Status<<app->Income<<"\n";
                ofile.close();
		return add;
	}

////////////////////////////////////////
void linklist::displayAll(NodeType *head)
{
	NodeType* current = head;
	while(current != NULL)
	{
		cout <<current->Id_Num;
		cout << current->Name;
		cout << current->Phone;		
		cout << current->Status;
		cout << current->Income;
		current = current-> next;
	}
}
NodeType *linklist::RemoveMember(NodeType *head,int Num)

{
NodeType *p,*q;
cout<<"请输入一个删除的信息:\n";
cin>> Num;
p =head;
while(Num!=p->IdNum&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(Num==p->IdNum)
{
if(p==head)
head=p->next;
else
q->next=p->next;
delete p;
count--;
}
ofstream ofile;
ofile.open("Member.txt",ios::trunc)
p=NULL;
p=head;
while(head!=NULL)
{
ofile<<head->IdNum<<head->Name<<head->Phone<<head->Status<<head->Income<<"\n";
head = head->next;
}
return p;
}
void linklist::displayMember(NodeType *head)
{
 void searchList(NodeType *head)
{
NodeType *p,*q;
p=head;
cout<< "输入查询的号:"<<endl;
cin>>q->IdNum;
while(p!=0)
{
if(q->IdNum == p->IdNum)
cout<< q->IdNum<<q->Name<<q->Phone<<q->Status<<q->Income<<"\n"; 
p=p->next;
}
}
searchList(head);
}
////////////////////////////////////////
int main()
{

        NodeType *head;
        head=InputList();
	cout << "This program allows the user to add or remove members from a club register.\n"
	<<"The user may also display individual members or the entire list.\n\n";

	linklist li;
	char ch;

	//li.Input(long Id, string N, string P, char S, float In);
	//Main menu:

	do{
		cout<<"\n\n1. Add a member\n2. Remove a member \n3. Display a member \n4. Display  all members \n5. Quit";
		cout<<"\nEnter your choice:\t";
		cin>>ch;	//Stores menu choice

	}while(ch != 5);

	if(ch == 1)
		{
			head=li.AddList(head);
		}
		else if (ch == 2)
		{
                       int ID = 0;
                       cout<< "输入要删除的ID";
                       cin>>ID;
                       head=li.RemoveMember(head,ID);
		}
		else if (ch == 3)
		{
                      li.displayMember(head);
		}
		else if (ch == 4)
		{
			li.displayAll(head);
		}
	
	
	return 0;
}
Last edited on
Can not thank you enough for your help! I can see where to go from here. Thanks for taking the time to help me.
I'm sorry not to give your help, I am from China, my English sucks, some details can not express, but I will give you maximum effort to help.
You reply, let me know my way some bold, I would change.
Topic archived. No new replies allowed.