TheAlgorithms-C/data_structures/queue.c

89 lines
1.8 KiB
C
Raw Normal View History

2017-07-16 07:00:51 +03:00
////////////////////////////////////////////////////////////////////////////////
//INCLUDES
#include <stdio.h>
#include <stdlib.h>
////////////////////////////////////////////////////////////////////////////////
//MACROS: CONSTANTS
////////////////////////////////////////////////////////////////////////////////
//DATA STRUCTURES
struct node {
int data;
struct node* next;
struct node* pre;
} *head, *tail, *tmp;
////////////////////////////////////////////////////////////////////////////////
//GLOBAL VARIABLES
int count;
////////////////////////////////////////////////////////////////////////////////
//FORWARD DECLARATIONS
void create();
void enque(int x);
int deque();
int peek();
int size();
int isEmpty();
////////////////////////////////////////////////////////////////////////////////
//MAIN ENTRY POINT
int main(int argc, char const *argv[]) {
create();
enque(5);
return 0;
}
void create() {
head = NULL;
tail = NULL;
}
2017-07-18 04:37:58 +03:00
/**
* Puts an item into the Queue.
*/
2017-07-16 07:00:51 +03:00
void enque(int x) {
if(head == NULL) {
head = (struct node *)malloc(1 * sizeof(struct node));
head->data = x;
head->pre = NULL;
tail = head;
} else {
tmp = (struct node *)malloc(1 * sizeof(struct node));
tmp->data = x;
tmp->next = tail;
tail = tmp;
}
}
2017-07-18 05:13:45 +03:00
/**
* Takes the next item from the Queue.
*/
2017-07-16 07:00:51 +03:00
int deque() {
2020-04-21 13:38:03 +03:00
int returnData = 0;
2017-07-16 07:00:51 +03:00
if(head == NULL) {
printf("ERROR: Deque from empty queue.\n");
exit(1);
} else {
returnData = head->data;
if(head->pre == NULL)
head = NULL;
else
head = head->pre;
head->next = NULL;
}
2020-04-21 13:38:03 +03:00
return returnData;
2017-07-16 07:00:51 +03:00
}
2017-07-18 05:13:45 +03:00
/**
* Returns the size of the Queue.
*/
int size() {
return count;
2020-04-21 13:36:02 +03:00
}