标签:线性表逆序
顺序表
/// <summary>
/// 逆序输出
/// </summary>
public void Reverse()
{
if (length <= 1)
{
return;
}
for (int i = 0; i < length/ 2; i++)
{
T tmp=dataset[i];
dataset[i] = dataset[length - i - 1];
dataset[length - i - 1] = tmp;
}
}
2.单链表
/// <summary>
/// 逆序
/// </summary>
public void Reverse()
{
if (length <= 1)
{
return;
}
SNode<T> tmp = pHead;
SNode<T> t1 = tmp.Next;
SNode<T> t2 = t1.Next;
pHead.Next = null;
for (int i = 1; i < length-1; i++)
{
t1.Next = tmp;
tmp = t1;
t1 = t2;
t2 = t2.Next;
}
t1.Next = tmp;
pHead = t1;
}
3.循环链表
/// <summary>
/// 逆序
/// </summary>
public void Reverse()
{
if (length <= 1)
{
return;
}
SNode<T> tmp = pRear;
SNode<T> t1 = tmp.Next;
SNode<T> t2 = t1.Next;
for (int i = 0; i < length; i++)
{
t1.Next = tmp;
tmp = t1;
t1 = t2;
t2 = t2.Next;
}
pRear = t1;
}
4.双链表
/// <summary>
/// 逆序
/// </summary>
public void Reverse()
{
if (length <= 1)
{
return;
}
DNode<T> tmp = pHead;
DNode<T> t1 = tmp.Next;
DNode<T> t2 = t1.Next;
pHead.Next=null;
pRear = pHead;
for (int i = 1; i < length-1; i++)
{
t1.Next = tmp;
tmp.Prior = t1;
tmp = t1;
t1 = t2;
t2 = t2.Next;
}
t1.Next = tmp;
tmp.Prior = t1;
t1.Prior = null;
pHead = t1;
}
5.测试程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinearStruct;
namespace LineaTest
{
class Program
{
static void Main(string[] args)
{
/*顺序表测试
ILinearList<int> lst=new SeqList<int>(10);
for (int i = 0; i < 10; i++)
{
lst.Insert(i, i+10);
}
lst.Reverse();
for (int i = 0; i < 10; i++)
{
Console.Write(lst[i].ToString() + " ");
}
*/
/*单链表测试
ILinearList<int> lst = new SLinkList<int>();
for (int i = 0; i < 10; i++)
{
lst.Insert(i, i + 10);
}
lst.Reverse();
for (int i = 0; i < 10; i++)
{
Console.Write(lst[i].ToString() + " ");
}
*/
/*循环链表测试*/
/*
ILinearList<int> lst = new CLinkList<int>();
for (int i = 0; i < 10; i++)
{
lst.Insert(i, i + 10);
}
lst.Reverse();
for (int i = 0; i < 10; i++)
{
Console.Write(lst[i].ToString() + " ");
}*/
/*双链表测试*/
ILinearList<int> lst = new DLinkList<int>();
for (int i = 0; i < 10; i++)
{
lst.Insert(i, i + 10);
}
lst.Reverse();
for (int i = 0; i < 10; i++)
{
Console.Write(lst[i].ToString() + " ");
}
Console.ReadKey();
}
}
}
本文出自 “虎哥的博客” 博客,请务必保留此出处http://7613577.blog.51cto.com/7603577/1565914
标签:线性表逆序
原文地址:http://7613577.blog.51cto.com/7603577/1565914