C语言 使用 ADT 的链表问题



赋值:编写一个算法,从keybaord读取整数列表,使用链表实现创建整数列表,并打印结果。

在过去的几天里,我一直在为这个程序而苦苦挣扎。我正在使用我书中的链表ADT,所以我一定搞砸了链表的创建并添加到链表。

问题

1.It 在我的整数的第二次输入后退出

我相信所有问题都可以在主要中找到

任何帮助将不胜感激!我认为只是我对指针的有限了解在这里伤害了我。

.

#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: %in ", listCount(intlist));
    }
    for(i = 0; i < usersize; i++){
        printf("Your Number %in", 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;
    }
}

下面的行对你来说是否看起来很可疑...!
scanf("Your intlist count is: %in ", listCount(intlist));

相关内容

  • 没有找到相关文章

最新更新