I am having trouble getting this program to work. So far I have these methods bu
ID: 3553050 • Letter: I
Question
I am having trouble getting this program to work. So far I have these methods but not sure if they are all correct. Any help with getting the rest and the entire program to work would be great! I especially would like some help with the "removeChar" method and the "reverse" method. PLEASE HELP!! class Node {
private char letter;
private Node next;
public Node(char ch, Node link) {
letter = ch;
next = link;
}
public void setLetter(char letter) {
this.letter = letter;
}
public char getLetter() {
return letter;
}
public void setNext(Node next) {
this.next = next;
}
public Node getNext() {
return next;
}
class Word {
// instance variable pointing to the head of the linked list
private Node head;
// default constructor
public Word() {
head = null;
}
// copy constructor
public Word(Word w) {
this.head = copy(w.head);
}
private static Node copy(Node l){
Node newL;
if (l == null){ //easiest case
newL = null;
}
else {
newL = new Node(l.getLetter(), copy (l.getNext()));
}
return newL;
}
// constructor from a String
public Word( String s ) {
Node pt;
head = null;
for(int i = s.length()-1; i >= 0; i--) {
pt = new Node(s.CharAt(i), head);
head = pt;
}
}
// for output purposes -- override Object version
// no spaces between the characters, no linefeeds/returns
public String toString() {
return toString(head);
}
private static String toString(Node L) {
String Word = "";
if (L == null){ //do nothing
}
else {
Word = L.getLetter() + toString(L.getNext());
}
return Word;
}
// how long is this Word
public int length() {
return length(head);
}
private static int length(Node L) {
int count;
if (L == null)
count = 0;
else
count = 1 + length(L.getNext());
return count;
}
// append ch to the head of this Word
public void addHead( char ch ) {
// append ch to the tail of this Word
public void addTail( char ch ) {
head = addTail(head, ch);
}
private static Node addTail(Node L, int num) {
if(Word == null)
Word = new Node(ch, null);
else
Word.setNext(addTail(Word.getNext(), ch));
return Word;
}
// modify this Word so it is reversed
public void reverse() {
private static Node rev(Node L) {
// remove all occurrences of ch from this Word
public void removeChar( char ch ) {
// remove the first occurrence of the Word w from this Word
public void removeWord(Word w) {
head = removeWord(head, w);
}
private static Node removeWord( Node L, Word w )
{
if(L == null)
{ //do nothing
}
else if(L == w.head){
L = L.getNext();
}
else {
//remove the word
L.setNext(removeWord(L.getNext(), w));
}
return L;
}
// concatenate a copy of s to the end of this Word
public void concatenate( Word s ) {
this.head = concatenate(head, s);
}
private static Node concatenate(Node L, Word s) {
if(L ==null) {
L = null;
}
else {
L = new Node(L.getLetter(), concatenate(L.getNext(), s));
L.setNext(concatenate(L.getNext(), s));
}
return L;
}
// make a copy of every occurrence of ch in this Word
// for example, if this Word is abbcbccb, doubleChar ( 'b' ) should
// change the Word to abbbbcbbccbb
public void doubleChar( char ch ) {
head = doubleChar(head, ch);
}
public static Node doubleChar(Node L, char ch) {
if(L == null)
{
}
else if(L.getLetter() == ch) {
}
else {
L.setNext(doubleChar(L.getNext(), ch));
}
return L;
}
}
}
// who are you?
public static String myName() {
}
Explanation / Answer
"removeChar(char ch )" defined :
public void removeChar( char ch ) //removeChar(char ch) function definition
{
node ptr=this.head,prev=NULL;
while(ptr!=NULL)
{
if(ptr.letter==ch)
{
if(prev==NULL)
{
this.head=this.head.next;
}
else
prev.next=prev.next.next;
}
prev=ptr;
ptr=ptr.next;
}
}
"reverse()" function defined :
public void reverse()
{
node current, nextNode, iterator;
if(head==NULL) //for empty word
return;
current=head;
next= head.next;
iterator=NULL;
while(nextNode != NULL) //reversing
{
current.next = iterator;
iterator= current;
current=nextNode;
nextNode =nextNode.next;
}
head= current;
head.next = iterator;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.