2013-09-30 22 views
0

作業:編寫從keybaord讀取整數列表的算法,使用鏈表實現創建列表並打印結果。使用ADT的鏈表問題

過去幾天我一直在爲這個計劃而苦苦掙扎。我正在使用在我的書中找到的鏈接列表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: %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; 
    } 
} 
+0

如果你在addNode(intlist,array [i]);''上有錯誤,你是如何編譯的? – this

+1

歡迎來到SO。僅供參考,沒有人會坐在這裏,瀏覽你的代碼頁面。去閱讀這個http://stackoverflow.com/questions/how-to-ask和http://sscce.org/ – OldProgrammer

+0

使用DevC++讓我不用發送指針就可以使用它。我將它改爲addNode(intlist,&array [i]);但仍然會遇到同樣的問題。 – user2829351

回答

0

下面的句子對你來說似乎有點魚腥味!
scanf("Your intlist count is: %i\n ", listCount(intlist));