0

割り当て: キーボードから整数のリストを読み取り、リンクされたリストの実装を使用してそれらのリストを作成し、結果を出力するアルゴリズムを作成します。

私はこのプログラムでここ数日苦労してきました。私は自分の本にある Linked List ADT を使用しているので、リンク リストの作成とリンク リストへの追加を間違えているに違いありません。

問題

1.整数の2回目の入力後に終了します

すべての問題はメインで見つかると思います

どんな助けでも大歓迎です!ここで私を傷つけているのは、ポインターに関する私の限られた知識だけだと思います。

.

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

//List Type Definitions
typedef struct node{
    void* dataPtr;
    struct node* link;
}NODE;

typedef struct{
    int count;
    NODE* pos;
    NODE* head;
    NODE* rear;
    int (*compare) (void* argu1, void* argu2);
}LIST;

//Prototype Declarations
LIST* createList(int (*compare) (void* argul,void* argu2));
LIST* destroyList (LIST* list);
int addNode(LIST* pList, void* dataInPtr);
bool removeNode(LIST* pList, void* pArgu, void** pDataOut);
bool searchList (LIST* pList, void* pArgu, void** pDataOut);
static bool retrieveNode (LIST* pList, void* pArgu, void** dataOutPtr);
bool traverse (LIST* pList, int fromWhere, void** dataPtrOut);
int listCount (LIST* pList);
bool emptyList (LIST* pList);
bool fullList (LIST* pList);
static bool _insert (LIST* pList, NODE* pPre, void* dataInPtr);
static void _delete (LIST* pList, NODE* pPre, NODE* pLoc, void** dataOutPtr);
static bool _search (LIST* pList, NODE** pPre, NODE** pLoc, void* pArgu);
//End of List ADT definitions

//--------------------------------------------------------------
int main(int argc, char **argv)
{
    LIST* intlist;
    int *intpointer;
    intlist = createList(&intpointer);

    int usersize, i;
    printf("How many number do you want in your linked list: ");
    scanf("%d",&usersize);
    int array[usersize];


    for(i = 0; i < usersize; i++){
        scanf("%d", &array[i]);
        addNode(intlist, array[i]);
        scanf("Your intlist count is: %i\n ", listCount(intlist));
    }

    for(i = 0; i < usersize; i++){
        printf("Your Number %i\n", array[i]);
    }
}

//--------------------------------------------------------------
LIST* createList(int (*compare) (void* argu1,void* argu2)){
    LIST* list;

    list = (LIST*) malloc (sizeof (LIST));
    if(list){
        list->head = NULL;
        list->pos = NULL;
        list->rear = NULL;
        list->count = 0;
        list->compare = compare;
    }

    return list;
}
//--------------------------------------------------------------
LIST* destroyList (LIST* pList){
    NODE* deletePtr;

    if(pList){
        while(pList->count > 0){
            free (pList->head->dataPtr);

            deletePtr = pList->head;
            pList->head = pList->head->link;
            pList->count--;
            free(deletePtr);
        }
        free(pList);
    }
    return NULL;
}
//--------------------------------------------------------------
int addNode(LIST* pList, void* dataInPtr){
    bool found;
    bool success;

    NODE* pPre;
    NODE* pLoc;

    found = _search (pList, &pPre, &pLoc, dataInPtr);
    if (found){
        return(+1);
    }

    success = _insert (pList, pPre, dataInPtr);
    if(!success){
        return (-1);
    }
    return(0);
}
//--------------------------------------------------------------
bool removeNode(LIST* pList, void* keyPtr, void** dataOutPtr){
    bool found;

    NODE* pPre;
    NODE* pLoc;

    found = _search (pList, &pPre, &pLoc, keyPtr);
    if(found){
        _delete (pList, pPre, pLoc, dataOutPtr);
    }

    return found;
}
//--------------------------------------------------------------
bool searchList(LIST* pList, void* pArgu, void** pDataOut){
    bool found;

    NODE* pPre;
    NODE* pLoc;

    found = _search (pList, &pPre, &pLoc, pArgu);
    if(found){
        *pDataOut = pLoc->dataPtr;
    }else{
        *pDataOut = NULL;
    }
    return found;
}
//--------------------------------------------------------------
static bool retrieveNode (LIST* pList, void* pArgu, void** dataOutPtr){
    bool found;

    NODE* pPre;
    NODE* pLoc;

    found = _search(pList, &pPre, &pLoc, pArgu);
    if(found){
        *dataOutPtr = pLoc->dataPtr;
        return true;
    }

    *dataOutPtr = NULL;
    return false;
}
//--------------------------------------------------------------
bool traverse (LIST* pList, int fromWhere, void** dataPtrOut){
        if(pList->count == 0){
            return false;
        }
    if(fromWhere == 0){
        pList->pos = pList->head;
        *dataPtrOut = pList->pos->dataPtr;
        return true;
    }else{
        if(pList->pos->link == NULL){
            return false;
        }else{
            pList->pos = pList->pos->link;
            *dataPtrOut = pList->pos->dataPtr;      
            return true;
        }
    }
}
//--------------------------------------------------------------
int listCount(LIST* pList){
    return pList->count;
}
//--------------------------------------------------------------
bool emptyList(LIST* pList){
    return(pList-> count == 0);
}
//--------------------------------------------------------------
bool fullList(LIST* pList){
    NODE* temp;

    if((temp = (NODE*)malloc(sizeof(*(pList->head))))){
        free(temp);
        return false;
    }
    return true;
}
//--------------------------------------------------------------
static bool _insert (LIST* pList, NODE* pPre, void* dataInPtr){
    NODE* pNew;

    if(!(pNew = (NODE*) malloc(sizeof(NODE)))){ 
        return false;
    }

    pNew->dataPtr = dataInPtr;
    pNew->link = NULL;

    if(pPre == NULL){
        pNew->link = pList->head;
        pList->head = pNew;
        if (pList->count == 0){
            pList->rear = pNew;
        }
    }else{
        pNew->link = pPre->link;
        pPre->link = pNew;

        if(pNew->link == NULL){
            pList->rear = pNew;
        }
    }

    (pList->count)++;
    return true;
}
//--------------------------------------------------------------
void _delete(LIST* pList, NODE* pPre, NODE* pLoc, void** dataOutPtr){
    *dataOutPtr = pLoc->dataPtr;
    if(pPre == NULL){
        pList->head = pLoc->link;
    }else{
        pPre->link = pLoc->link;
    }

    if(pLoc->link == NULL){
        pList->rear = pPre;
    }

    (pList->count)--;
    free(pLoc);

    return;
}
//--------------------------------------------------------------
bool _search (LIST* pList, NODE** pPre, NODE** pLoc, void* pArgu){
    #define COMPARE ( ((* pList->compare) (pArgu, (*pLoc)->dataPtr)) )
    #define COMPARE_LAST ((*pList->compare) (pArgu, pList->rear->dataPtr))

    int result;

    *pPre = NULL;
    *pLoc = pList->head;
    if (pList->count == 0){
        return false;
    }

    if (COMPARE_LAST > 0){
        *pPre = pList->rear;
        *pLoc = NULL;
        return false;
    }

    while ( (result = COMPARE) > 0){
        *pPre = *pLoc;
        *pLoc = (*pLoc)->link;
    }

    if(result == 0){
        return true;
    }else{
        return false;
    }
}
4

1 に答える 1

0

以下の行は怪しいと思われますか...!
scanf("Your intlist count is: %i\n ", listCount(intlist));

于 2013-10-14T16:46:01.320 に答える