Thursday, January 15, 2009

去除数组中重复的值,在组成新的数组

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;//

namespace ArrayAndArrayList
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[9];
arr[0] = 1;
arr[1] = 9;
arr[2] = 7;
arr[3] = 7;
arr[4] = 8;
arr[5] = 9;
arr[6] = 3;
arr[7] = 9;
arr[8] = 10;

ArrayList al = new ArrayList();
for (int i = 0; i < arr.Length; i++)
{
bool b = false;
int temp = arr[i];
for (int j = i + 1; j < arr.Length; j++)
{
if (temp == arr[j])
{
b = true;
}
}
if (!b)
{
al.Add(temp);
}
}
//for (int i = 0; i < arr.Length; i++)
//{
// for (int j = i + 1; j < arr.Length; j++)
// {
// if (arr[i] != arr[j])
// {
// b = false;
// }
// else
// {
// b = true;
// i++;
// }
// }
// if (b == false)
// {
// al.Add(arr[i]);
// }
//}

Console.WriteLine(al.Count);
foreach (int i in al)
{
Console.WriteLine(i);
}
}
}
}

Doubly Linked List

namespace DoubleLinkedList
{
///
///
///

public class DBNode
{
public Object Element;
public DBNode nextLink;
public DBNode prevLink;

public DBNode()
{
Element = null;
nextLink = null;
prevLink = null;
}

public DBNode(Object theElement)
{
Element = theElement;
nextLink = null;
prevLink = null;
}
}

///
///
///

public class DoublyLinkedList
{
protected DBNode header;

public DoublyLinkedList()
{
header = new DBNode("header");
}

public DBNode Find(Object Item)
{
DBNode current = new DBNode();
current = header;
while (current.Element != Item)
{
current = current.nextLink;
}
return current;
}

public void Insert(Object newItem,Object after)
{
DBNode current = new DBNode();
DBNode newNode = new DBNode(newItem);
current = Find(after);
newNode.nextLink = current.nextLink;
newNode.prevLink = current;
current.nextLink = newNode;
}

public void Remove(Object n)
{
DBNode p = Find(n);
p.prevLink.nextLink = p.nextLink;
p.nextLink.prevLink = p.prevLink;
p.prevLink = null;
p.nextLink = null;
}

public DBNode FindLast()
{
DBNode current = new DBNode();
current = header;
while (current.nextLink != null)
{
current = current.nextLink;
}
return current;
}

public void PrintReverse()
{
DBNode current = new DBNode();
current = FindLast();
while (current.prevLink != null)
{
Console.WriteLine(current.Element);
current = current.prevLink;
}
}

public void PrintObverse()
{
DBNode current = new DBNode();
current = header;
while (current.nextLink != null)
{
Console.WriteLine(current.nextLink.Element);
current = current.nextLink;
}
}

}

class Program
{
static void Main(string[] args)
{
DoublyLinkedList myList = new DoublyLinkedList();
myList.Insert("kevin1", "header");
myList.Insert("kevin2","kevin1");
myList.Insert("kevin3", "kevin2");
myList.Insert("kevin4", "kevin3");
myList.Insert("kevin5", "kevin4");

myList.Remove("kevin3");
myList.PrintReverse();
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~");
myList.PrintObverse();

}
}
}