using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinkedList
{
public interface IListDS<T>
{
int GetLength();
void Clear();
bool IsEmpty();
void Append(T item);
void Insert(T item, int i);
T Delete(int i);
//T GetElem(int i);
int Locate(T value);
}
public class Node<T>
{
private T _data;
private Node<T> _next;
public Node(T val, Node<T> P)
{
_data = val;
_next = P;
}
public Node(T val)
{
_data = val;
}
public Node(Node<T> P)
{
_next = P;
}
public Node()
{
_data = default(T);
}
public T Data
{
get
{
return _data;
}
set
{
_data = value;
}
}
public Node<T> Next
{
get
{
return _next;
}
set
{
_next = value;
}
}
}
public class LinkedList<T> : IListDS<T>
{
private Node<T> _head;
public Node<T> Head
{
get
{
return _head;
}
set
{
_head = value;
}
}
public LinkedList()
{
_head = null;
}
public int GetLength()
{
Node<T> p = _head;
int len = 0;
while (p != null)
{
len++;
p = p.Next;
}
return len;
}
public void Clear()
{
_head = null;
}
public bool IsEmpty()
{
if (_head == null)
{
return true;
}
else
{
return false;
}
}
public void Append(T iteam)
{
Node<T> p = _head;
Node<T> q = new Node<T>(iteam);
if (_head == null)
{
_head = q;
return;
}
while (p.Next != null)
{
p = p.Next;
}
p.Next = q;
}
public void Insert(T item, int i)
{
if (IsEmpty() || i < 1)
{
throw new ArgumentException("List is empty or position is error.");
}
if (i == 1)
{
Node<T> p = new Node<T>(item);
p.Next = _head;
_head = p;
return;
}
Node<T> q = _head;
Node<T> r = new Node<T>();
int j = 0;
while (q.Next != null && j < i)
{
r = q;
q = q.Next;
j++;
}
if (j == i)
{
Node<T> t = new Node<T>(item);
r.Next = t;
t.Next = q;
}
else
{
throw new ArgumentException("Position is error.");
}
}
public void InsertPost(T item, int i)
{
if (IsEmpty() || i < 1)
{
throw new ArgumentException("List is empty or position is error");
}
Node<T> q = _head;
int j = 0;
while (q.Next != null && j < i)
{
q = q.Next;
j++;
}
if (j == i)
{
Node<T> t = new Node<T>(item);
t.Next = q.Next;
q.Next = t;
}
else
{
throw new ArgumentException("Position is error.");
}
}
public T Delete(int i)
{
if (IsEmpty() || i < 1)
{
throw new ArgumentException("List is null or position is error.");
}
Node<T> p = _head;
Node<T> q = new Node<T>();
int j = 0;
while (p.Next != null && j<i)
{
q = p;
p = p.Next;
j++;
}
if (j == i)
{
q.Next = p.Next;
return p.Data;
}
else
{
throw new ArgumentException("The " + i + "th node is not exsit.");
}
}
public T GetItem(int i)
{
if (IsEmpty() || i < 1)
{
throw new ArgumentException("List is empty or postion is error.");
}
Node<T> p = _head;
int j = 0;
while (p.Next != null && i < j)
{
p = p.Next;
j++;
}
if (j == i)
{
return p.Data;
}
else
{
throw new ArgumentException("The "+ i + "th node is not exsit.");
}
}
public int Locate(T item)
{
if (IsEmpty())
{
throw new ArgumentException("List is empty.");
}
Node<T> p = _head;
int i = 0;
while (p!=null)
{
if (p.Data.Equals(item))
{
return i;
}
p = p.Next;
i++;
}
throw new ArgumentException("Can't find item in LinkedList");
}
public LinkedList<int> CreateLinkedListHead()
{
LinkedList<int> L = new LinkedList<int>();
int d = Int32.Parse(Console.ReadLine());
while (d != -1)
{
Node<int> p = new Node<int>(d);
p.Next = L.Head;
L.Head = p;
d = Int32.Parse(Console.ReadLine());
}
return L;
}
public LinkedList<int> CreateLinkedListTail()
{
LinkedList<int> L = new LinkedList<int>();
Node<int> R = new Node<int>();
int d = Int32.Parse(Console.ReadLine());
R = L.Head;
while (d != -1)
{
Node<int> p = new Node<int>(d);
if (L.Head == null)
{
L.Head = p;
}
else
{
R.Next = p;
}
R = p;
d = Int32.Parse(Console.ReadLine());
}
if (R != null)
{
R.Next = null;
}
return L;
}
public void DisplayLinkedList(LinkedList<T> L)
{
Node<T> p = L.Head;
while (p != null)
{
Console.Write(p.Data + " ");
p = p.Next;
}
Console.WriteLine();
}
public void ReverseLinkedList(LinkedList<T> L)
{
Node<T> curr = L.Head;
Node<T> next = null;
Node<T> nextnext = null;
if (curr == null || curr.Next == null)
{
return;
}
while (curr.Next != null)
{
next = curr.Next;
nextnext = next.Next;
next.Next = L.Head;
L.Head = next;
curr.Next = nextnext;
}
}
public LinkedList<int> MergeLinkedList(LinkedList<int> Ha, LinkedList<int> Hb)
{
LinkedList<int> Hc = new LinkedList<int>();
Node<int> p = Ha.Head;
Node<int> q = Hb.Head;
Node<int> c = new Node<int>();
while (p!=null && q!=null)
{
if (p.Data > q.Data)
{
c = q;
q = q.Next;
}
else
{
c = p;
p = p.Next;
}
Hc.Append(c.Data);
}
if (q!=null)
{
p = q;
}
while (p !=null)
{
Hc.Append(p.Data);
p = p.Next;
}
return Hc;
}
}
class Program
{
static void Main(string[] args)
{
LinkedList<int> L = new LinkedList<int>();
L = L.CreateLinkedListTail();
LinkedList<int> M = new LinkedList<int>();
M = M.CreateLinkedListTail();
LinkedList<int> N = new LinkedList<int>().MergeLinkedList(L, M);
//L.DisplayLinkedList(L);
//L.ReverseLinkedList(L);
//L.DisplayLinkedList(L);
N.DisplayLinkedList(N);
}
}
}