C#實現(xiàn)順序表(線性表)完整實例
本文實例講述了C#實現(xiàn)順序表(線性表)的方法。分享給大家供大家參考,具體如下:
基本思想是使用數(shù)組作為盛放元素的容器,數(shù)組一開始的大小要實現(xiàn)確定,并使用一個Pointer指向順序表中最后的元素。順序表中的元素是數(shù)組中元素的子集。順序表在內(nèi)存中是連續(xù)的,優(yōu)勢是查找,弱勢是插入元素和刪除元素。
為避免裝箱拆箱,這里使用泛型,代替object。使用object的例子可以參照本站這篇文章://www.jb51.net/article/87603.htm,這個鏈接中的例子實現(xiàn)的是隊列,并沒 有使用Pointer來標(biāo)識 順序表中最后一個元素,而是動態(tài)的調(diào)整數(shù)組的大小,這與本例明顯不同,動態(tài)調(diào)整數(shù)組大小開銷較大。使用object同樣可以完成順序表數(shù)據(jù)結(jié)構(gòu),但是頻繁裝箱拆箱造成較大的開銷,應(yīng)使用泛型代替。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinearList
{
public interface IListDS<T>
{
int GetLength();
void Insert(T item, int i);
void Add(T item);
bool IsEmpty();
T GetElement(int i);
void Delete(int i);
void Clear();
int LocateElement(T item);
void Reverse();
}
//順序表類
class SequenceList<T>:IListDS<T>
{
private int intMaxSize;//最大容量事先確定,使用數(shù)組必須先確定容量
private T[] tItems;//使用數(shù)組盛放元素
private int intPointerLast;//始終指向最后一個元素的位置
public int MaxSize
{
get { return this.intMaxSize; }
set { this.intMaxSize = value; }
}
public T this[int i]//索引器方便返回
{
get { return this.tItems[i]; }
}
public int PointerLast
{
get { return this.intPointerLast; }
}
public SequenceList(int size)
{
this.intMaxSize = size;
this.tItems = new T[size];//在這里初始化最合理
this.intPointerLast = -1;//初始值設(shè)為-1,此時數(shù)組中元素個數(shù)為0
}
public bool IsFull()//判斷是否超出容量
{
return this.intPointerLast+1 == this.intMaxSize;
}
#region IListDS<T> 成員
public int GetLength()
{
return this.intPointerLast + 1;//不能返回tItems的長度
}
public void Insert(T item, int i)//設(shè)i為第i個元素,從1開始。該函數(shù)表示在第i個元素后面插入item
{
if (i < 1 || i > this.intPointerLast + 1)
{
Console.WriteLine("The inserting location is wrong!");
return;
}
if (this.IsFull())
{
Console.WriteLine("This linear list is full! Can't insert any new items!");
return;
}
//如果可以添加
this.intPointerLast++;
for(int j=this.intPointerLast;j>=i+1;j--)
{
this.tItems[j] = this.tItems[j - 1];
}
this.tItems[i] = item;
}
public void Add(T item)
{
if (this.IsFull())//如果超出最大容量,則無法添加新元素
{
Console.WriteLine("This linear list is full! Can't add any new items!");
}
else
{
this.tItems[++this.intPointerLast] = item;//表長+1
}
}
public bool IsEmpty()
{
return this.intPointerLast == -1;
}
public T GetElement(int i)//設(shè)i最小從0開始
{
if(this.intPointerLast == -1)
{
Console.WriteLine("There are no elements in this linear list!");
return default(T);
}
if (i > this.intPointerLast||i<0)
{
Console.WriteLine("Exceed the capability!");
return default(T);
}
return this.tItems[i];
}
public void Delete(int i)//設(shè)i最小從0開始
{
if (this.intPointerLast == -1)
{
Console.WriteLine("There are no elements in this linear list!");
return;
}
if (i > this.intPointerLast || i < 0)
{
Console.WriteLine("Deleting location is wrong!");
return;
}
for (int j = i; j < this.intPointerLast; j++)
{
this.tItems[j] = this.tItems[j + 1];
}
this.intPointerLast--;//表長-1
}
public void Clear()
{
this.intPointerLast = -1;
}
public int LocateElement(T item)
{
if (this.intPointerLast == -1)
{
Console.WriteLine("There are no items in the list!");
return -1;
}
for (int i = 0; i <= this.intPointerLast; i++)
{
if (this.tItems[i].Equals(item))//若是自定義類型,則T類必須把Equals函數(shù)override
{
return i;
}
}
Console.WriteLine("Not found");
return -1;
}
public void Reverse()
{
if (this.intPointerLast == -1)
{
Console.WriteLine("There are no items in the list!");
}
else
{
int i = 0;
int j = this.GetLength() / 2;//結(jié)果為下界整數(shù),正好用于循環(huán)
while (i < j)
{
T tmp = this.tItems[i];
this.tItems[i] = this.tItems[this.intPointerLast - i];
this.tItems[this.intPointerLast - i] = tmp;
i++;
}
}
}
#endregion
}
class Program
{
static void Main(string[] args)
{
}
}
}
基于順序表的合并排序:
//基于順序表的合并排序
static private SequenceList<int> Merge(SequenceList<int> s1,SequenceList<int> s2)
{
SequenceList<int> sList = new SequenceList<int>(20);
int i = 0;
int j = 0;
while(i<=s1.PointerLast&&j<=s2.PointerLast)
{
if (s1[i] < s2[j])
{
sList.Add(s1[i]);
i++;
}
else
{
sList.Add(s2[j]);
j++;
}
}
if (i > s1.PointerLast)
{
while (j <= s2.PointerLast)
{
sList.Add(s2[j]);
j++;
}
return sList;
}
else//即j>s2.PointerLast
{
while (i <= s1.PointerLast)
{
sList.Add(s1[i]);
i++;
}
return sList;
}
}
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#遍歷算法與技巧總結(jié)》、《C#程序設(shè)計之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家C#程序設(shè)計有所幫助。
您可能感興趣的文章
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實現(xiàn)多線程下載文件的方法
- 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實現(xiàn)遠(yuǎn)程關(guān)閉計算機(jī)或重啟計算機(jī)的方法
- 01-10C#自定義簽名章實現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實現(xiàn)方法
- 01-10winform實現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法


