in this assignment i have to remove vowels from a string using a linked list. the linked list and link code is from a textbook and cannot be changed if the code alters the data structure. for some reason when i implement this code it gives me a logical error where the program only removes all instances of the first vowel in a string instead of moving through all vowels of the string and removing each one with all instances. i would appreciate if you could tell me the problem and solution in words and not in code. The code is in java. output
in this assignment i have to remove vowels from a string using a linked list. the linked list and link code is from a textbook and cannot be changed if the code alters the data structure. for some reason when i implement this code it gives me a logical error where the program only removes all instances of the first vowel in a string instead of moving through all vowels of the string and removing each one with all instances. i would appreciate if you could tell me the problem and solution in words and not in code. The code is in java.
output
please enter a string.
research
h
c
r
a
e
s
e
r
false
List (first -->last): research
List (first -->last): research
List (first -->last): rsarch
CODE
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;
char keyChar;
System.out.println("please enter a string.");
String inString = input.nextLine();
for(int i = inString.length() - 1; i >= 0; i--)
{
character = inString.charAt(i);
System.out.println(character);
list.insertFirst(character);
}
System.out.println(list.isEmpty());
list.displayList();
list.displayList();
for(int i = 0; i < inString.length(); i++)
{
if(inString.charAt(i) == 'a'|| inString.charAt(i) == 'e'||inString.charAt(i) == 'i'||inString.charAt(i) == 'o'||inString.charAt(i) == 'u')
{
keyChar = inString.charAt(i);
for(int j = 0; i < inString.length(); i++)
{
if(!(list.isEmpty()))
{
list.delete(keyChar);
}
}
}
}
list.displayList();
}
}
LINKED LIST OBJECT
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 delete(char key)
{
link current = first;
link previous = first;
while(current.dData != key)
{
if(current.next == null)
{
return null;
}
else
{
previous = current;
current = current.next;
}
}
if( current == first)
{
first = first.next;
}
else
{
previous.next = current.next;
}
return current;
}
public void displayList()
{
System.out.print("List (first -->last): ");
link current = first;
while(current != null)
{
current.displayLink();
current = current.next;
}
System.out.println("");
}
public link find(char key)
{
link current = first;
while(current.dData != key)
{
if(current.next == null)
{
return null;
}
else
{
current = current.next;
}
}
return current;
}
}
LINK OBJECT
public class link
{
public char dData;
public link next;
public link prev;
public link(char dd)
{
dData = dd;
}
public void displayLink()
{
System.out.print(dData);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps