알고리즘2009. 9. 16. 22:54

/* in file stackdec.h */

 

#include <stdio.h>

#include <stdlib.h>

 

#define EMPTY        0

#define FULL        10000

 

typedef char        DATA;

 

struct elem

{

DATA                        d;

struct elem                *next;

};

 

typedef struct elem ELEM;

 

struct stack

{

int cnt;   /* a count of the element */

ELEM *top;

};

 

typedef struct stack STACK;



리스트개념으로 스택


/* in file stack.h */

 

#include <stdlib.h>

#include "stackdec.h"

 

/* first intializing stack */

void initialize(STACK *stk)

{

stk -> cnt = 0;

stk -> top = NULL;

}

 

void push(DATA dt, STACK *stk)

{

ELEM *p;

 

p = (ELEM*)malloc(sizeof(ELEM));

p -> d = dt;

p -> next = stk -> top;

stk -> top = p;

stk -> cnt++;

}

 

DATA pop(STACK *stk)

{

DATA dt;

ELEM *p;

 

dt = stk -> top -> d;

p = stk -> top;

 

stk -> top = stk -> top -> next;

stk -> cnt --;

 

free(p);

return dt;

}

 

DATA top(STACK *stk)

{

return (stk -> top -> d);

}

 

int full(const STACK *stk)

{

return (stk -> cnt == FULL)? 1:0;

}

int empty(const STACK *stk)

{

return (stk -> cnt == EMPTY)? 1:0;

}


베이직 자료구조는 진짜;;

'알고리즘' 카테고리의 다른 글

heap_sort (concept)  (0) 2009.09.16
insert_sort  (0) 2009.09.16
Binary_Tree(simple)  (0) 2009.09.16
Queue (list)  (0) 2009.09.16
Stack  (0) 2009.09.16
linked_list  (0) 2009.09.16
Posted by 멍충한아싸

댓글을 달아 주세요