473,246 Members | 3,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

Errors in linked list implementation

1
Hello all!

I am a completly newbie at C and I dont have good basis from Java so I cant get the linked lists part from C. I am trying to run the code from the function that is written at the end of this message but it has run-time errors so its hard to find the bugs. I really know that my logic is not so good since I cant understand that bit so well. I am using dm compiler. Could somebody possibly tell me what my mistakes are.

Thank you very much,

Vanessa.
Expand|Select|Wrap|Line Numbers
  1. /**********header file******/
  2.  
  3. /* Header files. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <time.h>
  8.  
  9. /* Constants. */
  10. #define TITLE_LEN 40
  11. #define AUTHOR_LEN 15
  12. #define REPORT_FILE "report.txt"
  13. #define RESTOCK_LEVEL 5
  14. #define SIZE 100
  15.  
  16.  
  17. typedef struct saleStruct {
  18.    int year, month, day;
  19.    int quantity;
  20.    float price;
  21.    struct saleStruct* next;
  22. } SaleType;
  23.  
  24. typedef struct bookStruct {
  25.    int id;
  26.    char title[TITLE_LEN + 1];
  27.    char author[AUTHOR_LEN + 1];
  28.    int year;
  29.    float price;
  30.    int count;
  31.    int saleCount;
  32.    SaleType* head;
  33.    struct bookStruct* next;
  34. } BookType;
  35.  
  36. typedef struct bookListStruct {
  37.    BookType* head;
  38.    int count;
  39. } BookListType;
  40.  
  41. /*******function to load data***************/
  42.  
  43. void loadData(BookListType* booklist, char* booksFile, char* salesFile) {
  44.  
  45.     FILE *bp,*sp;
  46.     char line[SIZE],*date,*title,*author,*result=NULL,*result2=NULL,delims[]=":";
  47.     float bprice,sprice;
  48.     int i,id,year,count,y,m,d,quantity;
  49.  
  50.     BookType *newBook,*preB,*currB,*current;
  51.     SaleType *newSale,*currSale;
  52.  
  53.     if((bp=fopen(booksFile,"r"))==NULL){
  54.         printf("Error openning %s\n - Exiting...",booksFile);
  55.         exit(0);
  56.  
  57.     }
  58.         while((fgets(line, SIZE-1, bp))!= NULL ) {
  59.  
  60.                 line[strlen(line) - 1] = '\0';
  61.             //    printf("Strlen(line):%d\n",strlen(line));
  62.             //    printf("%s\n",line);
  63.  
  64.                 result=strtok(line,delims);
  65.                 id = atoi(result);
  66.                 result=strtok(NULL,delims);
  67.                 title = result;
  68.                 result=strtok(NULL,delims);
  69.                 author = result;
  70.                 result=strtok(NULL,delims);
  71.                 year = atoi(result);
  72.                 result=strtok(NULL,delims);
  73.                 bprice = atof(result);
  74.                 result=strtok(NULL,delims);
  75.                 count= atoi(result);
  76.                 result=strtok(NULL,delims);
  77.  
  78.             //    printf("%d\t %s\t%s",id,title,author);
  79.             //    printf("%d %f %d\n",year,bprice,count);
  80.  
  81.  
  82.                 if((newBook=malloc(sizeof(BookType)))==NULL) {
  83.                  printf("malloc unsuccesfull for BookType - Exiting\n");
  84.                  exit(0);
  85.                 }
  86.  
  87.  
  88.                 newBook->id = id;
  89.                 strcpy(newBook->title, title);
  90.                 strcpy(newBook->author, author);
  91.                 newBook->year=year;
  92.                 newBook->price=bprice;
  93.                 newBook->count=count;
  94.  
  95.             /*    newBook->next=booklist->head;
  96.                 booklist->head=newBook;
  97.                 booklist->count+=1;*/
  98.  
  99.  
  100.                 preB=NULL;
  101.                 currB=booklist->head;
  102.                  while(currB != NULL) {
  103.                  preB = currB;
  104.                  currB = currB->next;
  105.                  }
  106.                  if(preB == NULL) {
  107.                   booklist->head = newBook;
  108.                  } 
  109.                  else {
  110.                  preB->next = newBook;
  111.                  }
  112.  
  113.         }
  114.     fclose(bp);
  115.  
  116.     if((sp=fopen(salesFile,"r"))==NULL){
  117.         printf("Error openning %s\n - Exiting...",salesFile);
  118.         exit(0);
  119.     }
  120.  
  121.     while((fgets(line,SIZE-1,sp))!=NULL){
  122.  
  123.                 line[strlen(line) - 1] = '\0';
  124.  
  125.                 result=strtok(line,delims);
  126.                 id = atoi(result);
  127.                 result=strtok(NULL,delims);
  128.                 date=result;
  129.                 result=strtok(NULL,delims);
  130.                 sprice = atof(result);
  131.                 result=strtok(NULL,delims);
  132.                 quantity =atoi(result);
  133.                 result=strtok(NULL,delims);
  134.  
  135.                 result2=strtok(date,"/");
  136.                 d=atoi(result2);
  137.                 result2=strtok(NULL,"/");
  138.                 m=atoi(result2);
  139.                 result2=strtok(NULL,"/");
  140.                 y=atoi(result2);
  141.                 result2=strtok(NULL,"/");
  142.  
  143.  
  144.             //    printf("id: %d\nd: %d\nm: %d\ny: %d\nprice: %f\nquantity: %d",id,d,m,y,sprice,quantity);
  145.  
  146.  
  147.             if((newSale=malloc(sizeof(SaleType)))==NULL) {
  148.              printf("malloc unsuccesfull for SaleType - Exiting\n");
  149.               exit(0);
  150.             }
  151.  
  152.  
  153.                 current=booklist->head;
  154.  
  155.             //    while(current!=NULL){        
  156.  
  157.                  if(current->id==id){
  158.                     newSale->year=y;
  159.                     newSale->month=m;
  160.                     newSale->day=d;
  161.                     newSale->price=sprice;
  162.                     newSale->quantity=quantity;
  163.  
  164.                     newSale->next=currB->head;
  165.                     current->head=newSale;
  166.                     printf("Year of sale %d\tId  %d\t%s\n",newBook->head->year,id,newBook->title);
  167.                     current->saleCount+=1;
  168.                     booklist->count+=1;
  169.                     printf("Sale count %d\n",current->saleCount);
  170.  
  171.                 }
  172.                 current=current->next;
  173.                 //}
  174.  
  175.  
  176.      }fclose(sp);
  177.  
  178. }
Aug 24 '07 #1
1 1705
sicarie
4,677 Expert Mod 4TB
I'm betting your compiler already told you where the mistakes were. The output is cryptic, but usually useful. Can you post the compiler's error messages?

PS - Please use code tags in the future - especially on large blocks of code like that.

Thanks!
Aug 24 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Tim Henderson | last post by:
Hi i have some errors in my code that i can't find the reason for here is the error i have 3 classes: -Song -Album -Artist Song has 3 pieces of data with getter and setter meathods: -Name...
3
by: George M Jempty | last post by:
Is there a good linked list implementation in the public domain? Or that someone on this group has already written that they'd like to put in the public domain, right here on this newsgroup? I've...
5
by: Darryl B | last post by:
I can not get anywhere on this project I'm tryin to do. I'm not expecting any major help with this but any would be appreciated. The assignment is attached. The problem I'm having is trying to set...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
8
by: farhanb | last post by:
hello, I am writing a simple linked list implementation. When I use function insert1 I must allocate to head in my main to get it to work otherwise list stays empty but when I use function...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
9
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node *...
8
by: Jeff Bown | last post by:
Consider implementing a (doubly) linked list. The simplest strategy is to provide functions item_t add_item(item_t predecessor); void delete_item(item_t item); where add_item allocates memory...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.