![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2007
Posts: 21
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5
![]() |
Re: Linked lists problem
Quote:
__________________
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 |
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Re: Linked lists problem
I'll take pity.
tp = new daytemps();
tp -> day = 3;
tp -> hi = 101;
tp -> low = 70;
tp -> next -> now = first; |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |