LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-20-2003, 11:08 PM   #1
jclark00001
Member
 
Registered: Jan 2003
Distribution: RH 8.0 with KDE
Posts: 59

Rep: Reputation: 15
c++ linked lists


ok im working on this project with linked lists for my C++ class and the point of the program is to load data from a text document and create a linked list from that data which resembles the train. The linked list is a chain of Node objects (a class i defined which contains a pointer to the next node in the linked list and a car class (which contains a type of data and the amount of data numbers)). Heres my dillemma: my schools CS website appears to be down so i cant access any of the examples or anything, im supposed to have data input from another text document so that the train stops at various "stations" and buys and sells some of its stuff. im starting on the Sell function and so far all im getting is a seg fault error. i have a good idea what to do next, but i want the seg fault to go away before i start doing anymore things. heres my code so far for the sell function:

void Train::SellItems(std::string typeitem, int numitems){
Node *prev, *cur;

prev=front;
cur=prev->link;

for (int i = 0; i < size-1; i++){
while (numitems!=0){
if (cur->car.GetType() == "typeitem"){ //****************

if (typeitem=="grain"){

}

//more empty if statements are here...
}
prev=prev->link;
cur=cur->link;
}
}
}

the error occurs where i have the commented *******, can anyone tell me why i cant do that? (GetType() is a public member function) thanks
 
Old 02-20-2003, 11:11 PM   #2
jclark00001
Member
 
Registered: Jan 2003
Distribution: RH 8.0 with KDE
Posts: 59

Original Poster
Rep: Reputation: 15
i accidentally left an extra } or two in there from my if statements, ignore those hehe
 
Old 02-20-2003, 11:47 PM   #3
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
It's hard to tell based on the few lines posted, but I do see one possible problem.

When you execute this statement
Code:
prev=front;
cur=prev->link;
I'm assuming that prev becomes the first node in the link and cur becomes the second. What if there is no second??? You should check to see if the link is null before trying access data at the next node, like below.
Code:
if (cur->car.GetType() == "typeitem"){ //****************
 
Old 02-20-2003, 11:57 PM   #4
jclark00001
Member
 
Registered: Jan 2003
Distribution: RH 8.0 with KDE
Posts: 59

Original Poster
Rep: Reputation: 15
hmm ok, i think ill try to get through the list using a for loop, ill just have to rethink my logic a little. the way i had it before had too much annoying stuff.
 
Old 02-21-2003, 02:45 AM   #5
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
It's been a while since I constructed a list. I just remember how to do it in C, and not C++ because I'm not familar with C++ file I/O, especially fsteams. Maybe this will help in some way.

Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct Record {
  int num;
  char name[120];
};

typedef struct myList {
  struct Record node_rec;
  struct myList *next;
}Node_t;

struct Record GetRecord(char buffer[]) {
  struct Record rec;
  sscanf(buffer,"%d%s",&rec.num, rec.name);
  return rec;
}
Node_t * AddNode(Node_t *headp, struct Record rec) {
  Node_t *newp = malloc (sizeof (Node_t));
  newp->next = NULL;
  newp->node_rec.num = rec.num;
  strcpy (newp->node_rec.name, rec.name);
  newp->next = headp;
  headp = newp;
  return headp;
}

Node_t * BuildList(Node_t *headp, FILE *fptr) {
  char buffer[256];
  struct Record rec;

  while ( fgets(buffer,256,fptr) != NULL ) {
    rec = GetRecord(buffer);
    headp = AddNode(headp, rec);
  }
  fclose(fptr);
  return headp;
}
void DisplayList(Node_t *headp) {
  while (headp) {
    printf("%d %s\n", headp->node_rec.num, headp->node_rec.name);
    headp = headp->next;
  }
}

int main() {
  Node_t *list = NULL;

  FILE *fptr = fopen("myfile.txt","r");
  if(fptr == NULL) exit(1);

  list = BuildList(list, fptr);
  DisplayList(list);

  return 0;
}
Here is the sample file: myfile.txt
Code:
234 name1
444 name2
555 name3
223 name4
115 name5
 
Old 02-21-2003, 03:26 AM   #6
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
And ofcourse I forgot to free the list memory

Code:
void FreeList(Node_t *headp) {
  Node_t *freep = headp;

  while (headp) {
    headp = headp->next;
    free(freep);
    freep = headp;
  }
}

Last edited by GtkUser; 02-21-2003 at 03:27 AM.
 
Old 02-21-2003, 05:07 AM   #7
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
I remember once that I had made a Standard C++ class with a private linked list. The self referential structure was a member of the implementation and the list methods contained the behavior for adding, sorting, and deleting nodes. I have a final exam next wednesday otherwise I'd try to figure out how I made that class. It was cool.
 
Old 02-21-2003, 10:13 AM   #8
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
With pointers, always initialize to NULL when you create them:

MyClass * foo = NULL;

And *always* check pointers to see if they are null before you do any kind of class reference with them. Anytime you do:

foo->bar()

do:

if (foo != NULL)

first. It will save you many headaches

Last edited by wapcaplet; 02-21-2003 at 10:15 AM.
 
Old 02-21-2003, 10:18 AM   #9
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Your shouldnt use for loops to loop over linked lists. You need a while loop as gtk uses:
Code:
  while (headp) {
    printf("%d %s\n", headp->node_rec.num, headp->node_rec.name);
    headp = headp->next;
 
Old 02-22-2003, 10:21 AM   #10
Earp
LQ Newbie
 
Registered: Jan 2003
Location: Rapid City, SD USA
Distribution: Soon to be Gentoo
Posts: 15

Rep: Reputation: 0
Try and post a better discription of the problem, along with struct/class definitions. It's kinda hard to help when you've only posted a small piece of the program.

Also, try and use some kind of common indentation scheme like gtkuser uses, it's sooooo much easier to read.

Last edited by Earp; 02-22-2003 at 10:22 AM.
 
Old 02-23-2003, 03:40 PM   #11
jclark00001
Member
 
Registered: Jan 2003
Distribution: RH 8.0 with KDE
Posts: 59

Original Poster
Rep: Reputation: 15
hey thanks for the help, it took a while but i finally figured it out, i ended up taking the while approach. thanks guys
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked Lists and Queues Kroenecker Programming 2 04-09-2005 02:59 AM
Linked Lists leonidg Programming 7 03-10-2005 03:07 AM
queue and linked lists Palamides Programming 2 03-09-2005 09:08 PM
Linked Lists - What and Why? scuzzman Programming 9 12-31-2004 11:51 AM
c++ doubly linked lists durden2.0 Programming 4 02-25-2004 06:56 PM


All times are GMT -5. The time now is 06:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration