我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0

          導航

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(39)

          隨筆檔案(43)

          文章分類(304)

          文章檔案(257)

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          單向鏈表的簡單實現

          要求:寫一個示例程序,顯示一個單鏈表.這個鏈表的功能:
          1.在鏈表頭插入一個數據項
          2.在鏈表頭刪除一個數據項
          3.遍歷鏈表顯示它的內容
          實現代碼如下:
          package com;
          /**
           * 結點類
           * 
          @author zdw
           *
           
          */

          class Link
          {
              
          //數據區域
              public int data;
              
          //指針區域
              public Link next;
              
          //構造一個數據為data的結點,默認指針為空
              public Link(int data)
              
          {
                  
          this.data = data;
                  next 
          = null;
              }

              
          //顯示結點的數據
              public void displayLink()
              
          {
                  System.out.println(
          "Data:" + data);
              }

          }

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

          class LinkList
          {
              
          //頭結點
              private Link first;
              
          //構造一個空的鏈表
              public LinkList()
              
          {
                  first 
          = null;
              }

              
              
          public void insertLink(int data)
              
          {
                  
          //構造一個內容為data的結點
                  Link newLink = new Link(data);
                  
          //將first中的地址賦予新建的結點指針區域中
                  newLink.next = first;
                  
          //讓first結點的指針指向newLink
                  first = newLink;
              }

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

              
              
          public Link deleteLink()
              
          {
                  
          //將first中保存到臨時變量temp中
                  Link temp = first;
                  
          //將下一個結點的指針地址轉移到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中的內容是:");
                  ll.displayLinkList();
                  
          //LinkList不為空
                  while(!ll.isEmpty())
                  
          {
                      
          //刪除
                      ll.deleteLink();
                  }

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


          }

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

          /**
           * 結點類
           * 
           * 
          @author zdw
           * 
           
          */

          class Link
          {
              
          // 數據區域
              public int data;
              
          // 指針區域
              public Link next;

              
          // 構造一個數據為data的結點,默認指針為空
              public Link(int data)
              
          {
                  
          this.data = data;
                  next 
          = null;
              }


              
          // 顯示結點的數據
              public void displayLink()
              
          {
                  System.out.println(
          "Data:" + data);
              }

          }


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

          class LinkList
          {
              
          // 頭結點
              private Link first;

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


              
          public void insertLink(int data)
              
          {
                  
          // 構造一個內容為data的結點
                  Link newLink = new Link(data);
                  
          // 將first中的地址賦予新建的結點指針區域中
                  newLink.next = first;
                  
          // 讓first結點的指針指向newLink
                  first = newLink;
              }


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

              
          //查找
              public Link find(int key)
              
          {
                  
          //從第一個開始查找
                  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中保存到臨時變量temp中
                  Link temp = first;
                  
          // 將下一個結點的指針地址轉移到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中的內容是:");
                  ll.displayLinkList();
                  
          // LinkList不為空
                  Link f = ll.find(3);
                  
          if(f != null)
                  
          {
                      System.out.println(
          "已經找到:" + f.data);
                  }

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

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

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

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


          }



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

          主站蜘蛛池模板: 上饶市| 古丈县| 安仁县| 宜城市| 清远市| 武城县| 新民市| 玛曲县| 定远县| 裕民县| 梅河口市| 通城县| 九龙坡区| 昌黎县| 呼和浩特市| 凤庆县| 若尔盖县| 宁国市| 武宁县| 浦东新区| 平湖市| 娱乐| 东乌| 拜城县| 米林县| 新干县| 辛集市| 贵阳市| 金乡县| 柳河县| 阿拉尔市| 吴川市| 三河市| 马鞍山市| 共和县| 凤城市| 光山县| 定结县| 神池县| 江源县| 上虞市|