<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我的漫漫程序之旅

    專注于JavaWeb開發
    隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
    數據加載中……

    單向鏈表的簡單實現

    要求:寫一個示例程序,顯示一個單鏈表.這個鏈表的功能:
    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 々上善若水々 閱讀(1778) 評論(0)  編輯  收藏 所屬分類: 數據結構與算法

    主站蜘蛛池模板: 一区二区无码免费视频网站| 全部在线播放免费毛片| 成人电影在线免费观看| 免费人成视频在线观看视频| 亚洲xxxx视频| 成年轻人网站色免费看| 亚洲白嫩在线观看| 免费专区丝袜脚调教视频| 亚洲精品中文字幕无乱码| 成人无遮挡毛片免费看| 99久久免费国产特黄| 国产精品亚洲精品日韩已方| 一级午夜a毛片免费视频| 亚洲精品tv久久久久久久久久| 九九久久国产精品免费热6 | 欧洲精品免费一区二区三区| 一本天堂ⅴ无码亚洲道久久| 手机在线免费视频| a级毛片毛片免费观看久潮| 亚洲熟女乱色一区二区三区 | 久久精品亚洲综合| 91大神免费观看| 亚洲中文无码a∨在线观看| 99爱免费观看视频在线| 亚洲国语在线视频手机在线| A级毛片内射免费视频| 相泽南亚洲一区二区在线播放| 亚洲国产天堂久久久久久| 久九九精品免费视频| 国产亚洲精品仙踪林在线播放| a级亚洲片精品久久久久久久| 午夜免费啪视频在线观看| 亚洲欧洲日韩极速播放| 亚洲精品线在线观看| A级毛片内射免费视频| 一级毛片**不卡免费播| 72pao国产成视频永久免费| 久久精品国产亚洲精品2020| 国产麻豆免费观看91| 日韩a毛片免费观看| 亚洲日本VA午夜在线电影|