我的漫漫程序之旅

          專注于JavaWeb開發(fā)
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數(shù)據(jù)加載中……

          單向鏈表的簡單實(shí)現(xiàn)

          要求:寫一個(gè)示例程序,顯示一個(gè)單鏈表.這個(gè)鏈表的功能:
          1.在鏈表頭插入一個(gè)數(shù)據(jù)項(xiàng)
          2.在鏈表頭刪除一個(gè)數(shù)據(jù)項(xiàng)
          3.遍歷鏈表顯示它的內(nèi)容
          實(shí)現(xiàn)代碼如下:
          package com;
          /**
           * 結(jié)點(diǎn)類
           * 
          @author zdw
           *
           
          */

          class Link
          {
              
          //數(shù)據(jù)區(qū)域
              public int data;
              
          //指針區(qū)域
              public Link next;
              
          //構(gòu)造一個(gè)數(shù)據(jù)為data的結(jié)點(diǎn),默認(rèn)指針為空
              public Link(int data)
              
          {
                  
          this.data = data;
                  next 
          = null;
              }

              
          //顯示結(jié)點(diǎn)的數(shù)據(jù)
              public void displayLink()
              
          {
                  System.out.println(
          "Data:" + data);
              }

          }

          /**
           * 鏈表類
           * 
          @author zdw
           *
           
          */

          class LinkList
          {
              
          //頭結(jié)點(diǎn)
              private Link first;
              
          //構(gòu)造一個(gè)空的鏈表
              public LinkList()
              
          {
                  first 
          = null;
              }

              
              
          public void insertLink(int data)
              
          {
                  
          //構(gòu)造一個(gè)內(nèi)容為data的結(jié)點(diǎn)
                  Link newLink = new Link(data);
                  
          //將first中的地址賦予新建的結(jié)點(diǎn)指針區(qū)域中
                  newLink.next = first;
                  
          //讓first結(jié)點(diǎn)的指針指向newLink
                  first = newLink;
              }

              
          //判斷LinkList是否為空
              public boolean isEmpty()
              
          {
                  
          return first == null;
              }

              
              
          public Link deleteLink()
              
          {
                  
          //將first中保存到臨時(shí)變量temp中
                  Link temp = first;
                  
          //將下一個(gè)結(jié)點(diǎn)的指針地址轉(zhuǎn)移到first中
                  first = first.next;
                  
          return temp;
              }

              
              
          public void displayLinkList()
              
          {
                  Link current 
          = first;
                  
          while(current != null)
                  
          {
                      current.displayLink();
                      current 
          = current.next;
                  }

                  System.out.println();
              }

          }

          /**
           * 測試類
           * 
          @author zdw
           *
           
          */

          public class LinkListApp
          {
              
          public static void main(String[] args)
              
          {
                  LinkList ll 
          = new LinkList();
                  ll.insertLink(
          1);
                  ll.insertLink(
          2);
                  ll.insertLink(
          3);
                  ll.insertLink(
          4);
                  System.out.println(
          "linkList中的內(nèi)容是:");
                  ll.displayLinkList();
                  
          //LinkList不為空
                  while(!ll.isEmpty())
                  
          {
                      
          //刪除
                      ll.deleteLink();
                  }

                  System.out.println(
          "刪除后的內(nèi)容是:");
                  ll.displayLinkList();
              }


          }

          現(xiàn)在要求為該程序加入按指定鍵查找和刪除的方法.并加以測試.
          代碼如下:
          package com;

          /**
           * 結(jié)點(diǎn)類
           * 
           * 
          @author zdw
           * 
           
          */

          class Link
          {
              
          // 數(shù)據(jù)區(qū)域
              public int data;
              
          // 指針區(qū)域
              public Link next;

              
          // 構(gòu)造一個(gè)數(shù)據(jù)為data的結(jié)點(diǎn),默認(rèn)指針為空
              public Link(int data)
              
          {
                  
          this.data = data;
                  next 
          = null;
              }


              
          // 顯示結(jié)點(diǎn)的數(shù)據(jù)
              public void displayLink()
              
          {
                  System.out.println(
          "Data:" + data);
              }

          }


          /**
           * 鏈表類
           * 
           * 
          @author zdw
           * 
           
          */

          class LinkList
          {
              
          // 頭結(jié)點(diǎn)
              private Link first;

              
          // 構(gòu)造一個(gè)空的鏈表
              public LinkList()
              
          {
                  first 
          = null;
              }


              
          public void insertLink(int data)
              
          {
                  
          // 構(gòu)造一個(gè)內(nèi)容為data的結(jié)點(diǎn)
                  Link newLink = new Link(data);
                  
          // 將first中的地址賦予新建的結(jié)點(diǎn)指針區(qū)域中
                  newLink.next = first;
                  
          // 讓first結(jié)點(diǎn)的指針指向newLink
                  first = newLink;
              }


              
          // 判斷LinkList是否為空
              public boolean isEmpty()
              
          {
                  
          return first == null;
              }

              
          //查找
              public Link find(int key)
              
          {
                  
          //從第一個(gè)開始查找
                  Link current = first;
                  
          while(current.data != key)
                  
          {
                      
          if(current.next == null)
                          
          return null;
                      
          else
                          
          return current.next;    
                  }

                  
          return current;
              }

              
          //用指定key刪除
              public Link delete(int key)
              
          {
                  Link current 
          = first;
                  Link previous 
          = first;
                  
          while(current.data != key)
                  
          {
                      
          if(current.next == null)
                          
          return null;
                      
          else
                      
          {
                          previous 
          = current;
                          current 
          = current.next;
                      }

                  }

                  
          if(current == first)
                  
          {
                      first 
          = first.next;
                  }

                  
          else
                  
          {
                      previous.next 
          = current.next;
                  }

                  
          return current;
              }

              
              
          public Link deleteLink()
              
          {
                  
          // 將first中保存到臨時(shí)變量temp中
                  Link temp = first;
                  
          // 將下一個(gè)結(jié)點(diǎn)的指針地址轉(zhuǎn)移到first中
                  first = first.next;
                  
          return temp;
              }


              
          public void displayLinkList()
              
          {
                  Link current 
          = first;
                  
          while (current != null)
                  
          {
                      current.displayLink();
                      current 
          = current.next;
                  }

                  System.out.println();
              }

          }


          /**
           * 測試類
           * 
           * 
          @author zdw
           * 
           
          */

          public class LinkListApp
          {
              
          public static void main(String[] args)
              
          {
                  LinkList ll 
          = new LinkList();
                  ll.insertLink(
          1);
                  ll.insertLink(
          2);
                  ll.insertLink(
          3);
                  ll.insertLink(
          4);
                  System.out.println(
          "linkList中的內(nèi)容是:");
                  ll.displayLinkList();
                  
          // LinkList不為空
                  Link f = ll.find(3);
                  
          if(f != null)
                  
          {
                      System.out.println(
          "已經(jīng)找到:" + f.data);
                  }

                  
          else
                  
          {
                      System.out.println(
          "沒有該項(xiàng)");
                  }

                  Link d 
          = ll.delete(3);
                  
          if(d != null)
                  
          {
                      System.out.println(
          "成功刪除:" + d.data);
                  }

                  
          else
                  
          {
                      System.out.println(
          "沒有該項(xiàng)");
                  }

                  System.out.println(
          "刪除后的內(nèi)容是:");
                  ll.displayLinkList();
              }


          }



          posted on 2007-12-29 12:08 々上善若水々 閱讀(1784) 評論(0)  編輯  收藏 所屬分類: 數(shù)據(jù)結(jié)構(gòu)與算法

          主站蜘蛛池模板: 安义县| 江山市| 甘肃省| 乳源| 航空| 临朐县| 饶河县| 健康| 博爱县| 阆中市| 西青区| 元江| 嘉荫县| 微山县| 宝丰县| 广饶县| 峡江县| 涿鹿县| 岫岩| 灵山县| 澳门| 廉江市| 西贡区| 和田市| 天全县| 潞城市| 凯里市| 岳西县| 惠水县| 庄河市| 舒城县| 晋宁县| 广宗县| 嘉荫县| 兴宁市| 资中县| 拉孜县| 观塘区| 中西区| 南木林县| 汉寿县|