-
Notifications
You must be signed in to change notification settings - Fork 2
/
command_queue.c
78 lines (62 loc) · 1.12 KB
/
command_queue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "shell.h"
#include <stdlib.h>
#include <stdio.h>
/**
* create_queue - Creates the queue of nodes
*
* Return: Address of node
*/
queue_t *create_queue()
{
queue_t *q = malloc(sizeof(queue_t));
if (!q)
return (NULL);
q->front = q->rear = NULL;
return (q);
}
/**
* enqueue - Adds new node to the front
*
* @q: Pointer to queue
*
* @separator: Used for character that
* that separates each command
*
* @command: Points to the first char
* in the stream
*
* Return: (0) failure (1) success
*/
int enqueue(queue_t *q, char separator, char **command)
{
command_t *new_node = create_command(separator, command);
if (!new_node)
return (0);
if (!q->rear)
{
q->front = new_node;
q->rear = new_node;
return (1);
}
q->rear->next = new_node;
q->rear = new_node;
return (1);
}
/**
* dequeue - Removes the node after executed
*
* @q: Pointer to the queue
*
* Return: Node that was executed
*/
command_t *dequeue(queue_t *q)
{
command_t *old_node = NULL;
if (!q->front)
return (NULL);
old_node = q->front;
q->front = q->front->next;
if (!q->front)
q->rear = NULL;
return (old_node);
}