48 #define MAIN_MENU_ROW "--- QUEUE/STACK DEMO ---\nMENU: 0=Exit 1=Enqueue 2=Dequeue 3=Push 4=Pop 5=Dequeue/Push 6=Print\nSelection "
52 void my_destroy(
void *data);
54 void print(
const void *data);
57 void enqueue_node(
Queue que);
58 void dequeue_node(
Queue que);
59 void push_node(
Stack stk);
60 void pop_node(
Stack stk);
66 void enqueue_push_nodes(
Queue que,
Stack stk,
int nr_of_ele);
71 void my_destroy(
void *data)
79 return strchr(
"YyNn", ch) ? 1 : 0;
83 void print(
const void *data)
85 printf(
" %02d", *(
int *)data);
89 void enqueue_push_nodes(
Queue que,
Stack stk,
int nr_of_ele)
94 printf(
"--- CREATING QUEUE AND STACK (%d nodes each), RANDOM INTEGER DATA ---\n", NR_OF_ITEMS);
99 pi = (
int *)malloc(
sizeof(
int));
105 assert(retval == OK);
108 pi = (
int *)malloc(
sizeof(
int));
114 assert(retval == OK);
116 }
while (++i < nr_of_ele);
118 printf(
"\nCurrent queue and stack status: ");
119 printf(
"\nQueue(%d nodes): ",
QUEUEsize(que));
121 printf(
"\nStack(%d nodes): ",
STACKsize(stk));
127 void enqueue_node(
Queue que)
135 printf(
"--- ENQUEUE NODE TO QUEUE ---\n");
136 printf(
"\nCurrent queue status(%d nodes): ",
QUEUEsize(que));
139 tmp =
read_int(
"\nEnter integer data of node to be enqueued (-1=Quit): ", 0, 0);
144 pi = (
int *)malloc(
sizeof(
int));
151 printf(
"\nFatal error enqueing data - exiting...!");
157 sprintf(mess,
"Node %d will be enqueued!", *pi);
164 void dequeue_node(
Queue que)
167 char mess[BUFSIZ], ans;
178 printf(
"--- DEQUEUE NODE FROM QUEUE ---\n");
179 printf(
"\nCurrent queue status(%d nodes): ",
QUEUEsize(que));
191 sprintf(mess,
"\nAbout to dequeue node %d.. - Continue? (y/n): ", *ptmp);
194 if (ans ==
'y' || ans ==
'Y')
198 printf(
"\nFatal error dequeing data - exiting...!");
204 sprintf(mess,
"Node %d will be dequeued!", *pi);
216 void push_node(
Stack stk)
224 printf(
"--- PUSH NODE ON STACK ---\n");
225 printf(
"\nCurrent stack status(%d nodes): ",
STACKsize(stk));
228 tmp =
read_int(
"\nEnter integer data of node to be pushed (-1=Quit): ", 0, 0);
233 pi = (
int *)malloc(
sizeof(
int));
240 printf(
"\nFatal error pushing data - exiting...!");
246 sprintf(mess,
"Node %d will be pushed on stack!", *pi);
253 void pop_node(
Stack stk)
256 char mess[BUFSIZ], ans;
267 printf(
"--- POP NODE FROM STACK ---\n");
268 printf(
"\nCurrent stack status(%d nodes): ",
STACKsize(stk));
280 sprintf(mess,
"\nAbout to pop node %d.. - Continue? (y/n): ", *ptmp);
283 if (ans ==
'y' || ans ==
'Y')
285 if ((
STACKpop(stk, (
void **)&pi)) != OK)
287 printf(
"\nFatal error popping data - exiting...!");
293 sprintf(mess,
"Node %d will be popped!", *pi);
308 char mess[BUFSIZ], ans;
319 printf(
"--- DEQUEUE NODE FROM QUEUE AND PUSH ON STACK ---\n");
320 printf(
"\nCurrent queue/stack status: ");
337 sprintf(mess,
"\nAbout to dequeue node %d - and push it on stack.. - Continue? (y/n): ", *ptmp);
340 if (ans ==
'y' || ans ==
'Y')
344 printf(
"\nFatal error dequeing data - exiting...!");
352 printf(
"\nFatal error pushing data - exiting...!");
357 sprintf(mess,
"Node %d will be dequeued - and pushed on stack..!", *pi);
371 printf(
"--- PRINT QUEUE AND STACK ---\n");
372 printf(
"\nCurrent contents: ");
387 printf(
"--- FINAL STATUS ---\n");
388 printf(
"\nFinal list contents: ");
404 srand((
unsigned int)time(NULL));
406 if ((myqueue =
QUEUEinit(my_destroy)) == NULL)
408 printf(
"\nFatal error - bailing out...!");
413 if ((mystack =
STACKinit(my_destroy)) == NULL)
415 printf(
"\nFatal error - bailing out...!");
421 enqueue_push_nodes(myqueue, mystack, NR_OF_ITEMS);
425 menu_choice =
menu(MAIN_MENU_ROW, 0, 6);
430 enqueue_node(myqueue);
433 dequeue_node(myqueue);
442 dequeue_push_node(myqueue, mystack);
445 print_queue_stack(myqueue, mystack);
448 final_status(myqueue, mystack);