Contacts (Linked List) Can any one help

Can any one help, I dont understand what I am really doing and having trouble wrapping my head around the meaning of my class and its members and the member functions.
This is my assignment

Assignment Two - Contacts (Linked List)
You will be building a linked list. Make sure to keep track of both the head and tail nodes.
(1) Create three files to submit
• Contacts.h - Class declaration
• Contacts.cpp - Class definition
• main.cpp - main() function
(2) Build the node class for your contacts per the following specifications:
• Parameterized constructor. Parameters are name followed by phone number.
• Public member functions
o InsertAfter()
o GetName() - Accessor
o GetPhoneNumber - Accessor
o GetNext() - Accessor
o PrintContactNode()
• Private data members
o string contactName
o string contactPhoneNum
o ContactNode* nextNodePtr

Example. of PrintContactNode() output:
Name: Roxanne Hughes
Phone number: 443-555-2864


(3) In main(), prompt the user for three contacts and output the user's input. Create three ContactNodes and use the nodes to build a linked list.

Example:
Person 1
Enter name:
Roxanne Hughes
Enter phone number:
443-555-2864
You entered: Roxanne Hughes, 443-555-2864

Person 2
Enter name:
Juan Alberto Jr.
Enter phone number:
410-555-9385
You entered: Juan Alberto Jr., 410-555-9385

Person 3
Enter name:
Rachel Phillips
Enter phone number:
310-555-6610
You entered: Rachel Phillips, 310-555-6610

(4) Output the linked list.
Example:
CONTACT LIST
Name: Roxanne Hughes
Phone number: 443-555-2864

Name: Juan Alberto Jr.
Phone number: 410-555-9385

Name: Rachel Phillips
Phone number: 310-555-6610
we need three sections
contacts.h given by professor
contacts.cpp
main.cpp



contacts.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef CONTACTS_H
#define CONTACTS_H

#include <string>
using namespace std;


class ContactNode {
public:
    ContactNode();
    ContactNode(string initName, string initPhoneNum, ContactNode* nextLoc = 0);
    void InsertAfter(ContactNode* nodePtr);
    string GetName() const;
    string GetPhoneNumber() const;
    ContactNode* GetNext();
    void PrintContactNode();

private:
    string contactName;
    string contactPhoneNum;
    ContactNode* nextNodePtr;
};



what i have come up with, it tells me i need an initializer before GETNAME and GETPHONENUM??
contacts.cpp
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
 
#include<iostream>
#include <string>
#include <vector>
using namespace std;

#include "Contacts.h"

	ContactNode::ContactNode(){
	contactName = "no name";
	contactPhoneNum = "000-000-0000";
	
	
	
	}
	
    ContactNode::ContactNode(string initName, string initPhoneNum, ContactNode* nextLoc){
    	contactName  = initName;
    	contactPhoneNum = initPhoneNum;
    	this->nextNodePtr =nextLoc;
    	return;
    }
    
    void ContactNode::InsertAfter(ContactNode* nodePtr){
    	ContactNode* tmpNext=0;
    	
    	tmpNext = this -> nextNodePtr = nodePtr;
    	nodePtr -> nextNodePtr = tmpNext;
    	return;
    	
    }
    
	void ContactNode::string GetName() const;
    
	void ContactNode::string GetPhoneNumber() const;
    
	ContactNode* ContactNode::GetNext(){
		return this->nextNodePtr;
	}
    
	void ContactNode::PrintContactNode(){
		cout << "Full Name: " << this->contactName << endl << "Phone Number: " << this-> contactPhoneNum << endl; 
	return;


my main.cpp is


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
stream>
#include <vector>
#include <string>


using namespace std;
  
#include "Contacts.h"

int main(){


cout << "Please Enter the Contact information for three people:"

int numCont = 3

for (int i = 0; i < numCont; i++){

	cout << "Person " << i+1<< ":" << endl << "Enter Name: " << endl;
	cin >> 	

}
	
	
	
	
	
system ("Pause");
return 0;	
}
you swapped types on your functions.

get name and get phone are strings in the class header but void below .. string seems to be correct.

closed account (48T7M4Gy)
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
#include<iostream>
#include <string>
#include <vector>
using namespace std;
#include "Contacts.h"

ContactNode::ContactNode(){
    contactName = "no name";
    contactPhoneNum = "000-000-0000";
}

ContactNode::ContactNode(string initName, string initPhoneNum, ContactNode* nextLoc){
    contactName  = initName;
    contactPhoneNum = initPhoneNum;
    this->nextNodePtr =nextLoc;
    return;
}

void ContactNode::InsertAfter(ContactNode* nodePtr){
    ContactNode* tmpNext=0;
    
    tmpNext = this -> nextNodePtr = nodePtr;
    nodePtr -> nextNodePtr = tmpNext;
    return;
    
}

string ContactNode::GetName() const{  // <--
    return contactName;
}

string ContactNode::GetPhoneNumber() const{ // <--
    return contactPhoneNum;
}

ContactNode* ContactNode::GetNext(){
    return this->nextNodePtr;
}

void ContactNode::PrintContactNode(){
    cout << "Full Name: " << this->contactName << endl << "Phone Number: " << this-> contactPhoneNum << endl;
    return;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
#include <string>


using namespace std;

#include "Contacts.h"

int main(){
    cout << "Please Enter the Contact information for three people:\n"; //<--
    
    int numCont = 3; //<--
    
    for (int i = 0; i < numCont; i++){
        
        cout << "Person " << i+1<< ":" << endl << "Enter Name: " << endl;
        //cin >> ????
        
    }
    
    return 0;
}


Your code is OK as far as it goes. To get it to run there need to be a few changes marked with the arrows. Some needs to be completed such as the cin statement to create a new contact and therefor a new node.

Once you can create individual contacts and get them working you can then add them as nodes (pointers) to the linked list you build into the Class. You need head node class member etc as for an ordinary linked list
Last edited on
Topic archived. No new replies allowed.