Java演算法(鏈表操作執行個體)

來源:互聯網
上載者:User

Java演算法(鏈表操作執行個體)

代碼:

package com.xu.main;

import java.util.Scanner;

public class P3_1 {

 /**
  * @功能:鏈表操作執行個體
  * @作者:
  * @日期:2012-10-15
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  CLType node,head = null;
  CLType CL = new CLType();
  String key,findkey;
  Scanner input = new Scanner(System.in);
  System.out.println("鏈表測試。先輸入鏈表中的資料,格式為:關鍵字     姓名     年齡");
  do{
   DATA nodeData = new DATA();
   nodeData.key = input.next();
   if(nodeData.key.equals("0"))
   {
    break;         //若輸入0,則退出
   }
   else
   {
    nodeData.name = input.next();
    nodeData.age = input.nextInt();
    head = CL.CLAddEnd(head, nodeData);   //在鏈表尾部添加結點
   }
  }while(true);
  CL.CLAllNode(head);        //顯示所有結點
  
  System.out.println("示範插入結點,輸入插入位置的關鍵字:");
  findkey = input.next();
  System.out.println("輸入插入結點的資料(關鍵字      姓名     年齡):");
  DATA nodeData = new DATA();
  nodeData.key = input.next();
  nodeData.name = input.next();
  nodeData.age = input.nextInt();
  head = CL.CLInsertNode(head, findkey, nodeData);  //插如結點
  CL.CLAllNode(head);          //顯示
  
  System.out.println("示範刪除結點,輸入要刪除的關鍵字:");
  key = input.next();
  CL.CLDeleteNode(head, key);
  CL.CLAllNode(head);
  
  System.out.println("示範在鏈表中尋找,輸入尋找關鍵字:");
  key = input.next();
  node = CL.CLFindNode(head, key);
  if(node != null)
  {
   nodeData = node.nodeData;
   System.out.printf("關鍵字%s對應的結點為(%s,%s,%d)",key,nodeData.key,nodeData.name,nodeData.age);
   
  }
  else
  {
   System.out.printf("在鏈表中未找到關鍵字為%s的結點!",key);
  }
  
 }

}

class DATA {
 String key; // 關鍵字
 String name;
 int age;
}

class CLType // 定義鏈表結構
{
 DATA nodeData = new DATA();
 CLType nextNode;

 @SuppressWarnings("unused")
 CLType CLAddEnd(CLType head, DATA nodeData) // 追加結點
 {
  CLType node, htemp;
  if ((node = new CLType()) == null) {
   System.out.println("申請記憶體失敗!");
   return null;
  } else {
   node.nodeData = nodeData; // 儲存資料
   node.nextNode = null; // 設定結點引用為空白,即為表尾
   if (head == null) // 頭引用
   {
    head = node;
    return head;

   }
   htemp = head;
   while (htemp.nextNode != null) // 尋找鏈表的末尾
   {
    htemp = htemp.nextNode;
   }
   htemp.nextNode = node;
   return head;
  }

 }
 
 @SuppressWarnings("unused")
 CLType CLAddFirst(CLType head,DATA nodeData)  //在頭結點增加結點
 {
  CLType node;
  if((node = new CLType()) == null)
  {
   System.out.println("申請記憶體失敗!");
   return null;
  }
  else
  {
   node.nodeData = nodeData;     //儲存資料
   node.nextNode = head;      //指向頭引用所指結點
   head = node;        //頭引用指向新增結點
   return head;
  }  
 }
 
 CLType CLFindNode(CLType head,String key)  //尋找結點
 {
  CLType htemp;        
  htemp = head;        //儲存鏈表頭引用
  while(htemp != null)      //若結點有效,則進行尋找
  {
   if(htemp.nodeData.key.compareTo(key)==0) //若結點關鍵字與傳入的關鍵字相同
   {
    return htemp;      //返回該結點引用
   }
   htemp = htemp.nextNode;     //處理下一結點
  }
  return null;
 }
 
 @SuppressWarnings("unused")
 CLType CLInsertNode(CLType head,String findkey,DATA nodeData) //插入結點
 {
  CLType node,nodetemp;
  if((node = new CLType())==null)    //分配記憶體結點的內容
  {
   System.out.println("申請記憶體 失敗!");
   return null;
   
  }
  node.nodeData = nodeData;     //儲存結點中的資料
  nodetemp = CLFindNode(head,findkey);
  if(nodetemp!=null)       //若找到要插入的結點
  {
   node.nextNode = nodetemp.nextNode;  //新插入結點指向關鍵結點的下一節點
   nodetemp.nextNode = node;    //設定關鍵結點指向新插入結點
   
  }
  else
  {
   System.out.println("未找到正確的插入位置!");
   //free(node);        //釋放記憶體
  }
  return head;
  
 }
 
 int CLDeleteNode(CLType head,String key)   //刪除結點
 {
  CLType node,htemp;
  htemp = head;
  node = head;
  while(htemp!=null)
  {
   if(htemp.nodeData.key.compareTo(key)==0)
   {
    node.nextNode=htemp.nextNode;   //使前一節點指向當前結點的下一節點
    //free(htemp);       //釋放記憶體
    return 1;
   }
   else
   {
    node = htemp;       //指向當前結點
    htemp = htemp.nextNode;     //指向下一節點
   }
   
  }
  return 0;
 }
 
 int CLLength(CLType head)       //計算鏈表長度
 {
  CLType htemp;
  int len = 0;
  htemp = head;
  while(htemp!=null)        //遍曆整個鏈表
  {
   len ++;
   htemp = htemp.nextNode;      //處理下一結點
  }
  return len;
  
 }
 
 void CLAllNode(CLType head)       //顯示鏈表
 {
  CLType htemp;
  DATA nodeData ;
  htemp = head;
  System.out.printf("當前鏈表共有%d個結點。鏈表所有資料如下:\n",CLLength(head));
  while(htemp != null)       //迴圈處理鏈表每個結點
  {
   nodeData = htemp.nodeData;
   System.out.printf("結點(%s,%s,%d)\n",nodeData.key,nodeData.name,nodeData.age);
   htemp = htemp.nextNode;
  }
 }
}

運行結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.