Monday, February 2, 2009

tutorial on linked list

a lot of students studying advanced C++ have some trouble understanding the concept of linked list. this is either due to lack of imagination, or lack of understanding of fundamental operations. but essentially, the problem arises due to students studying basic C++ tend to memorize topics rather than trying to build a deep understanding of the same.

you wouldn't think of memorizing addition, would you? addition is a primitive operation that everyone understands to the bone. a+b refers to the outcome of someone giving you 'b' dollars when you already have 'a' dollars in your purse, or vice versa (i think that concepts are much clearer when money is involved :D). the "vice versa" becomes really important since a+b=b+a which is the commutative property. similarily, it doesn't matter in which order you get the money (a+b)+c = a+(b+c). this is the associative property.

anyways, so much so for kindergarten math. coming to linked lists now. before understanding linked lists, it is necessary to understand pointers. so please refer to my post on pointers if you think that pointers are nasty things planted on earth by the evil cybermen from outer universe. (i'm watching too much dr. who nowadays!)

(once you are happy with pointers, proceed)

a linked list is a linked collection of items of the same type such that first value knows where is the second value stored, the second value knows about the third value and so on. for example, a list of students can be encoded as a linked list (LL). If I want to display the list of students, I need to start from somewhere. Thus, the location of the first student, (to whom no one points - so sad) is important and should be stored somewhere. If the location of the first student is lost, then that of the second student is also lost (since the first student has that info) and so on.
A simple list looks like the following figure. It simply contains a set of integers, not stored in sorted order.
You can view this as ID of students standing in a queue. The person at the front of the queue has ID 8, the next one 3 and so on. It is useful to divide each item (from now on referred to as "node") as containing two parts:

1. data
2. link to next node
Thus we come to our first C++ definition, that of a node. A node is an abstract data type containing value and address of next node. From the discussion on pointers, what contains address of a node -- pointer to a node :) So,

struct node
{
int data; //this can be int, double, even another structure object
node* next;
};
please be clear that the node DOES NOT contain another node, it simply contains address of another node.

Since it's important to have address of the first node somewhere, we call it "head", or "first", or "top" or "front" depending on the situation. I call it head over here. This is implemented as:

node* head;
head = new node;//allocate memory for a node and store it's location in head
head->data=8 //data part of node to which head points becomes 8
head->next=NULL //right now, head does not contain any address, thus it's the only node

If you want to add another node

node* temp=new node;
temp->data=3;
temp->next=NULL;
head->next=temp;

Sorry, the above image is a bit too small, but if you click on it, it will open in a new window/tab and is viewable.

I can also move the "head" around so that the first item changes. This happens when the first person in the queue (head) is served and now is no longer a part of the queue. Look at the following statement:

head=head->next;

the right hand side of this statement (head->next) contains address of temp (87)



so it simplifies to head=87

thus head now contains address 87 rather than the old address 84.
thus head now points to the second node.
thus the second node now becomes the first node :)


what happens to the old first node?!?!?! it is still in the memory. so it you want to remove it from memory, you use:


node* old=head //old points to same location as head (84)
head=head->next;
delete old //delete node contained at address inside old (84)


------------------------------------


Now let us look at some more useful operations on lists.


Operation 1: To check if list is empty or not?


The list is empty if head contains no address. That is, head is NULL. Which also tells us that when we create a list, we should assign NULL to head. Hence, the correct way to initialize a list is:


node* head=NULL;

bool isEmpty(node* head)
{
if(head==NULL)
return true;
else
return false;
}


---------------------------------------

Operation 2: To insert an item at the front of the list


Assuming that the list is containing a set of items where order isn't important. We pass the node pointer by reference (that is the actual head is modified rather than a duplicate copy of head being passed to the function).


We first create a node by reserving memory space.
We contain address of old head in node temp.
We move head so that head now contains address of temp.


void insert(node* &head, int item)
{
node* temp=new node;
temp->data=item;
temp->next=head;
head=temp;
}


Even if head was NULL (inserting item into an empty list), the statement temp->next=head means that temp->next contains NULL which is also correct :)


-----------------------------------


Operation 3: Deleting item from front of list:


void delete(node* &head)
{
if(head!=NULL)
{
node* temp=head;
head=head->next;
delete temp;
}
//if head is already NULL, it means nothing remaining to delete
}


-------------------------------


Operation 4: Traverse through the list (go through the list for some xyz purpose)


void traverse(node* head) //not passing by reference since I don't want to modify actual head
{
while(head)
{
cout<data<<" ";
head=head->next; //please remember the actual head is not modified since a duplicate
//pointer in memory is there, that contains same address as head
}
}


For those of you, who still are freaked out by me modifying "head" in the function, use the following function varient:


void traverse(node* head) //not passing by reference since I don't want to modify actual head
{
node* current=head;
while(current)
{
cout<data<<" ";
current=current->next;
}
}


------------------


Tutorial part 2 coming soon to a cinema near you!


cheers
gaurav

Wednesday, January 28, 2009

recipe for marathi kadi / marathi kadhi

my wife, gunjan, is extremely fond of marathi or maharashtrian kadi. The recipe is quite simple. Here it goes:

Ingredients
Yoghurt - one bowl
Gramflour - one heaped tablespoon
salt - to taste
sugar - 1 heaped tablespoon
green chilli - 3/4
ginger 1 finger sized (my finger :D)
mustard seeds 1/2 teaspoon
curry leaves about 15-20
oil 3 tablespoon
water 1.5ltr

crush the ginger and chilli

mix the yoghurt and gramflour and whisk together gently (you don't want butter to come out of the yoghurt now, do you!?) till no lumps, to which add sugar, salt, and water. mix well till uniform and add crushed ginger and chilli.

heat oil on high in a deap saucepan and add mustard seeds and curry leaves. after 5-10 seconds, add the yoghurt mix and keep stirring till the boil comes. boil for another 5 mins on high heat, stirring regularly, and then turn heat to mid (4 to 5 o'clock) and stir intermittently for 20 mins. Done :)

Hope you enjoy this :p

cheers
gaurav

ACS

I, not-so-recently, applied for skills assessment (essentially that an approval of the fact that i am skilled enough to work in the IT industry) with ACS - Australian Computing Society. The application was lodged on 1st Dec and as of today, not even an officer has been assigned my case. A computing organization, that evaluates your credentials, taking two months (and counting) to assign the case to a person - makes you think, doesn't it :D

Sunday, January 18, 2009

the ABSOLUTE easiest recipe

hi all,

this is a simpler form of the khichdi recipe I posted a couple of weeks back. it's a shove-everything-in-and-just-don't-forget-about-it kind of recipe.

ingredients:
1 bowl low starch rice (preferably basmati)
1 bowl split green moong daal (lentil)
1 big potato - diced (on the bigger side)
1 carrot - diced
4 bowls of water
1/2 teaspoon chilli powder
1/2 teaspoon turmeric powder
1/2 teaspoon cumin powder
4-5 whole dried chillies/ fresh chilli padi (if you like it hot!)
salt to taste

wash the rice and lentil together and get any excess starch off them. mix ALL the ingredients together, cover and cook on high heat for 7 mins, stir, then on 3 o'clock (low heat) for 20 mins.

DONE !!!!

cheers
gaurav

Saturday, January 17, 2009

all in a day's time

this is what i call a "clint" syndrome and occurs everytime you watch a clint eastwood movie. be it million dollar baby or the latest "gran torino". i cry during the movie at least once and think a lot after it, especially about running back and spending more time with family.

Chapter 1: IELTS

anyways, the day started with us waking up to giving IELTS exam. went to macquarie university and funnily, sitting for the exam in the very room I usually deliver undergraduate lectures. the immigration requires EVERYONE except citizens of US, UK, Canada and a few other countries they think can communicate in English. I don't want exemption because I know I can communicate satisfactorily in English. I don't want an exemption because my medium of education during my school, undergrad, postgrad and phd has been English. I don't want an exemption because I have finished PhD from an Australian university. Although, I DO want an exemption on the basis that I already scored 8/9 in IELTS four years back right before coming to Australia.

So does the immigration department really think that four years in Australia may have deteriorated by English-speaking/writing/listening/reading capabilities? There is no flaw or loophole in the legislation, mind you. It's an outright business with the geovernment, British Council, IDP and Cambridge Universities being the beneficiaries. Would it really be so unimaginable to expect the government to waive the English test requirements for anyone who has scored above a certain level?

For example, if the IELTS requirements for a particular kind of visa is 6/9, waive IELTS for anyone who scored 8 or more. Basically, your current score is the same as your previous score given that you have taken the IELTS within the last 3 years AND is one less than your previous score if the last test was taken more than three years back. Of course, the government can charge you for each time you request the scores. This will atleast save a lot of time of a lot of people throughout Australia and rest of the world. Of course, it would put a couple of people out of work, which is, probably, the universal reason for any business one can say (providing jobs to people).

I hope the government stops, what I think, is exploitation of people who want to make Australia home. Let's give everyone a fair go - isn't that what Australia claims to be for?!?

----------------------

Chapter 2: Gran Torino

There's just something about a Clint Eastwood-directed movie that makes me cry :( He is such an emotional-blackmailing, sentiment-exploiting son-of-a-bitch! Anyways, I will just say that the movie was good - watch it to know more about it.

----------------------

Chapter 3: The Gaza "war"

Of course, it's a damn war - 1188 people dead out of which 410 are children - and this is just the Australian media. Isn't that 410 children too many? Isn't that the lives of 410 innocent souls whose crime was to be born to Palestinian or Israeli parents? Israel wants to justify it's war by saying that it is trying to minimize civilians casualities and that Hamas is hiding amongst civilians. Well, if they are - too bad! You can't ethically strike civilian locations even if you know it has a few targets. It's a 50-50 split between real targets and innocent civilians; that's not discrete! that's not carefully selected strikes. Those are the actions of a fed-up team of incapable politicians and army-men who are tired and want to take the easy way out.

I honestly, don't know the accurate history of the middle-eastern conflict and I don't care to know it. What I know, and what every single person in the world should be ashamed of is the innocent people and especially the kids who died in this war. Shame on each one of us who turns a blind eye towards it and shame on Israel for taking this cowardly easy route and shame on Hamas for exposing civilians to their enemies and shame on the US and UK politicians for turning their backs on this strike stating that "Israel has a moral right to defend itself".

It's been a sad couple of decades for the global society with unipolar earth after the long-term manipulation and eventual demise of U.S.S.R. and total world domination - economically and politically by the United States of America. But I see a more balanced world in the next 10 years with China willing to speak up and not fearing the downward effects on it'e economic output and India becoming more visible to the world as well. France, Germany, Brazil and all the other "neutral" countries not letting the USA get away with what it sees as it's moral right.

I just want to say that I miss all the 410 children like my own children. The heart bleeds to know that they won't be playing in the street tomorrow morning, that they won't be banging the doors to announce to their mums that they are hungry, that they won't be sneaking up in the bedroom because they are scared, that they won't be stealing your hearts and defeating you with their smiles. Each kid should grow up to choose a path in life and no parent should have to live with the grief of losing a part of their own. I am sure these children have been taken by God in it's stride and I KNOW that they are in place which HAS TO BE better than this one.

Miss you all...
Gaurav

Saturday, January 3, 2009

recipe for chicken kebabs





hey folks

i recently marinaded some chicken for curry and had excess leftover in the fridge. the kebabs from them turned out pretty darn good! here's the marinade for 500grams of chicken thigh fillet.

cut the chicken in the size of around that of a golf ball

marinade- yoghurt 100grams, turmeric 1 teaspoon, garam masala 1/2 teaspoon (that's it!)

add all the ingredients and mix thoroughly using hands. this is important so that we don't have lumpy yoghurt.

leave in the fridge for 48-72 hours. take out, drain excess fluids, put the kebabs on to the skewers, and serve with sliced onions :)

cheers
gaurav

Wednesday, December 3, 2008

Mutton Biryani


This is a recipe I obtained from some site and reading it, it sounds quite promising.

Mutton Biryani ( Pakistani) 85mins



Hot Lamb Main Course Pakistan Asia
Serves 4-6

Ingredients
240ml/8fl.oz. Ghee or Oil
2 Onions, finely chopped
675g/1-1/2lb Mutton or Lamb, cut into 2.5cm/1 inch cubes
Salt to taste
2 teasp Red Chili Powder
2 teasp Turmeric Powder
1 ½ teasp Black Cumin
4 2.5cm/1-inch pieces Cinnamon Sticks
4 Cloves
4 Cardamom Pods
2 Bay Leaves
2 teasp Ginger Paste
2 teasp Garlic Paste
720ml/24fl.oz. Water
4 Tomatoes, chopped
180ml/6fl.oz. Plain Yoghurt
675g/1-1/2lb Rice
1 tbsp Freshly chopped Coriander
a few Mint Leaves
1 teasp Lemon Juice

Instructions

1. Heat the oil in a large saucepan, add the onions and fry until golden brown.

2. Add the mutton and fry over a high heat for 5 minutes, turning from time to time.

3. Add the Salt, Red Chili Powder, Turmeric Powder, Black Cumin, Cinnamon Sticks Cloves, Cardamom Pods, Bay Leaves, Ginger and Garlic Pastes together with the water, mix well then reduce the heat, partially cover and cook for 45 minutes.

4. Meanwhile, place the rice in another saucepan, cover with 5cm/2-inches of water, bring to the boil then reduce the heat and simmer for 10 minutes only until half cooked. Drain well and set aside.

5. After the 45 minutes cooking time, stir the yoghurt into the meat mixture and cook gently for a further 5 minutes.

6. Place one quarter of the half cooked rice on the bottom of a large saucepan then top with one quarter of the meat mixture. Continue layering until the rice and meat are finished. Sprinkle with the chopped coriander, mint leaves and lemon juice, cover and cook over a medium heat for 5-10 minutes unit rice is completely cooked. Serve hot.