for some reason in the list object when i try to insert the code into a linked list it inserts the characters backwards instead of forwards. I have to input characters from a user created string individually into a linked list. the linked list cannot change because the teacher requires me to use the specified code in my textbook. i would appreciate if you would explain the problem and solution in words and not code. the code is in Java is there a way to get this program to output correctly? OUTPUT please enter a string. coppermakerimprint List (first -->last): tnirpmirekamreppoc MAIN FUNCTION import java.util.Scanner; /** * Write a description of class test here. * * @author (your name) * @version (a version number or a date) */ public class test { // instance variables - replace the example below with your own public static void main(String[] args) { Scanner input = new Scanner(System.in); linkedList list = new linkedList(); char character; System.out.println("please enter a string."); String inString = input.nextLine(); for(int i = 0; i < inString.length(); i++) { character = inString.charAt(i); list.insertFirst(character); } list.displayList(); } } LINKED LIST OBJECT /** * Write a description of class linkedList here. * * @author (your name) * @version (a version number or a date) */ public class linkedList { private link first; public linkedList() { first = null; } public boolean isEmpty() { return (first==null); } public void insertFirst(char id) { link newLink = new link(id); newLink.next = first; first = newLink; } public link deleteFirst() { link temp = first; first = first.next; return temp; } public void displayList() { System.out.print("List (first -->last): "); link current = first; while(current != null) { current.displayLink(); current = current.next; } /** * Write a description of class link here. * * @author (your name) * @version (a version number or a date) */ public class link { public char dData; public link next; public link(char dd) { dData = dd; } public void displayLink() { System.out.print(dData); } } System.out.println(""); } } LINK OBJECT
for some reason in the list object when i try to insert the code into a linked list it inserts the characters backwards instead of forwards. I have to input characters from a user created string individually into a linked list. the linked list cannot change because the teacher requires me to use the specified code in my textbook. i would appreciate if you would explain the problem and solution in words and not code. the code is in Java is there a way to get this program to output correctly?
OUTPUT
please enter a string.
coppermakerimprint
List (first -->last): tnirpmirekamreppoc
MAIN FUNCTION
import java.util.Scanner;
/**
* Write a description of class test here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class test
{
// instance variables - replace the example below with your own
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
linkedList list = new linkedList();
char character;
System.out.println("please enter a string.");
String inString = input.nextLine();
for(int i = 0; i < inString.length(); i++)
{
character = inString.charAt(i);
list.insertFirst(character);
}
list.displayList();
}
}
LINKED LIST OBJECT
/**
* Write a description of class linkedList here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class linkedList
{
private link first;
public linkedList()
{
first = null;
}
public boolean isEmpty()
{
return (first==null);
}
public void insertFirst(char id)
{
link newLink = new link(id);
newLink.next = first;
first = newLink;
}
public link deleteFirst()
{
link temp = first;
first = first.next;
return temp;
}
public void displayList()
{
System.out.print("List (first -->last): ");
link current = first;
while(current != null)
{
current.displayLink();
current = current.next;
}
/**
* Write a description of class link here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class link
{
public char dData;
public link next;
public link(char dd)
{
dData = dd;
}
public void displayLink()
{
System.out.print(dData);
}
}
System.out.println("");
}
}
LINK OBJECT
Trending now
This is a popular solution!
Step by step
Solved in 3 steps