The qingfro9 Blog.
2015년 11월 30일 월요일
코딩 공부 : stack
dkom하면서 linked list를 보는데 어찌나 햇갈리던지.. 코딩을 그동안 너무 안했나 ..?
하루하루 간단한 자료구조랑 secure coding, python 공부를 하자! 요즘 윈프공부해야하는데 ㅠㅠ 웹해킹에 빠져서 ..켘
#include
#include
#include
using namespace std; #define IS_FULL(ptr) (!(ptr)) #define IS_EMPTY(ptr) (!(ptr)) typedef struct { int key; } element; typedef struct stack* stack_ptr; typedef struct stack { element item; stack_ptr link; }; void push(stack_ptr *top, element item); element pop(stack_ptr *top); void stack_print(stack_ptr top); int main(void) { int n; element item; stack_ptr top = NULL; while (1) { printf("\n STACK (1.PUSH , 2.POP)\n"); scanf("%d\n",&n,1); switch (n) { case 1: printf("\nitem input : "); scanf("%d",&item); push(&top, item); break; case 2: printf("\n POP = %d\n", pop(&top)); break; default: exit(0); break; } stack_print(top); } return 0; } void push(stack_ptr *top, element item) { stack_ptr temp = (stack_ptr)malloc(sizeof(stack)); if(IS_FULL(temp)){ printf("\n STACK IS FULL\n"); return; } temp->item = item; temp->link = *top; *top = temp; } element pop(stack_ptr *top) { stack_ptr temp = *top; element item; item.key = 0;//초기화 안해줘서 error나서 초기화 ㅇㅇ if (IS_EMPTY(temp)) { printf("\nSTACK IS EMPTY\n"); return item; } item = temp->item; *top = temp->link; free(temp); return item; return item; } void stack_print(stack_ptr top){ printf("STACK LIST\n"); for (; top; top = top->link) printf("%4d\n", top->item); }
댓글 없음:
댓글 쓰기
최근 게시물
이전 게시물
홈
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기