Skip to content

Commit c34bbb9

Browse files
committed
Queue overhaul
1 parent 8aa2afb commit c34bbb9

File tree

2 files changed

+59
-40
lines changed

2 files changed

+59
-40
lines changed

queue.c

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,118 @@
11
#include "queue.h"
22

33

4-
54
int main(){
65

7-
Peek();
8-
Enqueue(7);
9-
Peek();
6+
Queue_Type* q1 = create(q1);
7+
Peek(q1);
8+
Enqueue(q1, 7);
9+
Peek(q1);
1010

1111
int dequeued_val;
12-
Dequeue(&dequeued_val);
13-
Peek();
12+
Dequeue(q1, &dequeued_val);
13+
Peek(q1);
1414

15-
Dequeue(&dequeued_val);
16-
Enqueue(2);
17-
Enqueue(4);
18-
Peek();
15+
Dequeue(q1, &dequeued_val);
16+
Enqueue(q1, 2);
17+
Enqueue(q1, 4);
18+
Peek(q1);
19+
Dequeue(q1, &dequeued_val);
1920

2021
return 0;
2122
}
2223

23-
void Enqueue (int val){
24+
Queue_Type* create(Queue_Type* q){
25+
26+
q = (Queue_Type*) malloc (sizeof(Queue_Type));
27+
28+
if (NULL == q){
29+
30+
printf("create() unsuccessful due to malloc failure\n");
31+
return NULL;
32+
}
33+
q->first = NULL;
34+
q->last = NULL;
35+
return q;
36+
}
37+
38+
void Enqueue (Queue_Type* q, int val){
2439

2540
QUEUEMEMBER* qmember = (QUEUEMEMBER*) malloc (sizeof(QUEUEMEMBER));
2641

2742
if (NULL == qmember){
2843

29-
printf ("Enqueue unsuccessful due to malloc() failure.\n");
44+
printf ("Enqueue() unsuccessful due to malloc failure.\n");
3045
return;
3146
}
3247

3348
qmember->data = val;
3449
qmember->ptr = NULL;
3550

36-
if (NULL == first){
51+
if (NULL == q->first){
3752

38-
first = qmember;
53+
q->first = qmember;
3954
}
40-
if (NULL == last){
55+
if (NULL == q->last){
4156

42-
last = first;
57+
q->last = q->first;
4358
}
4459
else{
4560

46-
last->ptr = qmember;
47-
last = qmember;
61+
q->last->ptr = qmember;
62+
q->last = qmember;
4863
}
4964

5065
printf("Queue after Enqueue():\n");
51-
Print(first);
66+
Print(q);
5267
return;
5368
}
5469

55-
void Dequeue (int* dqd_val){
70+
void Dequeue (Queue_Type* q, int* dqd_val){
5671

57-
if (NULL == first){
72+
if (NULL == q->first){
5873

5974
printf ("Empty Queue. Nothing to Dequeue\n");
6075
return;
6176
}
6277

63-
QUEUEMEMBER* temp = first;
78+
QUEUEMEMBER* temp = q->first;
6479

65-
if (first == last){ // when there's only one item in Queue
80+
if (q->first == q->last){ // when there's only one item in Queue
6681

67-
last = NULL;
82+
q->last = NULL;
6883
}
6984

70-
first = first->ptr;
85+
q->first = q->first->ptr;
7186

7287
*dqd_val = temp->data;
7388

7489
free(temp);
7590

7691
printf("Queue after Dequeue():\n");
77-
Print(first);
92+
Print(q);
7893
return;
7994
}
8095

81-
void Peek(){
96+
void Peek(Queue_Type* q){
8297

83-
if (NULL == first){
98+
if (NULL == q->first){
8499
printf("Empty Queue. Nothing to peek\n");
85100
return;
86101
}
87102

88-
printf("First item in Queue = %d\nLast item in Queue = %d\n", first->data, last->data);
103+
printf("First item in Queue = %d\nLast item in Queue = %d\n", q->first->data, q->last->data);
89104

90105
return;
91106
}
92107

93-
void Print(QUEUEMEMBER* beg){
108+
void Print(Queue_Type* q){
94109

95-
if (NULL == beg){
110+
if (NULL == q->first){
96111
printf("Empty Queue. Nothing to print\n");
97112
return;
98113
}
99114

100-
QUEUEMEMBER* iter = beg;
115+
QUEUEMEMBER* iter = q->first;
101116

102117
while(iter->ptr != NULL){
103118

queue.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#ifndef QUEUE_H
22
#define QUEUE_H
33

4-
#include "LL.h"
4+
#include "LL_shared.h"
55
#include <stdio.h>
66

7-
typedef NODE QUEUEMEMBER; // alias for NODE
7+
typedef NODE QUEUEMEMBER; // alias for NODE
88

9-
static QUEUEMEMBER* first = NULL; // first is the LL head node pointer
10-
static QUEUEMEMBER* last = NULL; // last is the LL tail node pointer
9+
typedef struct Queue{
10+
QUEUEMEMBER* first;
11+
QUEUEMEMBER* last;
12+
} Queue_Type;
1113

12-
void Enqueue (int);
13-
void Dequeue (int*);
14-
void Peek (void);
15-
void Print(QUEUEMEMBER*); // for debug purposes // expects first pointer
14+
15+
Queue_Type* create (Queue_Type*);
16+
void Enqueue (Queue_Type*, int);
17+
void Dequeue (Queue_Type*, int*);
18+
void Peek (Queue_Type*);
19+
void Print(Queue_Type*); // for debug purposes
1620

1721
#endif // QUEUE_H

0 commit comments

Comments
 (0)