-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
70 lines (55 loc) · 1.06 KB
/
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "queue.h"
void init_queue(queue* s){
*s = NULL;
return;
}
void push(queue* q, char* d){
queue tmp = *q;
char* temp = (char*)malloc(sizeof(char)*100);
queue nq = (queue)malloc(sizeof(struct node));
strcpy(temp, d);
if(!nq || !temp){
printf("\nQueue Overflow");
return;
}
nq->val = temp;
nq->next = NULL;
if(!tmp){
*q = nq;
tmp = nq;
return;
}
while(tmp->next){
tmp = tmp->next;
}
tmp->next = nq;
return;
}
char* top(queue s){
if(s) return s->val;
return NULL;
}
char* back(queue s){
if(s){
while(s->next)
s = s->next;
return s->val;
}
return NULL;
}
bool isEmpty(queue s){
return (s == NULL);
}
void pop(queue* s){
if(!isEmpty(*s)){
queue tmp = *s;
*s = (*s)->next;
free(tmp);
return;
}
return;
}