2014-04-17 38 views
0

對於類我要編寫一個程序來處理數據並將其分類到單獨的列表中。C:在刪除雙鏈表中的第一項時出現Segfault錯誤

到目前爲止,我創建了一個列表來保存數據,然後爲每個單獨的「客戶」和他們的「購買書籍」創建一個列表數組。

我在嘗試從列表中刪除第一項時收到段錯誤。

Please enter the name of your input file. 
input1.txt 
Reading in: 'input1.txt' 
5 10 1 407 2 8127 1 8131 4 22048 5 407 3 64 5 22026 2 19 1 406 3 162779 
Just created 5 lists. 
Customer ID: 1 BookID: 407 
2 8127 1 8131 4 22048 5 407 3 64 5 22026 2 19 1 406 3 162779 
Customer ID: 2 BookID: 8127 
1 8131 4 22048 5 407 3 64 5 22026 2 19 1 406 3 162779 
Customer ID: 1 BookID: 8131 
4 22048 5 407 3 64 5 22026 2 19 1 406 3 162779 
Customer ID: 4 BookID: 22048 
5 407 3 64 5 22026 2 19 1 406 3 162779 
Customer ID: 5 BookID: 407 
3 64 5 22026 2 19 1 406 3 162779 
Customer ID: 3 BookID: 64 
5 22026 2 19 1 406 3 162779 
Customer ID: 5 BookID: 22026 
2 19 1 406 3 162779 
Customer ID: 2 BookID: 19 
1 406 3 162779 
Customer ID: 1 BookID: 406 
3 162779 
Customer ID: 3 BookID: 162779 
List is empty. 

Segmentation fault (core dumped) 

這裏是我的主代碼:

// Create x amount of array with x being the first number in 'holder'. 
ListHndl list[getFirst(holder)]; 
int i; 
for (i = 1; i <= getFirst(holder); i++) { 
    list[i] = newList(); 
} 
printf("Just created %d lists.\n", getFirst(holder)); 


deleteFirst(holder); // Delete the # of customers 
deleteFirst(holder); // Delete the # of purchases 

// Iterate and process 'holder'. 
long customer, bookID; 
while (!isEmpty(holder)) { 
    customer = getFirst(holder); 
    deleteFirst(holder); 
    bookID = getFirst(holder); 
    deleteFirst(holder); 
    printf("Customer ID: %d\t BookID: %d\n",customer, bookID); 
    // insertOrder(list[customer], bookID); 
    printList(NULL, holder); 
} 

這裏的list.h的一部分:

typedef struct NodeStruct { 
     long data; 
     struct NodeStruct* next; 
     struct NodeStruct* prev; 
} NodeStruct; 

//  Rename NodeStruct* as NodePtr 
typedef NodeStruct* NodePtr; 

typedef struct ListStruct { 
     NodePtr first; 
     NodePtr last; 
     NodePtr current; 
} ListStruct; 

//  ListHndl is just a ListStruct* renamed. 

這裏的deleteFirst功能:

void deleteFirst(ListHndl L) { 
     assert (!isEmpty(L)); 
     NodePtr tmp = L->first; 
     if(L->first->next == NULL) 
      L->last = NULL; 
     else L->first->next->prev = NULL; 
     L->first = L->first->next; 
     free(tmp); 

} 

這裏的insertSort功能。

void insertOrder(ListHndl L, long data) { 

     NodePtr tmp = newNode(); 
    tmp->data = data; 

    NodePtr prev = NULL; 
    NodePtr curr = L->first; 

    while (curr != NULL && data > curr->data) { 
     prev = curr; 
     curr = curr->next; 
    } 
    if (curr == NULL) L->last = tmp; 
    if (prev == NULL) L->first = tmp; 
    else prev->next = tmp; 
    tmp->next = curr; 

} 

任何想法?謝謝。 這是什麼的valgrind顯示:

==498== Invalid read of size 8 
==498== at 0x400D41: getFirst (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store) 
==498== by 0x400AA1: main (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store) 
==498== Address 0x0 is not stack'd, malloc'd or (recently) free'd 
==498== 
==498== 
==498== Process terminating with default action of signal 11 (SIGSEGV) 
==498== Access not within mapped region at address 0x0 
==498== at 0x400D41: getFirst (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store) 
==498== by 0x400AA1: main (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store) 
==498== If you believe this happened as a result of a stack 
==498== overflow in your program's main thread (unlikely but 
==498== possible), you can try to increase the size of the 
==498== main thread stack using the --main-stacksize= flag. 
==498== The main thread stack size used in this run was 10485760. 
==498== 
==498== HEAP SUMMARY: 
==498==  in use at exit: 952 bytes in 17 blocks 
==498== total heap usage: 39 allocs, 22 frees, 1,480 bytes allocated 
==498== 
==498== 24 bytes in 1 blocks are still reachable in loss record 1 of 4 
==498== at 0x4A06A2E: malloc (in /opt/rh/devtoolset-2/root/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) 
==498== by 0x400B81: newList (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store) 
==498== by 0x400958: main (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store) 
==498== 
==498== 120 bytes in 5 blocks are still reachable in loss record 2 of 4 
==498== at 0x4A06A2E: malloc (in /opt/rh/devtoolset-2/root/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) 
==498== by 0x400B81: newList (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store) 
==498== by 0x400A34: main (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store) 
==498== 
==498== 240 bytes in 10 blocks are still reachable in loss record 3 of 4 
==498== at 0x4A06A2E: malloc (in /opt/rh/devtoolset-2/root/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) 
==498== by 0x400C0C: newNode (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store) 
==498== by 0x40111C: insertOrder (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store) 
==498== by 0x400B02: main (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store) 
==498== 
==498== 568 bytes in 1 blocks are still reachable in loss record 4 of 4 
==498== at 0x4A06A2E: malloc (in /opt/rh/devtoolset-2/root/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) 
==498== by 0x35CF8671CA: __fopen_internal (iofopen.c:76) 
==498== by 0x4008E8: openFile (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store) 
==498== by 0x40094A: main (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store) 
==498== 
==498== LEAK SUMMARY: 
==498== definitely lost: 0 bytes in 0 blocks 
==498== indirectly lost: 0 bytes in 0 blocks 
==498==  possibly lost: 0 bytes in 0 blocks 
==498== still reachable: 952 bytes in 17 blocks 
==498==   suppressed: 0 bytes in 0 blocks 
==498== 
==498== For counts of detected and suppressed errors, rerun with: -v 
==498== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6) 
make: *** [valgrind] Segmentation fault (core dumped) 

編輯:我認爲試圖刪除的第一個元素時,只有一個留在列表元素時出現的問題。

+0

它究竟在哪裏崩潰?此外,這不是所有的代碼 - 問題可能很容易在你沒有顯示的功能之一。見http://www.sscce.org/ –

+0

你的ListHndl是一個指針?它如何被傳遞給insertOrder?你可以通過寫一個打印函數來驗證至少2 -3個插入是否正確完成?問題/代碼是相當抽象的,所以很難猜測問題出在上面的代碼中。 – fayyazkl

+0

@fayyazkl,對不起。代碼已更新。 – user3288944

回答

0

當刪除含有至少2個元素的列表的第一個元素,將刪除的列表中其他支路的第一個元素:

L->first->next->prev = NULL; 
此後

,應引用NULL編指針機智的指令

L->first = L->first->next; 

相反,你應該寫

L->first = tmp->next; 

說明: 此分析假定您的鏈表執行x->next->prev == x

+0

我將它更新爲您的建議,同樣的錯誤仍然存​​在。 – user3288944