Taking my original file stack.c with all the stack functions in it, I need to change so this is a queue file.
What I believe is the only difference is the POP function because in a queue the last item in is the last item out. I know this involves two pointers, but I couldn't follow the example in class. Could someone explain the organization of the POP function?
here's the stack code
Code:
/*****************/
/* stack.c */
/****************/
#include <stdlib.h>
#include "stack.h"
struct node {
int data;
struct node * next;
};
struct node *first;
int count;
void make_empty()
{
first = NULL;
count = 0;
}
int is_empty(void)
{
return count == 0;
}
int is_full(void)
{
return 0;
}
void push(int insert_value)
{
struct node *new_node;
new_node=malloc(sizeof(struct node));
if (new_node == NULL) {
printf("Error in push: stack is full.\n");
exit (EXIT_FAILURE);
}
new_node->data = insert_value;
new_node->next = first;
first = new_node;
count++;
}
int pop(void)
{
struct node * top_node;
int i;
if (is_empty()) {
printf("Error in pop: stack is empty.\n");
exit (EXIT_FAILURE);
}
printf("%d deleted successfully.\n",first->data);
top_node=first;
i = first->data;
first = first->next;
free(top_node);
count--;
return i;
}
void print_list()
{
struct node *ptr;
if (first == NULL) {
printf("List is empty.\n\n");
}
else {
printf("\n\nHere is your list: \n");
for (ptr=first;ptr;ptr=ptr->next)
printf("%d\n",ptr->data);
return;
}
}