CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2011
    Posts
    3

    How to sort a singly linked LinkedList?

    I have tried having a look around on the web at different forums and can't decipher how to do this.

    This is my Link class:

    //////////////////////////////////////////////////////
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace LinkedListGen
    {
    class LinkGen<T>
    {
    private T data;
    private LinkGen<T> next;

    public LinkGen(T item)
    {
    data = item;
    next = null;
    }

    public LinkGen(T item, LinkGen<T> list)
    {
    data = item;
    next = list;
    }

    public T HeadList
    {
    set { this.data = value; }
    get { return this.data; }
    }

    public LinkGen<T> TailList
    {
    set { this.next = value; }
    get { return this.next; }
    }


    }
    }
    //////////////////////////////////

    And these are I guess the methods from the LinkedList class I believe would be needed(?):

    //////////////////////////////////
    public void AppendItem(T item) // Add items to back of list
    {
    LinkGen<T> temp = list;

    if (temp == null)
    {
    list = new LinkGen<T>(item);
    }

    else
    {
    while (temp.TailList != null)
    {
    temp = temp.TailList;
    }

    temp.TailList = new LinkGen<T>(item);
    }

    }



    public void Sort()
    {
    //I need the sort method here ofc.
    }



    public void AddItem(T item) //add item to front of list
    {
    list = new LinkGen<T>(item, list);
    }

    //////////////////////////////////////////////

    Please help lol, ty!

  2. #2
    Join Date
    Mar 2011
    Posts
    3

    Re: How to sort a singly linked LinkedList?

    OH Sorry, Also I am using .NET v4.0 I believe (Visual Studio 2010 Pro)

  3. #3
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: How to sort a singly linked LinkedList?

    Hi Accendo.

    I guess you could "roll your own" linked list class, certainly I have done exactly that just for the sheer fun of it, but I wanted to make certain that you're aware that .NET provides a List<T> class, and that it provides all of the support you could possibly want (and much more!).

    Microsoft has already done all the heavy lifting.

    Still, if ya wanna write your own, just for drill, Enjoy !

    Best wishes.

    Old Fool.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to sort a singly linked LinkedList?

    Quote Originally Posted by ThermoSight View Post
    Hi Accendo.

    I guess you could "roll your own" linked list class, certainly I have done exactly that just for the sheer fun of it, but I wanted to make certain that you're aware that .NET provides a List<T> class, and that it provides all of the support you could possibly want (and much more!).
    Agreed, but I would note that List<T> is not implemented as a linked list. There is of course the LinkedList<T> collection class if you (for whatever reason) need really fast insertions/deletions and don't care about losing constant time random access.
    Last edited by BigEd781; March 9th, 2011 at 02:34 AM.

  5. #5
    Join Date
    Jan 2010
    Posts
    1,133

    Re: How to sort a singly linked LinkedList?

    Quote Originally Posted by BigEd781 View Post
    Agreed, but I would note that List<T> is not implemented as a linked list.
    You could be right, but how do you know? Internally, it could be.
    .NET Reflector or something alike? Don't have it with me right now, and didn't check before. Docs maybe?
    Unless your point was that you don't get to manipulate individual containing elements directly?

    BTW, LinkedList<T> is a doubly linked list.
    Last edited by TheGreatCthulhu; March 9th, 2011 at 05:48 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured