Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define N 100
- struct stack {
- int top;
- int rear;
- int element[N];
- };
- struct queue {
- int top;
- int rear;
- int element[N];
- };
- void push(int x, struct stack *S) {
- if (S->top == 0) {
- printf("Error\n");
- exit(1);
- }
- S->element[--S->top] = x;
- }
- void pop(struct stack *S) {
- if (S->top >= N) {
- printf("Error\n");
- exit(1);
- }
- printf("%d ", S->element[S->top++]);
- }
- void enqueue(int x, struct queue *Q) {
- if (Q->top == Q->rear) {
- Q->rear = 0;
- Q->top = 0;
- Q->element[Q->rear++] = x;
- return;
- }
- if (Q->rear == N) {
- printf("Error\n");
- exit(1);
- }
- Q->element[Q->rear++] = x;
- }
- void dequeue(struct queue *Q) {
- if (Q->rear <= Q->top) {
- printf("Error\n");
- exit(1);
- }
- printf("%d ", Q->element[Q->top++]);
- }
- int main(void) {
- int x;
- struct stack S;
- struct queue Q;
- S.top = N;
- Q.top = Q.rear = 0;
- while (1) {
- printf("整数を入力してください: ");
- scanf("%d", &x);
- if (x < 0) break;
- push(x, &S);
- enqueue(x, &Q);
- }
- printf("スタック構造の場合\n");
- while (S.top < N)
- pop(&S);
- printf("\nキュー構造の場合\n");
- while (Q.top < Q.rear)
- dequeue(&Q);
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement