2014-03-31 152 views
0
#define _CRT_SECURE_NO_WARNINGS 

#include <stdio.h> 
#include <stdlib.h> 
typedef struct listnode{ 
    int item; 
    struct listnode *next; 
}ListNode; 

typedef struct _linkedlist{ 
    ListNode *head; 
    int size; 
} LinkedList; 

void printList(LinkedList *ll); 
int sizeList(LinkedList *ll); 
int insertSorted(LinkedList *ll, int value); 
int removeDuplicates(LinkedList *ll); 

int main() 
{ 
    int choice, i = 0; 
    ListNode *temp=NULL; 
    LinkedList *ll=NULL; 

    printf("1. create LinkedList\n2. insertSorted\n3. removeDuplicates\nChoose an option: "); 
    scanf("%d", &choice); 
    switch (choice) 
    { 
    case 1: 
     printf("Enter a list of numbers, terminated by the value -1: "); 
     scanf(" %d", &i); 
     while (i != -1){ 
      if (ll == NULL) 
      { 
       ll = malloc(sizeof(LinkedList)); 
       temp = ll; 
      } 
      else 
      { 
       temp->next = malloc(sizeof(ListNode)); 
       temp = temp->next; 

      } 
      temp->item = i; 
      scanf(" %d", &i); 
     } 
     temp->next = NULL; 
     printList(&ll); 
     printf("Size of linked is %d", sizeList(&ll)); 
     break; 
    case 2: 

    default: 
     break; 
    } 
} 

void printList(LinkedList *ll) 
{ 
    ListNode *temp = ll->head; 
    if (temp == NULL) 
     return; 
    while (temp!=NULL) 
    { 
     printf("%d ", temp->item); 
     temp = temp->next; 
    } 
    printf("\n"); 
} 

int sizeList(LinkedList *ll) 
{ 
    int size=0; 

    ListNode *temp = ll->head; 
    if (temp == NULL) 
     return 0; 

    while (temp != NULL) 
    { 
     size++; 
     ll->size = size; 
     temp = temp->next; 
    } 
    return ll->size; 
} 

我想創建一個鏈表並計算鏈表的大小並輸出它。我設法得到大小並打印出列表,但最後,我的程序顯示一個調試錯誤,並指出運行時檢查失敗#2 - 圍繞變量'll'的堆棧已損壞。我可以知道爲什麼會發生這種情況?LinkedList堆棧損壞

回答

1

其中一個不正確的地方是你的main()函數。

if (ll == NULL) 
{ 
    ll = malloc(sizeof(LinkedList)); 
    temp = ll; // <- This is incorrect!! 
} 
else 
{ 
    temp->next = malloc(sizeof(ListNode)); 
    temp = temp->next; 
} 

tempListNode,你怎麼能爲它分配一個LinkedList?類似地,與該行,既templl指向同一存儲器和上一個操作將覆蓋另一個。

這也許應該是這樣的:

if (ll == NULL) 
{ 
    ll = malloc(sizeof(LinkedList)); 
    temp = malloc(sizeof(ListNode)); 
    temp->next = NULL; 
    ll->head = temp; 
    ll->size = 1; 
} 
else 
{ 
    temp->next = malloc(sizeof(ListNode)); 
    temp = temp->next; 
} 

但是,這太不一樣好,因爲它可以。我寧願:

int main() 
{ 
    int choice, i = 0; 
    ListNode *temp=NULL; 
    LinkedList ll = { NULL, 0 }; 

    printf("1. create LinkedList\n2. insertSorted\n3. removeDuplicates\nChoose an option: "); 
    scanf("%d", &choice); 
    switch (choice) 
    { 
    case 1: 
     printf("Enter a list of numbers, terminated by the value -1: "); 
     scanf(" %d", &i); 
     while (i != -1){ 
      if (ll.head == NULL) 
      { 
       temp = malloc(sizeof(ListNode)); 
       ll.head = temp; 
      } 
      else 
      { 
       temp->next = malloc(sizeof(ListNode)); 
       temp = temp->next; 
      } 
      temp->item = i; 
      scanf(" %d", &i); 
     } 
     temp->next = NULL; // <- Please take a second look at this line. What happens if your first entry is -1? 
     printList(&ll); // <- This too... 
     printf("Size of linked is %d", sizeList(&ll)); // <- and this as well... 
     break; 
    case 2: 

    default: 
     break; 
    } 
} 

這是因爲沒有需要動態分配LinkedList。只要程序正在運行,它就是活着的,所以沒有必要把它放在堆上。

還有其他問題,您的代碼,我會建議橡膠迴避你的代碼和強烈建議閱讀this link on debugging