链表(C#)

using System;

//单链表
class Link
{
    public int iData;       //data item
    public double dData;    //data item
    public Link next;       //next link in list

    public Link(int id, double dd)     //constructor
    {
        iData = id;     //initalize data
        dData = dd;     //('next' is automatically set to null)
    }

    public void DisplayLink()   //display ourself
    {
        Console.Write("{{{0}, {1}}}", iData, dData);
    }   // end class Link
}

class LinkList
{
    private Link first;     //ref to first link on list

    public LinkList()  //constructor
    {
        first = null;       //no items on list yet
    }

    public bool IsEmpty()   //true if list is empty
    { 
        return (first == null);
    }
                            //insert at start of list
    public void InsertFirst(int id, double dd)
    {                                       
        Link newLink = new Link(id, dd);    //make new link
        newLink.next = first;           //newLink-->first
        first = newLink;                //first-->newLink
    }

    public Link DeleteFirst()       //delete first item
    {                               //(assumes list not empty)
        Link temp = first;          //save reference to link
        first = first.next;         //delete it: first-->old first next
        return temp;                //return deleted link
    }

    public void DisplayList()
    {
        Console.Write("List(first-->last):");

        Link current = first;   //start at beginning of list
        while (current != null) //until end of list
        {
            current.DisplayLink();  //print data
            current = current.next; //move to next link
        }
        Console.WriteLine();
    }
}//end class LinkList

class LinkListApp
{
    static void Main(string[] args)
    {
        LinkList theList = new LinkList();  //make new list

        theList.InsertFirst(22, 2.99);      //insert four items
        theList.InsertFirst(44, 4.99);
        theList.InsertFirst(66, 6.99);
        theList.InsertFirst(88, 8.99);

        theList.DisplayList();          //display list

        while (!theList.IsEmpty())      //until is empty,
        {
            Link aLink = theList.DeleteFirst();     //delete link
            Console.Write("Delete ");       //display it
            aLink.DisplayLink();
            Console.WriteLine();
        }
        theList.DisplayList();        //display list
        Console.ReadLine();
    }//end main

}   //end class LinkListApp



using System;


//查找和删除指定链接点
class Link
{
    public int iData;   //data item(key)
    public double ddData;   //data item
    public Link next;   //next link in list


    public Link(int id, double dd) //constructor
    {
        iData = id;
        ddData = dd;
    }


    public void DisplayLink()   //display ourself
    {
        Console.Write("{{{0},{1}}}", iData, ddData);
    }
}


class LinkList
{
    public Link first;      //ref to first link on list


    public LinkList()       //constructor
    {
        first = null;       //no links on list yet
    }


    public void InsertFirst(int id, double dd)
    {                       //make new link
        Link newLink = new Link(id, dd);
        newLink.next = first;           //it points to old first link
        first = newLink;                //now first points to this
    }


    public Link Find(int key)   //find link with given key
    {                           //(assumes non-empty list)
        Link current = first;   //start at 'first'
        while (current.iData != key)
        {
            if (current.next == null)   //if end of list,
                return null;            //didn't find it
            else                        //not end of list,
                current = current.next;    //goto next link
        }


        return current;                 //found it
    }


    public Link Delete(int key)     //delete link with given key
    {                               //(assumes non-empty list)
        Link current = first;       //search for link
        Link previous = first;


        while (current.iData != key)
        {
            if (current.next == null)   //didn't find it
                return null;
            else
            {
                previous = current;     //go to next link
                current = current.next;
            }
        }
        if (current == first)           //if first link,
            first = first.next;         //change first
        else                            //otherwise,
            previous.next = current.next; //bypass it
        return current;
    }


    public void DisplayList()   //display the list
    {
        Console.WriteLine("List(first-->last):");
        Link current = first;   //start at beginning of list
        while (current != null) //util end of list,
        {
            current.DisplayLink();  //print data
            current = current.next;   //move to next link
        }
        Console.WriteLine();
    }
}


class LinkList2App
{
    static void Main(string[] args)
    {
        LinkList theList = new LinkList();  //make list


        theList.InsertFirst(22, 2.99);      //insert 4 items
        theList.InsertFirst(44, 4.99);
        theList.InsertFirst(66, 6.99);
        theList.InsertFirst(88, 8.99);


        theList.DisplayList();          //display list


        Link f = theList.Find(44);      //find item


        if (f != null)
            Console.WriteLine("Found link with key {0}", f.iData);
        else
            Console.WriteLine("Cann't find link");


        Link d = theList.Delete(66);    //delete  item
        if (d != null)
            Console.WriteLine("Delete link with key {0}", d.iData);
        else
            Console.WriteLine("Cann't delete link");


        theList.DisplayList();  //display list


        Console.ReadLine();
    }//end main
}//end class LinkList2App


using System;

//双端链表
class Link
{
    public long dData;          //data item
    public Link next;           //next link in list

    public Link(long d)         //constructor
    {
        dData = d;
    }

    public void DisplayLink()   //display this link
    {
        Console.Write("{0} ", dData);
    }
}

class FirstLastList
{
    private Link first;
    private Link last;

    public FirstLastList()      //constructor
    {
        first = null;           //no links on list yet
        last = null;
    }

    public bool IsEmpty()       //true if no links
    {
        return first == null;
    }

    public void InsertFirst(long dd)    //insert at front of list
    {
        Link newLink = new Link(dd);    //make new link

        if (IsEmpty())          // if empty list
            last = newLink;     //newlink <-- last
        newLink.next = first;   //newlink --> first
        first = newLink;        //first --> newLink
    }

    public void InsertLast(long dd)     //insert at end of list
    {
        Link newLink = new Link(dd);    //make new link

        if (IsEmpty())                   //if empty list
            first = newLink;             //first --> newLink
        last.next = newLink;             //old last --> newLinkx
        last = newLink;                  //last --> newLink
    }

    public long DeleteFirst()      //delete first link
    {
        long temp = first.dData;    //(assume non-empty list)
        if (first.next == null)     //if only one item
            last = null;            //null<--last
        first = first.next;         //first --> old next
        return temp;
    }

    public void DisplayList()
    {
        Console.Write("List(first-->last):");
        Link current = first;       //start at beginning
        while (current != null)      //until end of list
        {
            current.DisplayLink();      //print data
            current = current.next;     //move to next link
        }
        Console.WriteLine();
    }
} //end class FirstLastLink


class FirstLastApp
{
    static void Main(string[] args)
    {                           //make a new list
        FirstLastList theList = new FirstLastList();

        theList.InsertFirst(22);    //insert at front
        theList.InsertFirst(44);
        theList.InsertFirst(88);

        theList.InsertLast(11);     //insert at rear
        theList.InsertLast(33);
        theList.InsertLast(55);

        theList.DisplayList();      //display the list

        theList.DeleteFirst();      //delete first two items
        theList.DeleteFirst();

        theList.DisplayList();      //display again

        Console.ReadLine();
    }//end main
}//end class FirstLastApp


using System;

//有序链表.
class Link
{
    public long dData;
    public Link next;

    public Link(long dd)
    {
        dData = dd;
    }

    public void DisplayLink()
    {
        Console.Write("{0} ", dData);
    }
}


class SortedList
{
    private Link first;
    public SortedList()
    {
        first = null;
    }

    public bool IsEmpty()
    {
        return first == null;
    }

    public void Insert(long key)
    {
        Link newLink = new Link(key);
        Link previous = null;
        Link current = first;

        while (current != null && key > current.dData)
        {
            previous = current;
            current = current.next;
        }

        if (previous == null)
            first = newLink;

        else
            previous.next = newLink;
        newLink.next = current;

    }

    public Link Remove()
    {
        Link temp = first;
        first = first.next;
        return first;
    }

    public void DisplayList()
    {
        Console.Write("List(first -->last):");
        Link current = first;
        while (current != null)
        {
            current.DisplayLink();
            current = current.next;
        }

        Console.WriteLine();
    }
}


class SortedListApp
{
    public static void Main(string[] args)
    {
        SortedList theSortedList = new SortedList();
        theSortedList.Insert(20);
        theSortedList.Insert(40);

        theSortedList.DisplayList();

        theSortedList.Insert(10);
        theSortedList.Insert(30);
        theSortedList.Insert(50);

        theSortedList.DisplayList();

        theSortedList.Remove();

        theSortedList.DisplayList();

        Console.ReadLine();

    }
}

//表插入排序

using System;

class Link
{
    public long dData;
    public Link next;

    public Link(long dd)
    {
        dData = dd;
    }
}

class SortedList
{
    private Link first;

    public SortedList()
    {
        first = null;
    }

    public SortedList(Link[] linkArr)
    {
        first = null;
        for (int j = 0; j < linkArr.Length; j++)
            Insert(linkArr[j]);

    }

    public void Insert(Link k)
    {
        Link previous = null;
        Link current = first;

        while (current != null && k.dData > current.dData)
        {
            previous = current;
            current = current.next;
        }

        if (previous == null)
            first = k;
        else
            previous.next = k;

        k.next = current;
    }

    public Link Remove()
    {
        Link temp = first;
        first = first.next;
        return temp;
    }
}


class ListInsertionSortApp
{
    public static void Main(String[] args)
    {
        int size = 10;

        Link[] linkArray = new Link[size];

        Random ranObj = new Random();

        for (int j = 0; j < size; j++)
        {
            int n = ranObj.Next(100);
            //这里如果省掉前面Random ranObj = new Random();
            //直接用int n = new Random().Next(100);
            //为什么每次循环都生成同一个数?即10个n相等.
            //Console.WriteLine("{0}", n);
            Link newLink = new Link(n);
            linkArray[j] = newLink; 
        }

        Console.WriteLine("Unsorted array: ");
        for (int j = 0; j < size; j++)
        {
            Console.Write("{0} ", linkArray[j].dData);
        }
        Console.WriteLine();

        SortedList theSortedList = new SortedList(linkArray);

        for (int j = 0; j < size; j++)
            linkArray[j] = theSortedList.Remove();

        Console.Write("Sorted Array: ");
        for (int j = 0; j < size; j++)
            Console.Write("{0} ", linkArray[j].dData);

        Console.ReadLine();

    }

}



没有更多推荐了,返回首页