Question

Linked List in Java The data node should be modeled somewhat like this: class node{ int...

Linked List in Java

The data node should be modeled somewhat like this:

class node{

int node iNum;

node next;

}

Write a program that creates a linked list and loads it with the numbers 0 to 9. Start with an empty list and then use a "for loop" to fill it.

Create a linked list class, a node class, etc.

Routines like makeNode and findTail should be methods in the linked list class.

Create a showList function to display the list.

Make a small text menu for the program so allow the user to input how many nodes they want in the list.

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

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Vamsi
*/
class node{

int iNum;

node next;

}
public class LinkedList {
  
node head;
node tail;
//constructor
LinkedList()
{
head=tail=null;
  
}
//method to insert to LinkedList
void insert(int d)
{
if(head==null)
{
head=tail = makeNode(d);
}
else
{
tail.next = makeNode(d);
tail=tail.next;
  
}
}
//method to return tail
int findTail()
{
if(tail!=null)
return tail.iNum;
return -1;//tail is empty
}
  
node makeNode(int d)
{
node n = new node();
n.iNum=d;
n.next=null;
return n;
}
//method to show list
void showList()
{
System.out.println("List: ");
node temp=head;
while(temp!=null)
{
System.out.print(temp.iNum+" ");
temp=temp.next;
}
  
System.out.println();
}
  
//main method
public static void main(String argv[])
{
//creating object
LinkedList l = new LinkedList();
//now adding elements to linked list using for loop
for(int i=0;i<10;i++)
l.insert(i);
l.showList();
//now printing list
  
}
}

output:

run:
List:
0 1 2 3 4 5 6 7 8 9
BUILD SUCCESSFUL (total time: 0 seconds)

Add a comment
Know the answer?
Add Answer to:
Linked List in Java The data node should be modeled somewhat like this: class node{ int...
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
  • Create a linked list with the following features. A Node class that stores the data type...

    Create a linked list with the following features. A Node class that stores the data type of your choice. A constructor that creates a dummy header node. void display() -- A method for printing the list. void add(item) -- A method for adding a value to the beginning of the list void addEnd(item) -- A method of adding a value to the end of the list. bool contains(item) -- A method for finding a specified value in the list. int...

  • Write a java program implementing the Linked list. It should be on an small office who...

    Write a java program implementing the Linked list. It should be on an small office who has 5 employees. The program ask the user for ID, First name, Last name and what field the work in(eg: accounting, programmer, HR etc). Each employee (with all the information of that paticular employee) should be placed in one node in the program. The program should repeat and ask the user for all 5 employees information. Also when you display the Linked list it...

  • #1. Single linked list - find... Extra info/hint? It's free For this problem, I have a...

    #1. Single linked list - find... Extra info/hint? It's free For this problem, I have a complete linked list program which supports the following commands from the user: insert, display, delete, average, find, insertBefore, insertAfter. insert allows them to insert a number into the current list, display will display the numbers in the current list etc. The program is complete except that I have removed the body of the method for find. Given the following class for the nodes in...

  • 117% Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class....

    117% Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class. It should implement a linked list that will hold values of type int. It should use an IntNode class to implement the nodes in the linked list. The linked list must have the following methods: . A constructor with no parameters which creates an empty list. . void add (int data) -adds data to the front of the list. .A constructor IntlinkedList(int[]) which will...

  • Derive a class called Stack from the linked list described in Assignment 2 (list of Dates)....

    Derive a class called Stack from the linked list described in Assignment 2 (list of Dates). This means the Stack class will inherit all the properties (data and functions) of the linked list. But, since a stack only allows pushing and popping at the front of the list only, you will need to prevent the operations at the back. To do this, derive the Stack class in such a way that the base class (LinkedList) functions become private in the...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype...

    Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node    //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...

  • Submit a source.cpp file with the following: struct node { int data; node* next; } a...

    Submit a source.cpp file with the following: struct node { int data; node* next; } a for loop that creates a singly linked list of nodes with the values 0-9 stored in them. a for loop that prints off the data of the singly linked list, each one on its own line ******************************************************************************************

  • Using Java You are given a Node class and a List class: public class Node {...

    Using Java You are given a Node class and a List class: public class Node {    int   data;     Node next; } public class List {     Node first; } You are also given a Stack class. The following functions are available for use: public class Stack { public boolean isEmpty(){}; public void push(int n){}; public int pop(){};} Write a Java method snglyRevToStck that pushes the data found in a linked list t in reverse order into the stack...

  • Create a linked list of at least 15 different values that are prompted for and entered...

    Create a linked list of at least 15 different values that are prompted for and entered while the program is running. Appending a node attaches that node to the end of the list while inserting a node places it in order maintaining a sorted list. Create a menu driven program where you have the options to appended, insert, display, and delete a node. Display the list after first appending data showing it in no specific order, delete all nodes and...

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