LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c++ linked lists (https://www.linuxquestions.org/questions/programming-9/c-linked-lists-46431/)

jclark00001 02-20-2003 10:08 PM

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

jclark00001 02-20-2003 10:11 PM

i accidentally left an extra } or two in there from my if statements, ignore those hehe

crabboy 02-20-2003 10:47 PM

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"){ //****************

jclark00001 02-20-2003 10:57 PM

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.

GtkUser 02-21-2003 01:45 AM

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


GtkUser 02-21-2003 02:26 AM

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;
  }
}


GtkUser 02-21-2003 04:07 AM

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.

wapcaplet 02-21-2003 09:13 AM

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 :)

crabboy 02-21-2003 09:18 AM

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;


Earp 02-22-2003 09:21 AM

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.

jclark00001 02-23-2003 02:40 PM

hey thanks for the help, it took a while but i finally figured it out, i ended up taking the while approach. thanks guys


All times are GMT -5. The time now is 12:30 AM.