<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 々上善若水々 閱讀(1779) 評論(0)  編輯  收藏 所屬分類: 數據結構與算法

    主站蜘蛛池模板: 亚洲今日精彩视频| 免费观看四虎精品成人| 成年女人免费视频播放体验区| 亚洲人成人无码.www石榴| 亚洲综合色在线观看亚洲| 久久香蕉国产线看免费| 亚洲A∨精品一区二区三区下载| 国产av无码专区亚洲av果冻传媒 | 青青青国产手机频在线免费观看 | 成人爽a毛片免费| 亚洲入口无毒网址你懂的| 亚洲乱码中文字幕综合234| 免费观看无遮挡www的视频| 菠萝菠萝蜜在线免费视频| 久久精品国产亚洲AV无码麻豆| 国产裸模视频免费区无码| 四虎国产成人永久精品免费| 亚洲AV永久无码精品网站在线观看| 亚洲国产精品无码久久久蜜芽| 好吊妞在线新免费视频| 久久免费观看国产精品| 鲁啊鲁在线视频免费播放| 亚洲小说区图片区| 国产亚洲av片在线观看16女人| 日本牲交大片免费观看| 国产精品久久久久久久久免费| 中文在线免费不卡视频| www亚洲精品久久久乳| 亚洲成年人电影网站| 国产亚洲成av人片在线观看| 国产一区二区免费在线| 久久久久久国产精品免费免费| 久久久99精品免费观看| 亚美影视免费在线观看| 在线精品自拍亚洲第一区| 激情综合亚洲色婷婷五月 | 在线观看人成视频免费无遮挡| 久久精品熟女亚洲av麻豆 | 亚洲乱色熟女一区二区三区蜜臀| 老汉色老汉首页a亚洲| 亚洲av永久无码精品古装片|