2014-03-31 3 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; 
} 

링크 된 목록을 만들고 링크 된 목록의 크기를 계산하여 출력하고 싶습니다. 나는 크기를 가져 와서 목록을 인쇄 할 수 있지만 결국에는 내 프로그램이 디버그 오류를 보여 주며 변수 'll'주위의 런타임 검사 실패 # 2 - 스택이 손상되었다고 표시합니다. 이런 일이 왜 일어 났는지 알 수 있습니까?LinkedList 스택이 손상됨

답변

1

올바르지 않은 것 중 하나는 main() 기능입니다.

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

temp 어떻게 당신이 그것을 LinkedList에게 할당 할 수 있습니다하는 ListNode입니까? 마찬가지로, 그 라인으로, 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