-
[CS 자료구조] 스택에 대한 개념WEB & CS 2023. 11. 8. 12:42
스택
- 후입선출 (LIFO)
- 최근에 들어온 데이터가 가장 먼저 나간다.

배열 스택
class ArrayStack { private int top; private int stackSize; private char itemArray[]; public ArrayStack(int stackSize){ top = -1; this.stackSize = stackSize; itemArray = new char[this.stackSize]; } public boolean isEmpty(){ return (top == -1); } public boolean isFull(){ return (top == this.stackSize-1); } public void push(char item){ if(isFull()){ System.out.println("Inserting fail! Array Stack is full!!"); } else{ itemArray[++top] = item; System.out.println("Inserted Item : " + item); } } public char pop(){ if(isEmpty()) { System.out.println("Deleting fail! Array Stack is empty!!"); return 0; } else{ return itemArray[top--]; } } public void delete(){ if(isEmpty()){ System.out.println("Deleting fail! Array Stack is empty!!"); } else { top--; } } public char peek(){ if(isEmpty()){ System.out.println("Peeking fail! Array Stack is empty!!"); return 0; } else return itemArray[top]; } public void printStack(){ if(isEmpty()) System.out.println("Array Stack is empty!! %n %n"); else{ System.out.printf("Array Stack>> "); for(int i=0; i<=top; i++) { System.out.printf("%c ", itemArray[i]); } System.out.println(); } } } public class Main{ public static void main(String args[]){ int stackSize = 5; char deletedItem; ArrayStack S = new ArrayStack(stackSize); S.push('A'); // Inserted Item : A S.printStack(); // Array Stack>> A S.push('B'); // Inserted Item : B S.printStack(); // Array Stack>> A B S.push('C'); // Inserted Item : C S.printStack(); // Array Stack>> A B C deletedItem = S.pop(); if(deletedItem != 0) { System.out.println("deleted Item : " + deletedItem); // deleted Item : C } S.printStack(); // Array Stack>> A B } }리스트 스택
interface Stack{ boolean isEmpty(); void push(char item); char pop(); void delete(); char peek(); } class StackNode{ char data; StackNode link; } class LinkedStack implements Stack{ private StackNode top; public boolean isEmpty(){ return (top == null); } public void push(char item){ StackNode newNode = new StackNode(); newNode.data = item; newNode.link = top; top = newNode; System.out.println("Inserted Item : " + item); } public char pop(){ if(isEmpty()) { System.out.println("Deleting fail! Linked Stack is empty!!"); return 0; } else{ char item = top.data; top = top.link; return item; } } public void delete(){ if (isEmpty()){ System.out.println("Deleting fail! Linked Stack is empty!!"); } else { top = top.link; } } public char peek(){ if (isEmpty()){ System.out.println("Peeking fail! Linked Stack is empty!!"); return 0; } else return top.data; } public void printStack(){ if(isEmpty()) System.out.printf("Linked Stack is empty!! %n %n"); else{ StackNode temp = top; System.out.println("Linked Stack>> "); while(temp != null){ System.out.printf("\t %c \n", temp.data); temp = temp.link; } System.out.println(); } } } public class Main{ public static void main(String args[]){ char deletedItem; LinkedStack LS = new LinkedStack(); LS.push('A'); LS.printStack(); LS.push('B'); LS.printStack(); LS.push('C'); LS.printStack(); deletedItem = LS.pop(); if(deletedItem != 0) { System.out.println("deleted Item : " + deletedItem); } LS.printStack(); /* Inserted Item : A Linked Stack>> A Inserted Item : B Linked Stack>> B A Inserted Item : C Linked Stack>> C B A deleted Item : C Linked Stack>> B A */ } }출처)
'WEB & CS' 카테고리의 다른 글
[CS 자료구조] 큐에 대한 내용 (0) 2023.11.08 객체 지향 프로그래밍의 개념 및 4가지 특징 (0) 2023.10.10 WAS(Web Application Server)와 WS(Web Server) 차이점 (0) 2023.10.04 Redirect와 Forward의 차이점 (0) 2023.09.29 프로세스(Process) & 스레드(Thread) 정리 (0) 2023.08.13