代码之家  ›  专栏  ›  技术社区  ›  shinjuo

有关链接列表的一些帮助

  •  0
  • shinjuo  · 技术社区  · 15 年前

    好吧,我已经更新了不少代码。我遇到了一个新问题,但似乎走上了正确的道路。现在,当我输入数字时,它只是不断地吐出我输入的第一个数字,而不是移动到下一个数字。

    主.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <cType.h>
    #include "list.h"
    
    
    #define amount 3 
    
    //Prototypes
    void printList(LIST* number);
    
    int main(){
        int i;
        int* dataPtr;
        int number;
    
        LIST* numberList;
        numberList = createList();
    
        printf("Please enter %d numbers for the linked list\n", amount);
    
        for(i = 0; i < amount; i++){
              printf("#%d: ", i+1);
              scanf("%d", &number);
              dataPtr = malloc(sizeof(int));
              *dataPtr = number;
              addNode(numberList, dataPtr);
        }
        printList(numberList);
        system("PAUSE");    
        return 0;
    }
    
    void printList(LIST* number){
         int* dataPtr;
         while (!emptyList(number)){
               traverse(number,0, (void*)&dataPtr);
               printf("%d\n", *dataPtr);
               addNode(number, dataPtr);
         }
    }
    

    H表

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <cType.h>
    
    //List ADT Type Definitions
    typedef struct node{
            void* dataPtr;
            struct node* link;
            } NODE;
    
    typedef struct{
            int count;
            NODE* pos;
            NODE* head;
            NODE* rear;
            } LIST;
    //Prototype Declarations
    LIST* ceateList(void);
    
    bool traverse (LIST* pList, int fromWhere, void** dataOutPtr);
    
    int listCount (LIST* pList);
    bool emptyList (LIST* pList);
    bool fullList (LIST* pList);
    
    bool addNode (LIST* pList, void* dataInPtr);
    

    C表

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <cType.h>
    
    #include "list.h"
    
    LIST* createList(void){
          LIST* list;
    
          list = (LIST*) malloc (sizeof(list));
    
          if(list){
                   list->head = NULL;
    
                   list->rear = NULL;
                   list->count = 0;
          }
          return list;
    }
    
    bool addNode(LIST* pList, void* dataInPtr){
           NODE* pNew;
    
           if(!(pNew = (NODE*) malloc(sizeof(NODE))))
                     return false;
    
           pNew->dataPtr = dataInPtr;
           pNew->link =  NULL;
    
           if(pList->count == 0){
                   pNew->link = pList->head;
                   pList->head = pNew;
                   if(pList->count == 0)
                     pList->rear = pNew;
           }
           else{
                pNew->link = pNew;
    
                if(pNew->link == NULL)
                   pList->rear = pNew;
           }
           (pList->count)++;
           return true;
    }
    
    
    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;
    }
    
    int listCount(LIST* pList){
        return pList->count;
    }
    
    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;
              }
         }
    }
    
    3 回复  |  直到 15 年前
        1
  •  1
  •   gnud    15 年前

    当你打电话 _insert() 您尚未初始化 pPre . 在C语言中,未初始化的内存不一定是 NULL .

    另外,如果我是你,我会把 pos 来自的指针 LIST 类型-使用它会使代码非常不可重入。

        2
  •  5
  •   Tronic    15 年前
    list = (LIST*) malloc (sizeof(list));
    

    指针的大小,而不是结构的大小。

        3
  •  0
  •   pm100    15 年前

    我认为学习使用调试器是个好主意。它是任何devs技能集的基本部分。

    如果这是在使用gcc的Linux系统上(很可能),那么用-g编译代码并使用gdb运行它。如果你需要更多的指导,那么就这么说

    附言:我同意前面的海报,你不应该使用所有的大写变量名。传统上,所有的大写字母都是宏和常量