Posts


Cocaine Cowgirl, O Holy Night, and I Don’t want to miss a Thing are progressing nicely.


Book List: Precalculus - It has been seven years since I have done math, so I will start with this. Calculus Made Easy Applied Cryptography PostgreSQL - Finish few remaining chapters Data Structures and Problem Solving Using Java Programming Linux Games Bookkeeping and Accounting Nemsis

Guitar: 10 new songs that I will be learning are: Everlong - foo fighters Cocaine Cowgirls - Matt Mays I don’t want to miss a thing - aerosmith 1979 - smashing pumpkins (I do believe I used to know this one) Killer in me - smashing pumpkins Heart of Gold - Neil Young O Holy Night …..


I am reading through a chapter on binary trees. The first simple example they give is traversing through directory structures. As the book is for java the attached file is the example translated to c#.

Once I have finished the chapter I will upload the binary tree example translated to c#.

Here is an example postorder recursion through a directory tree (attachment main.cs). See attachment Main.cs for a different example.

using System.IO;

class RecurseDirectory
{
	public static void Main(string[] args)
	{
		RecurseDirectory r = new RecurseDirectory();
		RecurseDirectory.PrintDirsRecursively(new System.IO.DirectoryInfo(@"\\Directory\Location"), 0);

		Console.Read();
	}
	
	
	public static void PrintDirsRecursively(System.IO.DirectoryInfo source, int depth) 
	{
		foreach (System.IO.DirectoryInfo dir in source.GetDirectories())
		{
			PrintTabs(depth);
			System.Console.WriteLine(source.FullName);
			PrintDirsRecursively(dir, depth+1);
		}
		foreach (System.IO.FileInfo file in source.GetFiles())
		{
			PrintTabs(depth);
			System.Console.WriteLine(file.FullName);
		}
	}


	private static void PrintTabs(int depth)
	{
		for (int j=0; j < depth; j++)
		{
			System.Console.Write("\t");
		}
	}
}

Here is a simple recursive directory/file copy function.

using System.IO;

public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target) 
{
    foreach (DirectoryInfo dir in source.GetDirectories())
    {
        CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
    }
    foreach (FileInfo file in source.GetFiles())
    {
        file.CopyTo(Path.Combine(target.FullName, file.Name), true); //overwrite
    }
}

I am rereading some chapters in “Data Structures & Problem Solving Using Java”. I just finished reading chapter 17 on linked lists. Here is the singly linked list and sorted linked list from this chapter done in c#.

LinkedList - the list itself SortedLinkedList - the sorted linked list ListNode - represents the node LinkedListIterator - represents the position

using System;
using System.Collections.Generic;

namespace LinkedLists
{
	public class ListNode<T>
	{
		private T elem;
		private ListNode<T> nextNode;
		
		public T Element
		{
			get {return elem;}
			set {elem = value;}
		}
		
		public ListNode<T> Next
		{
			get {return nextNode;}
			set {nextNode = value;}
		}
		
		public ListNode(T theElement) : 
			this(theElement, null)
		{
		}
		
		public ListNode(T theElement, ListNode<T> n)
		{
			elem = theElement; nextNode = n;
		}
		
		
	}
	
	public class LinkedListIterator<T>
	{
		private ListNode <T> cur;
		
		public LinkedListIterator(ListNode<T> theNode)
		{
			cur = theNode;
		}
		
		public ListNode<T> Current
		{
			get{return cur;}
			set{cur = value;}
		}
		
		public bool IsValid()
		{
			return cur != null;
		}
		
		public T Retrieve()
		{
			if ( IsValid() )
			{ 
				return cur.Element;
			}
			else
			{
				return default(T); //null;
			}
		}
		
		public void Advance()
		{
			if (IsValid())
			{
				cur	= cur.Next;
			}
		}
	}
	
	public class LinkedList<T>
	{
		private ListNode<T> header;
		
		/// <summary>
		/// Construct the List.
		/// </summary>
		public LinkedList()
		{
			this.header = new ListNode<T> (default(T)); //(null);
		}
		
		public bool IsEmpty()
		{
			return this.header.Next == null;
		}
		
		public void MakeEmpty()
		{
			this.header.Next = null;
		}
		
		/// <summary>
		/// Return an iterator representing the header node.
		/// </summary>
		/// <returns>
		/// A <see cref="LinkedListIterator"/>
		/// </returns>
		public LinkedListIterator<T> Zeroth()
		{
			return new LinkedListIterator<T> (this.header);
		}	
		
		/// <summary>
		/// Return an iterator representing the first node in the list.
		/// </summary>
		/// <returns>
		/// A <see cref="LinkedListIterator"/>
		/// </returns>
		public LinkedListIterator<T> First()
		{
			return new LinkedListIterator<T> (this.header.Next);
		}
		
		public LinkedListIterator<T> Find(T x)
		{
			ListNode<T> itr = this.header;
			
			while(itr != null && !itr.Element.Equals(x))
			{
				itr = itr.Next;
			}
			
			return new LinkedListIterator<T>(itr);
		}
		
		/// <summary>
		/// Remove the first occurrence of an item.  The code is not foolproof.
		/// There may be two iterators, and one can left dangling if the other
		/// removes a node.
		/// </summary>
		/// <param name="x">
		/// A <see cref="T"/>
		/// </param>
		public void Remove(T x)
		{
			LinkedListIterator<T> p = FindPrevious(x);
			if(p.Current.Next != null)
			{
				p.Current.Next = p.Current.Next.Next;
			}
		}
		
		/// <summary>
		/// Return iterator prior to the first node containing an item.
		/// </summary>
		/// <param name="x">
		/// A <see cref="T"/>
		/// </param>
		/// <returns>
		/// A <see cref="LinkedListIterator"/>
		/// </returns>
		public LinkedListIterator<T> FindPrevious(T x)
		{
			ListNode<T> itr = this.header;
			
			while (itr.Next != null && !itr.Next.Element.Equals(x))
			{
				itr = itr.Next;
			}
			return new LinkedListIterator<T>(itr);
		}
		
		public void Insert(T x, LinkedListIterator<T> p)
		{
			if(p != null && p.Current != null)
			{
				p.Current.Next = new ListNode<T>(x, p.Current.Next);
			}
		}
		
		public static void PrintList(LinkedList<T> theList)
		{
			if (theList.IsEmpty())
			{
				System.Console.WriteLine("Empty List");
			}
			else
			{
				LinkedListIterator<T> itr = theList.First();
				for( ; itr.IsValid(); itr.Advance())
				{
					System.Console.WriteLine(itr.Retrieve());
				}
			}
			
			System.Console.WriteLine("");
		}
	}
	
	public class SortedLinkedList<T> : LinkedList<T>
	{
		public new void Insert(T x, LinkedListIterator<T> p)
		{
			Insert(x);
		}
		
		public void Insert(T x)
		{
			LinkedListIterator<T> prev = Zeroth();
			LinkedListIterator<T> curr = First();
			
			//while(curr.IsValid() && ( x.CompareTo(curr.Retrieve() ) > 0) )
			while(curr.IsValid() && (!EqualityComparer<T>.Default.Equals(x, curr.Retrieve())) ) 
			{
				prev.Advance();
				curr.Advance();
			}
			
			base.Insert(x, prev);
		}
	}


        class MainClass
	{
		public static void Main(string[] args)
		{
			
			
	        LinkedList<int> theList = new LinkedList<int>( );
	        LinkedListIterator<int> theItr;
	        int i;
	
	        theItr = theList.Zeroth( );
	        theList.PrintList(theList);
	
	        for( i = 0; i < 10; i++ )
	        {
	            theList.Insert( i , theItr );
	            theList.PrintList( theList );
	            theItr.Advance( );
	        }
	        
	
	        for( i = 0; i < 10; i += 2 )
			{
	            theList.Remove( i );
			}
	
	        for( i = 0; i < 10; i++ )
			{
	            if( ( i % 2 == 0 ) == ( theList.Find( i ).IsValid( ) ) )
				{
	                Console.WriteLine( "Find fails!" );
				}
			}
	
	        Console.WriteLine( "Finished deletions" );
	        theList.PrintList( theList );
		}
	}

}