using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpDataStructure
{
public class BubbleSort
{
private int currentLength;
public int MaxLength { private set; get; }
private int[] intArray;
private BubbleSort()
{
}
public int Count
{
get
{
return currentLength;
}
}
public BubbleSort(int maxLength)
{
intArray = new int[maxLength];
this.MaxLength = maxLength;
this.currentLength = 0;
}
public void Add(int value)
{
if (currentLength == this.MaxLength)
{
throw new Exception("this array has full.");
}
intArray[currentLength] = value;
currentLength++;
}
public void Remove()
{
if (currentLength == 0)
{
throw new Exception("this array is empty.");
}
currentLength--;
}
public int GetAt(int index)
{
if (index > currentLength)
{
throw new Exception("Out of region.");
}
return intArray[index];
}
public void Display()
{
for (int i = 0; i < currentLength; i++)
{
Console.WriteLine(intArray[i]);
}
Console.Read();
}
private void Swap(int index)
{
int tempValue = intArray[index];
if (intArray[index] > intArray[index + 1])
{
intArray[index] = intArray[index + 1];
intArray[index + 1] = tempValue;
}
}
public void SortByBobble(int length)
{
for (int i = 0; i < length-1; i++)
{
Swap(i);
}
length--;
if (length > 0)
{
SortByBobble(length);
}
}
public void SortByBobble()
{
for (int i=currentLength-1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
Swap(j);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpDataStructure
{
public class InsertSort
{
private int currentLength;
public int MaxLength { private set; get; }
private int[] intArray;
private InsertSort()
{
}
public int Count
{
get
{
return currentLength;
}
}
public InsertSort(int maxLength)
{
intArray = new int[maxLength];
this.MaxLength = maxLength;
this.currentLength = 0;
}
public void Add(int value)
{
if (currentLength == this.MaxLength)
{
throw new Exception("this array has full.");
return;
}
intArray[currentLength] = value;
currentLength++;
}
public void Remove()
{
if (currentLength == 0)
{
throw new Exception("this array is empty.");
}
currentLength--;
}
public int GetAt(int index)
{
if (index > currentLength)
{
throw new Exception("Out of region.");
}
return intArray[index];
}
public void Display()
{
for (int i = 0; i < currentLength; i++)
{
Console.WriteLine(intArray[i]);
}
Console.Read();
}
private void Swap(int index)
{
int tempValue = intArray[index];
if (intArray[index] > intArray[index + 1])
{
intArray[index] = intArray[index + 1];
intArray[index + 1] = tempValue;
}
}
public void SortByInsert()
{
for (int i = 1; i < currentLength; i++)
{
int currentValue = intArray[i];
int j;
for (j = i; j > 0; j--)
{
if (intArray[j - 1] > currentValue)
{
intArray[j] = intArray[j - 1];
}
else
{
break;
}
}
intArray[j] = currentValue;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpDataStructure
{
public class LinkedNode
{
public object Value { set; get; }
public LinkedNode NextNode { set; get; }
private LinkedNode()
{}
public LinkedNode(object value, LinkedNode nextNode)
{
this.Value = value;
this.NextNode = nextNode;
}
}
public class LinkedList
{
public LinkedNode FirstNode {private set; get;}
public LinkedNode LastNode { private set; get; }
public LinkedList()
{
this.FirstNode = null;
this.LastNode = null;
}
public bool IsEmpty()
{
lock (this)
{
if (this.FirstNode == null)
{
return true;
}
else
{
return false;
}
}
}
public void InsertAtFront(object value)
{
lock (this)
{
if (IsEmpty())
{
this.FirstNode = new LinkedNode(value, null);
this.LastNode = this.FirstNode;
}
else
{
this.FirstNode = new LinkedNode(value, this.FirstNode);
}
}
}
public void Add(object value)
{
lock (this)
{
if (IsEmpty())
{
this.FirstNode = new LinkedNode(value, null);
this.LastNode = this.FirstNode;
}
else
{
this.LastNode.NextNode = new LinkedNode(value, null);
this.LastNode = this.LastNode.NextNode;
}
}
}
public void RemoveAtFirst()
{
lock (this)
{
if (IsEmpty())
{
throw new Exception("LinkedList is empty.");
}
if (this.FirstNode == this.LastNode)
{
this.FirstNode = null;
this.LastNode = null;
}
else
{
this.FirstNode = this.FirstNode.NextNode;
}
}
}
public void RemoveAtLast()
{
lock (this)
{
if (IsEmpty())
{
throw new Exception("LinkedList is empty.");
}
if (this.FirstNode == this.LastNode)
{
this.FirstNode = null;
this.LastNode = null;
}
else
{
LinkedNode currentNode = this.FirstNode.NextNode;
while (currentNode.NextNode != this.LastNode)
{
currentNode = currentNode.NextNode;
}
this.LastNode = currentNode;
this.LastNode.NextNode = null;
}
}
}
public void Insert(object value, int index)
{
lock (this)
{
if (index > Count)
{
throw new Exception("Out of index.");
}
if (index == 0)
{
this.FirstNode = new LinkedNode(value,this.FirstNode);
return;
}
if (index == this.Count)
{
LinkedNode insertNode = new LinkedNode(value,null);
this.LastNode.NextNode = insertNode;
this.LastNode = insertNode;
return;
}
LinkedNode currentNode = this.FirstNode;
int i = 0;
while (currentNode.NextNode != null)
{
if (i + 1 == index)
{
LinkedNode insertNode = new LinkedNode(value, currentNode.NextNode);
currentNode.NextNode = insertNode;
return;
}
i++;
currentNode = currentNode.NextNode;
}
}
}
public void Remove(int index)
{
lock (this)
{
if (index > Count)
{
throw new Exception("Out of index.");
}
if (index == 0)
{
this.FirstNode = this.FirstNode.NextNode;
return;
}
if (index == Count)
{
LinkedNode tempNode = this.FirstNode.NextNode;
while (tempNode.NextNode != this.LastNode)
{
tempNode = tempNode.NextNode;
}
this.LastNode = tempNode;
this.LastNode.NextNode = null;
return;
}
LinkedNode currentNode = this.FirstNode;
int i = 0;
while (currentNode.NextNode != null)
{
if (i + 1 == index)
{
currentNode.NextNode = currentNode.NextNode.NextNode;
return;
}
i++;
currentNode = currentNode.NextNode;
}
}
}
public void PrintLink()
{
lock (this)
{
if (IsEmpty())
{
return;
}
LinkedNode currentNode = this.FirstNode;
while (currentNode != null)
{
Console.WriteLine(currentNode.Value.ToString());
currentNode = currentNode.NextNode;
}
}
}
private int count;
public int Count
{
get
{
LinkedNode currentNode = this.FirstNode;
while (currentNode.NextNode != null)
{
count++;
currentNode = currentNode.NextNode;
}
return count;
}
}
public object this[int index]
{
get
{
if (index >= this.Count)
{
throw new Exception("out of region.");
}
LinkedNode currentNode = this.FirstNode;
int i = 0;
while (currentNode != null)
{
if (index == i)
{
break;
}
i++;
currentNode = currentNode.NextNode;
}
return currentNode.Value;
}
set
{
if (index >= this.Count)
{
throw new Exception("out of region.");
}
int i = 0;
LinkedNode curentNode = this.FirstNode;
while (curentNode != null)
{
if (i == index - 1)
{
curentNode.Value = value;
}
curentNode = curentNode.NextNode;
i++;
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpDataStructure
{
public class LoopQueue
{
private int maxSize;
private int last;
private int first;
private int[] intArray;
public int Length {private set; get; }
private LoopQueue()
{
}
public LoopQueue(int max)
{
this.maxSize = max;
this.last = -1;
this.first = 0;
this.Length = 0;
this.intArray = new int[max];
}
public void Insert(int value)
{
if (IsFull())
{
throw new Exception("Queue is full.");
}
if (this.last == maxSize - 1 )
{
this.last = -1;
}
this.last++;
this.intArray[last] = value;
this.Length++;
}
public int Remove()
{
if (IsEmpty())
{
throw new Exception("Queue is empty.");
}
if (this.first == maxSize - 1)
{
this.first = -1;
}
this.first++;
this.Length--;
return intArray[first];
}
public int PeekFront()
{
return intArray[last];
}
public int Peek()
{
this.Length--;
this.first++;
return intArray[first-1];
}
public bool IsEmpty()
{
return (this.Length == 0);
}
public bool IsFull()
{
return (this.Length == this.maxSize);
}
}
}
using System;
namespace CSharpDataStructure
{
/// <summary>
/// Summary description for ջ.
/// </summary>
public class MyStack
{
int top;
int max;
private int[] intArray;
public MyStack(int max)
{
top = -1;
this.max = max;
intArray = new int[max];
}
public void Push(int obj)
{
top ++;
if (top >= max)
{
throw new Exception("Stack is full.");
}
intArray[top] = obj;
}
public int Peek()
{
return intArray[top];
}
public int Pop()
{
top --;
if ( top + 1 < 0) {
throw new Exception("Stack is empty.");
}
return intArray[top + 1];
}
public bool IsEmpty()
{
return top == -1;
}
public bool IsFull()
{
return top == max - 1;
}
public int Size
{
get
{
return top + 1;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpDataStructure
{
public class SelectSort
{
private int currentLength;
public int MaxLength { private set; get; }
private int[] intArray;
private SelectSort()
{
}
public int Count
{
get
{
return currentLength;
}
}
public SelectSort(int maxLength)
{
intArray = new int[maxLength];
this.MaxLength = maxLength;
this.currentLength = 0;
}
public void Add(int value)
{
if (currentLength == this.MaxLength)
{
throw new Exception("this array has full.");
return;
}
intArray[currentLength] = value;
currentLength++;
}
public void Remove()
{
if (currentLength == 0)
{
throw new Exception("this array is empty.");
}
currentLength--;
}
public int GetAt(int index)
{
if (index > currentLength)
{
throw new Exception("Out of region.");
}
return intArray[index];
}
public void Display()
{
for (int i = 0; i < currentLength; i++)
{
Console.WriteLine(intArray[i]);
}
Console.Read();
}
private void Swap(int first, int second)
{
int tempValue = intArray[first];
intArray[first] = intArray[second];
intArray[second] = tempValue;
}
public void SortBySelect()
{
for (int i = 0; i < currentLength; i++)
{
int minIndex = i;
for (int j = i; j < currentLength; j++)
{
if (intArray[j] < intArray[minIndex])
{
minIndex = j;
}
}
Swap(i, minIndex);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpDataStructure
{
public class SortArray
{
private int[] intArray;
public int Length { private set; get; }
public int Max { private set; get; }
public SortArray(int max)
{
intArray = new int[max];
this.Length = 0;
this.Max = max;
}
private int BisectionSearch(int value)
{
int lowerBound = 0;
int upperBound = this.Length - 1;
while (true)
{
int curentIndex = (lowerBound + upperBound)/2;
if (lowerBound > upperBound)
{
return lowerBound;
}
if (intArray[curentIndex] == value)
{
return curentIndex;
}
else if (intArray[curentIndex] > value)
{
upperBound = curentIndex - 1;
}
else if (intArray[curentIndex] < value)
{
lowerBound = curentIndex + 1;
}
}
}
public void Add(int value)
{
if (this.Length == 0)
{
intArray[0] = value;
this.Length++;
return;
}
if (this.Length >= this.Max - 1)
{
throw new Exception("Array is full");
}
int position = BisectionSearch(value);
for (int i = this.Length; i > position; i--)
{
intArray[i] = intArray[i - 1];
}
intArray[position] = value;
this.Length++;
}
public void Remove(int value)
{
if (this.Length == 0)
{
throw new Exception("Array is empty");
}
int position = BisectionSearch(value);
if (position == -1)
{
return;
}
for (int i=position; i<this.Length-1; i++)
{
intArray[i] = intArray[i + 1];
}
this.Length--;
}
public void Display()
{
for (int i = 0; i < this.Length; i++)
{
Console.WriteLine(intArray[i].ToString());
}
Console.Read();
}
public int this[int index]
{
set
{
Add(value);
}
get { return intArray[index]; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpDataStructure
{
class Program
{
static void Main(string[] args)
{
//TestBobbleSort();
//TestInsertSort();
//TestSelectOrder();
//TestSortArray();
//TestLoopQueue();
TestLinkNode();
}
private static void TestBobbleSort()
{
BubbleSort mySort = new BubbleSort(10);
mySort.Add(4);
mySort.Add(6);
mySort.Add(6);
mySort.Add(3);
mySort.Add(6);
mySort.Add(7);
mySort.Add(9);
mySort.Add(5);
mySort.Add(1);
//mySort.SortByBobble(mySort.Count);
mySort.SortByBobble();
mySort.Display();
}
private static void TestInsertSort()
{
InsertSort mySort = new InsertSort(10);
mySort.Add(4);
mySort.Add(6);
mySort.Add(6);
mySort.Add(3);
mySort.Add(6);
mySort.Add(7);
mySort.Add(9);
mySort.Add(5);
mySort.Add(1);
mySort.SortByInsert();
mySort.Display();
}
private static void TestSelectOrder()
{
SelectSort mySort = new SelectSort(10);
mySort.Add(4);
mySort.Add(6);
mySort.Add(6);
mySort.Add(3);
mySort.Add(6);
mySort.Add(7);
mySort.Add(9);
mySort.Add(5);
mySort.Add(1);
mySort.SortBySelect();
mySort.Display();
}
private static void TestSortArray()
{
SortArray mySort = new SortArray(10);
mySort.Add(4);
mySort.Add(6);
mySort.Add(6);
mySort.Add(3);
mySort.Add(6);
mySort.Add(7);
mySort.Add(9);
mySort.Add(5);
mySort.Add(1);
mySort.Remove(5);
mySort.Remove(6);
mySort.Display();
}
private static void TestLoopQueue()
{
LoopQueue queue = new LoopQueue(5);
queue.Insert(1);
queue.Insert(2);
queue.Insert(3);
queue.Insert(4);
queue.Insert(5);
int value = queue.Peek();
value = queue.Peek();
value = queue.Peek();
queue.Insert(6);
queue.Insert(7);
}
private static void TestLinkNode()
{
LinkedList linkList = new LinkedList();
//linkList.InsertAtFront(1);
linkList.Add(1);
linkList.Add(2);
linkList.Add(3);
linkList.Add(4);
linkList.Add(5);
linkList.Add(6);
linkList.Add(7);
linkList.Add(8);
linkList.Add(9);
Console.WriteLine(string.Format("Count : {0}", linkList.Count));
linkList.RemoveAtFirst();
linkList.RemoveAtLast();
Console.WriteLine(string.Format("The No.1 is: {0}", linkList[0]));
linkList.PrintLink();
linkList[3] = "a";
linkList.PrintLink();
linkList.Insert("Archer", 7);
linkList.PrintLink();
linkList.Remove(0);
linkList.Remove(4);
linkList.Remove(5);
linkList.PrintLink();
Console.Read();
}
}
}
浙公网安备 33010602011771号