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
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

void list::load() { item *current; item *prev; ifstream fin; fin.open("List.txt"); prev = NULL; while(!fin.eof()) { current=new...
#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; /** 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> /* 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 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=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
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 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....
C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes: -head: first node in the list -tail: last node in the list #include "circDLLNode.h" template class { public: //Default constructor: creates an empty list (); //Destructor: deallocate memory ~(); ...
C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...
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...