-
March 8th, 2011, 04:09 PM
#1
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!
-
March 8th, 2011, 04:11 PM
#2
Re: How to sort a singly linked LinkedList?
OH Sorry, Also I am using .NET v4.0 I believe (Visual Studio 2010 Pro)
-
March 8th, 2011, 06:17 PM
#3
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.
-
March 8th, 2011, 11:24 PM
#4
Re: How to sort a singly linked LinkedList?
Originally Posted by ThermoSight
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.
-
March 9th, 2011, 05:45 AM
#5
Re: How to sort a singly linked LinkedList?
Originally Posted by BigEd781
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|