Question

void list::load() { item *current; item *prev; ifstream fin; fin.open("List.txt"); prev = NULL; while(!fin.eof()) { current=new...

void list::load()
{
    item *current;
    item *prev;
    ifstream fin;

    fin.open("List.txt");


    prev = NULL;
    while(!fin.eof())
    {
        current=new item;
        if (first == NULL) // we set first the first time
            first = current;
        fin>>current->name;
        current->previous=prev;
        current->next = NULL;
        if (prev != NULL)
            prev->next=current;
        prev = current; // we store the current as the prev for next iteration
    }

    fin.close();
}

Translate to Java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have written entire java code to test the code, you can only use the load() function inside list class for your question

Output:

CODE(contians comments explaining the code):

import java.io.File;

import java.util.Scanner;

//item class (this is a blueprint of doubly linked list node)

class item

{

item next;

item previous;

String name;

}

//list driver class

public class list

{

//first is the refernce to head of the list, initialized to null

item first=null;

//answer for your question

public void load()

{

//refernces current and prev of type item(pointing to doubly linked list's node)

item current,prev=null;

//open the file

File f=new File("List.txt");

try

{

//this must be in try-catch block, reading from a file

Scanner sc=new Scanner(f);

//loop until the end of the file

while(sc.hasNext())

{

//create a new node

current=new item();

//if head is null, newly created node is the head

if(first==null)

{

first=current;

}

//set the name of the current node as the value read from the file

current.name=sc.next();

//current's prev points to previous node

current.previous=prev;

//current is the last node so far, so current->next is null

current.next=null;

//prev will be null when loop runs first time, and null.next will result in exception

//so this condtion

if(prev!=null)

{

//previous node's next is newly created current node

prev.next=current;

}

//newly created node becomes the previous node

prev=current;

}

}

catch (Exception e)

{

//TODO: handle exception

}

}

//to display the list

public void display()

{

//loop until the end of the list and print each node's item

item f=first;

while(f!=null)

{

System.out.print(f.name+" ");

f=f.next;

}

System.out.println(" ");

}

//main function to test the result

public static void main(String[] args)

{

//create a list

list l=new list();

//load nodes

l.load();

//display the list

l.display();

}

}

Screenshot of the answer

Add a comment
Know the answer?
Add Answer to:
void list::load() { item *current; item *prev; ifstream fin; fin.open("List.txt"); prev = NULL; while(!fin.eof()) { current=new...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {...

    #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {     Contact item;     NodePtr next;     friend class ContactList; }; class ContactList { public:     ContactList(char* clfile) ;     ~ContactList();     void display       (ostream & output) const;     int   insert        (Contact record_to_insert);     int   insert        (ContactList contact_list);     int   remove        (Contact record_to_delete);     int   size          () const;     int   save          () const;     void find_by_lname (ostream & output, string lname) const;     void...

  • Language is in java. package com.cse.ds; public class Song<E> { E data; Song next; Song prev;...

    Language is in java. package com.cse.ds; public class Song<E> { E data; Song next; Song prev; /** Constructor to create singleton Song */ public Song(E element) { } /** Constructor to create singleton link it between previous and next * @param element Element to add, can be null * @param prevSong predecessor Song, can be null * @param nextSong successor Song, can be null */ public Song(E element, Song prevSong, Song nextSong) { //To be implemented } /** Remove this...

  • In def c++ IDE correct mistake and screenshot output ------------------------------------------------------------------------------ #define NULL 0 #include <iostream> /*...

    In def c++ IDE correct mistake and screenshot output ------------------------------------------------------------------------------ #define NULL 0 #include <iostream> /* Name: Rabia Saad Al-wethnani ID: 43503535 Section: 2486 */ using namespace std; struct Node { int item; //data Node *next; //pointer to the next node in the list }; Node* createNode(int); int isPresent(Node*, int); void appendNode(Node*&, int); void displayList(Node*); void printLists(Node*, Node*); void swapNodes(Node* &head, int s); void findIntersection(Node* first, Node* P); void findUnion(Node* first, Node* P); int main() { Node* L = NULL,...

  • JAVA: * You may not add any fields to the node or list classes. * You...

    JAVA: * You may not add any fields to the node or list classes. * You may not add any methods to the node class. * Do not change any 'Utility' methods that I have included. * You may NOT use any arrays or other Java classes ~ Testing purposes ~ public class DSIList {    Node first,last;        // three instance variables    int N;                   // maintain the size of the list    static class Node...

  • Public void add(linked list, int data){ Node node = new Node(data); node.next=null; node.prev=nul...

    public void add(linked list, int data){ Node node = new Node(data); node.next=null; node.prev=null; if(list.head==null){ list.head=node; } else{ Node n = list.head; Node temp = list.head; while(n.next!=null){ n=n.next; n.prev=temp; temp=temp.next; } n.next=node; node.prev=n; System.out.println(node.prev + " " + n.data); } } How can i turn this doubly linked list into a circular doubly linkedlist, using java

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • 1. void raw_push_front(const Object &x) { // insert x at the head of the list *without*...

    1. void raw_push_front(const Object &x) { // insert x at the head of the list *without* using the iterator classes // Place your code here. } 2. void raw_push_back(const Object &x) { // insert x at the tail of the list *without* using the iterator classes // Place your code here. } #ifndef LIST_H #define LIST_H #include <algorithm> using namespace std; template<typename Object> class List { private: // The basic doubly linked list node. // Nested inside of List, can...

  • C programming A linked list is a linear data structure that allows us to add and remove items fro...

    c programming A linked list is a linear data structure that allows us to add and remove items from the list very quickly, by simply changing a few pointers. There are many different variations of linked lists. We have studied the doubly-linked, circular, with a dummy-header-node version of a linked list. In the class notes we studied several functions to manipulate a Linked List. For this assignment you must write the code for the following additional linked list functions: addFirst,...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT