Added data_structure queue.c

This commit is contained in:
KylerSmith 2017-07-15 21:00:51 -07:00
parent 0fdb6766b7
commit 066a69bb43

75
data_structures/queue.c Normal file
View File

@ -0,0 +1,75 @@
////////////////////////////////////////////////////////////////////////////////
//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;
}
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;
}
}
int deque() {
int returnData;
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;
}
}