-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeque.c
More file actions
113 lines (94 loc) · 1.46 KB
/
deque.c
File metadata and controls
113 lines (94 loc) · 1.46 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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}
struct queue
{
struct node *rear;
struct node *front;
}
struct stack
{
struct node *top;
}
void push(struct stack *s, int x)
{
struct node *temp = malloc(sizeof(struct node));
if(!temp)
return NULL;
temp->data = x;
temp->next = NULL;
s->top = temp;
}
int pop(struct stack *s)
{
struct node *temp;
int x = 0;
if(isEmptyStack(s))
{
printf("Underflow\n");
return;
}
temp = s->top;
x = s->top->data;
s->top = s->top->next;
free(temp);
return x;
}
void enqueue(struct queue *q, int x)
{
struct node *new = malloc(sizeof(struct node));
if(!new)
return NULL;
new->data = x;
new->next = NULL;
if(q->rear)
q->rear->next = new;
q->rear = new;
if(q->front == NULL)
q->front = q->rear;
}
int isEmpty(struct queue *q)
{
if(q->front == q->rear == NULL)
return 1;
return 0;
}
int isEmptyStack(struct stack *s)
{
return (s->top == NULL);
}
int dequeue(struct queue *q)
{
int x = 0;
struct node *temp;
if(isEmpty(q))
{
printf("Underflow\n");
return -1;
}
temp = q->front;
x = q->front->data;
q->front = q->front->next;
free(temp);
return data;
}
void reverse(struct queue *q)
{
struct stack *S = malloc(sizeof(struct stack));
while(isEmpty(q))
push(S, dequeue(q));
while(!isEmptyStack(S))
enqueue(q, pop(S));
}
int main()
{
struct queue *q = malloc(sizeof(struct queue));
q->front = q->rear = NULL;
enqueue(q,1);
enqueue(q,2);
reverse(q);
}