최근에 데이터 구조를 검토하고 스택을 직접 구현했습니다. 스택은 한 위치의 삽입 및 삭제를 제한하는 테이블 유형입니다. 가장 기본적인 작업은 스택 안팎으로 이루어 지므로 "첫 번째 및 아웃"테이블이라고도합니다.
먼저 스택의 개념을 이해해 봅시다 :
스택은 테이블 헤더에서만 삽입 및 삭제 작업을 제한하는 선형 테이블입니다. 때로는 Lifo (First Out Table의 최신)라고도합니다. 이 개념을 이해하려면 먼저 "스택"의 원래 의미를 이해하여 본질을 파악할 수 있어야합니다.
"스택"은 상품이 저장되거나 승객이 창고 나 대중 교통 스테이션으로 확장 될 수있는 장소를 의미합니다. 따라서 데이터가 일시적으로 저장되는 장소를 나타내는 컴퓨터 필드에 도입되므로 스택으로 들어가 스택을 종료한다는 말이 있습니다.
구현 방법은 다음과 같습니다. 먼저 인터페이스를 정의한 다음이 인터페이스를 통해 선형 스택과 체인 스택을 구현하십시오. 코드는 다음과 같이 비교적 간단합니다.
패키지 com.peter.java.dsa.interfaces;/*** 스택 운영 정의** @author peter pan*/public interface stack <t> {/* query empty*/boolean isempty ();/* clear stack*/void clear ();/* 황소 스택*/t pop (); 스택의 상단에있는 요소이지만 제거하지 않지만*/t peek ();/*스택에서 객체의 위치를 반환*/int 검색 (t data);}.선형 스택 : 배열에서 구현되었습니다.
package com.peter.java.dsa.common; import com.peter.java.dsa.interfaces.stack;/** * 선형 스택 * * * @author peter pan */public class linearstack <t> 스택 <t> {@suppresswarnings ( "unchecked") t [])@object [16]; Boolean isempty () {// 자동 생성 된 메소드 stubreturn size == 0;}@public void void stubfor (int i = 0; i <t.length; i ++) {t [i] = null;} size = 0;}@avergend pop () {// auto regreded methods () {t [i] = null;} size = 0;} {// 0) {return null;} t tmp = t [size-1]; t [size-1] = null; size-; return tmp;}@public boolean 푸시 (t data) {// 자동 조성 된 메소드 stubif (size> = t.length) {resize ();} t [size ++] = data;}@everride int (true; 자동 생성 된 메서드 stubreturn size;}@public t peek () {// todo 자동 생성 메소드 stubif (size == 0) {return null;} else {return t [size -1];}}/ * 데이터가 데이터가없는 경우 -1 {tearide public indect (t data) {// auto auto -genderated index (to auto -genderated indel) 0; i <t.length; if (t [i] .equals (data)) {index = i; break;}}}@suppresswarnings (private void resize () {t [] new object [t.length * 2]; t [i]; t [i] = null;} t = tmp; tmp = null;}/ * 왼쪽에서 오른쪽에서 오른쪽으로 스택의 상단에서 오버리 공개 문자열 toString () {// a auto-renerated method stubstringbuffer buffer = new StringBuffer () -1; i---) {buffer.append (t [i] .tostring () + ","); buffer.replace (buffer.lastindexof ( "), buffer.lastindexof (", "));체인 스택 : 단일 링크 목록을 통해 구현되었습니다.
package com.peter.java.dsa.common; import com.peter.java.dsa.interfaces.stack; public class linkedstack <t> 구현 스택 <t> {private node top; private int size; @override public boolean isempty () {// to auto-gencerated size == 0; 자동 유발 된 메소드 stubtop = null; size = 0;}@public t pop () {// a auto-auto-regenated method stubt topvalue = null; if (top! = null) {topvalue = top.data; node oldtop = top = top.prev; oldtop.prev = null; 푸시 (t data) {// TODO 자동 생성 된 메소드 스터브 노드 OldTop = top; top = new Node (data); top.prev = oldtop; size ++; return true;}@public int longth () {// a auto auto-auto-auto a auto auto auto auto-aut a auto auto-gugenderated public teek () ! = null) {topValue = top.data;} return topValue;}@public int search (t data) {// todo 자동 생성 메소드 stubint index = -1; node tmp = top; for (int i = size -1; i> -1; i-) {if (tmp.data.equals (data))) {indep (index)) {indect. tmp.prev;}} tmp = null; null; return index;}@public string toString () {// todo 자동 생성 메소드 stubstringbuffer (); buffer.append ( "링크 된 스택 컨텐츠 : ["); node tmp = top; for (int i = 0; i ++). {buffer.append (tmp.toString () + ","); tmp = tmp.prev;} tmp = null; buffer.append ( "]"); buffer.replace ( ","), buffer.lastindexof ( ",") + 1, ");}}}}},} retue no node (}); 데이터; 노드 이전; public node (t data) {// todo 자동 생성 생성자 stubthis.data = data;}}}학습은 여전히 진행 중이며 코드는 향후 계속 업데이트 될 것입니다.
이것은 Java 언어 구현 데이터 구조 스택 코드에 대한 자세한 설명입니다. 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구는이 사이트의 다른 관련 주제를 계속 참조 할 수 있습니다. 단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!