2014년 4월 2일 수요일

자바단순연결 리스트(Singly Linked List)를 이용한 예제입니다.[자바개발자교육/자바교육/자바강좌/자바교육잘하는곳/자바교육추천/자바실무교육/JAVA/JAVA교육/JAVA학원/JAVA실무교육]

자바단순연결 리스트(Singly Linked List)를 이용한 예제입니다.[자바개발자교육/자바교육/자바강좌/자바교육잘하는곳/자바교육추천/자바실무교육/JAVA/JAVA교육/JAVA학원/JAVA실무교육]
 

참고하세요~

class ChainNode  {             
private PersonalData element;          //Node안에 저장된 item(Object)
private ChainNode next;                  //다음  Node를 가리키는 Reference

ChainNode() {}   
    ChainNode(PersonalData newElement) {
element = newElement;
next = null;
    }

    ChainNode(PersonalData newElement, ChainNode nextNode) {
element = newElement;
next = nextNode;
    }
 
    public void setElement(PersonalData newItem) {
element = newItem;
    }

    public PersonalData getElement() {
return element;
    }

    public void setNext(ChainNode nextNode) {
next = nextNode;
    }

    public ChainNode getNext() {
return next;
    }
}



class PersonalData {
    String name;
    String number;
   
    public PersonalData(String name, String number){
      this.name = name;
      this.number = number;     
    }
       
    public String getName() {
      return name;
    }

    public String getNumber() {
      return number; 
    }   
public void setNumber(String number) {
      this.number = number;
    }
   
    public boolean equals(Object o){     
      return name.equals(((PersonalData)o).getName());
    }
public String toString(){
      return name+"씨의 전화번호는" +number +" 입니다";
    }
}


public class PersonalDataManager {
    // The Node of the list
    private ChainNode firstNode; 

    // The number of elements in the list
    private int size;   

    public PersonalDataManager() {
firstNode = null;
size = 0;
    }
   
public void add(PersonalData element)  {
add(size, element);
}

    public void add(int index, PersonalData element)  {
// Check for a valid index
 if (index < 0 || index > size) throw new IndexOutOfBoundsException
              ("index = " + index + "  size = " + size);

// Case 1: index == 0
// Insert at the head of the list
if(index == 0) {
ChainNode newNode = new ChainNode(element);
newNode.setNext(firstNode);
firstNode = newNode;
size++;
return;
}

// Case 2: index != 0
// Insert somewhere else in the list
ChainNode previousNode = find(index);
ChainNode newNode = new ChainNode(element);
newNode.setNext(previousNode.getNext());
previousNode.setNext(newNode);
size++;
    }

public void remove(String name) {
for(int i = 0; i < size; i++) {
PersonalData p = get(i);
if (p.getName().equals(name)){
remove(i);
}
}
}
   
    public void remove(int index)  {
// Check for a valid index
checkIndex(index);

// Case 1: index == 0
// Delete the firstNode of the list
if(index == 0) {
firstNode = firstNode.getNext();
size--;
return;
}

// Case 2: index != 0
// Delete something in the middle of the list
ChainNode previousNode = find(index);
previousNode.setNext(previousNode.getNext().getNext());
size--;
    }

public PersonalData search(String name)  {
for(int i = 0; i < size; i++) {
PersonalData p = get(i);
if (p.getName().equals(name)){
return p;
}
}
System.out.println("data not found...");
return null;
}

public void change(String name, String tel)  {
PersonalData p = search(name);
if (p != null){
p.setNumber(tel);
}
}

public void printAll() {
for(int i = 0; i < size; i++) {
System.out.println(get(i));   
}
}


    public PersonalData get(int index)  {
// Check for a valid index
checkIndex(index);

// Case 1: index == 0
// Get the firstNode of the list
if(index == 0) {
return firstNode.getElement();
}

// Case 2: index != 0
// Get something from the middle of the list
ChainNode previousNode = find(index);
return((previousNode.getNext()).getElement());
    }


    // Returns the node *before* the desired one. 
    private ChainNode find(int index) {
int i;
ChainNode currentNode;

// Traverse the links until we get to the one just before the desired one. 
currentNode = firstNode;
for(i = 0; i < size; i++) {
    if(i == index-1)
return(currentNode);

    // bad index
    if(currentNode == null)
return(null);

    currentNode = currentNode.getNext();
}
return null;
    }

  // empties the list
    public void removeAll() {
firstNode = null;
size = 0;
    }

    // returns the length of the list
    public int size() {
return size;
    }

void checkIndex(int index)
  { 
        if (index < 0 || index >= size) throw new IndexOutOfBoundsException
              ("index = " + index + "  size = " + size);
    }

}


class TelephonebookMain {
    public static void main(String[] args) {
        PersonalDataManager manager = new PersonalDataManager();
        manager.add(new PersonalData("aaaa", "333-3333"));
        manager.add(new PersonalData("bbbb", "444-4444"));
        manager.add(new PersonalData("cccc", "555-5555"));
       
        //변경을 적용
        print(manager.search("bbbb"));
        manager.change("cccc","123-1234");
        print(manager.search("cccc")); 
System.out.println("------------------------------");
//삭제를 적용
        manager.remove("bbbb");       
        manager.printAll();       
    }
    public static void print(PersonalData p){
        System.out.println(p);       
    }
}
단순연결 리스트(Singly Linked List)를 이용한 예제입니다..
참고하세요~

class ChainNode  {             
private PersonalData element;          //Node안에 저장된 item(Object)
private ChainNode next;                  //다음  Node를 가리키는 Reference

ChainNode() {}   
    ChainNode(PersonalData newElement) {
element = newElement;
next = null;
    }

    ChainNode(PersonalData newElement, ChainNode nextNode) {
element = newElement;
next = nextNode;
    }
 
    public void setElement(PersonalData newItem) {
element = newItem;
    }

    public PersonalData getElement() {
return element;
    }

    public void setNext(ChainNode nextNode) {
next = nextNode;
    }

    public ChainNode getNext() {
return next;
    }
}



class PersonalData {
    String name;
    String number;
   
    public PersonalData(String name, String number){
      this.name = name;
      this.number = number;     
    }
       
    public String getName() {
      return name;
    }

    public String getNumber() {
      return number; 
    }   
public void setNumber(String number) {
      this.number = number;
    }
   
    public boolean equals(Object o){     
      return name.equals(((PersonalData)o).getName());
    }
public String toString(){
      return name+"씨의 전화번호는" +number +" 입니다";
    }
}


public class PersonalDataManager {
    // The Node of the list
    private ChainNode firstNode; 

    // The number of elements in the list
    private int size;   

    public PersonalDataManager() {
firstNode = null;
size = 0;
    }
   
public void add(PersonalData element)  {
add(size, element);
}

    public void add(int index, PersonalData element)  {
// Check for a valid index
 if (index < 0 || index > size) throw new IndexOutOfBoundsException
              ("index = " + index + "  size = " + size);

// Case 1: index == 0
// Insert at the head of the list
if(index == 0) {
ChainNode newNode = new ChainNode(element);
newNode.setNext(firstNode);
firstNode = newNode;
size++;
return;
}

// Case 2: index != 0
// Insert somewhere else in the list
ChainNode previousNode = find(index);
ChainNode newNode = new ChainNode(element);
newNode.setNext(previousNode.getNext());
previousNode.setNext(newNode);
size++;
    }

public void remove(String name) {
for(int i = 0; i < size; i++) {
PersonalData p = get(i);
if (p.getName().equals(name)){
remove(i);
}
}
}
   
    public void remove(int index)  {
// Check for a valid index
checkIndex(index);

// Case 1: index == 0
// Delete the firstNode of the list
if(index == 0) {
firstNode = firstNode.getNext();
size--;
return;
}

// Case 2: index != 0
// Delete something in the middle of the list
ChainNode previousNode = find(index);
previousNode.setNext(previousNode.getNext().getNext());
size--;
    }

public PersonalData search(String name)  {
for(int i = 0; i < size; i++) {
PersonalData p = get(i);
if (p.getName().equals(name)){
return p;
}
}
System.out.println("data not found...");
return null;
}

public void change(String name, String tel)  {
PersonalData p = search(name);
if (p != null){
p.setNumber(tel);
}
}

public void printAll() {
for(int i = 0; i < size; i++) {
System.out.println(get(i));   
}
}


    public PersonalData get(int index)  {
// Check for a valid index
checkIndex(index);

// Case 1: index == 0
// Get the firstNode of the list
if(index == 0) {
return firstNode.getElement();
}

// Case 2: index != 0
// Get something from the middle of the list
ChainNode previousNode = find(index);
return((previousNode.getNext()).getElement());
    }


    // Returns the node *before* the desired one. 
    private ChainNode find(int index) {
int i;
ChainNode currentNode;

// Traverse the links until we get to the one just before the desired one. 
currentNode = firstNode;
for(i = 0; i < size; i++) {
    if(i == index-1)
return(currentNode);

    // bad index
    if(currentNode == null)
return(null);

    currentNode = currentNode.getNext();
}
return null;
    }

  // empties the list
    public void removeAll() {
firstNode = null;
size = 0;
    }

    // returns the length of the list
    public int size() {
return size;
    }

void checkIndex(int index)
  { 
        if (index < 0 || index >= size) throw new IndexOutOfBoundsException
              ("index = " + index + "  size = " + size);
    }

}


class TelephonebookMain {
    public static void main(String[] args) {
        PersonalDataManager manager = new PersonalDataManager();
        manager.add(new PersonalData("aaaa", "333-3333"));
        manager.add(new PersonalData("bbbb", "444-4444"));
        manager.add(new PersonalData("cccc", "555-5555"));
       
        //변경을 적용
        print(manager.search("bbbb"));
        manager.change("cccc","123-1234");
        print(manager.search("cccc")); 
System.out.println("------------------------------");
//삭제를 적용
        manager.remove("bbbb");       
        manager.printAll();       
    }
    public static void print(PersonalData p){
        System.out.println(p);       
    }
}
단순연결 리스트(Singly Linked List)를 이용한 예제입니다..
참고하세요~

class ChainNode  {             
private PersonalData element;          //Node안에 저장된 item(Object)
private ChainNode next;                  //다음  Node를 가리키는 Reference

ChainNode() {}   
    ChainNode(PersonalData newElement) {
element = newElement;
next = null;
    }

    ChainNode(PersonalData newElement, ChainNode nextNode) {
element = newElement;
next = nextNode;
    }
 
    public void setElement(PersonalData newItem) {
element = newItem;
    }

    public PersonalData getElement() {
return element;
    }

    public void setNext(ChainNode nextNode) {
next = nextNode;
    }

    public ChainNode getNext() {
return next;
    }
}



class PersonalData {
    String name;
    String number;
   
    public PersonalData(String name, String number){
      this.name = name;
      this.number = number;     
    }
       
    public String getName() {
      return name;
    }

    public String getNumber() {
      return number; 
    }   
public void setNumber(String number) {
      this.number = number;
    }
   
    public boolean equals(Object o){     
      return name.equals(((PersonalData)o).getName());
    }
public String toString(){
      return name+"씨의 전화번호는" +number +" 입니다";
    }
}


public class PersonalDataManager {
    // The Node of the list
    private ChainNode firstNode; 

    // The number of elements in the list
    private int size;   

    public PersonalDataManager() {
firstNode = null;
size = 0;
    }
   
public void add(PersonalData element)  {
add(size, element);
}

    public void add(int index, PersonalData element)  {
// Check for a valid index
 if (index < 0 || index > size) throw new IndexOutOfBoundsException
              ("index = " + index + "  size = " + size);

// Case 1: index == 0
// Insert at the head of the list
if(index == 0) {
ChainNode newNode = new ChainNode(element);
newNode.setNext(firstNode);
firstNode = newNode;
size++;
return;
}

// Case 2: index != 0
// Insert somewhere else in the list
ChainNode previousNode = find(index);
ChainNode newNode = new ChainNode(element);
newNode.setNext(previousNode.getNext());
previousNode.setNext(newNode);
size++;
    }

public void remove(String name) {
for(int i = 0; i < size; i++) {
PersonalData p = get(i);
if (p.getName().equals(name)){
remove(i);
}
}
}
   
    public void remove(int index)  {
// Check for a valid index
checkIndex(index);

// Case 1: index == 0
// Delete the firstNode of the list
if(index == 0) {
firstNode = firstNode.getNext();
size--;
return;
}

// Case 2: index != 0
// Delete something in the middle of the list
ChainNode previousNode = find(index);
previousNode.setNext(previousNode.getNext().getNext());
size--;
    }

public PersonalData search(String name)  {
for(int i = 0; i < size; i++) {
PersonalData p = get(i);
if (p.getName().equals(name)){
return p;
}
}
System.out.println("data not found...");
return null;
}

public void change(String name, String tel)  {
PersonalData p = search(name);
if (p != null){
p.setNumber(tel);
}
}

public void printAll() {
for(int i = 0; i < size; i++) {
System.out.println(get(i));   
}
}


    public PersonalData get(int index)  {
// Check for a valid index
checkIndex(index);

// Case 1: index == 0
// Get the firstNode of the list
if(index == 0) {
return firstNode.getElement();
}

// Case 2: index != 0
// Get something from the middle of the list
ChainNode previousNode = find(index);
return((previousNode.getNext()).getElement());
    }


    // Returns the node *before* the desired one. 
    private ChainNode find(int index) {
int i;
ChainNode currentNode;

// Traverse the links until we get to the one just before the desired one. 
currentNode = firstNode;
for(i = 0; i < size; i++) {
    if(i == index-1)
return(currentNode);

    // bad index
    if(currentNode == null)
return(null);

    currentNode = currentNode.getNext();
}
return null;
    }

  // empties the list
    public void removeAll() {
firstNode = null;
size = 0;
    }

    // returns the length of the list
    public int size() {
return size;
    }

void checkIndex(int index)
  { 
        if (index < 0 || index >= size) throw new IndexOutOfBoundsException
              ("index = " + index + "  size = " + size);
    }

}


class TelephonebookMain {
    public static void main(String[] args) {
        PersonalDataManager manager = new PersonalDataManager();
        manager.add(new PersonalData("aaaa", "333-3333"));
        manager.add(new PersonalData("bbbb", "444-4444"));
        manager.add(new PersonalData("cccc", "555-5555"));
       
        //변경을 적용
        print(manager.search("bbbb"));
        manager.change("cccc","123-1234");
        print(manager.search("cccc")); 
System.out.println("------------------------------");
//삭제를 적용
        manager.remove("bbbb");       
        manager.printAll();       
    }
    public static void print(PersonalData p){
        System.out.println(p);       
    }

댓글 없음:

댓글 쓰기