forked from a-r-nida/HactoberFest2020-Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
62 lines (57 loc) · 1.19 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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *link;
};
void enqueue(struct node **head, int newData){
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = newData;
newNode->link = NULL;
if(*head == NULL){
*head = newNode;
}
else{
struct node *tail = *head;
while(tail->link != NULL)
tail = tail->link;
tail->link = newNode;
}
}
void dequeue(struct node **head){
if(*head == NULL)
printf("\nQueue is already empty");
else if((*head)->link == NULL){
*head = NULL;
printf("\nQueue is now empty");
}
else{
*head = (*head)->link;
}
}
void print(struct node *head){
while(head != NULL){
printf("%d ", head->data);
head = head->link;
}
}
int main(){
struct node *head = NULL;
enqueue(&head, 1);
enqueue(&head, 2);
enqueue(&head, 3);
print(head);
dequeue(&head);
printf("\n");
print(head);
dequeue(&head);
printf("\n");
print(head);
dequeue(&head);
printf("\n");
print(head);
dequeue(&head);
printf("\n");
print(head);
return 0;
}