VioletaBabel
큐 본문
FIFO.
프린터처럼 먼저 들어간 종이가 먼저 나옴.
--
배열로 구현
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <cstdio> int main() { int num[100], count = 0; char command; int comnum; while (1) { printf("입력 - i / 삭제 - d\n : "); scanf("%c", &command); if (command == 'i') { printf("입력할 자연수 : "); scanf("%d", &comnum); num[count++] = comnum; } else if (command == 'd' && count > 0) printf("%d\n", num[--count]); while (getchar() != '\n'); } } | cs |
--
연결리스트로 구현
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 | #include <cstdio> struct node { node *next; node *prev; int data; }; node *head, *foot; void init(); int main() { init(); node *newnode; char command; int comnum, count = 0; while (1) { printf("입력 - i / 삭제 - d\n : "); scanf("%c", &command); if (command == 'i') { printf("입력할 자연수 : "); scanf("%d", &comnum); newnode = new node; newnode->next = head->next; newnode->prev = head; newnode->next->prev = head->next = newnode; newnode->data = comnum; ++count; } else if (command == 'd' && count > 0) { newnode = foot->prev; foot->prev = newnode->prev; newnode->prev->next = foot; printf("%d\n", newnode->data); --count; delete newnode; } while (getchar() != '\n'); } } void init() { head = new node; foot = new node; head->next = foot->next = foot; head->prev = foot->prev = head; head->data = foot->data = -1; } | cs |
Comments