Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 26th, 2007, 10:42 PM   #1
jasonfrost
Newbie
 
Join Date: Aug 2007
Posts: 21
Rep Power: 0 jasonfrost is on a distinguished road
Linked lists problem

I am working on this program and as you'll see from the output, it prints out two objects. Now I am trying to include a 3rd object but I seem to be doing something wrong.

Here is the code...

#include <iostream>
using namespace std;

struct daytemps {               //struct to hold daily hi and low temperatures
        int day,hi,low,test;    //data members
        daytemps *next, *now;   //pointer to an object of struct type temp
};

void main() {
        daytemps *first, *tp;   //pointers to an object of struct type daytemps

        tp = new daytemps();    //create an unnamed object and assign the address to
                                //the pointer tp
        tp -> day = 1;          //put data in unnamed object
        tp -> hi = 98;
        tp -> low = 73;

        first = tp;             //assign the address of the unnamed object to the
                                //pointer first

        tp = new daytemps();    //create an unnamed object and assign the address to
                                //the pointer tp
        tp -> day = 2;          //put data in unnamed object
        tp -> hi = 95;
        tp -> low = 78;

        tp -> next = first;     //link the unnamed object to the object pointed to by
                                //first
        first = tp;             //assign the address of the unnamed object pointed
                                //to by tp to the pointer first; first should always
                                //point to the head of the list

        tp = new daytemps();

        tp -> day = 3;
        tp -> hi = 101;
        tp -> low = 70;

        tp -> next -> now = first;

        first = tp;

//*********output the data stored in the first element in the list*********
cout << first -> day << "---" << first -> hi << "---" << first -> low << endl;

//*********output the data stored in the second element in the list*********
cout << first -> next -> day << "---" << first -> next -> hi << "---"
     << first -> next -> low << endl;

cout << first -> next -> now -> day << "---" << first -> next -> now -> hi << "---" << first -> next -> now -> low << endl;

delete first -> next -> now;

delete first -> next;           //return the memory for the last object in the list to
                                //the free store of memory for reuse
delete first;                   //return the memory for the first object in the list to
                                //the free store of memory ro reuse
}

It compiles fine but when I run it I get a "core dump".

Any pointers you could give would be great. Thanks.
jasonfrost is offline   Reply With Quote
Old Oct 26th, 2007, 10:52 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Linked lists problem

I'm going to tell you what I tell nearly every one that comes here with a linked list problem. Get a pencil and paper. Go through your code, line by line, and construct the nodes and pointers just as you have instructed the processor to do. When you're through, "print it out" by following your instructions. Don't do it by memory or perception of what you think you've done. Do it precisely by your own written instructions.

If you don't find all your problems, post back.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 28th, 2007, 7:53 AM   #3
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: Linked lists problem

Quote:
Originally Posted by DaWei
Get a pencil and paper. Go through your code, line by line
Amen. Of course, it's helpful for many more problems than just linked list ones.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Oct 28th, 2007, 10:53 PM   #4
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 332
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
Re: Linked lists problem

Working it on paper is how i solved the process. It took me about seven pages. Draw a rectangle for each node. Divide the rectange into three parts and draw arrows connecting the appropriate parts together. go throught the process of making a new node and assigning values step by step on paper.

I have posted a working linked list example in java if you want to look it up.

By the way you used void main instead of int main. You need return 0 as well.
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Oct 29th, 2007, 2:17 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Re: Linked lists problem

I'll take pity.

        tp = new daytemps();

        tp -> day = 3;
        tp -> hi = 101;
        tp -> low = 70;

        tp -> next -> now = first;
is one cause of problem. tp->next is uninitialised. So assigning tp->next->now yields undefined behaviour.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked Lists Eric the Red C++ 6 Mar 19th, 2006 12:47 AM
Changing Array structures to Linked Lists? kalulu C 4 Nov 29th, 2005 6:33 AM
Linked List problem Berto C 3 Apr 4th, 2005 6:24 AM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM
string problem when passing in linked list quantz C++ 0 Feb 27th, 2005 10:11 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC