2021년 목표설정

이미지
기본적으로 작년에 달성하지 못한 것들을 하려고 생각중인데..코로나가 언제까지 이어질지, 한국이나 북해도는 갈 수 있을지..자격증은 응시 가능할지..여러가지가 불확실하다. 2021년은 무엇보다 정신적인 부분과 경제적인 부분에 중점을 두고 조금 더 치열하게 지내보고 싶다. 일본나이로도 30대 마지막 해, 이제 불혹에 접어드는 나이..복잡하지만 심플하게. 육체적목표 : 트라이에슬론 스탠다드 도전하기 정신적 : 자격증2개 도전 + 자체개발 서비스 론칭 가족적 : 가정의 평화를 유지하기 경제적 : 외식과 유흥비를 줄이고 부수입을 늘려서 결과적으로 저축하기 사회적 : 목표세미나를 포함해서 민단과 개인인맥의 활성화와 교류를 촉진하기

JAVA로 링크드리스트 스택 구현하기 + 괄호매칭 프로그램

어찌어찌 어설프게 구현해봤다...;;;;
괄호매칭 프로그램을 스택으로 구현하기 과제였는데..
자바로 링크드리스트 구현하는 예제가 별로 없어서 맨땅에 헤딩하듯이 구현했다.
프로그램 요구사항과 링크드리스트가 돌아가는 모양새를 대충 임시코드로 적어놓은 후에...
줄줄줄줄 문법대로 적어보니 어찌 에러없이 돌아가긴 한다;;;; 참 쉽죠잉~~~~ㅡ.ㅡ;;;;
평소에 워낙 코딩도 못 하고...제대로 한 줄 짜는 것 어려워서 덜덜덜 떨었는데...
조금씩 프로그래밍 실력이 늘어가는구나...생각이 든다.
확실히 #include 나 import java.io 부터 치고드는 것이 아니라..
뭘 만들어야 하는지 분석해서 그것을 노트에 그리거나 임시적으로 코드를 생성한 후..
차근차근 하나하나 만들어가는 것이 훨씬 더 쉽고 중요한 과정이라는 것을 절실하게 깨달았다.
이 코드가 비록 최적화 된 코드도 아니고 에러가 있을거라 생각하는데다가...(특히 null 할당부분)
링크를 중간에 추가하는 코드는 없지만 그래도 올려보자 아하하하하하

//////// 링크드리스트로 구현한 스택(MyStack.java)
public class MyStack
{
 private String strData;  // Data field on Node
 private MyStack pNext;  // Next Node Reference field
 private MyStack pPrev;  // previous Node Reference field

 static private int countNode = 0; // number of Node and size counter for Stack
 static private MyStack stackPointer = null; // Stack Pointer
 static private MyStack tempPointer = null; // temp Pointer for deleteNode logic.

 MyStack() // Constructor
 {
  this.pNext = null; // pNext initialize
  this.pPrev = null; // pPrev initialize
 }

 public String getData() // interface (method) for get by Data field
 {
  return this.strData; 
 }

 public void setData (String parenChar) // interface (method) for assign to Data field
 {
  this.strData = parenChar;
 }

// Definition of Linked List
 static void createNode(String inputParam) // create a new Node
 {
  if (countNode == 0) // make a Node
  {
   MyStack myList = new MyStack(); // instance of MyStack
   myList.setData(inputParam); // set data to Data field
   stackPointer = myList; // set stackPointer
   countNode++; // increase countNode
  }
  else // add a Node
  {
   MyStack myList = new MyStack(); // instance of MyStack
  
   // logic for add Node
   stackPointer.pNext = myList;
   myList.pNext = null;
   myList.pPrev = stackPointer;
   stackPointer = myList;
  
   myList.setData(inputParam); // set data to Data field
  
   countNode++; // increase countNode
  } 
 } // end of createNode()

 static void deleteNode() // delete Node
 {
  if (countNode == 0) // if none Node
  {
   System.out.println("NG.");
   System.exit(1);
  }
  else // can delete node
  {
   if (countNode >= 2) // have two nodes
   {
    // logic for delete node
    tempPointer = stackPointer.pPrev;
    stackPointer = null;
    stackPointer = tempPointer;
    stackPointer.pNext = null;
    tempPointer = null;
   
    countNode--; // decrease countNode
   }
   else // have one node
   {
    stackPointer = null;
    countNode--;
   }
  } // end of if
 
 } //end of deleteNode
// Linked List Definition End


//start Definition of Stack

 public void pop() // pop method
 {
  if ( empty() ) // if stack is empty, means none node
  {
     System.out.println("==>Stack overflow"); // Stack overflow
     System.exit(1);
  }
 
  deleteNode(); // function call to deleteNode
 }

 public void push(String inputStr) // pust method
 {
  if ( full() ) // if stack is full, means have more than 10 nodes
  {
   System.out.println("==>Stack underflow"); // Stack underflow
   System.exit(1);
  }
 
  createNode(inputStr); // function call to CreateNode with inputString
 }

 public boolean empty() // check stack empty
 {
  boolean testEmpty = true;
 
  if ( countNode == 0 )
   testEmpty = true;
  else
   testEmpty = false;
 
  return testEmpty;
 } // end of empty
 public boolean full() // check stack full
 {
  boolean testFull = true;

  if ( countNode >= 10 )
   testFull = true;
  else
   testFull = false;
 
  return testFull;
 } // end of IsFull()

} // end of MyStack()


//////////// 여기서부터는 괄호매칭 로직 (Balanced.java)

public class Balanced {
    public static void main(String[] args) {
 if (args.length != 1) {
     System.out.println("Usage: java Balanced PARENTHESES");
     System.exit(1);
 }
 String in = args[0];
 System.out.println("Input string: " + in);
 Balanced b = new Balanced();
 b.parse(in);
    }
   
    /**
     * @param input a parenthesis string
     */
    public void parse(String input) {
 int check = 0;
 char ch;
 MyStack stack = new MyStack();
 for (int i = 0; i < input.length(); i++) {
     ch = input.charAt(i);
     if (ch == '(') {
  if (!stack.full()) {
      stack.push("(");
      check++;
  } else {
      System.out.println("==>Stack overflow");
      error();
  }
     } else if (ch == ')') {
  if (!stack.empty()) {
      stack.pop();
      check--;
  } else {
      System.out.println("==>Stack underflow");
      error();
  }
     } else {
  System.out.println("Invalid character parsed.");
  error();
     }
 }
 if (stack.empty()) {
     System.out.println("OK.");
 } else {
     error();
 }
    }
 void error() {
 System.out.println("NG.");
 System.exit(1);
    }
}

댓글

이 블로그의 인기 게시물

성공적인 소셜커머스를 위한 10단계 전략

[C# & LINQ] 랜덤으로 데이터를 한 개 추출하는 방법

[메모] PostgreSQL에서 Insert 하는 경우 자동채번 PK가 중복에러 나는 경우