데이터 구조양 방향 순환 링크 의 자바 구현
22365 단어 데이터 구조
양 방향 순환 링크 의 특징
package list;
/**
* ================================ :========================================
*
* : ( )
*
* :
* 1.
* 2.
* 3.
* 4.
* 6.
*
* :
* 1. init:
* 2. destroy:
* 3. clear:
* 4. length:
* 5. get:
* 6. locateElem:
* 7. priorElem:
* 8. nextElem:
* 9. put:
* 10. insert:
* 11. replace:
* 12. delete:
* 13. isEmpty:
* 14. print:
*
* ======================================================================================
*/
/**
* @author
* @date 2018 7 25 2:26:08
* @version 1.0
* @project list
*/
public class CirLinkList implements List {
/* */
private int LENGTH ;
/* */
private Element head ;
/* */
private class Element{
Object data ;
Element next ;
}
/**
* @see list.List#init()
* @explain init :
* @throws
* @author
* @date 2018 7 25 2:26:52
*/
@Override
public void init() {
head = new Element() ;
head.next = head ;
LENGTH = 0 ;
}
/**
* @explain destroy :
* @throws
* @author
* @date 2018 7 25 2:29:34
*/
@Override
public void destroy() {
/* */
if(head == null){
System.out.println(" : !");
return ;
}
head = null ;
}
/**
* @explain clear :
* @throws
* @author
* @date 2018 7 25 2:30:53
*/
@Override
public void clear() {
/* */
if(head == null){
System.out.println(" : !");
return ;
}
head.next = null ;
}
/**
* @explain length :
* @return int
* @throws
* @author
* @date 2018 7 25 2:32:08
*/
@Override
public int length() {
/* */
if(head == null){
System.out.println(" : !");
return -1;
}
return LENGTH ;
}
/**
* @see list.List#get(int)
* @explain get :
* @param index
* @return
* @throws
* @author
* @date 2018 7 25 2:43:31
*/
@Override
public Object get(int index) {
/* */
if(head == null){
System.out.println(" : !");
return null;
}
/* index */
if(index > LENGTH || index<=0){
System.out.println(" : !");
return null;
}
/* */
Element p = head ;
for(int i=0 ; ireturn p.data;
}
/**
* @see list.List#locateElem(java.lang.Object)
* @explain locateElem :
* @param elem
* @return
* @throws
* @author
* @date 2018 7 25 2:45:09
*/
@Override
public int locateElem(Object elem) {
/* */
if(head == null){
System.out.println(" : !");
return -1;
}
int index = -1 ;
Element p = head.next ;
for(int i=0 ; iif(p.data.equals(elem)){
index = i+1 ;
break ;
}
p = p.next ;
}
return index;
}
/**
* @see list.List#priorElem(java.lang.Object)
* @explain priorElem :
* @param elem
* @return
* @throws
* @author
* @date 2018 7 25 2:46:25
*/
@Override
public Object priorElem(Object elem) {
/* */
if(head == null){
System.out.println(" : !");
return null;
}
Element prior = null ;
Element p = head;
while(p.next != head){
if(p.next.data.equals(elem)){
prior = p ;
break ;
}
p = p.next ;
}
if(p == head){
/* , */
Element q = head.next ;
while(q.next != head){
q = q.next ;
}
prior = q ;
return prior.data ;
}else if(p.next == head){
return null ;
}else{
prior = p ;
return prior.data ;
}
}
/**
* @see list.List#nextElem(java.lang.Object)
* @explain nextElem :
* @param elem
* @return
* @throws
* @author
* @date 2018 7 25 3:31:44
*/
@Override
public Object nextElem(Object elem) {
/* */
if(head == null){
System.out.println(" : !");
return null;
}
Element next = null ;
Element p = head.next;
while(p != head){
if(p.data.equals(elem)){
next = p.next ;
break ;
}
p = p.next ;
}
if(p.next == head){
next = p.next.next ;
return next.data ;
}else{
next = p.next ;
return next.data;
}
}
@Override
public void put(Object elem) {
/* */
if(head == null){
System.out.println(" : !");
return ;
}
/* */
Element e = new Element() ;
e.next = null ;
e.data = elem ;
/* */
Element p = head.next ;
while(p.next != head){
p = p.next ;
}
/* */
p.next = e ;
e.next = head ;
/* +1*/
LENGTH++ ;
}
/**
* @see list.List#insert(int, java.lang.Object)
* @explain insert : elem
* @param index
* @param elem
* @throws
* @author
* @date 2018 7 25 3:35:44
*/
@Override
public void insert(int index, Object elem) {
/* */
if(head == null){
System.out.println(" : !");
return ;
}
/* index */
if(index > LENGTH || index<=0){
System.out.println(" : !");
return ;
}
/* */
Element p = head ;
for(int i=0 ; i1 ; i++){
p = p.next ;
}
/* */
Element curElem = p ;
Element nextElem = p.next ;
Element e = new Element() ;
e.data = elem ;
e.next = nextElem ;
curElem.next = e ;
LENGTH++ ;
}
/**
* @see list.List#replace(int, java.lang.Object)
* @explain replace :
* @param index
* @param elem
* @throws
* @author
* @date 2018 7 25 3:40:45
*/
@Override
public void replace(int index, Object elem) {
/* */
if(head == null){
System.out.println(" : !");
return ;
}
/* index */
if(index > LENGTH || index<=0){
System.out.println(" : !");
return ;
}
/* */
Element p = head ;
for(int i=0 ; i/* */
p.data = elem ;
}
/**
* @see list.List#delete(int)
* @explain delete :
* @param index
* @throws
* @author
* @date 2018 7 25 3:42:49
*/
@Override
public void delete(int index) {
/* */
if(head == null){
System.out.println(" : !");
return ;
}
/* index */
if(index > LENGTH || index<=0){
System.out.println(" : !");
return ;
}
/* */
Element p = head ;
for(int i=0 ; i1 ; i++){
p = p.next ;
}
/* */
Element curElem = p ;
Element nextElem = p.next.next ;
/* , */
curElem.next = nextElem ;
}
/**
* @see list.List#isEmpty()
* @explain isEmpty :
* @return Boolean ,true ,false
* @throws
* @author
* @date 2018 7 25 3:44:04
*/
@Override
public boolean isEmpty() {
boolean flag = false ;
if(head.next == null){
flag = true ;
}
return flag ;
}
/**
* @see list.List#print()
* @explain print : ,
* @throws
* @author
* @date 2018 7 25 3:45:52
*/
@Override
public void print() {
/* */
if(head == null){
System.out.println(" : !");
return ;
}
/* */
Element p = head.next;
while(p != head){
/* */
System.out.print(p.data+" ");
p = p.next ;
}
System.out.println();
}
@Override
public String toString() {
String str = "LinkList:[" ;
Element p = head.next ;
while(p != head){
if(p.next != head){
str = str + p.data +"," ;
p = p.next ;
}else{
str = str + p.data ;
p = p.next ;
}
}
str = str+"]" ;
return str;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
정수 반전Udemy 에서 공부 한 것을 중얼거린다 Chapter3【Integer Reversal】 (예) 문자열로 숫자를 반전 (toString, split, reverse, join) 인수의 수치 (n)가 0보다 위 또는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.