Results 1 to 1 of 1
Thread: JAVA questions
- 10-28-2009, 11:07 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
JAVA questions
a) the following code gives ther partially completed implementation of a Singly Linked List. In this implementation, write the addToHead() method. Note that the instance variables of the Node class are defined as public so you can manipulate them directly.
public class node {
public Object data;
public Node next;
}
public class SinglyLinkedList {
private Node head;
private int size;
public SinglyLinkedList() {
head = null;
size = 0;
}
public void addToHead(Object o){
//complete this method
b)Write the addToTail()method for the SinglyLinkedList class.
public void addToTail(Object o){
//complete this method
c)What is the big-O running time of the addToHead() method?
d)What is the big-O running time of the addToTail() method?
My answer are:
addToHead:
Node node=new Node();
node.data=o;
if(size==0)
head=node;
else{
Node temp=head;
head=node;
node.next=temp;
}
addToTail:
Node node=new Node();
node.data=o;
if(size==0)
head=node;
else{
Node temp=head;
while(temp.next!=null)
temp=temp.next;
temp.next=node;
}
O(1)
O(n)
are those right? thank you
Similar Threads
-
Pls i need help with these 2 Java Questions
By jyde in forum New To JavaReplies: 23Last Post: 11-11-2008, 05:23 AM -
Pls i need help with these 2 Java Questions
By jyde in forum Advanced JavaReplies: 2Last Post: 10-19-2008, 07:33 PM -
Java Questions...
By Xarver in forum New To JavaReplies: 11Last Post: 09-20-2008, 05:37 AM -
Few Questions about java
By Grom in forum NetBeansReplies: 11Last Post: 09-13-2008, 02:26 PM -
few java questions
By hiaslpix in forum New To JavaReplies: 4Last Post: 01-01-2008, 05:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks