Repository navigation
Expand file tree
/
Copy pathlinkedqueue.h
More file actions
116 lines (104 loc) · 3.14 KB
/
Copy pathlinkedqueue.h
File metadata and controls
116 lines (104 loc) · 3.14 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#ifndef LINKEDQUEUE_JL
#define LINKEDQUEUE_JL
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
// Define how our queue node is structured.
typedef struct QueueNode{
int data; // The value of the node that we need to store.
struct QueueNode* next; // The pointer to the node behind it.
} QueueNode;
// Define how our (linear and dynamic) queue built on a linked list is structured. It has no size limit.
typedef struct{
QueueNode* front; // The pointer to the front node, the next one to leave (null if it's empty).
QueueNode* rear; // The pointer to the rear node, the last one that arrived (null if it's empty).
int size; // The number of elements in our queue.
} LinkedQueue;
/********************/
/* FUNCTIONS */
/********************/
// Set Up the Linked Queue. We start setting the front and rear pointers to null and size to 0.
void initLinkedQueue(LinkedQueue *queue){
queue->front = NULL;
queue->rear = NULL;
queue->size = 0;
}
// Check if the Queue is Empty. Returns 1 if it's empty or 0 if it has elements.
int isEmptyLinkedQueue(LinkedQueue *queue){
if(queue->front == NULL){
return 1;
}
else{
return 0;
}
}
// Add an Element to the Rear of the Queue. Returns 1 if successful or 0 if there's no memory.
int enQueueLinked(LinkedQueue *queue, int value){
QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
if(!newNode){
return 0;
}
else{
newNode->data = value;
newNode->next = NULL;
if(queue->rear == NULL){
queue->front = newNode;
}
else{
queue->rear->next = newNode;
}
queue->rear = newNode;
queue->size++;
return 1;
}
}
// Remove an Element from the Front of the Queue. Returns 1 if successful or 0 if the queue is empty.
int deQueueLinked(LinkedQueue *queue, int *value){
if(isEmptyLinkedQueue(queue)){
return 0;
}
else{
QueueNode* temp = queue->front;
*value = temp->data;
queue->front = temp->next;
if(queue->front == NULL){
queue->rear = NULL;
}
free(temp);
queue->size--;
return 1;
}
}
// Peek at the Front Element without Removing It. Returns 1 if successful or 0 if the queue is empty.
int peekLinkedQueue(LinkedQueue *queue, int *value){
if(isEmptyLinkedQueue(queue)){
return 0;
}
else{
*value = queue->front->data;
return 1;
}
}
// Print all Elements of the Queue. Displays elements from front to rear.
void printLinkedQueue(LinkedQueue *queue){
printf("[");
for(QueueNode* temp = queue->front; temp != NULL; temp = temp->next){
printf("%d", temp->data);
if(temp->next != NULL){
printf(", ");
}
}
printf("]\n");
}
// Free all the Nodes and Reset the Queue.
void freeLinkedQueue(LinkedQueue *queue){
QueueNode* temp = queue->front;
while(temp != NULL){
QueueNode* nextNode = temp->next;
free(temp);
temp = nextNode;
}
initLinkedQueue(queue);
}
#endif //LINKEDQUEUE_JL