Stuck on a linked list

This is a discussion on Stuck on a linked list within the C++ Programming forums, part of the General Programming Boards category; Okay for an assigment I have to create a date book using a linked list, but I am having trouble ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    Stuck on a linked list

    Okay for an assigment I have to create a date book using a linked list, but I am having trouble getting the thing to link and display. My display loop is infinite for some reason and data entry fills my entire list with that one node's data. I am also supposed to be able to delete, save to a file, restore from finle and clear the list. However, the list doesn't work. Someone give me a hand please.

    Code:
    #include <iostream.h>
    #include <string.h>
    
    struct object
    {
            int date;
            int time;
            string description;
            object *next;
    };
    
    object *front;
    
    void newEntry(object *newEvent);
    void displayList();
    
    
    using namespace std;
    main()
    {
            object *event;
            object *temp;
            event=new object;
            front=0;
    
    
            int i=0;
            int j=0;
    
            while(i==0)
            {
                     newEntry(event);
    
                     if(j==2)
                     displayList();
                     j++;
             }
    }
    
    void newEntry(object *newEvent)
    {
    
                    cout<<"Date: ";
                    cin>>newEvent->date;
                    cout<<"Time: ";
                    cin>>newEvent->time;
                    cout<<"Description: ";
                    getline(cin, newEvent->description);
    
                    newEvent->next=front;
                    front=newEvent;
                    newEvent=0;
    }
    
    void displayList()
    {
    
                            object *temp;
                            temp=front;
    
    
                            while(temp != 0)
                            {
                            cout<<temp->date;
                            cout<<temp->time<<"\n";
                            cout<<temp->description<<"\n";
                            system("pause");
                            }
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> My display loop is infinite for some reason
    Your loop runs while temp != 0, but you never change temp inside the loop. You have the same problem with while(i==0) in main.

    Also note that you never actually allocate space for your nodes. You need to use new to create a node, then set its data and add it to the list.

    Finally, you should be using <iostream> and <string>, not <iostream.h> and <string.h>, and your main function should be defined as int main().

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    object *event;
    object *temp;
    event=new object;

    I allocate memory in the variable declarations. And the rest of what you posted was just style issues that didn't help at all

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    the damned assignment guidelines

    Figured I should post these before people start posting anything else

    With this assignment you will learn how to manage lists of data. You will use a linked list to organize the data.

    The assignment is to write a menu driven program that manages a date book.

    The Menu commands will be:

    A....Add a new Entry

    D....Delete an Entry

    P....Display the date book on the screen in date/time order.

    S....Save the date book to a file. //copies the entire date book contents to a file

    R....Restore a date book from a file. //loads additional data from a file

    C....Clear the date book. //gets rid of the entries

    Q...Quit

    You must provide an input file for your program containing 6 entries.
    Whenever the user tries to save the date book, the program will output the current phonebook to a file in date order. You can overwrite any previous information in the file. The name of the output file will need to be prompted for.
    You will use a struct or class for your date book entry and a linked list to organize the data in the program. Your entry will contain day, month, year, time and event information.
    When you add an entry the program will ask the user for each of these fields on a separate line and dynamically allocate memory for storage of the information.
    When you delete a field the program will ask you for the date and time of the record that should be deleted. The entry must be removed from the list and reallocated to the heap.
    When you display the date book on the screen it must be in date/time order.
    Clearing the date book will reallocate all of the memory for the entries to the heap.

    Instructions:

    Your input file must have 6 entries.
    Your files should be called datebook.cpp and your input file should be called input.txt.
    The datebook.cpp file must contain at least 8 user defined functions.
    You must use function prototypes and don't use global variables.
    Your code should be well designed, well commented and written with good style.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> I allocate memory in the variable declarations.
    Yes, but you do that once. Your loop attempts to add a new entry over and over again. So you are just overwriting the previous entry.

    >> the rest of what you posted was just style issues that didn't help at all
    You must not have understood what I was saying.

    The comments on the headers are more than just style. It is about what is correct and what is incorrect. You should change it to learn the proper way to code. Even if you don't care about that, some people copy and paste code you post into their own compilers to help you by testing it themselves. If your code is wrong and has compiler errors, then it takes extra effort to help you. Since you are the one requesting help, you should be the one to invest that extra effort, especially for something so trivial.

    My first comment about while (temp != 0) is also not a style issue. It is your problem. That's why I put it first.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    my bad I apologize about the temp loop thing (I didn't see what you were talking about). But I'm using an almost perfectly ansi correct compiler and it doesn't work without .h at the end of the library files, or with the int in front of main. Being sick and stressed is not a good combo. Sorry.

  7. #7
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    What compiler are you using? int main is needed, and string.h is depracated. Any compiler that gives errors for int main isn't really a C++ compiler.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    No worries. When you are less stressed/sick then you might want to come back to it and figure out why the headers aren't working, but it is less important than the main problem right now.

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Sorry, didn't see sick/stressed. Sorry
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    But I'm using an almost perfectly ansi correct compiler
    ...you mean that Boreland [sic] fossil?

    And this is a bit more of a style issue: system("pause") is nonstandard; it relies on the system having an appropriate PAUSE command, which it may not. You're better off with the standard, well-defined, portable cin.get().

    You need to rethink your list construction and traversal logic. Try planning it out on a piece of paper first, then fit it into abstract pseudocode, before getting into the real deal.

    I allocate memory in the variable declarations.
    But you leave a lot of garbage behind -- delete your variables after you're done with them. Since you're using structs already, you might as well go for a constructor-deconstructor pair. Right now your code is basically C masquerading as C++.
    Last edited by jafet; 11-15-2006 at 07:07 PM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    yeah I fixed that, the garbage is just remnants of stupid randomness I used to try and get it to work. but does cin.get work for strings or is it only arrays, I've only seen it used with an array. And the only reason I'm using a struct because I'm required to.

  12. #12
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I think it works for anything, sort of like cin.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Code:
    #include <iostream.h>
    #include <string.h>
    
    struct object
    {
            int date;
            int time;
            string description;
            object *next;
    };
    
    object *front;
    
    void newEntry(object *newEvent);
    void displayList();
    void insert(object *newEvent);
    
    
    using namespace std;
    main()
    {
            object *event;
    
    
            front=0;
    
    
    
            int i=0;
    
    
            while(i==0)
            {
                     event=new object;
                     newEntry(event);
                     displayList();
             }
    }
    
    void newEntry(object *newEvent)
    {
    
                    cout<<"Description: ";
                    getline(cin, newEvent->description);
                    cout<<"Date: ";
                    cin>>newEvent->date;
                    cout<<"Time: ";
                    cin>>newEvent->time;
    
                    system("pause");
                    insert(newEvent);
    }
    
    void insert(object *newEvent)
    {
            object *temp;
    
    
            if(front==0)
            {
            newEvent->next=front;
            front=newEvent;
            newEvent=0;
            }
    
            if(front->date>newEvent->date)
            {
            newEvent->next=front;
            front=newEvent;
            newEvent=0;
            }
    
            if(front->date==newEvent->date)
            {
                    if(front->time<=newEvent->time)
                    {
                    newEvent->next=front;
                    front=newEvent;
                    newEvent=0;
                    }
            }
    
            else
            {temp=front;
    while(temp->next->date>newEvent->date)
    {
    temp=temp->next;
    }
    while(temp->next->time>newEvent->time)
    {
    temp=temp->next;
    }
    newEvent->next=temp->next;
    temp->next=newEvent;
    newEvent=temp=0;
    }
    }
    
    void displayList()
    {
    
                            object *temp;
                            temp=front;
    
    
                            while(temp != 0)
                            {
                            cout<<temp->date;
                            cout<<temp->time<<"\n";
                            cout<<temp->description<<"\n";
                            temp=temp->next;
                            }
    
    }
    my ifs are not working, sorry guys, linked lists are not clicking with me
    Last edited by projektaquarius; 11-15-2006 at 10:24 PM.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You just posted code, do you have a question or problem?

    I see you fixed the while (temp != 0) loop, but not the while (i == 0) loop. Your dynamic allocation looks ok from what I can see, although you should of course be adding code later to delete the memory.

    Your insert function has a problem. If front is 0, you enter that first if statement and you make the newEvent the front. But then you don't return from the function so it continues on and tries to add the newEvent again. You will get a crash there. Return from the function or use else or else if so that the code will only go through one of those blocks of code.

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    I know this is probably annoying you but, it's due in 11 hours and i have to work tomorrow morning.

    one question though, is anything wrong with this:

    Code:
    else
    {temp=front;
    while(temp->next->date>newEvent->date)
    {
    temp=temp->next;
    }
    while(temp->next->time>newEvent->time)
    {
    temp=temp->next;
    }
    newEvent->next=temp->next;
    temp->next=newEvent;
    newEvent=temp=0;
    }
    }
    because it's crashing my computer (not just program)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21