1. 스택

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
#include<stdio.h>
#define MAX 10000
 
/* 스택
top 은 -1로 초기화.
*/
typedef struct Stack {
    char memory[MAX];
    int top;
}Stack;
 
Stack stack;
 
int empty() {        
    if (stack.top == -1) {
        return 1;        
    }
    else {
        return 0;        
    }
}
 
int full() {
    if (stack.top == MAX-1) {
        return 1;
    }
    else {
        return 0;
    }
}
 
void push(char c) {
    if (full()) {
        printf("full");
    }else {
        stack.memory[++stack.top] = c;
    }
}
 
void pop() {
    if (empty) {
        printf("empty");
    }
    else {
        stack.top -= 1;
    }
}
cs


2. 큐

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
#include <stdio.h>
#define MAX 1000
 
/* 원형 큐
원형큐는 공간 하나를 비워 둔다. why)) 원형 큐가 비어있는지 알수있게 하기 위해서!!
full : rear +1 == front
empty : rear == front
*/
 
 
typedef struct CirQueue {
    int q[MAX];
    int front;
    int rear;
};
 
struct CirQueue cq;
 
void cir_push(int n) {
if ( cq.rear + 1== cq.front) {
        printf("full");
    }
    else {
        cq.q[(cq.rear + 1)%MAX] = n;
    }
}
 
int cir_pop(){
    if (cq.front == cq.rear) {
        printf("empty");
    }
    cq.q[(cq.front+1)%MAX];
    return cq.q[cq.front];
}
/* normal queue 
front, rear = 0 (초기화)
    => 단점: 공간이 낭비 됨
    => solution)) 원형 큐!!
*/
struct CirQueue qu;
 
void push(int n) {
    if (qu.rear == MAX -1) {
        printf("full");
    }
    else {
        qu.q[++qu.rear] = n;
    }
}
 
void pop() {
    if (qu.front == qu.rear) {
        printf("empty");
    }
    else {
        qu.front += 1;
    }
}
cs


---------------------------------------------------------------------------------------------

오늘의 문제 : 10828번(스택), 1874번(스택수열)


(+)추가문제 : 1918번(후위연산자)

---------------------------------------------------------------------------------------------


시작이 반이다!! 라는 시작으로 열심히 시작해 보겠습니다!! silverbell 화이팅!!

+ Recent posts