class node_test
{
int data;
node_test next;
public node_test() { }
public node_test(int data) { this.data = data; }
public int Data { get { return data; } set { data = value; } }
public node_test Next { get { return next; } set { next = value; } }
}
class List_test
{
node_test head;
int count;
public List_test() { }
public List_test(node_test head) { this.head = head; }
public int Count
{ get
{
if (head != null)
{
node_test p = head;
while (p.Next != null)
{
++count;
p = p.Next;
}
++count;
}
return count;
}
}
public node_test Head { get { return head; }set { head = value; } }
public void Addpend(int data)
{
node_test q = new node_test(data);
if (head == null)
{
head = q;
return;
}
node_test p = head;
while (p.Next != null)
{
p = p.Next;
}
p.Next = q;
}
public void Insert(int i, int data)
{
if (i < 1 || head == null) return;
node_test q = new node_test(data);
if (i == 1)
{
q.Next = head;
head = q;
return;
}
node_test p = head;
int j = 1;
node_test r = new node_test();
while (p.Next != null && j < i)
{
++j;
r = p;
p = p.Next;
}
if (i == j)
{
r.Next = q;
q.Next = p;
}
else
return;
}
public void InsertPos(int i, int data)
{
if (i < 1 || head == null) return;
node_test q = new node_test(data);
if (i == 1)
{
q.Next = head.Next;
head.Next = q;
return;
}
node_test p = head;
int j = 1;
while (p.Next != null && j < i)
{
++j;
p = p.Next;
}
if (i == j)
{
q.Next = p.Next;
p.Next = q;
}
else
return;
}
public int Delete(int i)
{
int temp = -1;
if (i < 1 || head == null) return temp;
if (i == 1)
{
node_test r = new node_test();
r = head;
head = head.Next;
temp = r.Data;
}
node_test p = head;
node_test q = new node_test();
int j = 1;
while (p.Next != null && j < i)
{
++j;
q = p;
p = p.Next;
}
if (i == j)
{
temp = p.Data;
q.Next = p.Next;
}
return temp;
}
public int GetIndex(int data)
{
if (head == null) return -1;
node_test p = head;
int j = 1;
while (p.Next != null && !p.Data.Equals(data))
{
++j;
p = p.Next;
}
return j;
}
public int Getvalue(int i)
{
if (head == null) return -1;
node_test p = head;
if (i == 1)
{
return head.Data;
}
int j = 1;
while (p.Next != null)
{
++j;
p = p.Next;
}
if (i == j)
{
return p.Data;
}
return -1;
}
public void Print()
{
if (head == null) return;
node_test p = head;
while (p.Next != null)
{
Console.WriteLine(p.Data);
p = p.Next;
}
Console.WriteLine(p.Data);
}
public void RevertList()
{
if (head == null) return;
node_test p = head.Next;
node_test q = new node_test();
head.Next = null;
while (p != null)
{
q = p;
p = p.Next;
q.Next = head;
head = q;
}
}
public int MidNode()
{
if (head == null) return -1;
if (head.Next.Next != null)
{
node_test fastNode = head.Next.Next;
node_test slowNode = head;
while (fastNode != null && fastNode.Next != null)
{
fastNode = fastNode.Next.Next;
slowNode = slowNode.Next;
}
return slowNode.Data;
}
return -1;
}
public List_test MergeList(List_test lis, List_test _lis)
{
if (lis.head == null && _lis.head == null) return null;
node_test ha = lis.head;
node_test hb = _lis.head;
List_test hc = new List_test();
while (ha.Next != null)
{
ha = ha.Next;
}
ha.Next = hb;
hc.head = lis.head;
return hc;
}
链表常见操作
最新推荐文章于 2024-05-11 17:59:02 发布