2016-04-09 22 views
0

こんにちは、私はリンクリストのライブラリを作成しますが、このエラーと問題が発生します。私は何をすべきかわかりません。私は最初のtypedef struct listNodeをリストで変更し、listItemで2番目のlistNodeを置き換え、次にlinked_listsで置き換えます。listItemですべてのlistNodeが正常に動作しますが、リストの初期化関数を実装する必要があります。リストと項目数の頭を格納する別の構造体を作ってください。このエラーが発生した時点で私はこの時点で到着しました。 linked_list.clinked_lists.clinked_lists.h構造体データ型のリンクリストの問題

linked_lists.hコード:

#ifndef linked_lists_h 
#define linked_lists_h 

#include <stdio.h> 

struct listNode; 

typedef struct listNode{ 

    int value; 
    struct list *next; 
}listNode; 

struct listaa{ 

    int count; 
    listNode *head; 
}; 


void deleteFirst(listNode *head); 
void display(listNode *head); 
void addInFront (listNode *head, int value); 
void addLast (listNode *head, int value); 
void deleteLast(listNode *head); 
void add_at_poz(listNode *head, int value, int poz); 
void insert_at_poz(listNode *head, int value, int poz); 
void delete_at_poz(listNode *head, int poz); 
void max(listNode *head); 
void min(listNode *head); 

#endif /* linked_lists_h */ 

linked_lists.cコード:私はここを参照してください

#include "linked_lists.h" 
#include "stdlib.h" 

// Single Linked Lists 

void display(listNode *head) { // Display a linked list. 
    if(head->next != NULL){ 
     listNode *current; 
     current = head; 
     while (current->next != NULL) { 
      current = current->next; 
      printf("%d, ", current->value); 
     } 
    } else { 
     printf("Lista este goala"); 
    } 
    printf("\n"); 
} 

//Adding value functions 

// Adding in front of the list 
void addInFront(listNode *head, int value) { 
    listNode *new_item; 
    new_item = (listNode *) malloc(sizeof(listNode)); 
    new_item->next = head->next; 
    new_item->value = value; 
    head->next = new_item; 

    printf("Valoare %d a fost adaugata.\n", value); 
} 

// Adding at the end of the list 
void addLast(listNode *head, int value) { 
    listNode *new_item,*current = head; 
    new_item = (listNode *) malloc(sizeof(listNode)); 
    while(current->next != NULL) 
     current = current->next; 
    new_item->value = value; 
    new_item->next = NULL; 
    current->next = new_item; 

} 


// Adding a new item at specified pozition 
void add_at_poz(listNode *head, int value, int poz) { 

    poz = poz - 1; 
    int iter = 0; 
    listNode *current = head, *new_item; 

    while(iter < poz) { 
     current = current->next; 
     iter++; 
    } 

    new_item = (listNode *)malloc(sizeof(listNode)); 
    new_item = current->next; 
    current->next = new_item; 
    new_item->value = value; 
} 

// Insert a new item at specified pozition 
void insert_at_poz(listNode *head, int value, int poz) { 

    poz = poz - 1; 
    int iter = 0; 
    listNode *current = head, *new_item; 

    while(iter < poz) { 
     current = current->next; 
     iter++; 
    } 

    new_item = (listNode *)malloc(sizeof(listNode)); 
    new_item->next = current->next; 
    current->next = new_item; 
    new_item->value = value; 
} 

// Remove items from list 

// Remove first item 
void deleteFirst(listNode *head) { 
    listNode *deletedItem; 
    deletedItem = head->next; 
    printf("Elementul %d a fost sters din fata.\n", deletedItem->value); 
    head->next = deletedItem->next; 
    free(deletedItem); 
} 

// Delete last item 
void deleteLast(listNode *head) { 

    listNode *deletedItem, *current; 

    current = head; 
    while(current->next->next != NULL) 
     current = current->next; 

    deletedItem = current->next; 
    printf("Ultimul elementul %d a fost sters\n",deletedItem->value); 

    current->next = NULL; 
    free(deletedItem); 
} 

void delete_at_poz(listNode *head,int poz) { 

    int iter = 0; 
    listNode *deletedItem, *current = head; 
    poz = poz - 1; 

    while(iter < poz) { 
     current = current->next; 
     iter++; 
    } 

    deletedItem = current->next; 
    current->next = deletedItem->next; 
    printf("Elementul de pe pozitia %d cu valoare %d a fost sters. \n", poz+1,deletedItem->value); 
    free(deletedItem); 
} 

void max(listNode *head) { 
    listNode *current; 
    int max = head->next->value; 

    current = head; 
    while (current->next != NULL) { 
     current=current->next; 
     if(current->value > max) 
      max = current->value; 

    } 
    printf("Maximul este %d.\n", max); 
} 

void min(listNode *head) { 
    listNode *current; 
    int min = head->next->value; 

    current = head; 
    while (current->next != NULL) { 
     current=current->next; 
     if(current->value < min) 
      min = current->value; 

    } 
    printf("Minimul este %d.\n", min); 
} 
+2

'struct list'はどこに定義されていますか?あなたは 'struct listaa'を持っていますが、' struct list'はありません。 – kaylum

答えて

1

投稿されたコードにはいくつかの問題があります。

#ifndef linked_lists_h 
#define linked_lists_h 

#include <stdio.h> 

struct listNode; 

typedef struct listNode{ 

    int value; 
    struct list *next; 
}listNode; 

struct listaa{ 

    int count; 
    listNode *head; 
}; 


void deleteFirst(listNode *head); 
void display(listNode *head); 
void addInFront (listNode *head, int value); 
void addLast (listNode *head, int value); 
void deleteLast(listNode *head); 
void add_at_poz(listNode *head, int value, int poz); 
void insert_at_poz(listNode *head, int value, int poz); 
void delete_at_poz(listNode *head, int poz); 
void max(listNode *head); 
void min(listNode *head); 

#endif /* linked_lists_h */ 

1)の2行は、ヘッダファイルに完全に不要であり、除去されるべきである:

#include <stdio.h> 

struct listNode; 

2)のtypedefが誤って宣言される:

typedef struct listNode{ 

    int value; 
    struct list *next; <<- no 'list' struct exists 
}listNode; 
ヘッダファイルで開始することができ

勧め:

struct listNode 
{ 
    int value; 
    struct listNode *next; <<- do NOT use the 'typedef' name here 
}; 

typedef struct listNode ListNode; 

3)この宣言は「不完全」構造体の定義が含まれています

struct listaa{ 

    int count; 
    listNode *head; <<- incomplete struct 
}; 

前構造体の定義から「typedefの」名前を使用して提案します。関数内

(名前は単なる総額以上に異なっている必要があります理由のもう一つの優れた例)

struct listaa{ 

    int count; 
    ListNode *head; 
}; 

4)パラメータリストには、すべてが「不完全な」構造体の参照が含まれているプロトタイプ:

void deleteFirst(listNode *head); 
void display(listNode *head); 
void addInFront (listNode *head, int value); 
void addLast (listNode *head, int value); 
void deleteLast(listNode *head); 
void add_at_poz(listNode *head, int value, int poz); 
void insert_at_poz(listNode *head, int value, int poz); 
void delete_at_poz(listNode *head, int poz); 
void max(listNode *head); 
void min(listNode *head); 

プロトタイプは 'struct listNode'またはtypedef 'ListNode'のどちらかを使用する必要があります

void deleteFirst (ListNode *head); 
void display  (ListNode *head); 
void addInFront (ListNode *head, int value); 
void addLast  (ListNode *head, int value); 
void deleteLast (ListNode *head); 
void add_at_poz (ListNode *head, int value, int poz); 
void insert_at_poz(ListNode *head, int value, int poz); 
void delete_at_poz(ListNode *head, int poz); 
void max   (ListNode *head); 
void min   (ListNode *head); 

垂直アラインメントで得られる可読性の向上に注目してください。

もちろん、すべての実際の関数のシグネチャは、次に「stdio.hのから何を使用していない、あなたのヘッダファイルを、「typedefは」名前ではなく、「不完全な」構造体定義

を使用するには、この同じ補正を必要とします'ヘッダファイルはそのファイルを#includeするべきではありません。むしろ#include <stdio.h>ステートメントをコードの本文に置きます。

注: '#include'ステートメントを書くとき。

#include "linked_lists.h" <<- notice the double quotes 

大きな違いは見つけるために、コンパイラが使用する検索順序です: `の#include文はフォーマットを使用する「ホームが成長」の書き込み時に

#include <stdio.h> <<- notice the `<` and `>` 

:システムヘッダファイルの場合は、フォーマットを使用します実際のヘッダーファイルこれにより、システムヘッダファイルを家庭用ヘッダファイルに置き換えることが可能になります。検索速度がやや速くなり、「自己文書化」コードの作成に役立ちます。

上記の問題が修正されると、コードはきちんとコンパイルされます

+0

ありがとう –

1

主な問題は、それがstruct listNode *nextする必要があり、あなたの最初のtypedef内部struct list *next;です。私は、Cによって提供される一般的なキーワードlistを認識していません。

関連する問題