/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication14;
import java.util.*;
class singlelinklist//單向鏈結
{
int choice3 ;
public void sl()
{
java.util.Scanner aaa=new java.util.Scanner(System.in);
java.util.Scanner bbb=new java.util.Scanner(System.in);
java.util.Scanner ccc=new java.util.Scanner(System.in);//建立Scanner物件
LinkedList<String> llist=new LinkedList<String>();//建立list物件
do{
Iterator iterator;
System.out.println("請選擇資料處理方式:");
System.out.println("------------------");
System.out.println("1 =>ADD節點");
System.out.println("2 =>Display節點");
System.out.println("3 =>Delete節點");
System.out.println("------------------");//說明
int choice=bbb.nextInt();
switch(choice)
{
case 1:
System.out.println("請輸入學生姓名:");
String name=aaa.nextLine();//輸入姓名
llist.addLast(name);//加入姓名到list
System.out.println("請輸入學生成績:");
String score=aaa.nextLine();//輸入成績
llist.addLast(score);//加入成績到list
break;
case 2:
for ( String stname: llist )
System.out.println("所有串列裡的資料:"+stname);//輸出list中所有內容
break;
case 3:
System.out.println("請輸入要刪除的節點內容:");
String choice2=ccc.nextLine();
llist.remove(choice2);//刪除所輸入的姓名
iterator = llist.iterator();
while (iterator.hasNext())
{
System.out.println("刪除一節點後"+"內容剩下:"+iterator.next());
}
break;
}
System.out.println("繼續其他功能?輸入1就繼續");//用while控制程式要不要繼續執行
choice3=bbb.nextInt();
}while(choice3 ==1);
}
}
class doublylinklist//雙向鏈結
{
int choice3 ;
public void dl()
{
java.util.Scanner bbb=new java.util.Scanner(System.in);
java.util.Scanner aaa=new java.util.Scanner(System.in);
java.util.Scanner ccc=new java.util.Scanner(System.in);//建立Scanner物件
LinkedList<String> llist=new LinkedList<String>();//建立list物件
LinkedList<String> sllist=new LinkedList<String>();//建立list物件
do{
Iterator iterator,iterator1;
System.out.println("請選擇資料處理方式:");
System.out.println("------------------");
System.out.println("1 =>ADD節點");
System.out.println("2 =>Display節點");
System.out.println("3 =>Delete節點");
System.out.println("------------------");//說明
int choice=bbb.nextInt();
switch(choice)
{
case 1:
System.out.println("請輸入學生姓名:");
String name=aaa.nextLine();//輸入姓名
System.out.println("加入姓名前一節點還是後一節點?");
System.out.println("-----------------------");
System.out.println("1 =>加入前一節點");
System.out.println("2 =>加入後一節點");
System.out.println("-----------------------");
int choice5=bbb.nextInt();
if(choice5==1)
{
llist.addFirst(name);//加入姓名到前list
}
else if(choice5==2)
{
llist.addLast(name);//加入姓名到後list
}
System.out.println("請輸入學生成績:");
String score=aaa.nextLine();//輸入成績
System.out.println("加入成績前一節點還是後一節點?");
System.out.println("-----------------------");
System.out.println("1 =>加入前一節點");
System.out.println("2 =>加入後一節點");
System.out.println("-----------------------");
int choice6=bbb.nextInt();
if(choice6==1)
{
sllist.addFirst(score);//加入成績到前list
}
else if(choice6==2)
{
sllist.addLast(score);//加入成績到後list
}
break;
case 2:
System.out.println("所有串列裡的資料:"+llist);//輸出list中所有內容
System.out.println("所有串列裡的資料:"+sllist);//輸出list中所有內容
break;
case 3:
System.out.println("請輸入要刪除的節點內容:");
String choice2=ccc.nextLine();
llist.remove(choice2);//刪除所輸入的姓名
sllist.remove(choice2);
iterator = llist.iterator();
iterator1 = sllist.iterator();
while (iterator.hasNext()&&iterator1.hasNext())
{
System.out.println("刪除一節點後"+"內容剩下:"+iterator.next());//輸出內容
System.out.println("刪除一節點後"+"內容剩下:"+iterator1.next());
}
break;
}
System.out.println("繼續其他功能?輸入1就繼續");//用while控制程式要不要繼續執行
choice3=bbb.nextInt();
}while(choice3 ==1);
}
}
public class DS
{
public static void main(String[] args)
{
java.util.Scanner aaa=new java.util.Scanner(System.in);
System.out.println("請選擇資料鏈結方式:");
System.out.println("------------------");
System.out.println("1 =>Single_Linked_List");
System.out.println("2 =>Doubly_Linked_List");
System.out.println("------------------");
int cinn=aaa.nextInt();
if (cinn==1) //輸入1單向鏈結
{
singlelinklist kk=new singlelinklist();
kk.sl();
}
else if (cinn==2) //輸入2雙向鏈結
{
doublylinklist kk=new doublylinklist();
kk.dl();
}
}
}