Question
I have my code working for the array section but I have a problem in the singly linked list that I don't know what it
I have my code working for the array section but I have a problem in the singly linked list that I don't know what it is if it's having trouble deleting or finding the position of a character. The correct output should be "Go to https://tinyurl.com/y7ao5oj2". But I get this as my output . I just need it to output the correct output in the " " above.
Heres my code in java.
/** The interface for our List (Abstract Data Type) */ interface IList { /** Adds the given value to the end of the list */ void append(char value); /** Adds the given value to the beginning of the list */ void prepend(char value); /** Deletes the container at the given position (a container holds a value) */ void deleteAt(int position); /** Returns the number of values currently in our list */ int size();
/** Retrieves the value at the given position */ char getValueAt(int position);
/** Searches for the FIRST occurence of a given value in our list. * If found, it returns the position of that value. * If not found, it returns -1 */ int positionOf(char value); }
/** Array implementation of our List */ class ListAsArray implements IList { // initialize array to a size of 30 elements // this will prevent the need to resize our array char array[] = new char[30]; int size = 0; // Adds the characters to the end of the array public void append(char value) { if (size == 0) { array[0] = value; } else { array[size] = value; } size += 1; } // Adds the characters to the front of the array public void prepend(char value) { for (int i = size + 1; i > 0; i--) { array[i] = array[i - 1]; } array[0] = value; size++; } // Deletes values at a certain position public void deleteAt(int position) { for (int i = position; i
/** Singly Linked List implementation of our List */ class ListAsLinkedList implements IList { Node cur; Node head; Node tail; int size = 0; // Adds the characters to the end of the list public void append(char value) { if (head == null) { cur = new Node(); cur.setData(value); head = tail = cur; } else { cur = tail; Node temp = new Node(); cur.setNext(temp); cur = cur.getNext(); tail = cur; tail.setData(value); } size += 1; } // Adds the characters to the front of the list public void prepend(char value) { if (head == null) { cur = new Node(); cur.setData(value); System.out.println(cur); head = tail = cur; System.out.println(head); System.out.println(cur); } else { Node temp = new Node(); temp.setNext(head); head = cur = temp; head.setData(value); } size += 1; } // Deletes values at a certain position public void deleteAt(int position) { cur = head; if (position
/** A singly linked list node for our singly linked list */ class Node { private char data; private Node next; // Sets the node public Node() { data = 0; next = null; } // Gets the value or character from the node public char getData() { return data; } // Gets the next node in the list public Node getNext() { return next; } // Sets the data public void setData(char data) { this.data = data; } // Sets what is linked next public void setNext(Node next) { this.next = next; } }
/** contains our entry point */ public class Main { /** entry point - DO NOT CHANGE the pre-existing code below */ public static void main(String[] args) { int[] numbers = {105,116,112,115,65,58,47,47,116,105,110,121,88,117,114,108,46,99,111,109,47}; int[] numbers2 = {97,59,111,53,33,111,106,42,50}; int[] numbers3 = {116,104,32,111,116,32,111,71}; /// List as an Array IList array = new ListAsArray(); // add values for(int num : numbers) { array.append((char)num); } for(int num : numbers3) { array.prepend((char)num); } // delete some values int position; position = array.positionOf((char)105); array.deleteAt(position); position = array.positionOf((char)65); array.deleteAt(position); position = array.positionOf((char)88); array.deleteAt(position); // print em position = 0; while (position
// delete some values position = linkedList.positionOf((char)59); linkedList.deleteAt(position); position = linkedList.positionOf((char)33); linkedList.deleteAt(position); position = linkedList.positionOf((char)42); linkedList.deleteAt(position); // print em position = 0; while (position Go to https://tinyurl.com/ya; 05!oj
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started