C++ and and linked lists? Help me!

Discussion in 'Mac Programming' started by DelisleBA.info, May 23, 2012.

  1. DelisleBA.info, May 23, 2012
    Last edited by a moderator: May 23, 2012

    macrumors member

    Joined:
    Apr 12, 2011
    Location:
    Athens, OH
    #1
    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;
    		} //
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. thread starter macrumors member

    Joined:
    Apr 12, 2011
    Location:
    Athens, OH
    #3
    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.
     
  4. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #4
    Incomplete code and an incomplete request for assistance leads to ...
     
  5. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    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
     
  6. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    #6
    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.
     
  7. macrumors regular

    Joined:
    Jun 5, 2010
    #7
    stop cheating yo.
     
  8. macrumors newbie

    Joined:
    Mar 16, 2011
    #8
    If we help you, will we get credited?
     
  9. thread starter macrumors member

    Joined:
    Apr 12, 2011
    Location:
    Athens, OH
    #9
    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.
     

Share This Page