In Java, write the method add(int i, E x) to add a value x in a node after the first i existing data nodes?
-
This problem refers to a doubly linked node ; list with a head sentinel node and a tail sentinel. The data member of the head and tail nodes is set to null and these nodes are not used to store data. Assume the Node class is package myutil; class Node<E> { E data; Node prev, next; Node(E d) { data = d; } Node() { this(null); } } and the (partially shown) MyList class is package myutil; public class List<E> { private Node<E> head, tail; private int sz; public List() { head = new Node<E>(); tail = new Node<E>(); head.next = tail; head.prev = null; tail.next = null; tail.prev = head; sz = 0; } public void addLast(E x) {...} public void add(int i, E x) { ... } public E removeFront() {...} public E remove(int i) {...} public int size() { return sz; } } Write the method add(int i, E x) to add a value x in a node after the first i existing data nodes. For i = 0, add(0, x) would add x in a node after the head sentinel as the new first data node. If (i < 0) or (i > size()) add(int i, E x) should throw an IndexOutOfBoundsException.
-
Answer:
Uhm I just started learning how to use nodes yesterday but I'm pretty sure you would do it like this. You said that the head and tail nodes are set to null but they are not o.0. And I am pretty sure you would need the head node. public void add( int i, E x){ Node<E> node = head; for (int b = 1; b < i; b++){ // goes through i amount of nodes node = head.next; } Node<E> temp = node.next; //temporarily stores what was supposed to come next node.next = new Node(x); //adds a new node where it should go node.next.next = temp; }
Igotaque... at Yahoo! Answers Visit the source
Related Q & A:
- How to add primary key from multiple table as a foreign key in a table in sql server 2008?Best solution by stackoverflow.com
- How to add a new table to a data source?Best solution by technet.microsoft.com
- Does anyone know how I can get bruins tickets and train tickets as a package deal and where I could do this?Best solution by Yahoo! Answers
- What should I do to prepare for a job interview at a grocery store?Best solution by answers.yahoo.com
- How can I add a clickable link to a question or answer on yahoo?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.