Contoh Program Queue Dengan Linked List

December 22, 2010
Below are two examples of implementing a linked and double linked list in C#. The framework already has a LinkedList
The links at the bottom of the page give a whole host of discussions on the subject, mostly from stackoverflow. You might wonder where you’d ever need to use a (double) linked list, and you might be right when it comes .NET however the class itself is used internally by Regex and by a System.Net timer. It’s not (as I previously said here) used by stacktraces for Exceptions, these are are handled by the StackTrace and StackFrame classes.
But anything that needs a parent-child relationship makes use of a form of it, and a single linked list is an alternative form of a stack or queue.
Contoh Program Queue Pada C; Macam-macam Linked List; Contoh Program Array Pada C; Pengertian Linked List; Lintasan Terpendek dengan Algoritma Dijkstra; Pernyataan PERFORM Pada Cobol; Contoh PERFORM – VARYING 1 Pada Cobol; Contoh PERFORM – VARYING 2 Pada Cobol; Contoh PERFORM – VARYING – AFTER 1 Pada Cobol; Contoh Program Perform.
As always with all the code on the site, if you spot a bug or something you think is a glaring load of rubbish, feel free to contact me on the contact page.
Linked List
publicstaticvoidMain() |
{ |
LinkedListlist=newLinkedList(); |
list.Insert('1'); |
list.Insert('2'); |
list.Insert('3'); |
list.Insert('4'); |
list.Insert('5'); |
Console.WriteLine('List: '+list); |
while (!list.IsEmpty) |
{ |
LinkdeletedLink=list.Delete(); |
Console.Write('Removed: '+deletedLink); |
Console.WriteLine(''); |
} |
Console.WriteLine('List: '+list); |
Console.Read(); |
} |
publicclassLink |
{ |
publicstringTitle { get; set; } |
publicLinkNextLink { get; set; } |
publicLink(stringtitle) |
{ |
Title=title; |
} |
publicoverridestringToString() |
{ |
returnTitle; |
} |
} |
publicclassLinkedList |
{ |
privateLink_first; |
publicboolIsEmpty |
{ |
get |
{ |
return_firstnull; |
} |
} |
publicLinkedList() |
{ |
_first=null; |
} |
publicLinkInsert(stringtitle) |
{ |
// Creates a link, sets its link to the first item and then makes this the first item in the list. |
Linklink=newLink(title); |
link.NextLink=_first; |
_first=link; |
returnlink; |
} |
publicLinkDelete() |
{ |
// Gets the first item, and then this to be the one it is linked forward to |
Linktemp=_first; |
if (_first!=null) |
_first=_first.NextLink; |
returntemp; |
} |
publicoverridestringToString() |
{ |
LinkcurrentLink=_first; |
StringBuilderbuilder=newStringBuilder(); |
while (currentLink!=null) |
{ |
builder.Append(currentLink); |
currentLink=currentLink.NextLink; |
} |
returnbuilder.ToString(); |
} |
} |
Doubly linked list
How to use srt files on iphone. As you can see, the implementation for a doubly linked list doesn’t differ much from a linked list. The extra functionality comes in the form of operations like deep copying, enumerators, advanced finds and inserts which I’ve left out; these are in framework’s LinkedList
publicstaticvoidMain() |
{ |
DoubleLinkedListlist=newDoubleLinkedList(); |
list.Insert('1'); |
list.Insert('2'); |
list.Insert('3'); |
DoubleLinklink4=list.Insert('4'); |
list.Insert('5'); |
Console.WriteLine('List: '+list); |
list.InsertAfter(link4, '[4a]'); |
Console.WriteLine('List: '+list); |
Console.Read(); |
} |
publicclassDoubleLink |
{ |
publicstringTitle { get; set; } |
publicDoubleLinkPreviousLink { get; set; } |
publicDoubleLinkNextLink { get; set; } |
publicDoubleLink(stringtitle) |
{ |
Title=title; |
} |
publicoverridestringToString() |
{ |
returnTitle; |
} |
} |
publicclassDoubleLinkedList |
{ |
privateDoubleLink_first; |
publicboolIsEmpty |
{ |
get |
{ |
return_firstnull; |
} |
} |
publicDoubleLinkedList() |
{ |
_first=null; |
} |
publicDoubleLinkInsert(stringtitle) |
{ |
// Creates a link, sets its link to the first item and then makes this the first item in the list. |
DoubleLinklink=newDoubleLink(title); |
link.NextLink=_first; |
if (_first!=null) |
_first.PreviousLink=link; |
_first=link; |
returnlink; |
} |
publicDoubleLinkDelete() |
{ |
// Gets the first item, and sets it to be the one it is linked to |
DoubleLinktemp=_first; |
if (_first!=null) |
{ |
_first=_first.NextLink; |
if (_first!=null) |
_first.PreviousLink=null; |
} |
returntemp; |
} |
publicoverridestringToString() |
{ |
DoubleLinkcurrentLink=_first; |
StringBuilderbuilder=newStringBuilder(); |
while (currentLink!=null) |
{ |
builder.Append(currentLink); |
currentLink=currentLink.NextLink; |
} |
returnbuilder.ToString(); |
} |
///// New operations |
publicvoidInsertAfter(DoubleLinklink, stringtitle) |
{ |
if (linknullstring.IsNullOrEmpty(title)) |
return; |
DoubleLinknewLink=newDoubleLink(title); |
newLink.PreviousLink=link; |
// Update the 'after' link's next reference, so its previous points to the new one |
if (link.NextLink!=null) |
link.NextLink.PreviousLink=newLink; |
// Steal the next link of the node, and set the after so it links to our new one |
newLink.NextLink=link.NextLink; |
link.NextLink=newLink; |
} |
} |
Links
- Threedifferentdiscussions on stackoverflow about linked lists.
- Wikipedia’s article isn’t that great but can help.
- Codeproject naturally has an implementation as you’d expect.
I'm Chris Small, a software engineer working in London. This is my tech blog. Find out more about me viaGithub, Stackoverflow, Resume