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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #include<stdio.h> #include<stdlib.h> typedef struct Node { int data; Node* next; }Node; typedef struct stack { int count; Node* top; }stack; //연결리스트 스택 push 에서는 count를 생각하지 않아도 된다. void push(stack* st, int data) { Node* New = (Node*)malloc(sizeof(Node)); New->data = data; New->next = st->top; st->top = New; st->count++; } //연결리스트 스택 pop 에서는 스택이 비어있는 경우(count==0), 그 외로 나누어서 풀면 된다. void pop(stack* st) { Node* New = (Node*)malloc(sizeof(Node)); if (st->count == 0) { printf("STACK IS EMPTY \n"); return; } New = st->top; st->top = st->top->next; printf("%d IS POPPED\n", New->data); st->count--; free(New); } void print(stack* st) { Node* New = (Node*)malloc(sizeof(Node)); for (New = st->top; New != NULL;) { printf("%d -> ", New->data); New = New->next; } printf("NULL\n"); } int main(void) { stack st; st.top = NULL; st.count = 0; int ch = -1; while (ch != 0) { printf("\n\n"); printf("하고싶은 명령을 입력하세요:\n"); printf("0번:종료 1번: push 2번: pop 3번:print \n"); scanf("%d", &ch); switch (ch) { case 1: int d; printf("삽입할 데이터를 입력하세요\n"); scanf("%d", &d); push(&st, d); break; case 2: pop(&st); break; case 3: print(&st); break; case 4: break; case 5: break; case 6: break; case 7: break; case 8: break; default: break; } } return 0; } | cs |
반응형
'잡다한 IT > 자료구조' 카테고리의 다른 글
연결 리스트로 완전 이진 탐색(BST) 구현 (0) | 2018.04.24 |
---|---|
연결리스트를 통한 큐 구현 (0) | 2018.04.24 |
링크드 리스트 구현 (0) | 2018.04.23 |
배열을 이용한 스택 (0) | 2018.04.22 |
배열을 이용한 큐 (0) | 2018.04.22 |