Skip to content
- 스택
출처 : http://terms.naver.com/entry.nhn?docId=2837556&cid=40942&categoryId=32841
- main() 함수
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream> #include <malloc.h> struct node{ int data; struct node *next; }; struct node *head = NULL; int main(int argc, char** argv) { for(int i=0; i<12; i++){ pushList(i+11); printList(); } for(int i=0; i<6; i++){ popList(); printList(); } for(int i=0; i<12; i++){ pushList(i+11); printList(); } freeList(); return 0; } |
- pushList() 함수
|
void pushList(int v){ struct node *p = (struct node *)malloc(sizeof(struct node)); p->data = v; p->next = head; head = p; } |
- popList() 함수
|
int popList(){ int v = 0; struct node *p = head; if(p == NULL){ printf("Empty.\n"); } else{ v = p->data; head = p->next; free(p); } } |
- printList()함수 : 스택에 저장된 값들을 출력
|
void printList(){ //top부처 0의 순으로 출력 struct node *p = head; while(p != NULL){ printf("%d ", p->data); p = p->next; } printf("\n"); } |
- freeList() 함수 : 프로그램을 종료하기전에 할당된 메모리 모두를 제거
|
void freeList(){ struct node *p = NULL; while(head != NULL){ p = head; head = head->next; free(p); } } |
error: Content is protected !!