1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include <stdio.h> #include <stdbool.h> struct _stack { int data; struct _stack * next; }typedef Stack; Stack* push(Stack* stack,int data) { Stack* newstack; newstack = (Stack*)malloc(sizeof(Stack*)); newstack->data = data; newstack->next = stack; return newstack; } bool isfull(Stack* stack) { if (stack == NULL) { return 0; } return 1; } void print(Stack* stack) { Stack* curr = stack; printf("현재스택 : "); while (curr != NULL) { printf("%d ", curr->data); curr = curr->next; } printf("\n"); } Stack* pop(Stack* stack) { if (isfull(stack)) { printf("삭제된 데이터 : %d\n", stack->data); stack=stack->next; return stack; } printf("스택이 비어있음\n"); } int main() { Stack* stack =NULL; Stack* curr; int data; stack = pop(stack); stack = push(NULL, 3); stack = push(stack, 5); stack = push(stack, 10); print(stack); stack = pop(stack); print(stack); } | cs |
pop에서 free를 해주고싶은데 할필요가없음?
push에 sizeof stack*말고 sizeof stack이고 pop에서 free 해줘야하고 isfull보단 isnotempty가 좀 더 어울리는 이름같네여 - 164260의 휘발성 계정
print내부에서 curr새로 생성안하고 stack변경해도 콜 바이 벨류로 받아온거라 상관없음 - 164260의 휘발성 계정
stdbool있으면 true false 있을텐데 그거로 써주는게 좀 더 이쁠듯 - 164260의 휘발성 계정
여ㅑ러가지 찍느라 curr 이랑 data는안지웟넹 쩃든감사요