Question

In: Computer Science

Implement the ADT character string as the class LinkedString by using a linked list of characters....

Implement the ADT character string as the class LinkedString by using a linked list of characters. Include the following LinkedString constructors and methods: LinkedString(char[] value) Allocates a new character linked list so that it represents the sequence of characters currently contained in the character array argument. LinkedString(String original) Initializes a new character linked list so that it represents the same sequence of characters as the argument. char charAt(int index) Returns the char value at the specified index. The first character in the linked character string is in position zero. LinkedString concat(LinkedString str) Concatenates the specified linked character string to the end of this linked character string. boolean isEmpty() Returns true if, and only if, length() is 0. int length() Returns the length of this linked character string. LinkedString substring(int beginIndex,int endIndex) Returns a new linked character string that is a substring of this linked character string. Implement LinkedString so that it mimics Java String class. For example, character positions start at zero. Also, keep track of the number of characters in the string; the length should be determined without traversing the linked list and counting.

you should have three files. LinkedString.java , LinkedStringInterface.java and LinkedStringException.java.

Solutions

Expert Solution

Hi. I’ve answered similar question before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

//LinkedString.java

public class LinkedString implements LinkedStringInterface {

      // an inner class representing a single Node

      class Node {

            // stored character

            char character;

            // pointer to the next node

            Node next;

            // constructor taking character and next node

            public Node(char character, Node next) {

                  this.character = character;

                  this.next = next;

            }

      }

      // pointer to head node

      private Node head;

      // number of characters

      private int length;

      // constructor taking char array

      public LinkedString(char[] value) {

            Node temp = null;

            // looping through each character in value

            for (int i = 0; i < value.length; i++) {

                  // initializing head if it is null

                  if (head == null) {

                        // creating a new Node with character=value[i]

                        head = new Node(value[i], null);

                        // pointing temp to head

                        temp = head;

                  } else {

                        // adding new node after temp

                        temp.next = new Node(value[i], null);

                        // advancing temp

                        temp = temp.next;

                  }

                  length++;

            }

      }

      // constructor taking String object

      public LinkedString(String value) {

            Node temp = null;

            // doing the same for String type object

            for (int i = 0; i < value.length(); i++) {

                  if (head == null) {

                        head = new Node(value.charAt(i), null);

                        temp = head;

                  } else {

                        temp.next = new Node(value.charAt(i), null);

                        temp = temp.next;

                  }

                  length++;

            }

      }

      // returns the character at a specified index, throws exception if index is

      // invalid

      public char charAt(int index) {

            if (index < 0 || index >= length) {

                  // invalid

                  throw new LinkedStringException("Invalid index");

            }

            Node temp = head;

            // finding node at index position

            for (int i = 0; i < index; i++) {

                  temp = temp.next;

            }

            // returns the character stored in temp node

            return temp.character;

      }

      // concatenates this LinkedString with another and returns the resultant

      // concatenated LinkedString. Note that this does not alter any existing

      // LinkedString object, rather creates a new one.

      public LinkedString concat(LinkedStringInterface str) {

            // creating a String

            String data = "";

            // appending all characters of this linked string to data

            for (int i = 0; i < length; i++) {

                  data += charAt(i);

            }

            // appending all characters of other linked string to data

            for (int i = 0; i < str.length(); i++) {

                  data += str.charAt(i);

            }

            // returning new linked string created using the string data.

            return new LinkedString(data);

      }

      // returns true if linked string is empty

      public boolean isEmpty() {

            return length == 0;

      }

      // returns the number of characters

      public int length() {

            return length;

      }

      // returns the substring of characters between begin and end

      public LinkedString substring(int beginIndex, int endIndex) {

            if (beginIndex >= length || beginIndex < 0 || endIndex < beginIndex

                        || endIndex >= length) {

                  throw new LinkedStringException("Invalid index/indices");

            }

            // appending all characters between begin and end index to data

            String data = "";

            for (int i = beginIndex; i <= endIndex; i++) {

                  data += charAt(i);

            }

            // creating a new LinkedString using this String

            return new LinkedString(data);

      }

      // returns a String representation of the LinkedString. used for testing

      // purposes

      public String toString() {

            String data = "";

            for (int i = 0; i < length; i++) {

                  data += charAt(i);

            }

            return data;

      }

}

// LinkedStringInterface.java

public interface LinkedStringInterface {

      public char charAt(int index);

      public LinkedStringInterface concat(LinkedStringInterface str);

      public boolean isEmpty();

      public int length();

      LinkedStringInterface substring(int beginIndex, int endIndex);

}

//LinkedStringException.java

public class LinkedStringException extends RuntimeException {

      public LinkedStringException(String msg) {

            super(msg);

      }

}

//Test.java (a simple tester program)

public class Test {

      public static void main(String[] args) {

            // creating LinkedString objects and testing all methods

            LinkedStringInterface str1 = new LinkedString(new char[] { 'H', 'e', 'l', 'l',

                        'o' });

            LinkedStringInterface str2 = new LinkedString("World");

            System.out.println("str1: " + str1);

            System.out.println("str2: " + str2);

            for (int i = 0; i < str1.length(); i++) {

                  System.out.println("str1 charAt(" + i + "): " + str1.charAt(i));

            }

            System.out.println("str1 length(): " + str1.length());

            LinkedStringInterface str3 = str1.concat(str2);

            System.out.println("str3: " + str3);

            System.out.println("str3 isEmpty(): " + str3.isEmpty());

            System.out.println("str3 substring(1,5): " + str3.substring(1, 5));

      }

}

/*OUTPUT*/

str1: Hello

str2: World

str1 charAt(0): H

str1 charAt(1): e

str1 charAt(2): l

str1 charAt(3): l

str1 charAt(4): o

str1 length(): 5

str3: HelloWorld

str3 isEmpty(): false

str3 substring(1,5): elloW


Related Solutions

In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using...
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using the adjacency matrix structure. LANGUAGE IS IN JAVA Comment for any questions Data structures and algorithms
JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Overview: implement the ADT List in Java. This program is meant to the ADT List from...
Overview: implement the ADT List in Java. This program is meant to the ADT List from the ground up In the lecture, we learned how to implement an ADT like the ArrayList you have used in Project 1. With this project, you have the chance to implement an ADT called MyList, which is a simplified replacement for the full-blown ArrayList. Requirements You will implement the MyList ADT according to the following: 1. MyList must implement the List interface. It will...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
In short, you’re going to implement a linked-list class for storing integers, using a provided main...
In short, you’re going to implement a linked-list class for storing integers, using a provided main program to help you interact and test your work. You’ll want to build the linked-list class function by function, working in “Develop” mode to test out each function you write. main.cpp is a read only file linkedlist.h is the file to work on. main.cpp #include #include #include "linkedlist.h" using namespace std; int main() { linkedlist LL; string cmd; int value, key; // // user...
implement the Queue ADT using the linked list approach #include "QueueLinked.h" template QueueLinked::QueueNode::QueueNode(const DataType& nodeData, QueueNode*...
implement the Queue ADT using the linked list approach #include "QueueLinked.h" template QueueLinked::QueueNode::QueueNode(const DataType& nodeData, QueueNode* nextPtr) { } template QueueLinked::QueueLinked(int maxNumber = Queue::MAX_QUEUE_SIZE) { } template QueueLinked::QueueLinked(const QueueLinked& other) { } template QueueLinked& QueueLinked::operator=(const QueueLinked& other) { } template QueueLinked::~QueueLinked() { } template void QueueLinked::enqueue(const DataType& newDataItem) throw (logic_error) { } template DataType QueueLinked::dequeue() throw (logic_error) {    DataType temp;    return temp; } template void QueueLinked::clear() { } template bool QueueLinked::isEmpty() const {    return false; } template...
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue...
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue object and try various operations below. Queue myQueue = new Queue(); myQueue.enqueue(“CPS 123”); myQueue.enqueue(“CPS 223”); myQueue.enqueue(“CPS 323”); myQueue.dequeue(); myQueue.enqueue(“CPS 113”); myQueue.enqueue(“CPS 153”); string course = myQueue.front(); // course should be CPS 223 size = myQueue.size(); // size should be 4 // output course and size
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT