数据结构学习(5):链表

using System;

namespace DataStructure.LinkedList
{
    
/// <summary>
    
/// Class1 的摘要说明。
    
/// </summary>

    public class ListNode
    
{
        
public string airport;
        
public ListNode link;
        
public ListNode previous;
    }

    
public class LinkedList
    
{
        
public int length;//结点数
        public ListNode firstNode;

        
public int size()
        
{
            
return length;
        }

        
        
public void insertNewSecondNode(string airportCode)
        
{
            
if (length==0return;
            ListNode newNode;
//让newNode成为一个ListNode变量
            newNode=new ListNode();//在newNode中存储一个新结点
            newNode.airport=airportCode;
            newNode.link
=firstNode.link;//让newNode连接到L的次结点
            newNode.previous=firstNode;
            firstNode.link
=newNode;//让L的第一个结点连接到newNode
            firstNode.previous=null;//
            length++;
        }


        
public ListNode listSearch(string airportCode)
        
{
            ListNode N;
            N
=firstNode;
            
while(N!=null)
            
{
                
if(airportCode.Equals(N.airport))
                
{
                    
return N;
                }

                
else
                    N
=N.link;

            }

            
return N;
        }


        
public void deleteLastNode()
        
{
            ListNode previousNode,currentNode;
            
if(firstNode!=null)
            
{
                
if(firstNode.link==null)
                
{
                    firstNode
=null;
                    length
--;
                }

                
else
                
{
                    previousNode
=firstNode;
                    currentNode
=firstNode.link;

                    
while(currentNode.link!=null)//前移两个指针
                    {
                        previousNode
=currentNode;
                        currentNode
=currentNode.link;
                    }

                    previousNode.link
=null;//把倒数第二个结点指向空
                    length--;
                }

            }

        }

        
public void insertNewLastNode(string airportCode)
        
{
            ListNode N
=new ListNode();//通过airport==airportNode和link==null构造
            N.airport=airportCode;
            N.link
=null;
            
//将N作为新的末端结点插入
            if(firstNode==null)
                firstNode
=N;
            
else
            
{
                
//利用结点指针P定位链表的末端结点
                ListNode P=firstNode;
                
while(P.link!=null)
                
{
                    P
=P.link;
                }


                
//最后将结点N链接到末端
                P.link=N;
            }

            length
++;
        }

        
public void print()
        
{
            ListNode N;
            
//首先,打印一个左括号
            Console.WriteLine("(");
            N
=firstNode;
            
//假如N没有指向空结点,则打印的airport
            
//并将N前移指向链表的下一个结点
            while(N!=null)
            
{
                Console.WriteLine(N.airport);
                N
=N.link;
                
if(N!=null)
                
{
                    Console.WriteLine(
",");
                }

            }

            Console.WriteLine(
")");
        }


        
public void  insertNewFirstNode(string A)
        
{
            ListNode N
=new ListNode();
            N.airport
=A;
            N.link
=firstNode;
            N.previous
=null;
            firstNode
=N;
        }

        
public LinkedList Clone()
        
{
            LinkedList NL
=new LinkedList();
            NL.firstNode
=this.firstNode;
            NL.length
=this.length;
            
return NL;
        }

        
public void deleteFirst()
        
{
            firstNode
=firstNode.link;
        }


        
        
public static void Main()
        
{
            LinkedList L
=new LinkedList();
            L.insertNewLastNode(
"DUS");
            L.insertNewLastNode(
"ORD");
            L.insertNewLastNode(
"SAN");
            
            L.print();
            L.insertNewFirstNode(
"CHN");
            L.print();
            L.deleteFirst();
            L.print();
            LinkedList NL
=L.Clone();
            NL.print();
            Console.WriteLine(L.length);
            Console.ReadLine();
            
        }


        
public void reverse()
        
{
            firstNode
=reverse1(firstNode);
//            Reverse采取下面的辅助方法reverse(L),让LinkedList头结点的firstNode
//            域指向链表L
        }

        
public ListNode reverse1(ListNode L)
        
{
            
if(L==null)
            
{
                
return null;//基本状态
            }

            
else
            
{
                ListNode head
=L;
                ListNode tail
=L.link;//将L划分为head和tail
                head.link=null;//掉转
                return concat(reverse1(tail),head);//递归
            }

        }


        
public ListNode concat(ListNode L1,ListNode L2)
        
{
            
if(L1==null)
                
return L2;//基本状态
            else
            
{
                L1.link
=concat(L1.link,L2);//递归
                return L1;
            }

        }

    }

}

posted @ 2006-12-14 19:07  大天使泰瑞尔  阅读(548)  评论(0编辑  收藏  举报