How to add after method works in linked list? - c#

class ListNode
{
public object Data { get; private set; }
public ListNode Next { get; set; }
public ListNode(object Element)
{
Data = Element;
}
public ListNode(object Element, ListNode NextNode)
{
Data = Element;
Next = NextNode;
}
public ListNode()
{
}
}
class LinkedList
{
ListNode first;
ListNode last;
public LinkedList()
{
first = null;
last = null;
}
public ListNode Find(object After)
{
ListNode current = new ListNode();
current= first;
while (current.Data != After)
current = current.Next;
return current;
}
public void Add(object newItem, object After)
{
ListNode current=new ListNode();
ListNode newNode=new ListNode();
current = Find(After);
newNode.Next = current.Next;
current.Next = newNode;
}
public void InsertAtFront(object Element)
{
if (IsEmpty())
{
first = last = new ListNode(Element);
}
else
{
first = new ListNode(Element,first);
}
}
bool IsEmpty()
{
return first == null;
}
public void Display()
{
ListNode current = first;
while (current!=null)
{
Console.WriteLine(current.Data);
current = current.Next;
}
}
}
I implement Find method for Add After specific element, but when I debug it showing me the object reference not set to an instance of an object exception. Please point out my mistake in Find method or with Add After method. thanks

current= first;
while (current.Data != After)
can result in a potential null reference issue. first can still be set to null from the constructor initialisation which would mean that current = null, which'd then result in null.Data which would throw a null reference exception.
This would fix the null reference issue in Find()
while (current != null && current.Data != After)
Fixing this would result in null being returned which would still result in issues in Add
current = Find(After);
newNode.Next = current.Next;
current.Next = newNode;
In this context, LinkedList is first initialised, current = Find(After) would mean current = null, causing another null reference exceptions on the next two lines.
public void Add(object newItem, object After)
{
if (IsEmpty())
{
InsertAtFront(newItem);
return;
}
ListNode newNode = new ListNode();
ListNode current = Find(After);
newNode.Next = current.Next;
current.Next = newNode;
}
This will fix both the Add and the Find methods to be usable in the form of:
LinkedList list = new LinkedList();
list.InsertAtFront("test");
list.Find(list.first.Data);
list.Add("test2", ll.first.Data);
This will make it workable, but I would however highly recommend reading into the implementation of linked lists or using one of the system collections as this implementation has quite a few potential issues.

Problem is here
while (current.Data != After)
current = current.Next;
When there is no After in your list you will eventually get current.Next equal to null
You need to check if current.Next is not null
while (current.Next != null && current.Data != After)
current = current.Next;
you should also fix your add logic (if you want to add elements into empty list)
public void Add(object newItem, object After)
{
if(IsEmpty())
{
InsertAtFront(newItem);
return;
}
ListNode newNode=new ListNode();
newNode.Data = newItem;
ListNode current = Find(After);
newNode.Next = current.Next;
current.Next = newNode;
}

Related

Clear in LinkedList for O(N). Why?

The built-in implementation of the LinkedList is a doubly-linked circular list.
I see the following implementation of the Clear method:
public void Clear() {
LinkedListNode<T> current = head;
while (current != null ) {
LinkedListNode<T> temp = current;
current = current.Next;
temp.Invalidate();
}
head = null;
count = 0;
version++;
}
So, this clearing method apparently works for O(N).
public sealed class LinkedListNode<T> {
internal LinkedList<T> list;
internal LinkedListNode<T> next;
internal LinkedListNode<T> prev;
internal T item;
public LinkedListNode( T value) {
this.item = value;
}
internal LinkedListNode(LinkedList<T> list, T value) {
this.list = list;
this.item = value;
}
public LinkedList<T> List {
get { return list;}
}
public LinkedListNode<T> Next {
get { return next == null || next == list.head? null: next;}
}
public LinkedListNode<T> Previous {
get { return prev == null || this == list.head? null: prev;}
}
public T Value {
get { return item;}
set { item = value;}
}
internal void Invalidate() {
list = null;
next = null;
prev = null;
}
}
I wonder why can't we just assign null to head instead of nulling all the references? As far as I can see, assigning null to the head will result in breaking the circle and all the nodes will be collected by GC pretty soon. Or am I missing something?
This is done so that once the linked list is cleared, anything holding references to its old nodes will throw errors if it tries to navigate the list.
If the old links weren't set to null, the old linked list would remain accessible if anything had a reference to any of its nodes, which would (a) hide the problem because the code would continue to apparently work and (b) keep alive the node objects in memory.

LinkedList Implementation Bug

I tried to implement my linked list below, It just that when I used my linked list, it seems that it's inserting a null entry in the middle. Anyone could take a look? The problem could be in the add end method.
LinkedList<T>
public class LinkedList<T> : IEnumerable<T>
{
public Node<T> Head { get; set; }
public int Count { get; set; }
public LinkedList()
{
Head = new Node<T>();
}
public void AddStart(T data)
{
if (Head == null)
{
Head = new Node<T> {Value = data};
}
else
{
var newNode = new Node<T> {Value = data, Next = Head};
Head = newNode;
}
Count++;
}
public void AddEnd(T data)
{
var newNode = new Node<T> { Value = data, Next = null};
var current = Head;
if (Head == null)
{
Head = newNode;
}
else
{
while (current.Next != null)
{
current = current.Next;
}
current.Next = newNode;
}
}
public IEnumerator<T> GetEnumerator()
{
Node<T> current = Head;
while (current != null)
{
yield return current.Value;
current = current.Next;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
Usage:
LinkedList<string> strings = new LinkedList<string>();
strings.AddStart("a");
strings.AddStart("b");
strings.AddStart("c");
strings.AddEnd("a");
strings.AddEnd("e");
strings.AddEnd("d");
Result: (Notice the null in the middle)
c
b
a
a
e
d
Usage:
LinkedList<string> strings = new LinkedList<string>();
strings.AddStart("a");
strings.AddStart("b");
strings.AddStart("c");
strings.AddEnd("a");
strings.AddEnd("e");
strings.AddEnd("d");
strings.AddStart("a");
strings.AddEnd("b");
strings.AddStart("a");
strings.AddStart("b");
strings.AddStart("c");
strings.AddEnd("a");
strings.AddEnd("e");
strings.AddEnd("d");
Result: (Notice the null in the middle)
c
b
a
a
c
b
a
a
e
d
b
a
e
d
You are creating an empty Node, when you initialize your LinkedList:
public LinkedList()
{
Head = new Node<T>();
}
At this point you essentially have a node without a value. This is displayed as an empty value.
Everytime you call AddStart, you insert items before hand. When calling AddEnd you insert items at the end. Since you call both methods equally, it appears that the initial node is in the middle.
You can resolve this by changing your constructor to this:
public LinkedList()
{
Head = null;
}
Or by removing the constructor alltogether. In both methods you are checking for a Head node with a value, so you do not need to initialize it without a value.
if (Head == null)
{
Head = new Node<T> {Value = data};
}
In your current state, you are checking if Head is null. Head is of Type Node, which, if it is a class, cannot be null, if you initialize it in your constrcutor (Head = new Node<T>(); //not null any longer). So you need to make up your mind on how you want to solve it.
Personally I'd just write a unit test to assert my logic and implementation is correct. If you assume that Head is null after initialisation, test for it:
[Fact]
public void AssertHeadIsNull()
{
var list = new LinkedList<int>();
Assert.Null(list.Head);
}
In your case, the test would have failed, and you could have backtracked the issue to your constructor.

How to sort an IntNode list

class IntNode
{
public int value { get; set; }
public IntNode next { get; set; }
public IntNode(int value)
{
this.value = value;
this.next = null;
}
public IntNode(int value, IntNode next)
{
this.value = value;
this.next = next;
}
public bool HasNext ()
{
return (this.next != null);
}
public override string ToString()
{
if (this.HasNext())
return this.value + " --> " + this.next;
else
return this.value + ". [end]";
}
}
If I am getting the first node of a list, how can I sort the list from smallest to highest?
Example:
for the list
5,-4,8,12,5,71,13,0
that list will be returned
-4,0,5,5,8,12,13,71
I have tried to do it but I can`t figure it out... Thanks!
What I have tried:
static IntNode Sort (IntNode head)
{
IntNode pos = head.next;
IntNode max = head;
IntNode current = head;
while (current != null)
{
while (pos != null)
{
if (pos.value > max.value) max = pos;
pos = pos.next;
}
head.next = max.next;
max.next = head;
head = max;
pos = current;
max = current;
current = current.next;
}
return head;
}
this is my homeword so pls help.
First, you need to be clear about your idea of how the algorithm for sorting is supposed to work. It helps to write it down in normal language.
From your code I see that you wander through your list and try to compare the current item with the next. That sounds like the right approach. The condition is right but the moving of values does not work in you solution.
if (pos.value > max.value)
The basic idea of sorting is moving the items.
In other words :
while it is not sorted yet and while the next element is not null and if the current item is larger than the next exchange the values then move to the next element and do it again.
// in the beginning you only need one helping IntNode variable
IntNode current = head;
// and a flag to stop the sorting
bool sorted = false;
I would suggest to switch the values
// the condition for the comparison
if (current.value > current.next.value)
{
int temp = current.value;
current.value = current.next.value;
current.next.value = temp;
// set the flag the a sort procedure has been made as indicator that it might still not be sorted
sorted = false;
}
The rest are 2 while-loops and a boolean variable to stop the outer loop.
EDIT:
Since you seem to have figured it out on your own, for the sake of completeness here is my version of the sorting method:
public static IntNode Sort(IntNode head)
{
IntNode current = head;
bool sorted = false;
while (!sorted)
{
// assume that at this run you actually finished your sorting job
sorted = true;
// set the current element to the first position
current = head;
while (current.next != null)
{
if (current.value > current.next.value)
{
int temp = current.value;
current.value = current.next.value;
current.next.value = temp;
// apparently you found an element that was not sorted, so you might not be done yet
sorted = false;
}
// move one element forward
current = current.next;
}
}
return head;
}
Implement the IComparable interface on your class.
class IntNode : IComparable {
// ...
public int CompareTo(object obj) {
if (obj == null) return 1;
IntNode otherNode = obj as IntNode ;
if (otherNode != null)
return this.value.CompareTo(otherNode.value);
else
throw new ArgumentException("Object is not an IntNode");
}
}
Put all your nodes into a list, then use the Sort method.
var allNodes = new List<IntNode>();
// add nodes to list ...
allNodes.Sort();

Reversing single linked list in C#

I am trying to reverse a linked list. This is the code I have come up with:
public static void Reverse(ref Node root)
{
Node tmp = root;
Node nroot = null;
Node prev = null;
while (tmp != null)
{
//Make a new node and copy tmp
nroot = new Node();
nroot.data = tmp.data;
nroot.next = prev;
prev = nroot;
tmp = tmp.next;
}
root = nroot;
}
It is working well. Was wondering if it possible to avoid creating new node. Would like to have suggestions on this.
That question gets asked a lot. When I was asked it in my interviews many years ago, I reasoned as follows: a singly-linked list is essentially a stack. Reversing a linked list is therefore a trivial operation on stacks:
newList = emptyList;
while(!oldList.IsEmpty())
newList.Push(oldList.Pop());
Now all you have to do is implement IsEmpty and Push and Pop, which are one or two lines tops.
I wrote that out in about twenty seconds and the interviewer seemed somewhat perplexed at that point. I think he was expecting me to take about twenty minutes to do about twenty seconds work, which has always seemed odd to me.
Node p = root, n = null;
while (p != null) {
Node tmp = p.next;
p.next = n;
n = p;
p = tmp;
}
root = n;
Years ago I missed out on a hipster-L.A.-entertainment-company ASP.NET MVC developer position because I could not answer this question :( (It's a way to weed out non-computer-science majors.) So I am embarrassed to admit that it took me way too long to figure this out in LINQpad using the actual LinkedList<T>:
var linkedList = new LinkedList<int>(new[]{1,2,3,4,5,6,7,8,9,10});
linkedList.Dump("initial state");
var head = linkedList.First;
while (head.Next != null)
{
var next = head.Next;
linkedList.Remove(next);
linkedList.AddFirst(next.Value);
}
linkedList.Dump("final state");
The read-only LinkedListNode<T>.Next property is what makes LinkedList<T> so important here. (Non-comp-sci people are encouraged to study the history of Data Structures---we are supposed to ask the question, Where does the linked list come from---why does it exist?)
You don't need to make a copy. Some pseudo code:
prev = null;
current = head;
next = current->next;
(while next != null)
current->next=prev
prev=current
current=next
next=current->next
This performed pretty well on Leetcode.
public ListNode ReverseList(ListNode head) {
ListNode previous = null;
ListNode current = head;
while(current != null) {
ListNode nextTemp = current.next;
current.next = previous;
previous = current;
current = nextTemp;
}
return previous;
}
Why not just have the head point at the tail, the tail point at the head, and go through the list reversing the direction in which prev points?
If you're not using a head and a tail, just go through the list reversing the prev relationships, and then make head point at the one that had a null prev when you got to it.
public Node ReverseList(Node cur, Node prev)
{
if (cur == null) // if list is null
return cur;
Node n = cur.NextNode;
cur.NextNode = prev;
return (n == null) ? cur : ReverseList(n, cur);
}
Here a sample code to reverse a linked list.
using System;
class Program
{
static void Main(string[] args)
{
LinkItem item = generateLinkList(5);
printLinkList(item);
Console.WriteLine("Reversing the list ...");
LinkItem newItem = reverseLinkList(item);
printLinkList(newItem);
Console.ReadLine();
}
static public LinkItem generateLinkList(int total)
{
LinkItem item = new LinkItem();
for (int number = total; number >=1; number--)
{
item = new LinkItem
{
name = string.Format("I am the link item number {0}.", number),
next = (number == total) ? null : item
};
}
return item;
}
static public void printLinkList(LinkItem item)
{
while (item != null)
{
Console.WriteLine(item.name);
item = item.next;
}
}
static public LinkItem reverseLinkList(LinkItem item)
{
LinkItem newItem = new LinkItem
{
name = item.name,
next = null
};
while (item.next != null)
{
newItem = new LinkItem
{
name = item.next.name,
next = newItem
};
item = item.next;
}
return newItem;
}
}
class LinkItem
{
public string name;
public LinkItem next;
}
linked list reversal recursive
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReverseLinkedList
{
class Program
{
static void Main(string[] args)
{
Node head = null;
LinkedList.Append(ref head, 25);
LinkedList.Append(ref head, 5);
LinkedList.Append(ref head, 18);
LinkedList.Append(ref head, 7);
Console.WriteLine("Linked list:");
LinkedList.Print(head);
Console.WriteLine();
Console.WriteLine("Reversed Linked list:");
LinkedList.Reverse(ref head);
LinkedList.Print(head);
Console.WriteLine();
Console.WriteLine("Reverse of Reversed Linked list:");
LinkedList.ReverseUsingRecursion(head);
head = LinkedList.newHead;
LinkedList.PrintRecursive(head);
}
public static class LinkedList
{
public static void Append(ref Node head, int data)
{
if (head != null)
{
Node current = head;
while (current.Next != null)
{
current = current.Next;
}
current.Next = new Node();
current.Next.Data = data;
}
else
{
head = new Node();
head.Data = data;
}
}
public static void Print(Node head)
{
if (head == null) return;
Node current = head;
do
{
Console.Write("{0} ", current.Data);
current = current.Next;
} while (current != null);
}
public static void PrintRecursive(Node head)
{
if (head == null)
{
Console.WriteLine();
return;
}
Console.Write("{0} ", head.Data);
PrintRecursive(head.Next);
}
public static void Reverse(ref Node head)
{
if (head == null) return;
Node prev = null, current = head, next = null;
while (current.Next != null)
{
next = current.Next;
current.Next = prev;
prev = current;
current = next;
}
current.Next = prev;
head = current;
}
public static Node newHead;
public static void ReverseUsingRecursion(Node head)
{
if (head == null) return;
if (head.Next == null)
{
newHead = head;
return;
}
ReverseUsingRecursion(head.Next);
head.Next.Next = head;
head.Next = null;
}
}
public class Node
{
public int Data = 0;
public Node Next = null;
}
}
}
Complexity O(n+m). Assuming head is the start node:
List<Node>Nodes = new List<Node>();
Node traverse= root;
while(traverse!=null)
{
Nodes.Add(traverse);
traverse = traverse.Next;
}
int i = Nodes.Count - 1;
root = Nodes[i];
for(; i>0; i--)
{
Nodes[i].Next = Nodes[i-1];
}
Nodes[0].Next=null;
In case you want a ready-made efficient implementation, I created an alternative to LinkedList that supports enumeration and reverse operations. https://github.com/NetFabric/NetFabric.DoubleLinkedList
public class Node<T>
{
public T Value { get; set; }
public Node<T> Next { get; set; }
}
public static Node<T> Reverse<T>(Node<T> head)
{
Node<T> tail = null;
while(head!=null)
{
var node = new Node<T> { Value = head.Value, Next = tail };
tail = node;
head = head.Next;
}
return tail;
}
The definition of ref is unnecessary because if you make the node as a reference type, it is OK to do:
public static void Reverse(Node root)
Also, the beauty of the interview question is less consumption of memory and in place reversal. Maybe a recursive way of doing it is also asked.

Creating a very simple linked list

I am trying to create a linked list just to see if I can, and I am having trouble getting my head around it. Does anyone have an example of a very simple implementation of Linked list using C#? All the examples I have found so far are quite overdone.
A Linked List, at its core is a bunch of Nodes linked together.
So, you need to start with a simple Node class:
public class Node {
public Node next;
public Object data;
}
Then your linked list will have as a member one node representing the head (start) of the list:
public class LinkedList {
private Node head;
}
Then you need to add functionality to the list by adding methods. They usually involve some sort of traversal along all of the nodes.
public void printAllNodes() {
Node current = head;
while (current != null)
{
Console.WriteLine(current.data);
current = current.next;
}
}
Also, inserting new data is another common operation:
public void Add(Object data) {
Node toAdd = new Node();
toAdd.data = data;
Node current = head;
// traverse all nodes (see the print all nodes method for an example)
current.next = toAdd;
}
This should provide a good starting point.
Based on what #jjnguy said, here's the full Console App example:
public class Node
{
public Node next;
public Object data;
}
public class LinkedList
{
private Node head;
public void printAllNodes()
{
Node current = head;
while (current != null)
{
Console.WriteLine(current.data);
current = current.next;
}
}
public void AddFirst(Object data)
{
Node toAdd = new Node();
toAdd.data = data;
toAdd.next = head;
head = toAdd;
}
public void AddLast(Object data)
{
if (head == null)
{
head = new Node();
head.data = data;
head.next = null;
}
else
{
Node toAdd = new Node();
toAdd.data = data;
Node current = head;
while (current.next != null)
{
current = current.next;
}
current.next = toAdd;
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Add First:");
LinkedList myList1 = new LinkedList();
myList1.AddFirst("Hello");
myList1.AddFirst("Magical");
myList1.AddFirst("World");
myList1.printAllNodes();
Console.WriteLine();
Console.WriteLine("Add Last:");
LinkedList myList2 = new LinkedList();
myList2.AddLast("Hello");
myList2.AddLast("Magical");
myList2.AddLast("World");
myList2.printAllNodes();
Console.ReadLine();
}
}
This one is nice:
namespace ConsoleApplication1
{
// T is the type of data stored in a particular instance of GenericList.
public class GenericList<T>
{
private class Node
{
// Each node has a reference to the next node in the list.
public Node Next;
// Each node holds a value of type T.
public T Data;
}
// The list is initially empty.
private Node head = null;
// Add a node at the beginning of the list with t as its data value.
public void AddNode(T t)
{
Node newNode = new Node();
newNode.Next = head;
newNode.Data = t;
head = newNode;
}
// The following method returns the data value stored in the last node in
// the list. If the list is empty, the default value for type T is
// returned.
public T GetFirstAdded()
{
// The value of temp is returned as the value of the method.
// The following declaration initializes temp to the appropriate
// default value for type T. The default value is returned if the
// list is empty.
T temp = default(T);
Node current = head;
while (current != null)
{
temp = current.Data;
current = current.Next;
}
return temp;
}
}
}
Test code:
static void Main(string[] args)
{
// Test with a non-empty list of integers.
GenericList<int> gll = new GenericList<int>();
gll.AddNode(5);
gll.AddNode(4);
gll.AddNode(3);
int intVal = gll.GetFirstAdded();
// The following line displays 5.
System.Console.WriteLine(intVal);
}
I encountered it on msdn here
Here is one with IEnumerable and a Recursive Reverse method though it is no faster than the while loop in the Reverse method both are O(n):
public class LinkedList<T> : IEnumerable
{
private Node<T> _head = null;
public Node<T> Add(T value)
{
var node = new Node<T> {Value = value};
if (_head == null)
{
_head = node;
}
else
{
var current = _head;
while (current.Next != null)
{
current = current.Next;
}
current.Next = node; //new head
}
return node;
}
public T Remove(Node<T> node)
{
if (_head == null)
return node.Value;
if (_head == node)
{
_head = _head.Next;
node.Next = null;
return node.Value;
}
var current = _head;
while (current.Next != null)
{
if (current.Next == node)
{
current.Next = node.Next;
return node.Value;
}
current = current.Next;
}
return node.Value;
}
public void Reverse()
{
Node<T> prev = null;
var current = _head;
if (current == null)
return;
while (current != null)
{
var next = current.Next;
current.Next = prev;
prev = current;
current = next;
}
_head = prev;
}
public void ReverseRecursive()
{
reverseRecursive(_head, null);
}
private void reverseRecursive(Node<T> current, Node<T> prev)
{
if (current.Next == null)
{
_head = current;
_head.Next = prev;
return;
}
var next = current.Next;
current.Next = prev;
reverseRecursive(next, current);
}
public IEnumerator<T> Enumerator()
{
var current = _head;
while (current != null)
{
yield return current.Value;
current = current.Next;
}
}
public IEnumerator GetEnumerator()
{
return Enumerator();
}
}
public class Node<T>
{
public T Value { get; set; }
public Node<T> Next { get; set; }
}
I am a beginner and this helped me:
class List
{
private Element Root;
}
First you create the class List which will contain all the methods.
Then you create the Node-Class, I will call it Element
class Element
{
public int Value;
public Element Next;
}
Then you can start adding methods to your List class. Here is a 'add' method for example.
public void Add(int value)
{
Element newElement = new Element();
newElement.Value = value;
Element rootCopy = Root;
Root = newElement;
newElement.Next = rootCopy;
Console.WriteLine(newElement.Value);
}
public class Node
{
private Object data;
public Node next {get;set;}
public Node(Object data)
{
this.data = data;
}
}
public class Linkedlist
{
Node head;
public void Add(Node n)
{
n.Next = this.Head;
this.Head = n;
}
}
using:
LinkedList sample = new LinkedList();
sample.add(new Node("first"));
sample.Add(new Node("second"))
I am giving an extract from the book "C# 6.0 in a Nutshell by Joseph Albahari and Ben Albahari"
Here’s a demonstration on the use of LinkedList:
var tune = new LinkedList<string>();
tune.AddFirst ("do"); // do
tune.AddLast ("so"); // do - so
tune.AddAfter (tune.First, "re"); // do - re- so
tune.AddAfter (tune.First.Next, "mi"); // do - re - mi- so
tune.AddBefore (tune.Last, "fa"); // do - re - mi - fa- so
tune.RemoveFirst(); // re - mi - fa - so
tune.RemoveLast(); // re - mi - fa
LinkedListNode<string> miNode = tune.Find ("mi");
tune.Remove (miNode); // re - fa
tune.AddFirst (miNode); // mi- re - fa
foreach (string s in tune) Console.WriteLine (s);
Here is a good implementation.
It is short, but implemented Add(x), Delete(x), Contain(x) and Print().
It avoid special process when add to empty list or delete the first element.
While most of other examples did special process when delete the first element.
The list can contain any data type.
using System;
class Node<Type> : LinkedList<Type>
{ // Why inherit from LinkedList? A: We need to use polymorphism.
public Type value;
public Node(Type value) { this.value = value; }
}
class LinkedList<Type>
{
Node<Type> next; // This member is treated as head in class LinkedList, but treated as next element in class Node.
/// <summary> if x is in list, return previos pointer of x. (We can see any class variable as a pointer.)
/// if not found, return the tail of the list. </summary>
protected LinkedList<Type> Previos(Type x)
{
LinkedList<Type> p = this; // point to head
for (; p.next != null; p = p.next)
if (p.next.value.Equals(x))
return p; // find x, return the previos pointer.
return p; // not found, p is the tail.
}
/// <summary> return value: true = success ; false = x not exist </summary>
public bool Contain(Type x) { return Previos(x).next != null ? true : false; }
/// <summary> return value: true = success ; false = fail to add. Because x already exist.
/// </summary> // why return value? If caller want to know the result, they don't need to call Contain(x) before, the action waste time.
public bool Add(Type x)
{
LinkedList<Type> p = Previos(x);
if (p.next != null) // Find x already in list
return false;
p.next = new Node<Type>(x);
return true;
}
/// <summary> return value: true = success ; false = x not exist </summary>
public bool Delete(Type x)
{
LinkedList<Type> p = Previos(x);
if (p.next == null)
return false;
//Node<Type> node = p.next;
p.next = p.next.next;
//node.Dispose(); // GC dispose automatically.
return true;
}
public void Print()
{
Console.Write("List: ");
for (Node<Type> node = next; node != null; node = node.next)
Console.Write(node.value.ToString() + " ");
Console.WriteLine();
}
}
class Test
{
static void Main()
{
LinkedList<int> LL = new LinkedList<int>();
if (!LL.Contain(0)) // Empty list
Console.WriteLine("0 is not exist.");
LL.Print();
LL.Add(0); // Add to empty list
LL.Add(1); LL.Add(2); // attach to tail
LL.Add(2); // duplicate add, 2 is tail.
if (LL.Contain(0))// Find existed element which is head
Console.WriteLine("0 is exist.");
LL.Print();
LL.Delete(0); // Delete head
LL.Delete(2); // Delete tail
if (!LL.Delete(0)) // Delete non-exist element
Console.WriteLine("0 is not exist.");
LL.Print();
Console.ReadLine();
}
}
By the way, the implementation in
http://www.functionx.com/csharp1/examples/linkedlist.htm
have some problem:
Delete() will fail when there is only 1 element.
(Throw exception at line "Head.Next = Current.Next;" because Current is null.)
Delete(position) will fail when deleting first element,
In other words, call Delete(0) will fail.
Dmytro did a good job, but here is a more concise version.
class Program
{
static void Main(string[] args)
{
LinkedList linkedList = new LinkedList(1);
linkedList.Add(2);
linkedList.Add(3);
linkedList.Add(4);
linkedList.AddFirst(0);
linkedList.Print();
}
}
public class Node
{
public Node(Node next, Object value)
{
this.next = next;
this.value = value;
}
public Node next;
public Object value;
}
public class LinkedList
{
public Node head;
public LinkedList(Object initial)
{
head = new Node(null, initial);
}
public void AddFirst(Object value)
{
head = new Node(head, value);
}
public void Add(Object value)
{
Node current = head;
while (current.next != null)
{
current = current.next;
}
current.next = new Node(null, value);
}
public void Print()
{
Node current = head;
while (current != null)
{
Console.WriteLine(current.value);
current = current.next;
}
}
}
public class DynamicLinkedList
{
private class Node
{
private object element;
private Node next;
public object Element
{
get { return this.element; }
set { this.element = value; }
}
public Node Next
{
get { return this.next; }
set { this.next = value; }
}
public Node(object element, Node prevNode)
{
this.element = element;
prevNode.next = this;
}
public Node(object element)
{
this.element = element;
next = null;
}
}
private Node head;
private Node tail;
private int count;
public DynamicLinkedList()
{
this.head = null;
this.tail = null;
this.count = 0;
}
public void AddAtLastPosition(object element)
{
if (head == null)
{
head = new Node(element);
tail = head;
}
else
{
Node newNode = new Node(element, tail);
tail = newNode;
}
count++;
}
public object GetLastElement()
{
object lastElement = null;
Node currentNode = head;
while (currentNode != null)
{
lastElement = currentNode.Element;
currentNode = currentNode.Next;
}
return lastElement;
}
}
Testing with:
static void Main(string[] args)
{
DynamicLinkedList list = new DynamicLinkedList();
list.AddAtLastPosition(1);
list.AddAtLastPosition(2);
list.AddAtLastPosition(3);
list.AddAtLastPosition(4);
list.AddAtLastPosition(5);
object lastElement = list.GetLastElement();
Console.WriteLine(lastElement);
}
public class Node<T>
{
public T item;
public Node<T> next;
public Node()
{
this.next = null;
}
}
class LinkList<T>
{
public Node<T> head { get; set; }
public LinkList()
{
this.head = null;
}
public void AddAtHead(T item)
{
Node<T> newNode = new Node<T>();
newNode.item = item;
if (this.head == null)
{
this.head = newNode;
}
else
{
newNode.next = head;
this.head = newNode;
}
}
public void AddAtTail(T item)
{
Node<T> newNode = new Node<T>();
newNode.item = item;
if (this.head == null)
{
this.head = newNode;
}
else
{
Node<T> temp = this.head;
while (temp.next != null)
{
temp = temp.next;
}
temp.next = newNode;
}
}
public void DeleteNode(T item)
{
if (this.head.item.Equals(item))
{
head = head.next;
}
else
{
Node<T> temp = head;
Node<T> tempPre = head;
bool matched = false;
while (!(matched = temp.item.Equals(item)) && temp.next != null)
{
tempPre = temp;
temp = temp.next;
}
if (matched)
{
tempPre.next = temp.next;
}
else
{
Console.WriteLine("Value not found!");
}
}
}
public bool searchNode(T item)
{
Node<T> temp = this.head;
bool matched = false;
while (!(matched = temp.item.Equals(item)) && temp.next != null)
{
temp = temp.next;
}
return matched;
}
public void DisplayList()
{
Console.WriteLine("Displaying List!");
Node<T> temp = this.head;
while (temp != null)
{
Console.WriteLine(temp.item);
temp = temp.next;
}
}
}
Add a Node class.
Then add a LinkedList class to implement the linked list
Add a test class to execute the linked list
namespace LinkedListProject
{
public class Node
{
public Node next;
public object data;
}
public class MyLinkedList
{
Node head;
public Node AddNodes(Object data)
{
Node node = new Node();
if (node.next == null)
{
node.data = data;
node.next = head;
head = node;
}
else
{
while (node.next != null)
node = node.next;
node.data = data;
node.next = null;
}
return node;
}
public void printnodes()
{
Node current = head;
while (current.next != null)
{
Console.WriteLine(current.data);
current = current.next;
}
Console.WriteLine(current.data);
}
}
[TestClass]
public class LinkedListExample
{
MyLinkedList linkedlist = new MyLinkedList();
[TestMethod]
public void linkedlisttest()
{
linkedlist.AddNodes("hello");
linkedlist.AddNodes("world");
linkedlist.AddNodes("now");
linkedlist.printnodes();
}
}
}
simple c# program to implement Single Link List with operations AddItemStart, AddItemEnd, RemoveItemStart, RemoveItemEnd and DisplayAllItems
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SingleLinkedList
{
class Program
{
Node head;
Node current;
int counter = 0;
public Program()
{
head = new Node();
current = head;
}
public void AddStart(object data)
{
Node newnode = new Node();
newnode.next = head.next;
newnode.data = data;
head.next = newnode;
counter++;
}
public void AddEnd(object data)
{
Node newnode = new Node();
newnode.data = data;
current.next = newnode;
current = newnode;
counter++;
}
public void RemoveStart()
{
if (counter > 0)
{
head.next = head.next.next;
counter--;
}
else
{
Console.WriteLine("No element exist in this linked list.");
}
}
public void RemoveEnd()
{
if (counter > 0)
{
Node prevNode = new Node();
Node cur = head;
while (cur.next != null)
{
prevNode = cur;
cur = cur.next;
}
prevNode.next = null;
}
else
{
Console.WriteLine("No element exist in this linked list.");
}
}
public void Display()
{
Console.Write("Head ->");
Node curr = head;
while (curr.next != null)
{
curr = curr.next;
Console.WriteLine(curr.data.ToString());
}
}
public class Node
{
public object data;
public Node next;
}
static void Main(string[] args)
{
Program p = new Program();
p.AddEnd(2);
p.AddStart(1);
p.AddStart(0);
p.AddEnd(3);
p.Display();
p.RemoveStart();
Console.WriteLine("Removed node from Start");
p.Display();
Console.WriteLine("Removed node from End");
p.RemoveEnd();
p.Display();
Console.ReadKey();
}
}
}
The selected answer doesn't have an iterator; it is more basic, but perhaps not as useful.
Here is one with an iterator/enumerator. My implementation is based on Sedgewick's bag; see http://algs4.cs.princeton.edu/13stacks/Bag.java.html
void Main()
{
var b = new Bag<string>();
b.Add("bike");
b.Add("erasmus");
b.Add("kumquat");
b.Add("beaver");
b.Add("racecar");
b.Add("barnacle");
foreach (var thing in b)
{
Console.WriteLine(thing);
}
}
// Define other methods and classes here
public class Bag<T> : IEnumerable<T>
{
public Node<T> first;// first node in list
public class Node<T>
{
public T item;
public Node<T> next;
public Node(T item)
{
this.item = item;
}
}
public void Add(T item)
{
Node<T> oldFirst = first;
first = new Node<T>(item);
first.next = oldFirst;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
return new BagEnumerator<T>(this);
}
public class BagEnumerator<V> : IEnumerator<T>
{
private Node<T> _head;
private Bag<T> _bag;
private Node<T> _curNode;
public BagEnumerator(Bag<T> bag)
{
_bag = bag;
_head = bag.first;
_curNode = default(Node<T>);
}
public T Current
{
get { return _curNode.item; }
}
object IEnumerator.Current
{
get { return Current; }
}
public bool MoveNext()
{
if (_curNode == null)
{
_curNode = _head;
if (_curNode == null)
return false;
return true;
}
if (_curNode.next == null)
return false;
else
{
_curNode = _curNode.next;
return true;
}
}
public void Reset()
{
_curNode = default(Node<T>); ;
}
public void Dispose()
{
}
}
}
I've created the following LinkedList code with many features. It is available for public under the CodeBase github public repo.
Classes:
Node and LinkedList
Getters and Setters: First and Last
Functions:
AddFirst(data), AddFirst(node), AddLast(data), RemoveLast(), AddAfter(node, data), RemoveBefore(node), Find(node), Remove(foundNode), Print(LinkedList)
using System;
using System.Collections.Generic;
namespace Codebase
{
public class Node
{
public object Data { get; set; }
public Node Next { get; set; }
public Node()
{
}
public Node(object Data, Node Next = null)
{
this.Data = Data;
this.Next = Next;
}
}
public class LinkedList
{
private Node Head;
public Node First
{
get => Head;
set
{
First.Data = value.Data;
First.Next = value.Next;
}
}
public Node Last
{
get
{
Node p = Head;
//Based partially on https://en.wikipedia.org/wiki/Linked_list
while (p.Next != null)
p = p.Next; //traverse the list until p is the last node.The last node always points to NULL.
return p;
}
set
{
Last.Data = value.Data;
Last.Next = value.Next;
}
}
public void AddFirst(Object data, bool verbose = true)
{
Head = new Node(data, Head);
if (verbose) Print();
}
public void AddFirst(Node node, bool verbose = true)
{
node.Next = Head;
Head = node;
if (verbose) Print();
}
public void AddLast(Object data, bool Verbose = true)
{
Last.Next = new Node(data);
if (Verbose) Print();
}
public Node RemoveFirst(bool verbose = true)
{
Node temp = First;
Head = First.Next;
if (verbose) Print();
return temp;
}
public Node RemoveLast(bool verbose = true)
{
Node p = Head;
Node temp = Last;
while (p.Next != temp)
p = p.Next;
p.Next = null;
if (verbose) Print();
return temp;
}
public void AddAfter(Node node, object data, bool verbose = true)
{
Node temp = new Node(data);
temp.Next = node.Next;
node.Next = temp;
if (verbose) Print();
}
public void AddBefore(Node node, object data, bool verbose = true)
{
Node temp = new Node(data);
Node p = Head;
while (p.Next != node) //Finding the node before
{
p = p.Next;
}
temp.Next = p.Next; //same as = node
p.Next = temp;
if (verbose) Print();
}
public Node Find(object data)
{
Node p = Head;
while (p != null)
{
if (p.Data == data)
return p;
p = p.Next;
}
return null;
}
public void Remove(Node node, bool verbose = true)
{
Node p = Head;
while (p.Next != node)
{
p = p.Next;
}
p.Next = node.Next;
if (verbose) Print();
}
public void Print()
{
Node p = Head;
while (p != null) //LinkedList iterator
{
Console.Write(p.Data + " ");
p = p.Next; //traverse the list until p is the last node.The last node always points to NULL.
}
Console.WriteLine();
}
}
}
Using #yogihosting answer when she used the Microsoft built-in LinkedList and LinkedListNode to answer the question, you can achieve the same results:
using System;
using System.Collections.Generic;
using Codebase;
namespace Cmd
{
static class Program
{
static void Main(string[] args)
{
var tune = new LinkedList(); //Using custom code instead of the built-in LinkedList<T>
tune.AddFirst("do"); // do
tune.AddLast("so"); // do - so
tune.AddAfter(tune.First, "re"); // do - re- so
tune.AddAfter(tune.First.Next, "mi"); // do - re - mi- so
tune.AddBefore(tune.Last, "fa"); // do - re - mi - fa- so
tune.RemoveFirst(); // re - mi - fa - so
tune.RemoveLast(); // re - mi - fa
Node miNode = tune.Find("mi"); //Using custom code instead of the built in LinkedListNode
tune.Remove(miNode); // re - fa
tune.AddFirst(miNode); // mi- re - fa
}
}
I have a doubly Linked List which can be used as a stack or a queue. If you look at the code and think about what it does and how it does it I bet you will understand everything about it. I am sorry but somehow I couldn't pate the full code here so I here is the link for the linkedlist(also I got the binary tree in the solution): https://github.com/szabeast/LinkedList_and_BinaryTree
A linked list is a node-based data structure. Each node designed with two portions (Data & Node Reference).Actually, data is always stored in Data portion (Maybe primitive data types eg Int, Float .etc or we can store user-defined data type also eg. Object reference) and similarly Node Reference should also contain the reference to next node, if there is no next node then the chain will end.
This chain will continue up to any node doesn't have a reference point to the next node.
Please find the source code from my tech blog - http://www.algonuts.info/linked-list-program-in-java.html
package info.algonuts;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
class LLNode {
int nodeValue;
LLNode childNode;
public LLNode(int nodeValue) {
this.nodeValue = nodeValue;
this.childNode = null;
}
}
class LLCompute {
private static LLNode temp;
private static LLNode previousNode;
private static LLNode newNode;
private static LLNode headNode;
public static void add(int nodeValue) {
newNode = new LLNode(nodeValue);
temp = headNode;
previousNode = temp;
if(temp != null)
{ compute(); }
else
{ headNode = newNode; } //Set headNode
}
private static void compute() {
if(newNode.nodeValue < temp.nodeValue) { //Sorting - Ascending Order
newNode.childNode = temp;
if(temp == headNode)
{ headNode = newNode; }
else if(previousNode != null)
{ previousNode.childNode = newNode; }
}
else
{
if(temp.childNode == null)
{ temp.childNode = newNode; }
else
{
previousNode = temp;
temp = temp.childNode;
compute();
}
}
}
public static void display() {
temp = headNode;
while(temp != null) {
System.out.print(temp.nodeValue+" ");
temp = temp.childNode;
}
}
}
public class LinkedList {
//Entry Point
public static void main(String[] args) {
//First Set Input Values
List <Integer> firstIntList = new ArrayList <Integer>(Arrays.asList(50,20,59,78,90,3,20,40,98));
Iterator<Integer> ptr = firstIntList.iterator();
while(ptr.hasNext())
{ LLCompute.add(ptr.next()); }
System.out.println("Sort with first Set Values");
LLCompute.display();
System.out.println("\n");
//Second Set Input Values
List <Integer> secondIntList = new ArrayList <Integer>(Arrays.asList(1,5,8,100,91));
ptr = secondIntList.iterator();
while(ptr.hasNext())
{ LLCompute.add(ptr.next()); }
System.out.println("Sort with first & Second Set Values");
LLCompute.display();
System.out.println();
}
}

Categories

Resources