스택은 컴퓨터에서 사용되는 기본 데이터 구조 중 하나로 데이터를 후입선출(LIFO : LastIn, FirstOut, FILO : FirstIn, LastOut) 구조로 유지하는 추상 데이터형(abstractdatatype)을 말한다.
#include <stdio.h>
#include <malloc.h>
#define MAX 10
int stack[MAX];
int top;
int push(int data)
{
if (top < MAX) {
stack[top++] = data;
return data;
}
printf("Stack overflow\n");
return -1;
}
int pop(void) {
if (top == 0) {
printf("Stack underflow\n");
return -1;
}
return stack[top--];
}
void printList() {
int i;
printf("Stack :");
for (i = top - 1; i >= 0; --i) {
printf(" %d", stack[i]);
}
printf("\n");
}
int main()
{
int i;
top = 0;
pop();
for (i = 0; i < 11 ; ++i) {
push(i);
}
printList();
return 0;
}
* 출처 : https://www.tutorialspoint.com/data_structures_algorithms/
'SW > 자료구조' 카테고리의 다른 글
Circular Queue (0) | 2019.01.06 |
---|---|
Queue (0) | 2019.01.06 |
Linked List 3 - Circular Linked List (0) | 2019.01.06 |
Linked List 2 - Doubly Linked List (0) | 2019.01.06 |
Linked List 1 - Simple Linked List (0) | 2018.12.31 |