线性表
List<string> list = new List<string>();
list.Add("12");
list.Add("asdkj");
list.Add("jsadklsss");
list.Add("jsadklsss11");
list.Add("ss");
list.Remove("12");
Console.WriteLine(list.IndexOf("ss"));
Console.WriteLine(list[0] + list[1]);
顺序表
内存中顺序存储,地址连续。
class SeqList<T> : Ilist<T>
{
/// <summary>
/// 顺序表实现方式
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
T Ilist<T>.this[int index] => throw new NotImplementedException();
private T[] data;//用来存储数据
private int count = 0;
public SeqList(int size)
{
data = new T[size];
count = 0;
}
public SeqList() : this(10) //默认构造函数
{
}
void Ilist<T>.Add(T item)
{
if(count == data.Length)
{
Console.WriteLine("List is full");
}
else
{
data[count++] = item;
}
}
void Ilist<T>.Clear()
{
count = 0;
}
T Ilist<T>.Delete(int index)
{
T t = data[index];
for(int i = index+1; i < count; i++)
{
data[i-1]= data[i];
}
count--;
return t;
}
T Ilist<T>.GetEle(int index)
{
if(index >= 0 && index < count - 1)
{
return data[index];
}
else
{
Console.WriteLine("not exsits");
return default(T);
}
}
int Ilist<T>.GetLength()
{
return count;
}
void Ilist<T>.Insert(T item, int index)
{
for(int i = count-1; i >= index; i--)
{
data[i+1] = data[i];
}
data[index] = item;
count++;
}
bool Ilist<T>.IsEmpty()
{
if (count == 0)
return true;
else
return false;
}
int Ilist<T>.Locate(T value)
{
for(var i = 0; i < count; i++)
{
if (data[i].Equals(value))
{
return i;
}
}
return -1;//不存在
}
}
public static void main(){
List<string> list = new List<string>();
list.Add("12");
list.Add("asdkj");
list.Add("jsadklsss");
list.Add("jsadklsss11");
list.Add("ss");
list.Remove("12");
Console.WriteLine(list.IndexOf("ss"));
Console.WriteLine(list[0] + list[1]);
}
单链表
internal class LinkList<T> : Ilist<T>
{
private Node<T> head; //头节点
private T data;
public LinkList()
{
head = null;
}
T Ilist<T>.this[int index]
{
get
{
Node<T> temp = head;
for (int i = 0; i < index; i++)
{
temp = temp.Next;
}
return temp.Data;
}
}
void Ilist<T>.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指向尾节点
temp.Next = newNode;
}
}
void Ilist<T>.Clear()
{
head = null;
}
T Ilist<T>.Delete(int index)
{
if(index == 0)
{
head = head.Next;
data = head.Data;
}
else
{
Node<T> temp = head;//头节点赋值给temp
for (int i = 0; i < index - 1; i++)
{
temp = temp.Next;//向后移动index次
}
Node<T> preNode = temp;
Node<T> currentNode = temp.Next;
data = currentNode.Data;
Node<T> nextNode = temp.Next.Next;
preNode.Next = nextNode;
}
return data;
}
T Ilist<T>.GetEle(int index)
{
return this[index];
}
int Ilist<T>.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;
}
void Ilist<T>.Insert(T item, int index)
{
Node<T> newNode = new Node<T>(item);
if(index == 0)//插入位置是头节点
{
newNode.Next = head;
head = newNode;
}
else
{
Node<T> temp = head;
for (int i = 0; i < index-1; i++)
{
temp = temp.Next;//向后移动index次
}
Node<T> preNode = temp;
Node<T> currentNode = temp.Next;
preNode.Next = newNode;
newNode.Next = currentNode;
}
}
bool Ilist<T>.IsEmpty()
{
return head == null;
}
int Ilist<T>.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;
}
}
}
双向链表
每个节点多了一个指向前驱节点的引用。
表头没有前驱节点,表尾没有后继节点。
循环链表
尾节点指向表头节点的单链表。
栈
受限制的线性表,先进后出
static void Main(string[] args)
{
Stack<string>stack = new Stack<string>();
stack.Push("213");
stack.Push("as");
Console.WriteLine(stack.Count);
stack.Pop();
stack.Push("ads");
stack.Pop();
Console.WriteLine(stack.Peek());//空栈取元素,会异常
stack.Clear();
Console.WriteLine(stack.Count);
}
栈的接口
internal interface IStackDS<T>
{
int Count { get; }//存储数据的个数
int GetLength();
bool IsEmpty();
void Clear();
void Push(T item);
T Pop();
T Peek();
}
顺序栈
internal class SeqStack<T> : IStackDS<T>
{
private T[] data;
private int top;
public SeqStack(int size)
{
data = new T[size];
top = -1;
}
public SeqStack()
{
data = new T[10];
top = -1;
}
int IStackDS<T>.Count
{
get { return top + 1; }
}
void IStackDS<T>.Clear()
{
top = -1;
}
int IStackDS<T>.GetLength()
{
return top + 1;
}
bool IStackDS<T>.IsEmpty()
{
return top == -1;
}
T IStackDS<T>.Peek()
{
return data[top];
}
T IStackDS<T>.Pop()
{
T temp = data[top];
top--;
return temp;
}
void IStackDS<T>.Push(T item)
{
top++;
data[top] = item;
}
}
链栈
internal class Node<T> //链式栈的节点
{
private T data;
private Node<T> next;
public Node()
{
data = default(T);
next = null;
}
public Node(T data)
{
this.data = data;
next = null;
}
public Node(T data, Node<T> next) : this(data)
{
this.next = next;
}
public Node(Node<T> next)
{
data = default (T);
this.next = next;
}
public T Data
{
get { return data; }
set { data = value; }
}
public Node<T> Next
{
get { return next; }
set { next = value; }
}
}
internal class LinkStack<T> : IStackDS<T>
{
private Node<T> top;//栈顶
private int count = 0;//栈中元素的个数
/// <summary>
/// 取得栈中元素的个数
/// </summary>
public int Count
{
get { return count; }
}
public void Clear()
{
count = 0;
top = null;
}
public int GetLength()
{
return count;
}
public bool IsEmpty()
{
return count == 0;
}
public T Peek()//取得栈顶数据
{
return top.Data;
}
public T Pop()
{
T temp = top.Data;
top = top.Next;
count--;
return temp;
}
public void Push(T item)
{
Node<T> newNode = new Node<T>(item);
count++;
newNode.Next = top;
top = newNode;
}
}
队列
操作受限的线性表
static void Main(string[] args)
{
Queue<int> queue = new Queue<int>();
queue.Enqueue(1);
queue.Enqueue(2);
queue.Enqueue(3);
Console.WriteLine(queue.Count);
int k = Convert.ToInt32(Console.ReadLine());
queue.Enqueue(k);
Console.WriteLine(queue.Dequeue());
Console.WriteLine(queue.Dequeue());
Console.WriteLine(queue.Dequeue());
Console.WriteLine(queue.Peek());
Console.WriteLine(queue.Count);
}
队列的接口
internal interface IQueue<T>
{
int Count { get; }
int GetLength();
bool IsEmpty();
void Clear();
void Enqueue(T item);
T Dequeue();
T Peek();
}
顺序队列
internal class SeqQueue<T> : IQueue<T>
{
private T[] data;
private int count;//当前元素个数
private int front;//队首元素索引减1
private int rear;//队尾元素索引
public SeqQueue(int size)
{
data = new T[size];
count = 0;
front = rear = -1;
}
public SeqQueue()
{
data = new T[10];
count = 0;
front = rear = -1;
}
public int Count
{
get { return count; }
}
public void Clear()
{
count = 0;
front = rear = -1;
}
public T Dequeue()
{
if(count <= 0)
{
Console.WriteLine("Queue is empty");
return default(T);
}
else
{
front = (front + 1)%data.Length;
T temp = data[front];
count--;
return temp;
}
}
public void Enqueue(T item)
{
if(count == data.Length)
{
Console.WriteLine("Queue is full");
}
else
{
rear = (rear + 1) % data.Length;
data[rear] = item;
count++;
}
}
public int GetLength()
{
return count;
}
public bool IsEmpty()
{
return count == 0;
}
public T Peek()
{
return data[(front + 1) % data.Length];
}
}
链队
internal class Node<T>
{
private T data;
private Node<T> next;
public Node(T data)
{
this.data = data;
}
public T Data
{
get { return data; }
set { data = value; }
}
public Node<T> Next
{
get { return next; }
set { next = value; }
}
}
internal class LinkQueue<T> : IQueue<T>
{
private int count;
private Node<T> front;//头节点
private Node<T> rear;//尾节点
public LinkQueue()
{
count = 0;
front = rear = null;
}
public int Count
{
set { count = value; }
get { return count; }
}
public void Clear()
{
count = 0;
front = rear = null;
}
public T Dequeue()
{
if(front == null)
{
Console.WriteLine("Queue is Empty.");
return default(T);
}
else
{
T temp = front.Data;
front = front.Next;
count--;
return temp;
}
}
public void Enqueue(T item) //尾部入队,头部出队
{
Node<T> newNode = new Node<T>(item);
count++;
if (rear == null)//空队
{
front = rear = newNode;
}
else
{
rear.Next = newNode;
rear = newNode;
}
}
public int GetLength()
{
return count;
}
public bool IsEmpty()
{
return count == 0;
}
public T Peek()
{
return front.Data;
}
}
串
string类的实现
internal class StringDS
{
private char[] data;
public StringDS(char[] array)
{
data = new char[array.Length];
for (int i = 0; i < data.Length; i++)
{
data[i] = array[i];
}
}
public StringDS(string str)
{
data = new char[str.Length];
for (int i = 0; i < data.Length; i++)
{
data[i] = str[i];
}
}
public char this[int index] //根据索引器取字符
{
get { return data[index]; }
}
public int GetLength()
{
return data.Length;
}
/*如果两个字符串一样,返回0
* 如果当前字符小于other,返回-1
* 如果当前字符大于other,返回1
*/
public int Compare(StringDS other)
{
int len = this.GetLength() < other.GetLength()?this.GetLength():other.GetLength();
//取得两个字符串中长度更小的字符串的长度
int index=-1;
for (int i = 0; i < len; i++)
{
if (this[i] != other[i])
{
index = i;
break;
}
}
if(index!= -1) //index为不等于字符出现的位置
{
if (this[index] > other[index])
{
return 1;
}
else
{
return -1;
}
}
else //短字符串和长字符串在重复区域匹配 或者 两字符串完全一样
{
if(this.GetLength() == other.GetLength())
{
return 0;
}
else
{
if(this.GetLength() > other.GetLength()) { return 1; }
else { return -1; }
}
}
}
}
数组
Array类
static void Main(string[] args)
{
int[] list = { 34, 72, 13, 44, 25, 30, 10 };
int[] list2 = new int[10];
Console.Write("原始数组: ");
foreach (int i in list)
{
Console.Write(i + " ");
}
Console.WriteLine();
// 逆转数组
Array.Reverse(list);
//搜索
Console.WriteLine("25在list中出现的位置是"+Array.IndexOf(list, 25));
//获取元素
Console.WriteLine(list.GetValue(0));
//复制
//Array.Copy(list, list2, 7);
list.CopyTo(list2, 0);
foreach (int i in list2)
{
Console.Write(i+" ");
}
Console.Write("逆转数组: ");
foreach (int i in list)
{
Console.Write(i + " ");
}
Console.WriteLine();
// 排序数组
Array.Sort(list,0,3);
Console.Write("局部排序数组前四个: ");
foreach (int i in list)
{
Console.Write(i + " ");
}
Console.WriteLine();
Console.ReadKey();
}