C# OOP: Singly Linked List

C# code of singly linked list

Step 7: Put the C# code together

This is the complete C# code of a linked list data structure:


  	
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
showmenu();
select();
            Console.WriteLine("...........................");
Console.ReadLine();
}

//List element class
class ListNode //T is the generic type.          {
public ListNode(T elem) {
val = elem;next = null;
}
public T val; //element data public ListNode next;//next link }

//linked list class class LinkedList {
public LinkedList() {
pfirst = plast = null; }
protected ListNode pfirst; //pfirst points to the first element of the linked list protected ListNode plast;//plast points to the last element of the linked list //Add a new item to the linked list public void insert(T val, int pos) {

ListNode newnode = new ListNode(val);
//empty list
if (pfirst == null && plast == null)
{

newnode.next = null;
pfirst = newnode;
plast = newnode;
Console.WriteLine("Inserted:{
0}
", newnode.val);
}

//Insert at the beginning of the list
else if (pos == 1)
{
newnode.next = pfirst;
pfirst = newnode;
Console.WriteLine("Inserted:{
0}
", newnode.val);
}

//Insert in the middle of the list
else if (pos > 1 && pos <= countitem())
{

ListNode ta;
ta = pfirst;
for (int t = 1; t < pos - 1; t = t + 1) {
ta = ta.next;
 }

newnode.next = ta.next;
ta.next = 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;
plast = newnode;
Console.WriteLine("Inserted:{
0}
", newnode.val);
}

else Console.WriteLine("Invalid position!");
}
//print all elements
public void showall()              {

ListNode 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!");
}
//count items in the linked list
public int countitem()              {
ListNode i;
int t = 0;
for (i = pfirst; i != null; i = i.next)
{

t = t + 1;
}
return t;
}
//delete an item from the linked list
public void delete(int pos)              {

if (countitem() > 0)
{
//make sure the list is not empty.
ListNode temp,del;
if (pos == 1)
{
//delete the first item
if(countitem()==1){
//The list contains only one item
pfirst=null;
plast=null;
}

else{
//The list contains more than one item
temp=pfirst;
pfirst=pfirst.next;
temp=null;
}
Console.WriteLine("Deleted");
}
else if (pos > 1 && pos <=countitem())
{
//delete middle item
temp=pfirst;
int i;
for(i=1;itemp=temp.next;}
//move to the item staying before the target item to be deleted
del=temp.next; //target item to be deleted
temp.next=del.next;
if(del.next==null)plast=temp; //delete last item
del=null;
Console.WriteLine ("Deleted");
}
else Console.WriteLine("Invalid position!");
}

else Console.WriteLine("No item found");
}
}
public static void showmenu(){
Console.WriteLine("=================================");
Console.WriteLine("Linked List Operations Menu");
Console.WriteLine("=================================");
Console.WriteLine("1.Add a new item");
Console.WriteLine("2.Delete an item");
Console.WriteLine("3.Show number of items");
Console.WriteLine("4.Show all items");
Console.WriteLine("5.Exit");
}
public static void select(){
LinkedList mylist=new LinkedList();
int val,ch, pos;
char yes = 'y';
while (yes == 'y') {
Console.Write("Enter your choice:");
ch =int.Parse(Console.ReadLine().ToString ());
switch (ch) {
case 1: Console.Write("Value:");
val = int.Parse(Console.ReadLine());
Console.Write("Position:"); pos = int.Parse(Console.ReadLine());
mylist.insert(val, pos);
break;
case 2: Console.Write("Position:"); pos = int.Parse(Console.ReadLine());
mylist.delete(pos);
break;
case 3: Console.WriteLine("Number of items:" + mylist.countitem());
break;
case 4: Console.WriteLine("All items:");
mylist.showall();
break;
case 5: Environment.Exit(0); break;
default: Console.WriteLine("Invalid choice!"); break;
}
Console.Write("Continue? Press y to continue:");
yes =char.Parse (Console.ReadLine());
}
}
}
}
 




Comments




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.