Showing posts with label c data structure. Show all posts
Showing posts with label c data structure. Show all posts

Thursday, October 6, 2011

The System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries.

The System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries.
  Class Description
Public class ArrayList Implements the IList interface using an array whose size is dynamically increased as required.
Public class BitArray Manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0).
Public class CaseInsensitiveComparer Compares two objects for equivalence, ignoring the case of strings.
Public class CaseInsensitiveHashCodeProvider Obsolete. Supplies a hash code for an object, using a hashing algorithm that ignores the case of strings.
Public class CollectionBase Provides the abstract base class for a strongly typed collection.
Public class Comparer Compares two objects for equivalence, where string comparisons are case-sensitive.
Public class DictionaryBase Provides the abstract base class for a strongly typed collection of key/value pairs.
Public class Hashtable Represents a collection of key/value pairs that are organized based on the hash code of the key.
Public class Queue Represents a first-in, first-out collection of objects.
Public class ReadOnlyCollectionBase Provides the abstract base class for a strongly typed non-generic read-only collection.
Public class SortedList Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index.
Public class Stack Represents a simple last-in-first-out (LIFO) non-generic collection of objects.
Public class StructuralComparisons Provides objects for performing a structural comparison of two collection objects.
  Structure Description
Public structure DictionaryEntry Defines a dictionary key/value pair that can be set or retrieved.
  Interface Description
Public interface ICollection Defines size, enumerators, and synchronization methods for all nongeneric collections.
Public interface IComparer Exposes a method that compares two objects.
Public interface IDictionary Represents a nongeneric collection of key/value pairs.
Public interface IDictionaryEnumerator Enumerates the elements of a nongeneric dictionary.
Public interface IEnumerable Exposes the enumerator, which supports a simple iteration over a non-generic collection.
Public interface IEnumerator Supports a simple iteration over a nongeneric collection.
Public interface IEqualityComparer Defines methods to support the comparison of objects for equality.
Public interface IHashCodeProvider Obsolete. Supplies a hash code for an object, using a custom hash function.
Public interface IList Represents a non-generic collection of objects that can be individually accessed by index.
Public interface IStructuralComparable Supports the structural comparison of collection objects.
Public interface IStructuralEquatable Defines methods to support the comparison of objects for structural equality.

Monday, May 16, 2011

A generic stack class


#include <iostream>
#include <new>
#include <string>
#include <sstream>

using namespace std;

#if !defined __STACK_H
#define __STACK_H

namespace stk{
template<class T>
class GGStack; // Forward declaration of GGStack class for overloaded <<
operator

template<class T>
ostream& operator<<(ostream &,GGStack<T> &); // template declaration of
<<
operator

template<class T>
class GGStack{
private:
T *p;
int top,length;

string str()const;
public:
GGStack();
GGStack(const int);
GGStack(const GStack<T>&);
~GStack();

void push(T);
T pop();
int get_length()const;
bool is_empty()const;
GStack<T> operator=(const GStack<T>&);

// only for basic types
friend ostream& operator<< <>(ostream&,GStack<T> &);

class GStackException{
private:
string desc;
public:
GStackException(string exp){ desc="Exception : "+exp; }
string get_exp(){ return desc; }
};
};

template<class T>
GStack<T>::GStack(){
top=-1;
length=0;
p=0;
}

template<class T>
GStack<T>::GStack(const int size){
top=-1;
length=size;
try{
p=new T[length];
}catch(bad_alloc ba){
cout<<"Memory can not be alllocated
";
return;
}
}

template<class T>
GStack<T>::GStack(const GStack<T> &o){
top=o.top;
length=o.length;
try{
p=new T[length];
}catch(bad_alloc ba){
cout<<"Memory allocation failed
";
return;
}
for(int i=0;i<length;i++)
p[i]=o.p[i];
}

template<class T>
GStack<T>::~GStack(){
if(p!=0)
delete [] p;
}

template<class T>
void GStack<T>::push(T elem){
if(p==0){
try{
p=new T[1];
}catch(bad_alloc ba){
throw GStackException("Memory fault
");
}
length++;
top++;
p[top]=elem;
}
else if(top==(length-1)){
T *q;
try{
q=new T[length+1];
}catch(bad_alloc ba1){
throw GStackException("Memory fault
");
}
for(int i=0;i<length;i++)
q[i]=p[i];
length++;
top++;
q[top]=elem;
delete [] p;
p=q;
}
else{
top++;
p[top]=elem;
}
}

template<class T>
T GStack<T>::pop(){
if(p==0 || top==-1){
throw GStackException("GStack empty!
");
}
T ret=p[top];
top--;
length--;

if(top==-1){
delete [] p;
p=0;
}
else{
T *q;
try{
q=new T[length];
}catch(bad_alloc ba){
throw GStackException("Memory fault
");
}
for(int i=0;i<length;i++)
q[i]=p[i];
delete [] p;
p=q;
}

return ret;
}

template<class T>
int GStack<T>::get_length()const{
return length;
}

template<class T>
bool GStack<T>::is_empty()const{
return ((p==0)? true : false);
}

template<class T>
string GStack<T>::str()const{  // private member function
if(p==0)
return string("");
stringstream ss;
for(int i=0;i<length;i++){
ss << p[i];
if(i!=(length-1))
ss << ", ";
}
//ss<<"
";
return ss.str();
}

template<class T>
GStack<T> GStack<T>::operator=(const GStack<T> &stk){
length=stk.length;
top=stk.top;

if(p!=0)
delete [] p;
try{
p=new T[length];
}catch(bad_alloc ba){
throw GStackException("Memory fault in copying!
");
}
for(int i=0;i<length;i++)
p[i]=stk.p[i];

return *this;
}

template<class T>
ostream& operator<<(ostream &o,GStack<T> &s){
o<<s.str();
return o;
}

} // namespace stk;

#endif

Wednesday, April 27, 2011

Deleting a tree

clear(struct node* pNode)
{
if (pNode != NULL)
{
clear(pNode->left);
clear(pNode->right);
delete pNode;
}
}

How to find out the height of a Tree?

tree_height(mynode *p) {
   if(p==NULL)return(0);
   if(p->left){h1=tree_height(p->left);}
   if(p=>right){h2=tree_height(p->right);}
   return(max(h1,h2)+1); }

Featured Posts

Kali Linux Remote Desktop: Access GNOME from Windows Using Native RDP

  Kali Linux + GNOME 50 + GNOME Remote Desktop + Windows Remote Desktop (MSTSC) Getting a full GNOME desktop remotely on Kali Linux can be ...