Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DelisleBA.info

macrumors member
Original poster
Apr 12, 2011
38
0
Athens, OH
My homework is to use a linked list for tracking friends b-days. I am having problems when inserting a new friend. It must be done in order using there b-days. When I call my function I prompt for data and insert the it using.

Code:
temp->link = NULL; // temp node with new data.	 	
temp->link = afterme->link; // afterme current afterme->link = next node
afterme->link = temp;

-----------FULL CODE BELOW--------------

FILENAME = inter.h Interface file

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>  // exit

using namespace std;

struct FriendData // Defition of structor.
{
	string	friendname;
	string	phonenumber;
	int		bday;
	int		bmonth;
	
	FriendData* link;	// link points to next FriendData.
};

class List
{
public:
	
	typedef FriendData* nodeptr; // 
	
	List(); // Default Constuctor.
	// Sets head = NULL;
	
void insert(); // Insert a new friend.
	// Prompt user for name and if not already on list, insert
	// new friend into list by date of birth.
	
	void sortbymonth(); // Search for all birthday by month.
	// Prompt for a month entered as a int 1...12 then show
	// all friends with a birhtday that month.
	
	void output(ostream& outf); // Write list back to file.
	// Clean data and write it back to file,
	
private:
	
	nodeptr head;
	
}; // End Class

// NON-MEMBER

int menu();

// Implementation filename = implem.cc

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>  // exit

#include "inter.h"

using namespace std;

void List::insert() // Insert a new friend function.
{
	nodeptr templist, temp, afterme;
	templist = head;
	
	temp = new FriendData;
	
	afterme = head;
	
	string name = "";
	string phone = "123-123-1234";
	int bday = 0;
	int bmonth = 0;
	int dontdo = 1; // Used as control stop when name already on list
					// or when new name already inserted.
	
	cout << "\n";
	cout << "ENTER NEW FRIEND NAME: ";
	cin.ignore();
	getline(cin,name);

cout << "\n";
		cout << "ENTER NEW FRIEND PHONE NUMBER: ";
		cin.ignore();
		getline(cin,phone);
		
		cout << "\n";
		cout << "ENTER NEW FRIEND BIRTH DAY: ";
		cin >> bday;
		
		cout << "\n";
		cout << "ENTER NEW FRIEND BIRTH MONTH: ";
		cin >> bmonth;

temp->friendname = name;
		temp->phonenumber = phone;
		temp->bday = bday;
		temp->bmonth = bmonth;

templist = head; // Reset list.
		
		if (bmonth < templist->bmonth)
		{temp->link = NULL;			
			temp->link = afterme->link;
			afterme->link = temp;
			
			cout << "\n";
			cout << "NEW Friend TEST = \"" << name << "\" ADDED.";
			cout << "\n";
			
			cout << "Press ENTER to continue.";
			cin.ignore(1);
			cin.get();
			cout << "\n\n"; // Wait for user to press enter.
			dontdo = 0;
		} //
 
Last edited by a moderator:

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
There were no question marks in your post. What problem are you having? What have you tried? What were the results? What results were you expecting? How can we help?

-Lee
 

DelisleBA.info

macrumors member
Original poster
Apr 12, 2011
38
0
Athens, OH
More info. ???

The new data gets posted after instead of before. ie

The first friend bday is 2/2 when I enter a new friend with a bday of 1/1 it gets inserted after 2/2 but it should go in front.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
This code looks like it will always put the new node in the second position. Doing your I/O in the insert method is questionable, it might be better to pass in the names and birthday, or a full node. You may also want to divide your FriendData structure into a struct or object Friend with the actual data and a different struct or object that holds an instance of a Friend and a node pointer.
You'll need to loop to find where to insert your new node. With a singly linked list you'll need to look ahead at the next node, checking if the new node's birthdate is between the current's and its next node's. If so, switch the current node's pointer to the new node, and the new node's pointer to current's next node. There are corner cases to handle like an empty list, a new node belonging at the start or end, equivalent dates, etc.

-Lee
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
With a single-linked list, you cannot easily link in behind the location you are looking at unless you retain the previous location as you move along. When you compare and find out your value is less than this one, you have no way to move backwards without holding onto the entry you just left. You could double-link the list so that backtracking is possible (can be useful in other ways as well), but then you have to make absolute certain your link pairs are properly maintained or your list could develop weird branching effects that seem to make no sense.
 

trevorde

macrumors newbie
Mar 16, 2011
29
0
My homework is to use a linked list for tracking friends b-days. I am having problems when inserting a new friend. It must be done in order using there b-days. When I call my function I prompt for data and insert the it using.

If we help you, will we get credited?
 

DelisleBA.info

macrumors member
Original poster
Apr 12, 2011
38
0
Athens, OH
Thanks for the help for those that actually help.

I made some progress using a two pointers.
One called trailing, set to head and the other called current pointing to head->link.

Thanks again.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.