473,796 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Errors in linked list implementation

1 New Member
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 1749
sicarie
4,677 Recognized Expert Moderator Specialist
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
1451
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 | i.e. song name
3
11877
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 googled and been disappointed. TIA George
5
2328
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 up the class link and tel_list. I set up a class person with strings for name, town, and number. I just don't know how to set up the classes w/ their methods (constructors and operations). I'm stuck on the assignment operator and the add and...
12
15101
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 should be kept for references to previous element of list. Here is my solution below: struct node {
8
1697
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 insert2 there is no need to allocate to head this is the correct way. why does this happen? why do we need to have a pointer to a pointer to head in the insert function for this to work?
4
4287
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 is stored in a double linked list, the whole list is sorted for the next round. My problem appears when subjecting the program to heavy load, that is, when I run the simulation for more than 10,000 miliseconds (every event occurs in...
51
8655
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 for linked lists, I couldn't find one. I read something about Mcilroy's "Optimistic Merge Sort" and studied some implementation, but they were for arrays. Does anybody know if Mcilroys optimization is applicable to truly linked lists at all?
9
2846
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 * nPtr; //the next node's pointer }
8
2732
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 for a new list item and returns it (or NULL), and delete_item frees that memory. However, if at startup the program immediately adds a large number of items to a list, then all those calls to malloc() become expensive.
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9531
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10018
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7553
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.