473,395 Members | 1,379 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,395 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 1712
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.