链表-双向链表

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Class DoubleLink
 * Description 双向链表提供了向前遍历和向后遍历的途径。而普通链表只能向后遍历.
 * Company OpenData
 * Author Chenlly
 * Date 09-02-07
 * Version 1.0
 */
class DLink {
 public int iData;
 public DLink next; // 后继节点
 public DLink previous;// 前继节点

 // 构造函数
 public DLink(int iData) {
  this.iData = iData;
 }

 // 查看节点信息
 public void displayLink() {
  System.out.println("" + iData + ",");
 }
}

public class DoubleLink {
 private DLink firstLink; //双向链表头部
 private DLink lastLink;  //双向链表尾部

 // 构造函数
 public DoubleLink() {
  firstLink = null;
  lastLink = null;
 }
 
 //判断链表是否为空
 public boolean isEmpty(){
  return firstLink == null;
 }
 
 // 创建新节点
 public DLink createDLink(int iData) {
  DLink dLink = new DLink(iData);
  return dLink;
 }
 
 
 // 在链表头部插入新节点
 public void insertFirst(DLink dLink) {
  if (isEmpty()) {
   lastLink = dLink;//头插法,lastLink就是第一个插入的节点
  } else {
   firstLink.previous = dLink;
   dLink.next = firstLink;
  }
  firstLink = dLink;
 }
 
 // 在链表尾部插入新节点
 public void insertLast(DLink dLink) {
  if (isEmpty()) {
   firstLink = dLink;//尾插法,firstLink就是第一个插入的节点
  } else {
   lastLink.next = dLink;
   dLink.previous = lastLink;
   lastLink = dLink;
  }
 }
 
 // 查找一个节点从头遍历
 public DLink findByIDFromFirst(int key){
  DLink current = firstLink;
  while (current != null){
   if (current.iData == key){
    return current;
   } else {
    current = current.next;
   }
  }
  return null;
 }
 
 // 查找一个节点从尾遍历
 public DLink findByIDFromLast(int key){
  DLink current = lastLink;
  while (current != null){
   if (current.iData == key){
    return current;
   } else {
    current = current.previous;
   }
  }
  return null;
 }
 
 // 删除一个节点
 public DLink remove(int key) {
  DLink current = firstLink;
  if (current.iData == key){//被删除的节点是第一个节点
   firstLink = current.next;
  } else {
   if (current.next == null) {//被删除的节点是最后一个个节点
    current.previous.next = null;
   } else {被删除的节点既不是第一个也不是最后一个
    while ( current != null){
     if (current.iData == key){
      current.previous.next = current.next;
      current.next.previous = current.previous;
     } else {
      current = current.next;
     }
    }
   }
  }
  return null;
 }

 // 显示双向链表信息从头遍历
 public void displayFromFirst() {
  System.out.println("first-->last");
  DLink current = firstLink;
  if (isEmpty()){
   System.out.println("这是一个空链表");
  }
  while (current != null){
   current.displayLink();
   current = current.next;
  }
 }
 
 // 显示双向链表信息从尾遍历
 public void displayFromLast() {
  System.out.println("last-->first");
  DLink current = lastLink;
  if (isEmpty()){
   System.out.println("这是一个空链表");
  }
  while (current != null){
   current.displayLink();
   current = current.previous;
  }
 }
 // 从键盘读入数据
 public static String readStr() {
  InputStreamReader ir = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(ir);
  String str = "";
  try {
   str = br.readLine();
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return str;
 }

 // 主调函数
 public static void main(String[] args) {
  System.out.println("请选择操作序列");
  System.out.println("1 插入新节点");
  System.out.println("2 查找指定节点");
  System.out.println("3 删除指定的节点");
  System.out.println("4 显示链表信息");
  System.out.println("5 退出");
  DoubleLink dl = new DoubleLink();
  while (true){
  System.out.println("请选择操作序列");
  int op = 0;
  try {
   op = Integer.parseInt(DoubleLink.readStr());
  } catch (Exception ex){
   System.out.println("输入了无效字符");
   //ex.printStackTrace();
  }
   switch (op){
    case 1:
     System.out.println("请选择查找类别(a:头插法,b:尾插法)");
     char ci = DoubleLink.readStr().charAt(0);
     System.out.println(""+ci);
     if (ci != 'a' && ci != 'b'){
      System.out.println("输入有无"); break;
     }
     System.out.println("请输入节点信息");
     int iData = 0;
     try {
      iData = Integer.parseInt(DoubleLink.readStr());
     } catch (Exception ex){
      System.out.println("输入了无效字符");
      //ex.printStackTrace();
     }
     DLink dLink = dl.createDLink(iData);
     if (ci == 'a'){
      dl.insertFirst(dLink);
     } else {
      if (ci == 'b' ){
       dl.insertLast(dLink);
      }
     }
     break;
    case 2:
     if (dl.isEmpty()){
      System.out.println("这是一个空链表"); break;
     }
     System.out.println("请输入查找关键字Key值");
     int key = 0;
     try {
      key = Integer.parseInt(DoubleLink.readStr());
     } catch (Exception ex){
      System.out.println("输入了无效字符");
      //ex.printStackTrace();
     }
     System.out.println("请选择查找类别(a:从头查找,b:从尾查找)");
     char cf = DoubleLink.readStr().charAt(0);
     DLink keyLink = null;
     if (cf == 'a'){
      keyLink = dl.findByIDFromFirst(key);
     } else {
      if (cf == 'b') {
       keyLink = dl.findByIDFromLast(key);
      } else {
       System.out.println("输入有无"); break;
      }
     }
     if (keyLink != null){
      System.out.println("find the DLink is:" +keyLink.iData);
     } else {
      System.out.println(" not find the DLink by the key ");
     }
     break;
    case 3:
     if (dl.isEmpty()){
      System.out.println("这是一个空链表"); break;
     }
     System.out.println("请输入删除关键字Key值");
     int dkey = 0;
     try {
      dkey = Integer.parseInt(DoubleLink.readStr());
     } catch (Exception ex){
      System.out.println("输入了无效字符");
      //ex.printStackTrace();
     }
     DLink dkeyLink = dl.remove(dkey);
     if (dkeyLink != null){
      System.out.println("节点" +dkeyLink.iData+ "已删除");
     } else {
      System.out.println("删除节点失败");
     }
     break;
    case 4:
     System.out.println("请选择查找类别(a:从头遍历,b:从尾遍历)");
     char cd = DoubleLink.readStr().charAt(0);
     if (cd == 'a'){
      dl.displayFromFirst();
     } else {
       if (cd == 'b'){
       dl.displayFromLast();
      } else {
        System.out.println("输入有误"); break;
       }
      }
     break;
    case 5:
     default:
      System.exit(0);
   }//end switch
  }//end while
 }//end main
}//end DoubleLink

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值