Question

In: Computer Science

hi guys, could someone tell my where this is going wrong please, its adding into a...

hi guys, could someone tell my where this is going wrong please, its adding into a double linked list, i have another method to add at the end which is working but the rest of this??

thanks

public boolean add(int index, Token obj) {
       if(obj == null) {
           throw new NullPointerException();
       }
       if (index < 0 || index > this.size()-1) {
           throw new IndexOutOfBoundsException();
       }
       // new node to add
       Node toADD = new Node(null,null,obj);
      
       //insert at the beginning of the list
       if(index == 0) {
          
           // make the next of the new node point to the previous head, and previous to null
           toADD.next = head;
           toADD.prev = null;
           //change the previous of head to the new node
           if(head != null) {
               head.prev = toADD;
           // set the new node to be the head;
           head = toADD;
           size++;
           }
       if(index >1) {
          
           for(int i =1; i<= index; i++ ) {
               if(index == i) {
                   Node oldNode = this.getNode(i);
                   toADD.prev = oldNode.prev;
                   oldNode.prev = toADD;
                   toADD.next = oldNode;
                   toADD.prev.next = oldNode;
                   size++;
                   }
       if(index == this.size()-1) {
           this.add(obj);

Solutions

Expert Solution

I can't See Your Full Code so, i can't Judge where You are doing wrong But to add At From Of doubly linked list simply do This.

 // Adding a node at the front of the list public void push(int new_data) {     /* 1. allocate node     * 2. put in the data */         Node new_Node = new Node(new_data);     /* 3. Make next of new node as head and previous as NULL */     new_Node.next = head;   new_Node.prev = null;   /* 4. change prev of head node to new node */   if (head != null)               head.prev = new_Node;   /* 5. move the head to point to the new node */         head = new_Node; } 

To Add After a Given Node use this method

 /* Given a node as prev_node, insert a new node after the given node */ public void InsertAfter(Node prev_Node, int new_data) {     /*1. check if the given prev_node is NULL */    if (prev_Node == null) {                System.out.println("The given previous node cannot be NULL ");          return;         }       /* 2. allocate node     * 3. put in the data */         Node new_node = new Node(new_data);     /* 4. Make next of new node as next of prev_node */     new_node.next = prev_Node.next;         /* 5. Make the next of prev_node as new_node */         prev_Node.next = new_node;      /* 6. Make prev_node as previous of new_node */         new_node.prev = prev_Node;      /* 7. Change previous of new_node's next node */        if (new_node.next != null)              new_node.next.prev = new_node; } 

Hope This Helps.


Related Solutions

Hi guys, I need someone to look over a some codes. My xcoder telling I have...
Hi guys, I need someone to look over a some codes. My xcoder telling I have error(s), and I need someone to look what causing these errors. I'm leaving the codes that are giving me errors. I don't want to give my whole code away for privacy reasons. Language is C 1. #include #include #include #define MAXCHAR 50 /*Here are the structs for the monsters, regions, and commonality.*/ /*typedef struct was provide in instruction*/ typedef struct monster{ int id; char...
Hi there, I am having difficulty with the attached homework question. If you guys could please...
Hi there, I am having difficulty with the attached homework question. If you guys could please help work through the steps. My balance sheet isn't balancing :/ These financial statement items for Fairview Corporation at year-end, July 31, 2017. Salaries and wages payable $2,080 Salaries and wages expense 57,500 Supplies expense 15,600 Equipment 18,500 Accounts payable 4,100 Service revenue 66,100 Rent revenue 8,500 Notes payable (due in 2020) 1,800 Common stock 16,000 Cash 29,200 Accounts receivable 9,780 Accumulated depreciation-equipment 6,000...
Hi! is there someone famiilar with organic chem that could read and edit my paragraph about...
Hi! is there someone famiilar with organic chem that could read and edit my paragraph about my lab report results. Im having trouble making it flow better, but not make it too wordy. The purpose of this laboratory experiment was to perform an E1 hydration reaction of 2-methylcyclohexanol with phosphoric acid in order to confirm Zaitsev’s rule by generating a mixture of alkene products as depicted in the reaction above. The mixture of products obtained was analyzed via FTIR and...
Can someone please tell me if these calculations are correct! I'm reviewing my notes, and my...
Can someone please tell me if these calculations are correct! I'm reviewing my notes, and my professor said to always multiply the lipids by 3 and then divide by 7 to get the total amount of cals of lipids per day... I'm not completely sure why you do that? Can someone explain. Why don't you just stop at 700 cals for lipids? 1. Calculate the number of calories and grams protein for the following TPN solution: D50W in 500cc 10%...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------ class Robot{ int serialNumber; boolean flies,autonomous,teleoperated; public void setCapabilities(int serialNumber, boolean flies, boolean autonomous, boolean teleoperated){ this.serialNumber = serialNumber; this.flies = flies; this.autonomous = autonomous; this.teleoperated = teleoperated; } public int getSerialNumber(){ return this.serialNumber; } public boolean canFly(){ return this.flies; } public boolean isAutonomous(){ return this.autonomous; } public boolean isTeleoperated(){ return this.teleoperated; } public String getCapabilities(){ StringBuilder str = new StringBuilder(); if(this.flies){str.append("canFly");str.append(" ");} if(this.autonomous){str.append("autonomous");str.append("...
Hi guys, I'm working on an assignment for my Java II class and the narrative for...
Hi guys, I'm working on an assignment for my Java II class and the narrative for this week's program has me a bit lost. I've put the narrative of the assignment in the quotation marks below. " Included with this assignment is an encrypted text file. A simple Caesar cipher was used to encrypt the data. You are to create a program which will look at the frequency analysis of the letters (upper and lowercase) to help “crack” the code...
Hi, please could someone be of assistance with this assignment for me. Thank you The objective...
Hi, please could someone be of assistance with this assignment for me. Thank you The objective of this assignment is to practice the use of control objects in GUI design. You are required to create a 4- function calculator. See pic below. Calculator.png The calculator should have buttons for all 10 numbers, the decimal point, the four operations (+,-,/,*), an equal “=” button, a "main display" (TextBox or RichTextBot), a backspace button and a clear “C” button. When you create...
Hi, I am struggling to understand this worksheet my professor gave us for practice. Could someone...
Hi, I am struggling to understand this worksheet my professor gave us for practice. Could someone make any sense of this? The scenario: Sodium is found largely in the extracellular compartment with concentrations between 130-145 mM with intracellular sodium concentrations between 3.5-5mM. This chemical difference gives sodium a large concentration gradient, which when permitted (by opening of a channel or through facilitated transport) will move down its concentration gradient to enter the cell. Sodium also has a favorable electrical gradient;...
Okay, can someone please tell me what I am doing wrong?? I will show the code...
Okay, can someone please tell me what I am doing wrong?? I will show the code I submitted for the assignment. However, according to my instructor I did it incorrectly but I am not understanding why. I will show the instructor's comment after providing my original code for the assignment. Thank you in advance. * * * * * HourlyTest Class * * * * * import java.util.Scanner; public class HourlyTest {    public static void main(String[] args)     {        ...
Hi guys. What’s happening with bitcoin and ripple price nowadays? It’s seems going not that well...
Hi guys. What’s happening with bitcoin and ripple price nowadays? It’s seems going not that well as before. Is it about to bounce back or even higher than last max. Or fall of empire? And also I need some opinions on what Cryptocurrencies i could pay attention as a long term investment and some for quick tradings? Thnx for attention
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT