數(shù)據(jù)結(jié)構(gòu):反向鏈表

字號(hào):

class Link//this class reverse the LinkedList
    { public int a;
    public Link next;
    }
    class createLink //the class create the LinkedList
    {
    Link header=null;
    Link p = null;
    Link temp = null;
    Link l=null; //Link k=null;
    Link g=null;
    public void create()
    { string str;
    int i;
    Console.WriteLine("Please enter number:");
    str=Console.ReadLine();
    while(str!="y")
    { i=Convert.ToInt32(str);
    temp=new Link();
    temp.a=i;
    temp.next=null;
    if(g==null)
    g=temp;
    if(header==null)
    header=temp;
    if(p==null)
    p=temp;
    else
    { p.next=temp;
    p=p.next;
    }
    Console.WriteLine("please enter number:");
    str=Console.ReadLine();
    }
    }
    public void display()
    { while(header!=null)
    { Console.WriteLine(header.a);
    header=header.next;
    }
    }
    public void reversed() // the mothod reversed the LinkedList
    { Link k=null;
    Link tmp=null;
    Link com =null;
    if(tmp==null)
    tmp=header.next;
    while(tmp!=null)
    { //if(com==null)
    // com=header;
    l=tmp;
    if(k==null)
    { header.next=null;
    k=header;
    }
    com=header;
    header=l;
    tmp=l.next;
    l.next=com;
    }
    }
    public void show()
    { while(l!=null)
    { Console.WriteLine(l.a);
    l=l.next;
    } } }
    class Tester
    { static void Main()
    { createLink cl=new createLink();
    cl.create();
    //cl.display();
    cl.reversed();
    cl.show();
    } } }