mirror of
https://github.com/TheAlgorithms/C.git
synced 2026-09-24 16:05:02 +00:00
feat: Moving queue file to it's directory and creating include file for it (#874)
* Putting queue in correct dir and creating include file for it * Update data_structures/queue/include.h missing one function, added Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
committed by
GitHub
parent
3cfdbb040d
commit
9bbec45d13
28
data_structures/queue/include.h
Normal file
28
data_structures/queue/include.h
Normal file
@@ -0,0 +1,28 @@
|
||||
//////////////////////////////////////////////////////////////////////////////////////
|
||||
/// INCLUDES
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// DATA STRUCTURES
|
||||
/**
|
||||
* Defining the structure of the node which contains 'data' (type : integer),
|
||||
* two pointers 'next' and 'pre' (type : struct node).
|
||||
*/
|
||||
|
||||
struct node
|
||||
{
|
||||
int data;
|
||||
struct node *next;
|
||||
struct node *pre;
|
||||
} * head, *tail, *tmp;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// FORWARD DECLARATIONS
|
||||
|
||||
void create();
|
||||
void enque(int x);
|
||||
int deque();
|
||||
int peek();
|
||||
int size();
|
||||
int isEmpty();
|
||||
73
data_structures/queue/queue.c
Normal file
73
data_structures/queue/queue.c
Normal file
@@ -0,0 +1,73 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// INCLUDES
|
||||
#include "include.h";
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// GLOBAL VARIABLES
|
||||
int count;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// MAIN ENTRY POINT
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
create();
|
||||
enque(5);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void create()
|
||||
{
|
||||
head = NULL;
|
||||
tail = NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts an item into the Queue.
|
||||
*/
|
||||
void enque(int x)
|
||||
{
|
||||
if (head == NULL)
|
||||
{
|
||||
head = (struct node *)malloc(sizeof(struct node));
|
||||
head->data = x;
|
||||
head->pre = NULL;
|
||||
tail = head;
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp = (struct node *)malloc(sizeof(struct node));
|
||||
tmp->data = x;
|
||||
tmp->next = tail;
|
||||
tail = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the next item from the Queue.
|
||||
*/
|
||||
int deque()
|
||||
{
|
||||
int returnData = 0;
|
||||
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;
|
||||
}
|
||||
return returnData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of the Queue.
|
||||
*/
|
||||
int size() { return count; }
|
||||
Reference in New Issue
Block a user