2017-11-23 13 views
0

나는 링크 된 목록을 사용하여 스택을 구현하는 데 지치 셨습니다. 그래서 나는 전역으로 만들었고 일부 스택 함수 (push, pop, isempty)를 만들었습니다. isempty와 push work는 훌륭하지만, 나는 pop 함수에 문제가있어, 잘 작동하지만 잘 모르겠지만 노드의 메모리를 해제하려고 할 때 (데이터를 저장 한 후에) poped가 작동하지 않고 오류가 발생합니다. 팝업 기능에서 "무료"라인을 삭제하면 멋지게 작동하지만 여기에서 문제가 있다는 것을 알고 있습니다. 사용 후 힙 메모리를 해제해야합니다 ... 그래서 어떻게해야합니까?C - 스택 구현, 무료 메모리 오류

이 코드의 일부입니다 :이 코드는 당신이 당신의 IsEmpty 함수를 (팝업 때 실수를 수정

#include <stdio.h> 
#include <stdlib.h> 


struct stack 
{ 
    int data; 
    struct stack* next; 
}; 
struct stack* top = NULL; ///stack is global, so push and pop can use it. 

int isEmpty() 
{ 
    if (top == NULL) 
     return 0; 
    else return 1; 
} 

void push(int x) 
{ 
    struct stack* temp = (struct stack*)malloc(sizeof(struct stack*)); 
    if (temp == NULL) 
    { 
     printf("Error! no allocation!!"); 
     return; 
    } 
    temp->data = x; 
    temp->next = top; 
    top = temp; 
} 
int pop() 
{ 
    struct stack* temp; 
    if (isEmpty() != 0) 
    { 
     temp = top; 
     int x = top->data; 
     top = top->next; 

     free(temp); 
     return x; 
    } 
    else 
    { 
     printf("stack is empty nothing to pop"); 
     return -1; 
    } 
} 

int main() 
{ 
    push(1); 
    push(2); 
    push(3); 
    push(4); 
    push(5); 
    push(6); 
    push(7); 

    int cur; 

    while (isEmpty()) 
    { 
     cur = pop(); 
     printf("|%d|--->", cur); 
    } 

    printf("\n"); 
    return 0; 
} 
+2

'구조체 스택 * 온도 = (구조체 스택 *) malloc을 (를 sizeof (구조체 스택 비어 때 isempty 그것이 0 (false)를 반환 논리적 아니었다 반전 명확히 *하려면)'- 스택 자체의 크기가 아닌 스택에 대한 포인터의 크기를 할당합니다. 또한 malloc의 결과를 캐스팅하는 것은 눈살을 찌푸리게합니다. –

+3

'struct stack * temp = malloc (sizeof * temp)' –

+0

"스택은 전역이므로 push와 pop은 사용할 수 있습니다." 그건 아주 잘못된 이유 다. 그러나 다시 한 번, 글로벌화 할 정당한 이유가 없을 것입니다. – bolov

답변

1

가) 역이었고, 당신은 당신이 누르면 포인터가 아니라 당신의 구조를 할당

는) 나는 그것이

#include <stdio.h> 
#include <stdlib.h> 


struct stack 
{ 
    int data; 
    struct stack* next; 
}; 
struct stack* top = NULL; ///stack is global, so push and pop can use it. 

int isEmpty() 
{ 
    return top == NULL; 
} 

void push(int x) 
{ 
    struct stack* temp = malloc(sizeof(struct stack)); 
    if (temp == NULL) 
    { 
     printf("Error! no allocation!!"); 
     return; 
    } 
    temp->data = x; 
    temp->next = top; 
    top = temp; 
} 
int pop() 
{ 
    struct stack* temp; 
    if (!isEmpty()) 
    { 
     temp = top; 
     int x = top->data; 
     top = top->next; 
     free(temp); 
     return x; 
    } 
    else 
    { 
     printf("stack is empty nothing to pop"); 
     return -1; 
    } 
} 

int main() 
{ 
    push(1); 
    push(2); 
    push(3); 
    push(4); 
    push(5); 
    push(6); 
    push(7); 

    int cur; 

    while (!isEmpty()) 
    { 
     cur = pop(); 
     printf("|%d|--->", cur); 
    } 

    printf("\n"); 
    return 0; 
} 
+0

고마워요, 메신저 C++로 사용하고 거기에 C 타입의 bool 타입이 있습니다. 어쨌든 내 코드를 바꿀 수있는 아이디어가 있습니다. 스택 세계 전역 수 없을까? 나는 여러 가지 방법을 시도했지만 allways는 .......... – 420Friendlly

+0

일반적으로 우리는 매개 변수로 스택을 제공합니다. C++ 뒤에는 스택에서 매개 변수로 주어진 것과 같은 것들이 있지만 developper 스택에서는 숨겨져 있습니다. :: pop() ==> pop (stack) – MadSquirrel