/*                                stack3.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
#include "stack3.h"
#include <stdlib.h>
#include <stdio.h>
 
#define STACKSIZE 100
 
void push(Stack *ps, int a)
{
    if(ps->tos== STACKSIZE)
    {
        fprintf(stderr, "stack is full!\n");
        exit(-1);
    }
    ps->array[ps->tos]=a;
    ps->tos++;
}
 
int pop(Stack *ps)
{
    if(ps->tos== 0)
    {
        fprintf(stderr, "stack is empty!\n");
        exit(-2);
    }
    ps->tos--;
    return ps->array[ps->tos];
}
 



/*                    teststack3.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
#include <stdio.h>
#include "stack3.h"
 
int main(void)
{
    Stack s1, s2;
    s1.tos=0;
    s2.tos=0;
    push(&s1, 100);
    push(&s1, 200);
    push(&s1, 300);
    
    push(&s2, 700);
    push(&s2, 800);
    push(&s2, 900);
    
    printf("s1 1st pop() : %d\n", pop(&s1));
    printf("s1 2st pop() : %d\n", pop(&s1));
    printf("s1 3st pop() : %d\n", pop(&s1));
 
    printf("s2 1st pop() : %d\n", pop(&s2));
    printf("s2 2st pop() : %d\n", pop(&s2));
    printf("s2 3st pop() : %d\n", pop(&s2));
    return 0;
}




/*                    stack3.h                */


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef STACK3_H
#define STACK3_H
 
#define STACKSIZE 100
 
typedef struct{
    int array[STACKSIZE];
    int tos;
} Stack;
 
void push(Stack *ps, int a);
int pop(Stack *ps);
 
#endif





'Study > 리눅스' 카테고리의 다른 글

리눅스 C언어로 컴파일하기 FIFO (1)  (0) 2014.09.12
리눅스 c언어로 컴파일하기 FILO (3)  (0) 2014.09.12
리눅스 c언어로 컴파일하기 FILO (2)  (0) 2014.09.12
IOT 수업장비  (0) 2014.09.11
안드로이드 아키텍쳐  (0) 2014.09.11
Posted by 차희빈

차희빈

달력