-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack-to-queue.c
86 lines (79 loc) · 1.59 KB
/
stack-to-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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <stdlib.h>
#define STACKSIZE 50
#define MAXQUEUE 50
#define TRUE 1
#define FALSE 0
struct stackelement {
int x;
};
struct stack {
int top;
struct stackelement items[STACKSIZE];
};
struct queue {
int items[MAXQUEUE];
int front, rear;
};
//fonksiyonlar
void push();
void insert();
int pop();
int popQueue();
int empty();
int main(int argc, char **argv)
{
struct stack s;
struct queue q;
q.front=0;
q.rear=0;
s.top = -1;
int x=5;
//kullanici 0 girene kadar yığıta eleman okuma
printf("lutfen eklemek istediginiz elemanlari giriniz..\n");
while(x!=0){
scanf("%d", &x);
push(&s,x);
}
//yığıttaki elemanlarin kuyruğa aktarımı
while(!empty(&s)){
int m = pop(&s);
insert(&q, m);
}
//kuyrugu ekrana yazdirma
printf("\n----kuyruk elemanlari----\n");
int temp = popQueue(&q);
while(q.front!=q.rear){
printf("%d\n" , popQueue(&q));
}
return 0;
}
//queuefunctions
void insert(struct queue *pq, int x){
pq->items[(pq->rear)++] = x;
}
int popQueue(struct queue *pq){
return (pq->items[(pq->front)++]);
}
//stackfunctions
int empty(struct stack *ps){
if(ps->top==-1)
return (TRUE);
else
return (FALSE);
}
void push(struct stack *ps, int x){
if(ps->top==STACKSIZE-1){
printf("stack overflow\n");
}
else
ps->items[++(ps->top)].x = x;
}
int pop(struct stack *ps){
if(empty(ps)){
printf("stack underflow\n");
}
else
return (ps->items[(ps->top)--].x);
return 0;
}