DS study : stack
This commit is contained in:
parent
241f938c92
commit
a1db4a6823
83
code_study/data_structure/DS_stack.c
Normal file
83
code_study/data_structure/DS_stack.c
Normal file
@ -0,0 +1,83 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_SIZE 100
|
||||
|
||||
typedef struct Stack Stack;
|
||||
|
||||
struct Stack {
|
||||
int data[MAX_SIZE];
|
||||
int top;
|
||||
|
||||
void (*push)(Stack *s, int value);
|
||||
int (*pop)(Stack *s);
|
||||
bool (*isEmpty)(Stack *s);
|
||||
};
|
||||
|
||||
void _push(Stack *s, int value){
|
||||
if(s->top == MAX_SIZE-1){
|
||||
printf("Stack is full!");
|
||||
return;
|
||||
}
|
||||
s->data[++(s->top)] = value;
|
||||
printf("stack : ");
|
||||
for(int i=0; i<=s->top; i++){
|
||||
printf("%d ",s->data[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int _pop(Stack *s){
|
||||
if(s->top == -1){
|
||||
printf("Stack is empty ");
|
||||
return -1;
|
||||
}
|
||||
return s->data[(s->top)--];
|
||||
}
|
||||
|
||||
bool _isEmpty(Stack *s){
|
||||
return s->top == -1;
|
||||
}
|
||||
|
||||
Stack creatStack(){
|
||||
Stack s;
|
||||
s.top = -1;
|
||||
s.push = _push;
|
||||
s.pop = _pop;
|
||||
s.isEmpty = _isEmpty;
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(){
|
||||
Stack stack = creatStack();
|
||||
while(true){
|
||||
char input[10];
|
||||
|
||||
printf("\n명령을 입력하세요 (push, pop, isEmpty, exit): ");
|
||||
scanf("%s", input);
|
||||
|
||||
if(!strcmp(input, "push")){
|
||||
int value;
|
||||
printf("스택에 넣을 값을 입력해주세요 : ");
|
||||
scanf("%d", &value);
|
||||
stack.push(&stack, value);
|
||||
}
|
||||
else if(!strcmp(input, "pop")){
|
||||
int poppedValue = stack.pop(&stack);
|
||||
|
||||
if(poppedValue != -1){
|
||||
printf("Pop value : %d\n", poppedValue);
|
||||
}
|
||||
}
|
||||
else if(!strcmp(input, "isEmpty")){
|
||||
printf("%s\n", stack.isEmpty(&stack) ? "스택이 비었습니다" : "스택이 비어있지 않습니다.");
|
||||
}
|
||||
else {
|
||||
printf("종료합니다\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
94
code_study/data_structure/DS_stack.java
Normal file
94
code_study/data_structure/DS_stack.java
Normal file
@ -0,0 +1,94 @@
|
||||
import java.util.*;
|
||||
|
||||
class Stack{
|
||||
private int[] data;
|
||||
private int top;
|
||||
private int maxSize;
|
||||
|
||||
Stack(int maxSize){
|
||||
data = new int[maxSize];
|
||||
top = -1;
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
void stackInfo(){
|
||||
if(top == -1){
|
||||
System.out.println("현재 스택 목록 : Empty!");
|
||||
return;
|
||||
}
|
||||
System.out.printf("현재 스택 목록 : ");
|
||||
for(int i=0; i<=top; i++) {
|
||||
System.out.printf("%d ",this.data[i]);
|
||||
}
|
||||
System.out.printf("\ntop : %d\n",this.top);
|
||||
}
|
||||
|
||||
void push(int value){
|
||||
if(top == maxSize - 1){
|
||||
System.out.println("스택이 꽉찼기 때문에 추가할 수 없습니다.");
|
||||
return;
|
||||
}
|
||||
this.data[++(this.top)] = value;
|
||||
}
|
||||
|
||||
void pop(){
|
||||
if(this.top == -1){
|
||||
System.out.println("스택이 비어있어서 처리할 수 없습니다.");
|
||||
return;
|
||||
}
|
||||
System.out.printf("빠져나온 데이터 : %d\n",this.data[(this.top)--]);
|
||||
}
|
||||
|
||||
int peek(){
|
||||
return this.data[top];
|
||||
}
|
||||
}
|
||||
|
||||
public class DS_stack {
|
||||
public static void main(String[] args) {
|
||||
Scanner Order = new Scanner(System.in);
|
||||
System.out.printf("스택의 최대길이를 입력해주세요. : ");
|
||||
int maxSize = Integer.parseInt(Order.nextLine());
|
||||
Stack stack = new Stack(maxSize);
|
||||
boolean exit = false;
|
||||
|
||||
while(!exit){
|
||||
System.out.println("======명령어 목록======");
|
||||
System.out.println("1. push <value>");
|
||||
System.out.println("2. pop");
|
||||
System.out.println("3. peek");
|
||||
System.out.println("4. info");
|
||||
System.out.println("5. exit");
|
||||
System.out.println("=====================");
|
||||
System.out.printf("명령어를 입력하세요 : ");
|
||||
String[] order = Order.nextLine().split(" ");
|
||||
if (order[0] != null){
|
||||
switch (order[0]){
|
||||
case "push":
|
||||
stack.push(Integer.parseInt(order[1]));
|
||||
break;
|
||||
case "pop":
|
||||
stack.pop();
|
||||
break;
|
||||
case "peek":
|
||||
System.out.printf("최상단 값 : %d\n",stack.peek());
|
||||
break;
|
||||
case "info":
|
||||
stack.stackInfo();
|
||||
break;
|
||||
case "exit":
|
||||
exit = true;
|
||||
System.out.println("종료합니다!");
|
||||
break;
|
||||
default:
|
||||
System.out.println("제대로 입력해주세요.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
else{
|
||||
System.out.println("명령어 목록을 잘 확인해서 다시 입력해주세요.");
|
||||
}
|
||||
}
|
||||
Order.close();
|
||||
}
|
||||
}
|
||||
64
code_study/data_structure/DS_stack.py
Normal file
64
code_study/data_structure/DS_stack.py
Normal file
@ -0,0 +1,64 @@
|
||||
import sys
|
||||
|
||||
input = sys.stdin.readline
|
||||
|
||||
class Stack:
|
||||
def __init__(self, maxSize):
|
||||
self.__size = maxSize
|
||||
self.__data = [0]*maxSize
|
||||
self.__top = -1
|
||||
|
||||
def stackInfo(self):
|
||||
if self.__top == -1 :
|
||||
print("Stack is Empty")
|
||||
return
|
||||
|
||||
print("====현재 스택목록({}/{})====".format(self.__top+1,self.__size))
|
||||
for i in range(self.__top+1):
|
||||
print(self.__data[i], end = " ")
|
||||
print()
|
||||
|
||||
def push(self, value):
|
||||
if self.__top == self.__size - 1 :
|
||||
print("스택이 꽉찼기 때문에 동작을 수행할 수 없습니다.")
|
||||
return
|
||||
self.__top += 1
|
||||
self.__data[self.__top] = value
|
||||
print("push 완료")
|
||||
|
||||
def pop(self):
|
||||
if self.__top == -1 :
|
||||
print("스택이 비어있어서 pop할 값이 없습니다")
|
||||
return
|
||||
print(self.__data[self.__top])
|
||||
self.__top -= 1
|
||||
|
||||
def peek(self):
|
||||
if self.__top == -1 :
|
||||
print("스택이 비어있어서 최상단 값이 없습니다")
|
||||
return
|
||||
print(self.__data[self.__top])
|
||||
|
||||
print("스택의 최대사이즈를 입력해줘!")
|
||||
stack = Stack(int(input()))
|
||||
|
||||
endPoint = False
|
||||
|
||||
while not endPoint :
|
||||
print("명령어 목록")
|
||||
print("push <value> | pop | info | peek | exit")
|
||||
order = input().split()
|
||||
match order[0]:
|
||||
case "push":
|
||||
stack.push(int(order[1]))
|
||||
case "pop":
|
||||
stack.pop()
|
||||
case "info":
|
||||
stack.stackInfo()
|
||||
case "peek":
|
||||
stack.peek()
|
||||
case "exit":
|
||||
print("종료")
|
||||
endPoint = True
|
||||
case _:
|
||||
print("제대로 된 명령어를 입력해주세요.")
|
||||
99
code_study/data_structure/DS_stack.swift
Normal file
99
code_study/data_structure/DS_stack.swift
Normal file
@ -0,0 +1,99 @@
|
||||
class Stack {
|
||||
private var data: [Int]
|
||||
private let maxSize: Int
|
||||
private var top: Int
|
||||
|
||||
init(size: Int) {
|
||||
self.maxSize = size
|
||||
self.data = Array(repeating: 0, count: size)
|
||||
self.top = -1
|
||||
}
|
||||
|
||||
func isFull() -> Bool {
|
||||
return self.top == self.maxSize - 1
|
||||
}
|
||||
|
||||
func isEmpty() -> Bool {
|
||||
return self.top == -1
|
||||
}
|
||||
|
||||
func push(to value: Int) {
|
||||
if isFull() {
|
||||
print("스택 오버플로우! 더 이상 추가할 수 없습니다.")
|
||||
return
|
||||
}
|
||||
self.top += 1
|
||||
self.data[top] = value
|
||||
}
|
||||
|
||||
func pop() -> Int? {
|
||||
if isEmpty() {
|
||||
return nil
|
||||
}
|
||||
let popValue = self.data[top]
|
||||
self.top -= 1
|
||||
return popValue
|
||||
}
|
||||
|
||||
func peek() -> Int? {
|
||||
if isEmpty() {
|
||||
return nil
|
||||
}
|
||||
return self.data[top]
|
||||
}
|
||||
|
||||
func info(){
|
||||
if isEmpty() {
|
||||
print("스택이 비어있습니다.")
|
||||
return
|
||||
}
|
||||
print("스택목록 : ", terminator: "")
|
||||
for i in 0...self.top {
|
||||
print(self.data[i], terminator: " ")
|
||||
}
|
||||
print()
|
||||
}
|
||||
}
|
||||
|
||||
let stack: Stack = Stack(size: 10)
|
||||
var exitFlag = true
|
||||
|
||||
while exitFlag {
|
||||
print("명령어를 입력하세요 (push [값], pop, peek, info, exit):")
|
||||
if let input = readLine(){
|
||||
let components = input.split(separator: " ")
|
||||
let order = String(components.first ?? "")
|
||||
|
||||
switch order {
|
||||
case "push":
|
||||
if components.count == 2, let value = Int(components[1]) {
|
||||
stack.push(to: value)
|
||||
print("\(value)를 push했습니다.")
|
||||
} else {
|
||||
print("push 명령어는 'push [숫자]' 형식으로 입력해야 합니다.")
|
||||
}
|
||||
case "pop":
|
||||
if let popValue = stack.pop() {
|
||||
print("Pop: \(popValue)")
|
||||
} else {
|
||||
print("스택이 비어있습니다.")
|
||||
}
|
||||
case "peek":
|
||||
if let peekValue = stack.peek() {
|
||||
print("Peek: \(peekValue)")
|
||||
} else {
|
||||
print("스택이 비어있습니다.")
|
||||
}
|
||||
case "info":
|
||||
stack.info()
|
||||
case "exit":
|
||||
exitFlag = false
|
||||
print("종료합니다")
|
||||
default:
|
||||
print("알 수 없는 명령어입니다.")
|
||||
}
|
||||
}
|
||||
else{
|
||||
print("제대로 입력해주세요")
|
||||
}
|
||||
}
|
||||
77
code_study/data_structure/DS_stack.ts
Normal file
77
code_study/data_structure/DS_stack.ts
Normal file
@ -0,0 +1,77 @@
|
||||
class Stack{
|
||||
private data: number[];
|
||||
private top: number;
|
||||
|
||||
constructor(){
|
||||
this.data = [];
|
||||
this.top = -1;
|
||||
}
|
||||
|
||||
public info(){
|
||||
if(this.top == -1){
|
||||
console.log("스택이 비어있습니다");
|
||||
return;
|
||||
}
|
||||
|
||||
let result: string = "Stack List(" + (this.top+1).toString() + " values) : "
|
||||
for(let i=0; i<=this.top; i++){
|
||||
result += this.data[i].toString() + " ";
|
||||
}
|
||||
|
||||
console.log(result);
|
||||
}
|
||||
|
||||
public push(value: number){
|
||||
this.data[++(this.top)] = value;
|
||||
console.log("push 성공!");
|
||||
}
|
||||
|
||||
public pop(): number | undefined {
|
||||
if(this.top === -1){
|
||||
return undefined;
|
||||
}
|
||||
return this.data[(this.top)--];
|
||||
}
|
||||
|
||||
public peek(): number | undefined {
|
||||
if(this.top === -1){
|
||||
return undefined;
|
||||
}
|
||||
return this.data[this.top];
|
||||
}
|
||||
}
|
||||
|
||||
let stack = new Stack();
|
||||
let exitFlag = true;
|
||||
|
||||
while(exitFlag){
|
||||
console.log("++++++++++++++++명령어모음++++++++++++++++");
|
||||
console.log("push <value> | pop | peek | info | exit");
|
||||
console.log("========================================");
|
||||
console.log("명령어를 입력해주세요!");
|
||||
const input:string[] = require("fs").readFileSync(0,"utf8").toString().trim().split(" ");
|
||||
switch(input[0])
|
||||
{
|
||||
case "push":
|
||||
stack.push(Number(input[1]));
|
||||
break;
|
||||
case "pop":
|
||||
var popValue = stack.pop();
|
||||
console.log(popValue===undefined ? "스택이 비었습니다." : "pop 완료 : " + popValue.toString());
|
||||
break;
|
||||
case "peek":
|
||||
var peekValue = stack.peek();
|
||||
console.log(peekValue===undefined ? "스택이 비었습니다." : "최상단 값 : " + peekValue.toString());
|
||||
break;
|
||||
case "info":
|
||||
stack.info();
|
||||
break;
|
||||
case "exit":
|
||||
exitFlag = false;
|
||||
console.log("종료합니다.")
|
||||
break;
|
||||
default:
|
||||
console.log("제대로 입력해주세요.")
|
||||
break;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user