Saturday, January 20, 2007

Linked List in C Sharp.NET

I got tones of emails, lately for Linklist and Tree data structure implemetation

Here is a simple Linked List Data structure in C Sharp.NET. A LinkedList will always have two class a LinkedList class and a Node Class.
I have also included most common options to insert and Delete nodes.


using System;
using System.Collections.Generic;
using System.Text;

namespace Algorithms
{
public class LinkedList
{
private Node head;

public LinkedList()
{
head = null;
}
public void Insert(int dataValue)
{
if (head == null)
{
Node nodeInsert = new Node(dataValue);
head = nodeInsert;
nodeInsert.NextNode = null;
}
else
{
Node nodeInsert = new Node(dataValue);
nodeInsert.NextNode = head;
head = nodeInsert;
}
}
public void InsertAtPosition(Node insertNode, int i)
{
if (i == 0)
{
insertNode.NextNode = head;
head = insertNode;
}
else
{
Node currentNode = head;
Node currentNodeNext = head.NextNode;
for (int j = 0; j < i; j++)
{
currentNode = currentNode.NextNode;
currentNodeNext = currentNode.NextNode;
}
if (currentNode != null)
{
currentNode.NextNode = insertNode;
insertNode.NextNode = currentNodeNext;
}


}
}
public void DeleteAtPosition(int i)
{
if (i == 0)
{
head = null;
}
else
{
Node currentNode = head;
Node currentNodeNext = head.NextNode;
for (int j = 0; j < i; j++)
{
currentNode = currentNode.NextNode;
currentNodeNext = currentNode.NextNode;
}
if (currentNode != null)
{
currentNode.NextNode = currentNodeNext.NextNode;
}
}
}
public bool Delete(Node nodeToDelete)
{
bool returnFlag = false;

if (head == null)
returnFlag = false;
else if (head.Equals(nodeToDelete))
{
head = null;
returnFlag = true;
}
else
{
Node checkNode = head;
Node checkNodeNext = head.NextNode;
while (checkNodeNext != null)
{
if (checkNodeNext.Equals(nodeToDelete))
{
checkNode.NextNode = checkNodeNext.NextNode;
checkNodeNext = null;
returnFlag = true;
}
}
}
return returnFlag;

}
public void Print()
{
Node firstNode = head;
while (firstNode != null)
{
Console.Write(firstNode.DataValue + " ");
firstNode = firstNode.NextNode;
}
}
public void Clear()
{
Node checkNode = head;
Node checkNodeNext;

while (checkNode != null)
{
checkNodeNext = checkNode.NextNode;
checkNode = null;
checkNode = checkNodeNext;
}

}

}
public class Node
{
private int dataValue;
private Node nextNode = null;

public Node(int data)
{
dataValue = data;
}
public Node NextNode
{
get
{
return nextNode;
}
set
{
nextNode = value;
}
}
public int DataValue
{
get
{
return dataValue;
}
set
{
dataValue = value;
}
}

}
}

Monday, December 25, 2006

First Non-Repeated Character from a UNICODE String using Hash Table

the only difference here is the input string can be UNICODE (65000 characters) instead of ASCII Code (256 characters) so using a hash table makes more sense


public static char FirstNonRepeatedHash(string stringToCheck)
{
Hashtable cHash = new Hashtable();
int length = stringToCheck.Length;
int i = 0;
bool hashPresent = false;
char charToReturn = '\0';

for (i = 0; i < length; i++)
{
hashPresent = cHash.Contains(stringToCheck[i]);
if (hashPresent)
{
cHash[stringToCheck[i]] = 1 + Convert.ToInt32(cHash[stringToCheck[i]]);
}
else
{
cHash.Add(Convert.ToChar(stringToCheck[i]), 1);
}

}

for (i = 0; i < length; i++)
{
if (Convert.ToInt32(cHash[stringToCheck[i]]) ==1 )
{
charToReturn = stringToCheck[i];
break;
}
}
return charToReturn;
}

First Non repeated Character in a String using an Array

Here is how to find the first non repeated character in an a string using an array
the assumption is the String is ASCII other wise check for the hash table function

For example If the input is "TEETOTALAR" The First non repeated Character is "O"


public static char FirstNonRepeatedArray(string stringToCheck)
{
int length = stringToCheck.Length;
int i = 0;
int[] intCollection = new int[256];
char returnChar = '\0';

for (i = 0; i < length; i++)
{
intCollection[stringToCheck[i]] = intCollection[stringToCheck[i]] + 1;
}
for (i = 0; i < length; i++)
{
if (intCollection[stringToCheck[i]] == 1)
{
returnChar = stringToCheck[i];
break;
}
}
return returnChar;
}

Saturday, November 25, 2006

String is a Palindrome !

How to check if a string is a palindrome ?
You can change the code a bit to reverse an array


public static bool IsPalindrome(string s)
{
int length =s.Length;
char[] chrArray = s.ToCharArray();
if (length == 0)
return false;
if (length == 1)
return true;

int start = 0;
int end = length - 1;

while (end > start)
{
if (chrArray[start] == chrArray[end])
{
start++;
end--;
}
else
{
return false;
}
}
return true;
}

Wednesday, October 25, 2006

Remove Characters from a String with limited Memory

How about, If we don't have enough memory we have to use a single array.
Here is how to do that with limited memory


public static string RemoveCharactersLimited(char[] s, string removeChars)
{
int i = 0, j = 0;

int lengthC = removeChars.Length;
int lengthS = s.Length;
int[] intCollection = new int[256];

for (i = 0; i < lengthC; i++)
{
intCollection[removeChars[i]] = 1;
}

i = j = 0;
for (i = 0; i < lengthS; i++)
{
if (intCollection[s[i]] != 1)
{
s[j] = s[i];
j++;
}
}
while (j < lengthS)
{
s[j] = '\0';
j++;
}

return new string(s);

}

Remove Characters from String C#.NET

The string manipulation function removes a set of characters fromthe input string

For example

Removing "RT" from "ROBERT FROST" will give you "OBE FOS"


public static string RemoveCharacters(string s, string removeChars)
{
int i=0,j=0;

int lengthC = removeChars.Length;
int lengthS = s.Length;
int[] intCollection = new int[256];
char[] s2 = new char[lengthS];

for (i = 0; i < lengthC; i++)
{
intCollection[removeChars[i]] = 1;
}

i = j = 0;
for (i = 0; i < lengthS; i++)
{
if (intCollection[s[i]] != 1)
{
s2[j] = s[i];
j++;
}
}

return new string(s2);

}

Monday, September 25, 2006

AtoI function - String to Integer in C# .NET

If you ever want to convert a string to integer, you have to go through the following checks


public static int StringToInt(string s)
{
int length = s.Length;
int i = 0;
int lastNumber = 0;
int returnNumber = 0;
bool numberNegative = false;
int startPoint = 0;

if (s[0] == '-')
{
numberNegative = true;
startPoint = 1;
}

for (i = startPoint; i < length; i++)
{
if (s[i] == ' ')
{
continue;
}
else
{
if ((s[i] >= '0') && s[i] <= '9')
{
returnNumber = s[i] - '0';
if (i > 0) lastNumber = lastNumber * 10;
lastNumber = lastNumber + returnNumber;
}
else
{
break;
}
}
}
if (numberNegative)
lastNumber = -1 * lastNumber;

return lastNumber;
}