here's a sample of a queue that a friend sent me.
I guess it works, however, I cannot theoretically use this as we were instructed to use the same interface as our stack.c file.
So I want to know how to incorporate what is here into my existing stack.c file.
The biggest confusing factor to this for me is that the functions for the queue are not the same names (and therefore not same functions in essence) as my stack.c file...
The other two confusing things are
1) I am not familiar with things such as (int Q **tail, Q **head, int item) which are the parameters of the insert1 function. I never used a variable pointer to a pointer. Is there a simpler method to do what this function is doing??
2) I am assuming I don't really have to change much in my stack.c functions, including changing the parameters of the most if any of the functions, but this code is passing variables in the rem (assuming POP) function and my stack did not. In trying to stay with the same interface, do I have to pass anything to the POP in my queue???
What I have preferred to do at all in learning the C language was find code that is simplest to follow to do the desired things/functions. I never straight copy anyone's code, for I find that I don't learn a darned thing, unless they put ample remarks in their code showing the purpose of the functions or code statements/expressions. So I am really intending on submitting code that makes sense to me, not just copy someone else's work for a grade.
Code:
#include <stdio.h>
#include <stdlib.h>
#define QSIZE 6
int count;
/*count is global because it needs to be changed by insert and remove fxs*/
typedef struct tag{
int n;
struct tag * next;
}Q;
/* in my stack there is a make_empty, pop and push and not a
rem, insert, or insert1 functions; the only three that are here
and in my stack are the 1st three.
*/
void initialize();
int empty();
int full();
int rem(Q *, Q *);
void insert(Q *, Q *, int);
void insert1(Q **, Q **, int);
main()
{
Q *tail, *head, *ptr;
int code, code1, i, n;
int i1, i2;
tail = malloc (sizeof(Q));
if (tail == NULL) {
printf("malloc failed to allocate storage - exit \n");
exit;
}
head = malloc (sizeof(Q));
if (head == NULL) {
printf("malloc failed to allocate storage - exit \n");
exit;
}
tail->next = head;
head->next = NULL;
initialize();
printf("Initially the queue is is empty?1=Yes 0=No\n %d\n", empty());
printf("Intially the queue is full?1=Yes 0=No\n %d\n", full());
printf("Enter 1 to continue, 0 to stop: ");
scanf("%d", &code);
while(code!=0)
{ printf("Enter 2 to insert an item, 3 to remove an item: ");
scanf("%d", &code1);
switch(code1)
{ case 2: printf("Enter an integer to be inserted: ");
scanf("%d", &i1);
if (count == 0 || count == 1)
{insert(tail, head, i1); break;}
if (count > 1)
insert1(&tail, &head, i1);
break;
case 3: i2=rem(tail, head);
if(i2==0)
printf("Nothing is removed\n");
else
printf("%d is removed\n", i2);
break;
default: printf("Wrong code\n");
}
printf("Currently in the queue:\n");
if(count==0)
printf("Nothing\n");
else
{
if (count == 1)
printf("%d ", head->n);
else
{
for (ptr=tail; ptr != NULL; ptr=ptr->next)
printf("%d ", ptr->n);
}
printf("\n");
}
printf("Enter 1 to continue, 0 to stop: ");
scanf("%d", &code);
}
return 0;
}
void initialize()
{
count=0;
}
int empty()
{
if (count == 0)
{ printf("QUEUE IS EMPTY\n");
return 1;
}
else return 0;
}
int full()
{
if (count == QSIZE)
{ printf("QUEUE IS FULL\n");
return 1;
}
else return 0;
}
/* What is this function doing? Is it the POP function?
rem = remove ???
*/
int rem(Q *tail, Q *head)
{
int temp;
Q *prev, *cur;
prev = tail;
cur = tail->next;
if (empty())
{printf("Error\n");
return 0;
}
while (cur->next != NULL)
{ prev = cur;
cur = cur->next;
}
temp = cur->n;
free(cur);
prev->next = NULL;
head = prev;
count--;
return temp;
}
/* I am guessing this is a POP function but there are
two insert functions here, what is the second one for???
*/
void insert(Q *tail, Q *head, int item)
{
if (full())
{printf("Queue is full. Overflow!\n");
return; }
if (count == 0)
{ head->n = item;
count++;
return; }
if (count == 1)
{ tail->n = item;
count++;
return; }
}
void insert1(Q **tail, Q **head, int item)
{
Q *new;
if (full())
{printf("Queue is full. Overflow!\n");
return; }
new = malloc(sizeof(Q));
if (new == NULL) {
printf("malloc failed to allocate storage - exit \n");
exit; }
new->n = item;
new->next = *tail;
*tail = new;
count++;
return;
}