Thread: I need Help with Double linked list

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    33

    I need Help with Double linked list

    Code:
    i have a problem trying to fix the following errors with dll.h . I need assistance.
    
    dll.h:46: two or more data types in declaration of `dllInit'
    dll.h:46: two or more data types in declaration of `dllInit'
    dll.h:46: warning: duplicate `static'
    dll.h:48: warning: parameter has incomplete type
    dll.h:52: warning: parameter has incomplete type
    dll.h:56: parse error before "dllForIter"
    dll.h:58: parse error before "f"
    dll.h:60: parse error before "a"
    dll.h:63: parse error before "dllRevIter"
    dll.h:65: parse error before "f"
    dll.h:67: parse error before "a"
    dll.c: In function `NodeInit':
    dll.c:5: invalid type argument of `->'
    dll.c:6: invalid type argument of `->'
    dll.c:7: invalid type argument of `->'
    
    my code is as shown below, dll.h dll.c
    
    #include <stdio.h>
    #include <stdlib.h>
    //dll.h         
    //The following two lines (and the last) prevent class dll
    //from being defined more than once even if the file is 
    //included more than once
    #ifndef dll_h
    #define dll_h
    //The following is the defirnation of the struct
    //and the methods are defines in matching .c file
    
    struct node
    {
       void *v;
       int *head;
       int *tail;
    };
    
    typedef struct node node;
    
    typedef struct dll{
      int sorted;
      struct node *head;
      struct node *tail;
      int size;
      int (*compare)(void*, int, void*, int);
    }dll;
    
    /* st */
    static struct dllForIter{
    
      struct node *temp; 
    };
    
    
    /* current node */
    //static node *cur=NULL;
    
    /* Reverse Iterator */
    static struct dllRevIter{
    
      struct node *temp;
    };
    
    
    void NodeInit(node n, void *v);
    
    void dllInit(dll *i, int (*compare)(void*, int, void*, int), int a);
     
    int dllAdd(dll d,void v, int b);
    
    int dllSize(dll s);
    
    int dllHas(dll h,void v,int l);
    
    void dllFinalize(dll v);
     
    void dllForInit(dll f,dllForIter i);
     
    void dllForHasNext(dllForIter f);
     
    void dllForNext(dllForIter a,int s); 
    
     
    void dllRevInit(dll r,dllRevIter i);
     
    void dllRevHasNext(dllRevIter  f);
     
    void dllRevNext(dllRevIter a,int s); 
    
    
    #endif
    
    
    
    
    
    #include "dll.h"
    void NodeInit(struct node n, void *v){
      n->num=v;
      n->tail=0;
      n->head=0;
    }
    void dllInit( dll *d, int (*compare)(void*, int, void*, int),int i){
      d->compare=compare;
      d->sorted=i;
      d->size=0;
    
      struct node *head=malloc(sizeof(struct node));
      char *a=malloc(4);
      a="head";
      NodeInit(head,a);
      head->head=1;
      d->head=head;
    
      struct node *tail=malloc(sizeof(struct Node));
      char *b=malloc(4);
      b="tail";
      NodeInit(tail, b);
      tail->tail=1;
      d->tail=tail;
    
      d->head->next=d->tail;
      d->tail->prev=d->head;
    }
    
    void dllForInit(struct dll *dl, struct dllForIter *dfi){
      dfi->cur=dl->head;
    }
    
    void dllRevInit(struct dll *dl, struct dllRevIter *dri){
      dri->cur=dl->tail;
    }
    
    void dllAdd(struct dll *dl, void *v, int s){
      where->size++;
      struct node *cur;
      current=(dl->head->next);
      struct node *newNode=malloc(sizeof(struct node));
      NodeInit(newNode,v);
      while(!cur->tail){
        if(!dl->sorted)
          cur=cur->next;
        else 
    if(dl->compare(cur->num,sizeof(cur->payload),newNode->num,sizeof(newNode->num))<0)
          cur=cur->next;
        else
          break;
      }
      newNode->next=cur;
      newNode->prev=cur->prev;
      cur->prev->next=newNode;
      cur->prev=newNode;
    }
    int dllSize(struct dll *dl){
      return where->size;
    }
    int dllHas(struct dll *dl, void *v , int s){
      struct node *cur=dl->head->next;
      
    while(dl->compare(current->num,sizeof(current->num),v,s)!=0){
        if(cur->tail)
          return 0;
        cur=cur->next;
      }
      return 1;
    }
    void dllFinalize(struct dll *dl){
    free(dl);
    }
    
    
    int dllForHasNext(struct dllForIter *dfi){
      return(!dfi->cur->next->tail);
    }
    void* dllForNext(struct dllForIter *dfi, int *s){
      dfi->cur=dfi->cur->next;
      *s=sizeof(dfi->cur->num);
      return dfi->cur->num;
    }
    
    int dllRevHasNext(struct dllRevIter *dri){
      return(!dri->cur->prev->head);
    }
    void* dllRevNext(struct dllRevIter *dri, int *s){
      dri->cur=dri->cur->prev;
      *s=sizeof(where->cur->num);
      return dri->cur->num;
    }
    
    void printdll(struct dll *dl){
      struct node *cur=this->head;
    
      while(cur->tail==0){
        printf("%s, ",cur->num);
        cur=cur->next;
      }
      printf("%s\n",cur->num);
    }
    
    int compare( void* p, int m, void* q, int n){
    return  strncmp((char*)p, (char*)q, n);
    
    }
    
    
    int main(){
    int i=2;
    dll m;
    dllInit(&m, compare, 0);
    dllAdd(&m, "a", 2);
    dllRevIter r;
    dllRevInit(&m, &r);
    dllRevNext(&r,&i);
    dllRevNext(&r,&i);
    printf("%i",r.current->head);
    }
    Last edited by axe; 03-11-2006 at 04:20 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Some of your problems can be traced to this:
    Code:
    static struct dllForIter{
    
      struct node *temp; 
    }
    You have no trailing semicolon, which means you had either planned on declaring an instance of this structure right then and forgot, or you like to abuse the static keyword, and just happened to forget the semicolon.

    Other than that, your indentation sucks. Learn to indent, and then maybe someone will be willing to help more than a quick glance.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int compare( void* p, int m, void* q, int n){
    return  strncmp((char*)p, (char*)q, n);
    You're missing a closing brace (}).

    Quzah, BTW: "alway" -> "always".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    33
    Code:
    hey,
    
    I have tried to fix the code but i still get erros from the static fuctions, any idea?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First, learn to indent. Then, post your code. Next, preview your post to make sure it looks good. Finally, press the submit button.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by axe
    Code:
      char *a=malloc(4);
      a="head";
    That does not do what you think.
    First of all you cannot copy an array of characters using the equals sign. all that does is change the pointer value of a to point to a string in the data section of the executable. You would have to use strcpy to do what you want.
    Secondly, you are only allocating 4 bytes, but in order to copy "head" into a, you would have to allocate 5 bytes, because you need to copy the null character as well.
    Lastly, the above code is a memory leak because the allocated memory's pointer is overwritten.

    That only covers barely 3% of your bugs. Clearly you are overly ambitious in attempting to write this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked List Problem
    By Shiggins in forum C++ Programming
    Replies: 4
    Last Post: 03-10-2009, 07:15 AM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM