PDA

View Full Version : C++ and and linked lists? Help me!




DelisleBA.info
May 23, 2012, 06:12 PM
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.

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

#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

#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;
} //



lee1210
May 23, 2012, 06:23 PM
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
May 23, 2012, 06:26 PM
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.

lloyddean
May 23, 2012, 06:30 PM
Incomplete code and an incomplete request for assistance leads to ...

lee1210
May 23, 2012, 09:01 PM
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
May 24, 2012, 01:49 AM
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.

softwareguy256
May 24, 2012, 05:40 AM
stop cheating yo.

trevorde
May 24, 2012, 06:24 AM
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
May 25, 2012, 07:40 PM
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.