C# example- linked list data structure


Linked list data structure

This is C# example code for a linkedlist data structure. If you are not sure about the linkedlist, i recommend to read: Algorithm and Data Structure tutorial.

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


namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
LinkedList<int> mylist = new LinkedList<int>();
mylist.insert(20, 1);
mylist.insert(30, 2);
mylist.insert(50, 3);
mylist.insert(10, 1);
mylist.delete(2);
mylist.showall();
Console.ReadLine();
}

class ListNode<T> //T is the generic type.
{
public ListNode(T elem) { val = elem; prev = next = null; }
public T val; //node data
public ListNode<T> prev;//previous link
public ListNode<T> next;//next link
}

class LinkedList<T>
{
public LinkedList() { pfirst = plast = null; }
protected ListNode<T> pfirst;
protected ListNode<T> plast;
public void insert(T val, int pos)
{
ListNode<T> newnode = new ListNode<T>(val);
//empty list
if (pfirst == null && plast == null)
{
pfirst = newnode;
plast = newnode;
Console.WriteLine("Inserted:{0}", newnode.val);
}
//Insert at the beginning of the list
else if (pos == 1)
{
ListNode<T> f;
f = pfirst;
pfirst = newnode;
pfirst.next = f;
f.prev = pfirst;
Console.WriteLine("Inserted:{0}", newnode.val);
}
//Insert in the middle of the list
else if (pos > 1 && pos < countitem())
{
ListNode<T> ta;
ta = pfirst;
for (int t = 1; t < pos; t = t + 1) { ta = ta.next; }
newnode.next = ta;
newnode.prev = ta.prev;
ta.prev.next = newnode;
ta.prev = newnode;
Console.WriteLine("Inserted:{0}", newnode.val);
}
else if (pos == countitem() + 1)
{

newnode.next = null; //The next link of the item is null.
plast.next = newnode;
newnode.prev = plast;
plast = newnode;
Console.WriteLine("Inserted:{0}", newnode.val);

}
else Console.WriteLine("Invalid position!");


}
public void showall()
{
ListNode<T> t;
if (countitem() > 0)
{
Console.WriteLine("All items in the list:");
for (t = pfirst; t != null; t = t.next)
{

Console.WriteLine(t.val);
}
}
else Console.WriteLine("No item found!");
}
public int countitem()
{
ListNode<T> i;
int t = 0;
for (i = pfirst; i != null; i = i.next)
{
t = t + 1;
}
return t;
}
public void delete(int pos)
{
if (countitem() > 0)
{ //make sure the list is not empty.
ListNode<T> temp;

if (pos == 1)
{//delete the first item
temp = pfirst;
pfirst = pfirst.next;
pfirst.prev = null;
temp = null;

}

else if (pos > 1 && pos < countitem())
{//delete middle item
temp = pfirst;
int i;
for (i = 1; i < pos; i = i + 1) { temp = temp.next; }
temp.prev.next = temp.next;
temp.next = temp.prev;
temp = null;

}
else if (pos == countitem())
{//delete the last item

  temp = plast;
  plast = plast.prev;
  plast.next = null;
  temp = null;

}

else Console.WriteLine("Invalid position!");

}

else Console.WriteLine("No item found");

       }
   }
 }

}




Comments

Ar. Daryan comment

 Ar. Daryan

You have an error in delete() method
.....
for (i = 1; i < pos; i = i + 1) { temp = temp.next; }
temp.prev.next = temp.next;
temp.next = temp.prev;
temp = null;
.....
Change it to
for (i = 1; i < pos; i = i + 1) { temp = temp.next; }
temp.prev.next = temp.next;
temp.next.prev = temp.prev;
temp = null;


2017-12-18
3534354456 comment

 3534354456

45645645654


2016-05-27



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.