DS stack 스택 자료구조 (C)

less than 1 minute read

STACK

  • stack is an ordered list in which insertions (push & add) and deletions (pop & remove) are made at on end called top
  • Last-In-First-Out (LIFO)

stack

ADT

  • objects : a finite ordered list with zero or more elements
  • functions :
Stack CreateS(maxStackSize) create an empty stack whos maximum size if maxStackSize
Boolean IsFull(stack,maxStackSize) if (number of elements in stack == maxStackSize)
return TRUE
else return FALSE
Stack Push(stack, item) if (IsFull(stack)) stack full
else insert item into top of stack and return
Boolean IsEmpty(stack) if (stack == CreateS(maxStackSize))
return TRUE
else return FALSE
Element Pop(stack) if (IsEmpty(stack)) return
else remove and return the element at th top of the stack

Push() & Pop()

보통 아래와 같은 형식으로 함수를 사용한다.

  • push(item)
  • item = pop()
void push(element item)
{
    if (top >= MAX_STACK_SIZE-1)  # 가득 차있으면 넣을  없음
        stackFull();
    stack[++top] = item;  # push 하면 top += 1 해줘야  (다음 element 가리키게)
}
void pop()
{
    if (top < 0)				# 비어있다면   없음
        return stackEmpty();
    return stack[--top];		# 하나를 빼줬으니, top -= 1 해줌!
}

Comments