Microsoft Interview Question
Software Engineer in Teststemplate<class T>
class LinkedList
{
T node[100];
};
and add pointer operators overloaded function.
Could any one explain what does template<class T> mean...
i'm a beginner...
also tell me in detail about the map class in C++,please
class LinkedList<T> where T : struct
{
public LinkedList(int length)
{
end = -1;
coll = new T?[length];
}
int end;
T?[] coll;
public void add(T ele)
{
if (end + 1 < coll.Length)
{
end++;
coll[end] = ele;
}
}
public void remove(T ele)
{
for (int i = 0; i <= end; i++)
{
if (object.Equals(coll[i], ele)) coll[i] = null;
}
defragement();
}
public void print()
{
for (int i = 0; i <= end; i++)
Console.WriteLine(coll[i]);
}
private void defragement()
{
int np = -1;
for (int i = 0; i <= end; i++)
{
if (coll[i] == null && np == -1) np = i;
else if (coll[i] == null) continue;
else
{
if (np != -1)
{
coll[np] = coll[i];
coll[i] = null;
np++;
}
}
}
if (np != -1) end = np - 1;
}
}
for c coders
#define MAXSIZE 100 //assuming u know the sze already
struct list
{
int arr[100][2]; //Assuming implementing list for integers
int head;
};
typedef struct list List;
//now initialize head to 0 and start inserting data from arr[0][0] and arr[0][1] //will point to next node and so on..
//remember to fill free nodes wth some perticular value so tjat u know it is free
Its not clear to me why do you have a 2D array in your struct definition ? Can you clarify ?
My Bit: In the data structure of the node itself store the index of next (and previous in doubly linkedlist) node. The basic difference is that in normal array based data structures the next element is in the next index(i+1) but in linkedlist it can be at different index.
struct node{
void * data;
int nextNodeIndex;
int prevNodeIndex;
}
typedef struct node LinkedListNode;
int headIndex;
//Create array of LinkedListNode of given n size.
//If n is not given, taken some default size (as in arraylist in java) and when full //double the size and copy the stored data in new array
head = -1, count = 0;
insert(int array, int node)
{
array[count]=node;
if(count == 0)
head=array[count];
count++;
}
int delete(int array, int node)
{
if(count==0)
retuen -1;
int n=0, i=0;
while(array[n]!=node)
n++;
for(i=count-n-1; i<count-1; i++)
array[i] = array[i+1];
count--;
return 1;
}
head = -1, count = 0;
insert(int array, int node)
{
array[count]=node;
if(count == 0)
head=array[count];
count++;
}
int delete(int array, int node)
{
if(count==0)
retuen -1;
int n=0, i=0;
while(array[n]!=node)
n++;
for(i=count-n-1; i<count-1; i++)
array[i] = array[i+1];
count--;
return 1;
}
Not possible unless size is known in advnace
- Anonymous August 19, 2010