Thursday, October 11, 2012

Linked List

Assalamualaikum. Peace be upon you, sadiqi and sadiqiah (my friend).

We were given a lab assignment involving building custom linked list and apply it. I was quite furious because I took way too long compared to previous 2 lab. Ah... and a good morning start this morning where I have the whole morning free-with Allah's will- help me build it from scratch and complete it. Well, I forgot the odd & even part, but that is easy after everything else.

Lab Exercise 3 Questions:
First, building the Node class. I found a Tutorial in D.I.C. and I like it. I want to try explaining myself. LinkedList have basic unit of node. This Node consists of 2 elements of data/info and link. Data/info store the data or information of that particular node. While link is a reference variable to store the address of next node in the chain. link will be null otherwise.

Node class:
For this specific assignment, I use int to store data. The best way would be using Object data type. Then I made an application as what exercise 1 want.

Exercise 1:


Then I made LinkedList. I made it for each method, and test each exercise. First I made public variables so I can made the right one. But this one, I change to private variables class so it needed getter.

LinkedList use Node as its units. They are all connected using link reference variable. And there are head, tail and current. Head is a reference variable to always store address of first element. Tail will be a reference variable of same type to always store address of last element. Current is mainly use to iterate/move along the LinkedList when required. These are needed to perform operation to the LinkedList. counter variable store the size of the LinkedList. It reduce the time-complexity of a method, I'm sorry I don't remember which one.

isEmpty() check of empty LinkedList by two way. See if counter == 0 or head==null.
getFirst() is simply returning the address stored in head to get first element.
getNext() will involve current or temporary variable. return current.link to return the next element address.

When adding element, if it's empty, it always about head and tail point to the newNode. Because the add method in this exercise are set to be front and back only, quite easy compared to other add method.

Remove element is as easy as setting the link reference to other element and set the element link as null. RemoveAtFront is easy because can use head. But RemoveAtBack requires tranversal to almost end of the list.


LinkedList:


Exercise 2:


Exercise 4:



Exercise 8:



No comments:

Post a Comment