package text_3;
public class Node_char {
private char date;
private Node_char next;
public Node_char(char date) {
super();
this.date = date;
}
public char getDate() {
return date;
}
public Node_char() {
super();
this.next=null;
}
public void setDate(char date) {
this.date = date;
}
public Node_char getNext() {
return next;
}
public void setNext(Node_char next) {
this.next = next;
}
}
这个实验需要注意的是:将一个完整的链表把其中的节点卸下来之后其节点空间将不再存在
如Node_char p=head, temp=null;
temp=p;
Addtail(temp);这个是将节点添加到另一个链表中
p=p.getnext();
这几行代码书错误的,如果使temp=p;Addtail(temp);则原始链表中的p,就不会存在于原始链表中,总之链表最重要的就是弄清楚链表节点存在的空间,每一次错误都是一次提高,加油。
package text_3;
import java.io.IOException;
public class linkdlst_2 {
private Node_char head;
private Node_char tail;
private int size=0;
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public linkdlst_2() {
super();
this.head=this.tail=null;
this.size=0;
}
/*
* 单链表插入链表,在尾节点
*/
public void Addtail(Node_char t){
if(size==0){
t.setNext(null);
head=tail=t;
size++;
}
else{
tail.setNext(t);
t.setNext(null);
tail=t;
size++;
}
}
/*
* 插入链表,在尾节点
*/
public void AddTail(Node_char t){
if(size==0){
t.setNext(t);
head=tail=t;
size++;
}
else{
tail.setNext(t);
t.setNext(head);
tail=t;
size++;
}
}
/*
* 打印链表
*/
public void PrintList(){
Node_char p=new Node_char();
p=head;
// try{
if(p==null){
System.out.println("空表");
}else{
tail.setNext(null);
while(p!=null){
if(p==tail){
System.out.print(p.getDate());
}else{
System.out.print(p.getDate()+" ");
// System.out.print("->");
}
p=p.getNext();
}
System.out.println();
}
// }catch(Exception e){
// e.printStackTrace();
// }
}
public int judge_null(){
if(head==null){
System.out.println("空表");
return 0;
}
else{
return 1;
}
}
public static void main(String[] args) throws IOException{
linkdlst_2 link_L=new linkdlst_2();
linkdlst_2 link_h1=new linkdlst_2();
linkdlst_2 link_h2=new linkdlst_2();
linkdlst_2 link_h3=new linkdlst_2();
System.out.println("输入链表节点:");
char c=(char)System.in.read();
while(c!='#'){
link_L.Addtail(new Node_char(c));
c=(char)System.in.read();
}
// link_L.Addtail(new Node_char('1'));
// link_L.Addtail(new Node_char('q'));
// link_L.Addtail(new Node_char('8'));
// link_L.Addtail(new Node_char('m'));
// link_L.Addtail(new Node_char('&'));
// link_L.Addtail(new Node_char('@'));
System.out.println("输出链表结点:");
link_L.PrintList();
Node_char p=new Node_char();
p=link_L.head;
// link_L.tail.setNext(null);
Node_char temp;
while(p!=null){
temp=p;
p=p.getNext();
// temp.setNext(null);//从单链表上面卸下来
if((temp.getDate()>='A' && temp.getDate()<='Z')||(temp.getDate()>='a' && temp.getDate()<='z')){
link_h1.AddTail(temp);
}else if (temp.getDate()>='0' && temp.getDate()<='9'){
link_h2.AddTail(temp);
}else{
link_h3.AddTail(temp);
}
}
// set(link_L,link_h1, link_h2, link_h3);
System.out.println("字母链表中的元素为");
link_h1.PrintList();
System.out.println("数字链表中的元素为");
link_h2.PrintList();
System.out.println("字符链表中的元素为");
link_h3.PrintList();
}
;
}