2016-03-29 17 views
0

Yeni başlayan bir sorunum var, ama bu şaşırtıcı bir şey. Ben basit bir bağlantılı liste oluşturmak için. Kavramı biliyorum, bu yüzden görev için standart bir kod yazdım. Bu kodu ana dosyanın en üstüne eklediğimde iyi derler, ancak bunları kendi c-File'ına (struct ve method signatures ile h-File ile) ayırdım ve buna ek olarak, birkaç hata alıyorum. Soruna geri dönersek, yapının aniden tanımsız olduğu. Ben olsun derleme hatalardan biri şudur:Basit liste. Ana dosyadaki konuşmalar (yöntemler, yapı), sperate dosyalarına (h ve c) uymaz. Niye ya?

slist.h:10: error: unnamed struct/union that defines no instances 

Not Satır 10 vurdu (ı sağlamak Fisrt kod örneğinde hat 4) sonudur.

typedef struct slist{ 
    struct slist *nextElement; 
    int value; 
}slist; 

/* 
* Generate a list element. Used in append, initialize, insertAtIndex and prepend. 
*/ 
slist* generateListElement(int value){ 
    slist* a=malloc(sizeof(slist)); 
    if(a==NULL){ 
     printf("Couldn't list element!\n"); 
     exit(EXIT_FAILURE); 
    } 
    a->value=value; 
    a->nextElement=NULL; 
    return a; 
} 

/* 
* Intizialise the list. 
*/ 
slist* initialize(int firstData){ 
    slist* a=generateListElement(1); 
    slist* b=generateListElement(firstData); 
    a->nextElement=b; 
    return a; 
} 

/* 
* Returns 1 if slist is empty 0 otherwise 
*/ 
int isEmpty(slist* list){ 
    return !(list->value>0); 
} 

/* 
* print the list to the console 
*/ 
void printList(slist* list){ 
    if(isEmpty(list)){ 
     printf("List is empty.\n"); 
    }else{ 
     int i=1; 
     list=list->nextElement; 
     while(list->nextElement!=NULL){ 
      printf("%d: %d\n", i, list->value); 
     } 
    } 
    return; 
} 

/* 
* Append a element to the end of the list 
*/ 
void append(slist* list,int value){ 
    slist* z=generateListElement(value); 
    while(list->nextElement!=NULL){ 
     list=list->nextElement; 
    } 
    list->nextElement=z; 
    return; 
} 

/* 
* Prepend a element to the start of the list 
*/ 
void prepend(slist* list,int value){ 
    slist* oldFirst=list->nextElement; 
    list->value++; 
    slist* a=generateListElement(value); 
    a->nextElement=oldFirst; 
    list->nextElement=a; 
    return; 
} 

/* 
* Returns pointer to value of element at given index, 0 if list empty or index out of range 
*/ 
int elementAtIndex(slist* list, int index){ 
    index=abs(index); 
    if(list->value==0 || list->value>=index){ 
     return 0; 
    } 
    for(int i=0; i<index && list->nextElement!=NULL; i++){ 
     list=list->nextElement; 
    } 
    return list->value; 
} 

/* 
* Insert element at given index, generate elements with value 0 if needed 
*/ 
void insertAtIndex(slist* list, int index, int value){ 
    index=abs(index); 
    slist* a=generateListElement(value); 
    if(list->value>index){ 
     list->value++; 
    }else{ 
     list->value+=1+(index-list->value); 
    } 
    for(int j=0; j<index; j++){ 
     if(list->nextElement==NULL){ 
      slist* insertElement=generateListElement(0); 
      list=insertElement; 
     }else{ 
      list=list->nextElement; 
     } 
    } 
    slist* nextToA=list->nextElement; 
    list->nextElement=a; 
    a->nextElement=nextToA; 
    return; 
} 

/* 
* Remove Element at given index, returns 1 in case of success 0 otherwise (list empty, index out of range etc.) 
*/ 
int removeElementAtIndex(slist* list,int index){ 
    index=abs(index); 
    if(index>list->value){ 
     return 0; 
    } 
    slist* a=list; 
    slist* b=list->nextElement; 
    for(int i=0; i<index; i++){ 
     a=a->nextElement; 
     b=b->nextElement; 
    } 
    slist* c=b; 
    b=b->nextElement; 
    a->nextElement=b; 
    free(c); 
    return 1; 
} 

/* 
* Deletes the entire list. 
*/ 
void deleteList(slist* list){ 
    slist* a=list; 
    slist* b=list->nextElement; 
    int index=a->value; 
    free(a); 
    for(int i=0; i<index; i++){ 
     a=b; 
     b=b->nextElement; 
     free(a); 
    } 
    return; 
} 

Sadece ders önlemek için: (Bu main.c ince uyar ama buna slist.c/h konur yoksa dediğim gibi) İşte benim kodudur sperate dahil olmak üzere yaklaşık dosyalarım şuna benzer: slist.h:

#ifndef slist 
#define slist 

/* 
* Starting point for slist is the next element the first element counts the elements in the list 
*/ 
typedef struct slist{ 
    struct slist* nextElement; 
    int value; 
}slist; 

/* 
* Generate a list element. Used in append, initialize, insertAtIndex and prepend 
*/ 
slist* generateListElement(int value); 

//... 

#endif 

slist.c:

#include <stdlib.h> 
#include <stdio.h> 
#include "slist.h" 

/* 
* Generate a list element. Used in append, initialize, insertAtIndex and prepend. 
*/ 
slist* generateListElement(int value){ 
//... 

ve main.c:

#include <stdio.h> 
#include <stdlib.h> 

#include "stack.h" 

int main(void) { //... 
boş jetonu başlık dosyasına slist her oluşumunu değiştirmek için ön işlemci söyler
+0

Söylenenin ne yaptığını biliyor musun? – immibis

cevap

0
#define slist 

. Özellikle, typedef ön işleme sonra olur:

typedef struct { 
    struct * nextElement; 
    int value; 
}; 

Aynı anahtar kelimenin çok aşırı kullanımları olduğu genellikle iyi bir fikir değil. Üstbilgi dosyasını, içerme muhafızı için farklı bir #define belirteci kullanacak şekilde değiştirmenizi öneririz. Yaygın bir sözleşme, başlangıç ​​dosya adını kullanmaktır. Örneğin:

#ifndef SLIST_H 
#define SLIST_H 

... 

#endif