자바단순연결 리스트(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);
}
}
오라클자바커뮤니티에서 운영, 개발자 전문교육, 개인80%환급 오엔제이프로그래밍실무교육센터(www.onjprogramming.co.kr)
평일주간(9:30~18:30) 개강
(4/07)[기업100%환급]SQL기초에서 Schema Object까지
(4/07)[기업100%환급]자바기초에서 JDBC, Servlet/JSP까지
(4/14)C#4.0,ADO.NET,Network 프로그래밍
(4/14)[기업100%환급]Spring ,MyBatis,Hibernate실무과정
(4/14)[기업100%환급]PL/SQL,ORACLE HINT,TUNING
평일야간(19:00~22:00) 개강
(4/04)웹퍼블리싱 마스터
(4/07)SQL초보에서실전전문가까지
(4/08)Spring3.X, MyBatis, Hibernate실무과정
(4/10)C#,ASP.NET마스터
(4/10)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(4/22)안드로이드개발자과정
주말(10:00~18:00) 개강
(4/05)웹퍼블리싱 마스터
(4/05)닷넷실무자를위한WPF개발자과정
(4/05)Spring3.X, MyBatis, Hibernate실무과정
(4/05)SQL초보에서실전전문가까지
(4/12)C#,ASP.NET마스터
(4/12)안드로이드개발자과정
(4/12)JAVA기초에서실무까지
평일주간(9:30~18:30) 개강
(4/07)[기업100%환급]SQL기초에서 Schema Object까지
(4/07)[기업100%환급]자바기초에서 JDBC, Servlet/JSP까지
(4/14)C#4.0,ADO.NET,Network 프로그래밍
(4/14)[기업100%환급]Spring ,MyBatis,Hibernate실무과정
(4/14)[기업100%환급]PL/SQL,ORACLE HINT,TUNING
평일야간(19:00~22:00) 개강
(4/04)웹퍼블리싱 마스터
(4/07)SQL초보에서실전전문가까지
(4/08)Spring3.X, MyBatis, Hibernate실무과정
(4/10)C#,ASP.NET마스터
(4/10)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(4/22)안드로이드개발자과정
주말(10:00~18:00) 개강
(4/05)웹퍼블리싱 마스터
(4/05)닷넷실무자를위한WPF개발자과정
(4/05)Spring3.X, MyBatis, Hibernate실무과정
(4/05)SQL초보에서실전전문가까지
(4/12)C#,ASP.NET마스터
(4/12)안드로이드개발자과정
(4/12)JAVA기초에서실무까지
댓글 없음:
댓글 쓰기