-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
113 lines (113 loc) · 3.37 KB
/
queue.c
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*handle queue case*/
/*****************************************************************************
* Copyright (C) Shyamli Agrawal [email protected]
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include"header.h"
/*Input: win-> window pointer, menu->submenu options, nchoice->number of choices*/
void queue(WINDOW* win, char *menu[], int nchoice) {
WINDOW* win1, *win2;
win1 = init(win, &win2);
int headn, tailn, flag = 0, i;
char tailc[4], data[4] ;
mybox* head = newbox(win2, U, C - 4*U, 2*U, "Head");
mybox* tail = newbox(win2, U, C + 2*U, 2*U, "Tail");
mybox **array = nboxes(win2, 3*(HL)/2, (2*C - 4*16)/2, 4, 16);
int brk = 0, choice = 1;
while(brk == 0) {
print_menu(win, menu, choice, nchoice);
choice = getchoice(win, menu, nchoice, choice);
if(flag == 0 && (choice == ENQUEUE || choice == DEQUEUE)) {
fancyprint(win, L - 3, C - 10, "Initialize First!");
choice = 1;
getch();
wmove(win, L-3, 2);
wclrtoeol(win);
continue;
}
switch(choice) {
case INIT:
headn = tailn = 0;
flag = 1;
flashwin(win1, 1);
wmove(win1, 2, 1);
whline(win1, 0, C-2);
flashwin(win2, 2);
inbox(win2, head, "0");
inbox(win2, tail, "0");
break;
case ENQUEUE:
if(tailn == MAXX - 1){
fancyprint(win, L - 3, C - 10, "Queue FULL!!");
getch();
break;
}
fancyprint(win,L-3, C-10, "Enqueue Data: ");
echo();
wgetnstr(win, data, 3);
noecho();
flashwin(win1, 1);
walkin(win1, 2*L-4, (1+tailn)*2, (C-U*2)/2 ,U*2, data);
flashwin(win2, 2);
inbox(win2, array[tailn++], data);
sprintf(tailc, "%d", tailn);
inbox(win2, tail, tailc);
break;
case DEQUEUE:
if(tailn == 0) {
fancyprint(win, L - 3, C - 10, "Queue EMPTY!!");
getch();
break;
}
i = headn;
flashwin(win1, 1);
if(i == tailn)
cleardisc(win1, 2+ i*2, (C-U*2)/2 ,U*2, " " );
else {
while(i < tailn-1) {
cleardisc(win1, 2+ i*2, (C-U*2)/2 ,U*2, " " );
napms(100);i++;
walkin(win1, 2+i*2, i*2, (C-U*2)/2 ,U*2, array[i]->data);
}
}
fancyprint(win, L - 3, C - 10, "Dequed Value: ");
wprintw(win, "%s", array[headn]->data);
flashwin(win2, 2);
inbox(win2, array[headn], " ");
i = headn;
while(i < tailn) {
inbox(win2, array[i], array[i+1]->data);
i++;
}
inbox(win2, array[--tailn], " ");
sprintf(tailc, "%d", tailn);
inbox(win2, tail, tailc);
break;
case BACK:
brk = 1;
break;
}
wmove(win, L-3, 1);
wclrtoeol(win);
box(win, 0, 0);
}
free(head);
free(tail);
free(array);
delwin(win1);
delwin(win2);
touchwin(stdscr);
}