标签:stat list 功能 temp return 接口 item code 有一个
线性结构是什么?
线性结构是一种数据结构,它有一对一的关系,就像一个长对,一个接一个,特点是,除第一个元素和最后一个元素外,其它元素前后只有一个元素。
简单示例1:
static void Main(string[] args) { //使用BCL中的List线性表 List<string> strList = new List<string>(); strList.Add("123"); strList.Add("456"); strList.Add("789"); Console.WriteLine(strList[1]); Console.WriteLine(strList.Count); strList.Remove("123"); //去除某一元素 Console.WriteLine(strList[1]); Console.WriteLine(strList.Count); }
输出为:456
3
789
2
线性表实现方式
顺序表:连续排放
单链表:
双向链表
循环链表
自设一个List<>:
首先创建一个接口,然后实现接口得所有方法,完程自建List功能:
class SeqList<T>:IListDS<T> { private T[] data; //存储数据 private int count = 0; public SeqList(int size) { data = new T[size]; count = 0; } public SeqList():this(10) //默认构造函数10 { } public void Add(T item) //添加物件 { if(count == data.Length) { Console.WriteLine("空间已存满"); } else { data[count] = item; count++; } } //取得数据个数 public int GetLength() //得到长度 { return count; } public T GetEle(int index) //返回索引 { if(index>=0&&index<=count-1) { return data[index]; } else { Console.WriteLine("索引不存在"); return default(T); } } public void Clear() { count = 0; } public bool IsEmpty() { return count == 0; } public void Insert(T item,int index) { for(int i=count-1;i>=count;i--) { data[i + 1] = data[i]; } data[index] = item; count++; } public T Delete(int index) { T temp = data[index]; for(int i=index+1;i<count;i++) { data[i - 1] = data[i]; } count--; return temp; } public T this[int index] { get { return GetEle(index); } } public int Locate(T value) { for(int i=0;i<count;i++) { if(data[i].Equals(value)) { return i; } } return -1; } }
测试:
static void Main(string[] args) { SeqList<string> seqList = new SeqList<string>(); seqList.Add("123"); seqList.Add("456"); seqList.Add("789"); Console.WriteLine(seqList.GetEle(1)); Console.WriteLine(seqList[1]); seqList.Insert("666",1); for(int i=0;i<seqList.GetLength();i++) { Console.WriteLine(seqList[i] + "."); } seqList.Clear(); Console.WriteLine(seqList.GetLength()); Console.ReadKey(); }
结果:
456
456
123.
666.
456.
789.
0 结果OK,功能实现
单链表: data next 示图:
class Node<T> { private T data; //存储数据 private Node<T> next; //指针 public Node() { data = default(T); next = null; } public Node(T value) { data=value; next = null; } public Node(T value,Node<T> next) { this.data = value; this.next = next; } public Node(Node<T> next) { this.next = next; } public T Data { get { return data; } set { data = value; } } public Node<T> Next { get { return next; } set { next = value; } } }
class LinkList<T>:IListDS<T> { private Node<T> head; public LinkList() { head = null; } public void Add(T item) { Node<T> newNode = new Node<T>(item); //头节点为空,那么新节点就是头节点 if(head==null) { head = newNode; } else {//把新来结点放链表尾部 Node<T> temp = head; while(true) { if(temp.Next!=null) { temp = temp.Next; } else { break; } } temp.Next = newNode; } } public void Insert(T item, int index) { Node<T> newNode = new Node<T>(item); if(index==0) { newNode.Next = head; } else { Node<T> temp = head; for(int i=1;i<index-1;i++) { temp = temp.Next; } Node<T> preNode = temp; Node<T> currentNode = temp; preNode.Next = newNode; newNode.Next = currentNode; } } public T Delete(int index) { T data = default(T); if(index == 0) { data = head.Data; head = head.Next; } else { Node<T> temp = head; for (int i = 1; i < index - 1; i++) { temp = temp.Next; } Node<T> preNode = temp; Node<T> currentNode = temp.Next; data = currentNode.Data; Node<T> nextNode = temp.Next.Next; preNode.Next = nextNode; } return data; } public int GetLength() { if (head == null) return 0; Node<T> temp = head; int count = 1; while(true) { if(temp.Next!=null) { count++; temp = temp.Next; } else { break; } } return count; } public void Clear() { head = null; } public bool IsEmpty() { return head == null; } public T this[int index] { get { Node<T> temp = head; for(int i=1;i<=index-1;i++) { temp = temp.Next; } return temp.Data; } } public T GetEle(int index) { return this[index]; } public int Locate(T value) { Node<T> temp = head; if(temp==null) { return -1; } else { int index = 0; while(true) { if(temp.Data.Equals(value)) { return index; } else { if(temp.Next!=null) { temp = temp.Next; } else { break; } } } return -1; } } }
标签:stat list 功能 temp return 接口 item code 有一个
原文地址:http://www.cnblogs.com/redkop/p/7724985.html