jinzha
BAN USER/*C# implementation*/
static LinkNode ReverstLinkListByN(LinkNode Head, int n)
{
LinkNode newHead = null;
LinkNode nHead = null, pre = null, cur = null, next = null, nTail = null, OldTail = null;
int count = 0;
if (Head != null)
{
cur = Head;
while (cur != null)
{
if (pre == null)
{
pre = cur;
cur = cur.Next;
nTail = pre;
count++;
}
else
{
next = cur.Next;
cur.Next = pre;
pre = cur;
cur = next;
count++;
}
if (count == n || cur == null)
{
nHead = pre;
if (newHead == null) newHead = nHead;
if (OldTail != null)
{
OldTail.Next = nHead;
}
OldTail = nTail;
//start to reset
count = 0;
pre = null;
}
}
nTail.Next = null;
}
return newHead;
}
static bool flag = false;
static bool returnFlag = false;
static void PrintPrimeter(TreeNode cur)
{
if (cur != null)
{
if (cur.Left != null || cur.Right != null)
{
//Do PreOrder and Print each number on the road.
if (!flag) Console.WriteLine(cur.Value);
PrintPrimeter(cur.Left);
PrintPrimeter(cur.Right);
//Print everything when we quite the recursion
if (returnFlag) Console.WriteLine(cur.Value);
}
else
{
//When we hit the first leaf node.
//Start to print nothing but all leaf
flag = true;
Console.WriteLine(cur.Value);
//Until we hit the most right node which is also the last leave node
//We set the returnFlag to quite the recursion
if (cur == MostRightTreeNode) returnFlag = true;
}
}
}
static TreeNode MostRightTreeNode = null;
static bool rflag = false;
static void FindRightMostNode(TreeNode cur)
{
if (cur != null)
{
if (cur.Left != null || cur.Right != null)
{
if (!rflag) FindRightMostNode(cur.Right);
if (!rflag) FindRightMostNode(cur.Left);
}
else
{
MostRightTreeNode = cur;
rflag = true;
}
}
}
- jinzha June 16, 2013static Node ReverseBlocksLinkedList(Node root, int portion)
{
int i = 1;
Node preRoot = null;
Node preNode = null;
Node curNode = root;
Node curRoot = null;
while (curNode != null)
{
curRoot = curNode;
while (i <= portion && curNode != null)
{
preNode = curNode;
curNode = curNode.Next;
i++;
}
preNode.Next = preRoot;
preRoot = curRoot;
i = 1;
}
return curRoot;
}
static Node ReverseWholeLinkedList(Node root)
{
Node cur = root;
Node next = null;
Node pre = null;
while (cur != null)
{
//Set the next to point to item next to current
next = cur.Next;
//reset the cur next to point to pre node
cur.Next = pre;
//make cur to be pre;
pre = cur;
//make cur node to be next
cur = next;
}
return pre;
}
- jinzha May 22, 2013static void Main(string[] args)
{
int[] a = new int[] {1,2,3,4 };
Node root0 = CreateAList(a);
int[] b = new int[] { 5, 6, 7 };
Node root1 = CreateAList(b);
Node c = CrossCombineTwoLinkList(root0, root1);
}
static Node CrossCombineTwoLinkList(Node a, Node b)
{
Node root = a;
Node aPre = null;
Node bPre = null;
while (a != null & b != null)
{
aPre = a;
a = a.Next;
bPre = b;
b = b.Next;
aPre.Next = bPre;
bPre.Next = a;
}
if (b != null)
{
aPre.Next = b;
}
return root;
}
class Queue
{
Stack enQueueStack = new Stack();
const int size = 10;
public void Enqueue(int n)
{
if (size > deQueueStack.Lenght)
{
while (deQueueStack.Lenght > 0)
{
enQueueStack.Push(deQueueStack.Pop());
}
enQueueStack.Push(n);
}
}
Stack deQueueStack = new Stack();
public int Dequeue()
{
while (deQueueStack.Lenght > 0)
{
deQueueStack.Push(enQueueStack.Pop());
}
return deQueueStack.Pop();
}
}
/*C# implementation*/
- jinzha June 18, 2013