using System;
class MainClass {
static void Main() {
Console.WriteLine("Hello World!");
LinkedList lst = new LinkedList(10);
lst.Append(20);
lst.PreAppend(40);
lst.insert(2,55);
lst.ReverseLinkedList();
}
}
public class Node
{
internal int data;
internal Node next;
public Node(int value)
{
this.data = value;
this.next = null;
}
}
public class LinkedList
{
internal Node head;
internal Node tail;
internal int length;
public LinkedList(int value)
{
Node newNode = new Node(value);
newNode.next = head;
this.head = newNode;
this.length = 1;
this.tail = this.head;
}
public void Append( int value)
{
Node newNode = new Node(value);
this.tail.next = newNode;
this.tail = newNode;
this.length++;
}
public void PreAppend( int value)
{
Node newNode = new Node(value);
newNode.next = this.head;
this.head = newNode;
this.length++;
}
public void insert(int index, int value)
{
int startindex = 0;
if (this.length == 0)
{
return;
}
else if(index == 0)
{
PreAppend(value);
return;
}
Node curnode = this.head;
while(curnode.next !=null)
{
if((index-1) == startindex)
{
Node newnode = new Node(value);
Node tempnode = curnode.next;
curnode.next = newnode;
newnode.next = tempnode;
this.length++;
return;
}
curnode = curnode.next;
startindex ++;
}
}
public void remove(int index)
{
Node curnode = this.head;
int startindex = 0;
Node prevnode = null;
while(curnode.next !=null)
{
if(index == startindex)
{
prevnode.next = curnode.next;
this.length--;
return;
}
prevnode = curnode;
curnode = curnode.next;
startindex ++;
}
}
public void ReverseLinkedList()
{
Node startNode = this.head;
LinkedList reverselist = null;
while(startNode.next != null)
{
if (reverselist == null)
{
reverselist = new LinkedList(startNode.data);
}
else
{
reverselist.PreAppend(startNode.data);
}
startNode = startNode.next;
}
reverselist.PreAppend(startNode.data);
Node curnode = reverselist.head;
while(curnode.next != null)
{
Console.WriteLine(curnode.data);
curnode = curnode.next;
}
Console.WriteLine(curnode.data);
}
}