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

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

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

    莊周夢蝶

    生活、程序、未來
       :: 首頁 ::  ::  :: 聚合  :: 管理

    C#實現鏈表

    Posted on 2007-03-29 17:02 dennis 閱讀(1997) 評論(1)  編輯  收藏 所屬分類: C#歷程 、數據結構與算法
    ??? 今天受一個帖子的刺激,再次復習起了數據結構與算法,那本《數據結構與算法(java版)》我還剩圖和高級排序的幾章沒看,工作上也沒我的事需要處理,就用C#重新寫了一遍鏈表結構,權作復習。

    定義List接口:
    ???public??interface?List
    ????{
    ????????
    bool?IsEmpty();
    ????????
    void?Unshift(Object?obj);
    ????????Object?Shift();
    ????????
    void?Push(Object?obj);
    ????????Object?Pop();
    ????????
    bool?Contain(Object?obj);
    ????????
    void?Delete(Object?obj);
    ????????
    void?PrintAll();
    ????????Object?getHead();
    ????????Object?getTail();
    ??????? void Clear();
    ????}

    實現單向鏈表:
    ?//單向鏈表
    ????public?class?SList:List
    ????{
    ????????
    private?SNode?head,?tail;

    ????????
    public?SList()
    ????????{
    ????????????
    this.head?=?this.tail?=?null;
    ????????}
    ????????
    public?bool?IsEmpty()
    ????????{
    ????????????
    return?head?==?null;
    ????????}
    ????????
    public?void?Unshift(Object?obj)
    ????????{

    ????????????head?
    =?new?SNode(obj,?head);
    ????????????
    if?(tail?==?null)
    ????????????????tail?
    =?head;
    ????????}
    ????????
    public?Object?Shift()
    ????????{
    ????????????
    if?(head?==?null)
    ????????????????
    throw?new?NullReferenceException();
    ????????????Object?value?
    =?head.value;
    ????????????
    if?(head?==?tail)
    ????????????????head?
    =?tail?=?null;
    ????????????
    else
    ????????????????head?
    =?head.next;
    ????????????
    return?value;
    ????????}

    ????????
    public?void?Push(Object?obj)
    ????????{
    ????????????
    if?(!IsEmpty())
    ????????????{
    ????????????????tail.next?
    =?new?SNode(obj);
    ????????????????tail?
    =?tail.next;
    ????????????}
    ????????????
    else
    ????????????????head?
    =?tail?=?new?SNode(obj);

    ????????}

    ????????
    public?Object?Pop()
    ????????{
    ????????????
    if?(head?==?null)
    ????????????????
    throw?new?NullReferenceException();
    ????????????Object?obj?
    =?tail.value;
    ????????????
    if?(head?==?tail)
    ????????????????head?
    =?tail?=?null;
    ????????????
    else
    ????????????{
    ????????????????
    //查找前驅節點
    ????????????????for?(SNode?temp?=?head;?temp.next?!=?null?&&?!temp.next.Equals(tail);?temp?=?temp.next)
    ????????????????????tail?
    =?temp;
    ????????????????tail.next?
    =?null;
    ????????????}
    ????????????
    return?obj;
    ????????}
    ????????
    public?void?PrintAll()
    ????????{
    ????????????
    string?result?=?"";
    ????????????
    for?(SNode?temp?=?head;?temp?!=?null;?temp?=?temp.next)
    ????????????????result?
    +=?"?"?+?temp.value.ToString();
    ????????????Console.WriteLine(result);
    ????????}

    ????????
    public?bool?Contain(Object?obj)
    ????????{
    ????????????
    if?(head?==?null)
    ????????????????
    return?false;
    ????????????
    else
    ????????????{
    ????????????????
    for?(SNode?temp?=?head;?temp?!=?null;?temp?=?temp.next)
    ????????????????{
    ????????????????????
    if?(temp.value.Equals(obj))
    ????????????????????????
    return?true;
    ????????????????}
    ????????????}
    ????????????
    return?false;
    ????????}

    ????????
    public?void?Delete(Object?obj)
    ????????{
    ????????????
    if?(!IsEmpty())
    ????????????{
    ????????????????
    if?(head?==?tail?&&?head.value.Equals(obj))
    ????????????????????head?
    =?tail?=?null;
    ????????????????
    else?if?(head.value.Equals(obj))
    ????????????????????head?
    =?head.next;
    ????????????????
    else
    ????????????????{
    ????????????????????
    //temp_prev為刪除值的前驅節點
    ????????????????????for?(SNode?temp_prev?=?head,?temp?=?head.next;?temp?!=?null;?temp_prev?=?temp_prev.next,?temp?=?temp.next)
    ????????????????????{
    ????????????????????????
    if?(temp.value.Equals(obj))
    ????????????????????????{
    ????????????????????????????temp_prev.next?
    =?temp.next;??//設置前驅節點的next為下個節點?
    ????????????????????????????if?(temp?==?tail)
    ???????????????????????????????tail?
    =?temp_prev;
    ????????????????????????????temp?
    =?null;
    ????????????????????????????
    break;
    ????????????????????????}
    ????????????????????}
    ????????????????}
    ????????????}
    ????????}
    ????????
    public??Object?getHead()
    ????????{
    ????????????
    return?this.head.value;
    ????????}
    ????????
    public?Object?getTail()
    ????????{
    ????????????
    return?this.tail.value;
    ????????}
    ? ? ? ? public void Clear()
    ??????? {

    ??????????? do
    ??????????? {
    ??????????????? Delete(head.value);
    ??????????? } while (!IsEmpty());
    ??????? }

    ????}

    ????
    class?SNode
    ????{
    ????????
    public?Object?value;
    ????????
    public?SNode?next;
    ????????
    public?SNode(Object?value,?SNode?next)
    ????????{
    ????????????
    this.value?=?value;
    ????????????
    this.next?=?next;
    ????????}
    ????????
    public?SNode(Object?value)
    ????????{
    ????????????
    this.value?=?value;
    ????????????
    this.next?=?null;
    ????????}
    ????}

    實現雙向鏈表:
    ?//雙向鏈表
    ????public?class?LinkedList:List
    ????{
    ????????
    private?LinkedNode?head,?tail;

    ????????
    public?LinkedList()
    ????????{
    ????????????head?
    =?tail?=?null;
    ????????}
    ????????
    public?bool?IsEmpty()
    ????????{
    ????????????
    return?head?==?null;
    ????????}

    ????????
    public?void?Unshift(Object?obj)
    ????????{
    ????????????
    if?(IsEmpty())
    ????????????????head?
    =?tail?=?new?LinkedNode(obj);
    ????????????
    else
    ????????????{
    ????????????????head?
    =?new?LinkedNode(obj,?null,?head);
    ????????????????head.next.prev?
    =?head;
    ????????????}

    ????????}
    ????????
    public?Object?Shift()
    ????????{
    ????????????
    if?(IsEmpty())
    ????????????????
    throw?new?NullReferenceException();
    ????????????Object?obj?
    =?head.value;
    ????????????
    if?(head?==?tail)
    ????????????????head?
    =?tail?=?null;
    ????????????
    else
    ????????????{
    ????????????????head?
    =?head.next;
    ????????????????head.prev?
    =?null;
    ????????????}
    ????????????
    return?obj;
    ????????}

    ????????
    public?void?Push(Object?obj)
    ????????{
    ????????????
    if?(IsEmpty())
    ????????????????head?
    =?tail?=?new?LinkedNode(obj);
    ????????????
    else
    ????????????{
    ????????????????tail?
    =?new?LinkedNode(obj,?tail,?null);
    ????????????????tail.prev.next?
    =?tail;
    ????????????}
    ????????}

    ????????
    public?Object?Pop()
    ????????{
    ????????????
    if?(IsEmpty())
    ????????????????
    throw?new?NullReferenceException();
    ????????????Object?value?
    =?tail.value;
    ????????????
    if?(head?==?tail)
    ????????????????head?
    =?tail?=?null;
    ????????????
    else
    ????????????{
    ????????????????tail?
    =?tail.prev;
    ????????????????tail.next?
    =?null;
    ????????????}
    ????????????
    return?value;
    ????????}
    ????????
    public?bool?Contain(Object?obj)
    ????????{
    ????????????
    if?(IsEmpty())
    ????????????????
    return?false;
    ????????????
    else
    ????????????{
    ????????????????
    for?(LinkedNode?temp?=?head;?temp?!=?null;?temp?=?temp.next)
    ????????????????????
    if?(temp.value.Equals(obj))
    ????????????????????????
    return?true;
    ????????????}
    ????????????
    return?false;
    ????????}

    ????????
    public?void?Delete(Object?obj)
    ????????{
    ????????????
    if?(IsEmpty())
    ????????????????
    throw?new?NullReferenceException();
    ????????????
    if?(head?==?tail)
    ????????????????head?
    =?tail?=?null;
    ????????????
    else
    ????????????{
    ????????????????
    for?(LinkedNode?temp?=?head;?temp?!=?null;?temp?=?temp.next)
    ????????????????{
    ????????????????????
    if?(temp.value.Equals(obj))
    ????????????????????{
    ????????????????????????
    if?(temp.value.Equals(obj))
    ????????????????????????{
    ????????????????????????????
    if?(temp?==?tail)
    ????????????????????????????{
    ????????????????????????????????tail?
    =?tail.prev;
    ????????????????????????????????tail.next?
    =?null;
    ????????????????????????????????
    break;
    ????????????????????????????}
    ????????????????????????????
    else?if?(temp?==?head)
    ????????????????????????????{
    ????????????????????????????????head.next.prev?
    =?null;
    ????????????????????????????????head?
    =?head.next;
    ????????????????????????????}

    ????????????????????????????
    else
    ????????????????????????????????temp.prev.next?
    =?temp.next;
    ????????????????????????}
    ????????????????????}
    ????????????????}

    ????????????}
    ????????}

    ????????
    public?void?PrintAll()
    ????????{
    ????????????
    string?result?=?"";
    ????????????
    for(LinkedNode?temp=head;temp!=null;temp=temp.next)
    ????????????????result?
    +=?"?"?+?temp.value.ToString();
    ????????????Console.WriteLine(result);
    ????????}
    ????????
    public?Object?getHead()
    ????????{
    ????????????
    return?this.head.value;
    ????????}
    ????????
    public?Object?getTail()
    ????????{
    ????????????
    return?this.tail.value;
    ????????}
    ? ? ? ? public void Clear()
    ??????? {
    ??????????? do
    ??????????? {
    ??????????????? Delete(head.value);
    ??????????? } while (!IsEmpty());

    ??????? }
    ??? }
    ????
    class?LinkedNode
    ????{
    ????????
    public?Object?value;
    ????????
    public?LinkedNode?prev;
    ????????
    public?LinkedNode?next;

    ????????
    public?LinkedNode(Object?value,?LinkedNode?prev,?LinkedNode?next)
    ????????{
    ????????????
    this.value?=?value;
    ????????????
    this.next?=?next;
    ????????????
    this.prev?=?prev;
    ????????}
    ????????
    public?LinkedNode(Object?value)
    ????????{
    ????????????
    this.value?=?value;
    ????????}
    ????}


    評論

    # re: C#實現鏈表  回復  更多評論   

    2011-09-24 14:15 by tbw
    可以不錯
    主站蜘蛛池模板: 国产免费女女脚奴视频网 | 国产亚洲一区区二区在线| 亚洲偷自拍另类图片二区| 国产1000部成人免费视频| 亚洲区精品久久一区二区三区| 日本视频在线观看永久免费| 亚洲日本va在线视频观看| 香蕉视频在线免费看| 亚洲永久无码3D动漫一区| 三级黄色免费观看| 国产亚洲精品一品区99热| 免费女人高潮流视频在线观看| 亚洲午夜精品久久久久久人妖| 97在线视频免费公开观看| 91亚洲视频在线观看| 女人18毛片a级毛片免费| 亚洲AV无码一区二区三区牲色 | 乱爱性全过程免费视频| 久久久久亚洲AV成人网人人网站 | 亚洲精品乱码久久久久久V| 女人18毛片水真多免费看| 国产综合成人亚洲区| 亚洲一区二区精品视频| 三级网站在线免费观看| 亚洲电影唐人社一区二区| 精品久久久久国产免费| 美女黄频视频大全免费的| 久久久久亚洲精品中文字幕| 在线观看免费视频一区| 亚洲视频在线免费播放| 免费看美女让人桶尿口| 一级毛片aaaaaa视频免费看| 亚洲国产精品一区二区久久hs| 精品免费人成视频app| 美女18毛片免费视频| 亚洲国产精品无码一线岛国| 免费影院未满十八勿进网站| 无套内射无矿码免费看黄| 久久精品国产精品亚洲艾| 91在线视频免费91| a在线视频免费观看在线视频三区|