stack.c
1.###
#include <stdio.h>
int stack[100];
int tos; // top of stack
void push(int data)
{
stack[tos] = data;
++tos;
}
int pop(void)
{
--tos;
return stack[tos];
}
int main(void)
{
push(100);
push(200);
push(300);
printf("1st pop() : %d\n", pop());
printf("2st pop() : %d\n", pop());
printf("3st pop() : %d\n", pop());
return 0;
}
###
2.###
main.c
#include <stdio.h>
#include "stack.h"
int main(void)
{
push(100);
push(200);
push(300);
printf("1st pop() : %d\n", pop());
printf("2st pop() : %d\n", pop());
printf("3st pop() : %d\n", pop());
return 0;
}
---
stack.h
#ifndef STACK_H
#define STACK_H
void push(int data);
int pop(void);
#endif
---
stack.c
#include "stack.h"
int stack[100];
int tos; // top of stack
void push(int data)
{
stack[tos] = data;
++tos;
}
int pop(void)
{
--tos;
return stack[tos];
}
###
3.###
main.c
// 여러개의 stack을 사용하기위해 구조체 사용
#include <stdio.h>
#include "stack.h"
int main(void)
{
Stack s1, s2;
initStack(&s1); //s1.tos = 0;
initStack(&s2); //s2.tos = 0;
push(&s1, 100);
push(&s1, 200);
push(&s1, 300);
printf("s1 1st pop() : %d\n", pop(&s1));
printf("s1 2st pop() : %d\n", pop(&s1));
printf("s1 3st pop() : %d\n", pop(&s1));
push(&s2, 432);
push(&s2, 543);
push(&s2, 654);
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;
}
---
stack.h
#ifndef STACK_H
#define STACK_H
#define STACKSIZE 100
typedef struct {
int array[STACKSIZE];
int tos;
} Stack;
void initStack(Stack* ps);
void push(Stack* ps, int data);
int pop(Stack* ps);
#endif
---
stack.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
void initStack(Stack* ps)
{
ps->tos = 0;
}
void push(Stack* ps, int data)
{
//full
if(ps->tos == STACKSIZE) {
fprintf(stderr, "stack is full\n"); //stdout는 버퍼를 쓰고, stderr은 버퍼를 쓰지 않고 출력한다.
exit(-1);
}
(*ps).array[(*ps).tos] = data;
++(*ps).tos;
}
int pop(Stack* ps)
{
//empty
if(ps->tos == 0) {
fprintf(stderr, "stack is empty\n");
exit(-2);
}
--ps->tos;
return ps->array[ps->tos];
}
###
4. ###
main.c
// 여러개의 stack을 사용하기위해 구조체 사용
#include <stdio.h>
#include "stack.h"
int main(void)
{
Stack s1, s2;
initStack(&s1, 10);
initStack(&s2, 10);
push(&s1, 100);
push(&s1, 200);
push(&s1, 300);
printf("s1 1st pop() : %d\n", pop(&s1));
printf("s1 2rd pop() : %d\n", pop(&s1));
printf("s1 3rd pop() : %d\n", pop(&s1));
push(&s2, 432);
push(&s2, 543);
push(&s2, 654);
printf("s2 1st pop() : %d\n", pop(&s2));
printf("s2 2rd pop() : %d\n", pop(&s2));
printf("s2 3rd pop() : %d\n", pop(&s2));
cleanupStack(&s1);
cleanupStack(&s2);
return 0;
}
---
stack.h
#ifndef STACK_H
#define STACK_H
//#define STACKSIZE 100
typedef struct {
//int array[STACKSIZE];
int *pArr;
int size;
int tos;
} Stack;
void initStack(Stack* ps, unsigned int size);
void cleanupStack(Stack* ps);
void push(Stack* ps, int data);
int pop(Stack* ps);
#endif
---
stack.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "stack.h"
void initStack(Stack* ps, unsigned int size)
{
ps->pArr = (int *)malloc(sizeof(int) * size);
assert(ps->pArr != NULL); // 참이면 프로그램이 계속 진행이되고 거짓이면 프로그램이 종료된다. assert는 release모드에서 사라진다.
ps->size = size;
ps->tos = 0;
}
void cleanupStack(Stack* ps)
{
free(ps->pArr);
}
void push(Stack* ps, int data)
{
assert(ps->tos != ps->size);
(*ps).pArr[(*ps).tos] = data;
++(*ps).tos;
}
int pop(Stack* ps)
{
assert(ps->tos);
--ps->tos;
return ps->pArr[ps->tos];
}
###
5.###
main.c
// 여러개의 stack을 사용하기위해 구조체 사용
// 메모리 상의 모든 data는 시작주소와 size만 알면 접근할 수 있다.
#include <stdio.h>
#include "stack.h"
int main(void)
{
Stack s1, s2;
initStack(&s1, 10, sizeof(int));
initStack(&s2, 100, sizeof(double));
int i = 100; push(&s1, &i);
i = 200; push(&s1, &i);
i = 300; push(&s1, &i);
pop(&s1, &i); printf("s1 1st pop() : %d\n", i);
pop(&s1, &i); printf("s1 2rd pop() : %d\n", i);
pop(&s1, &i); printf("s1 3rd pop() : %d\n", i);
double d = 1.1; push(&s2, &d);
d = 2.2; push(&s2, &d);
d = 3.3; push(&s2, &d);
pop(&s2, &d); printf("s2 1st pop() : %f\n", d);
pop(&s2, &d); printf("s2 2rd pop() : %f\n", d);
pop(&s2, &d); printf("s2 3rd pop() : %f\n", d);
cleanupStack(&s1);
cleanupStack(&s2);
return 0;
}
/*
re = pop();
int pop(void)
{
return stack[--tos];
}
int re;
pop(&re);
void pop(int* p)
{
*p = stack[--tos];
}*/
---
stack.h
#ifndef STACK_H
#define STACK_H
typedef struct {
//int *pArr;
void* pArr;
int eleSize;
int size;
int tos;
} Stack;
void initStack(Stack* ps, unsigned int size, int eleSize);
void cleanupStack(Stack* ps);
void push(Stack* ps, const void* pData);
void pop(Stack* ps, void* pData);
#endif
---
stack.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "stack.h"
void initStack(Stack* ps, unsigned int size, int eleSize)
{
ps->pArr = malloc(eleSize * size);
assert(ps->pArr ); // 참이면 프로그램이 계속 진행이되고 거짓이면 프로그램이 종료된다. assert는 release모드에서 사라진다.
ps->size = size;
ps->eleSize = eleSize;
ps->tos = 0;
}
void cleanupStack(Stack* ps)
{
free(ps->pArr);
}
void push(Stack* ps, const void* pData)
{
assert(ps->tos != ps->size);
//(*ps).pArr[(*ps).tos] = *pData;
memcpy((unsigned char*)ps->pArr + ps->eleSize * ps->tos, pData, ps->eleSize);
++(*ps).tos;
}
void pop(Stack* ps, void* pData)
{
assert(ps->tos);
--ps->tos;
//*pData = ps->pArr[ps->tos];
memcpy(pData, (unsigned char*)ps->pArr + ps->eleSize * ps->tos, ps->eleSize);
}
Comments
Post a Comment