Skip to content

Instantly share code, notes, and snippets.

@robbles
Forked from anonymous/gist:1963995
Created March 3, 2012 03:02
Show Gist options
  • Save robbles/1964023 to your computer and use it in GitHub Desktop.
Save robbles/1964023 to your computer and use it in GitHub Desktop.
StringADT.java
public class StringADT implements ADT{
private Node head;
public StringADT(){
head = null;
}
//allowed using charAt(i)
public void concat(StringADT list1, StringADT list2){
//Precondition: the lists exist or are null.
/*Postcondition: the implicit parameter this points to the
concatenation of list1 and list2; the nodes of the concatenated are separate
from the nodes of list1 and list2.*/
// head -> null
// head -> list1 -> ... -> null
// head -> list1 -> ... -> list2 -> ... ->null
Node newList = new Node();
head = newList;
Node node;
if(list1 != null) {
node = list1.head;
while(node != null) {
newList.value = node.value;
newList.next = new Node();
newList = newList.next;
node = node.next;
}
}
if(list2 != null) {
node = list2.head;
while(node != null) {
newList.value = node.value;
newList.next = new Node();
newList = newList.next;
node = node.next;
}
}
}
public void copy(StringADT list1){
Node newList = new Node();
head = newList;
Node node;
if(list1 != null) {
node = list1.head;
while(node != null) {
newList.value = node.value;
newList.next = new Node();
newList = newList.next;
node = node.next;
}
}
//Precondition: the list exists or is null .
//Postcondition: this points to a a copy of list1.
//The nodes of the copied list are separate from the nodes of list1.
}
public void repeats(StringADT list1 ,int n){
Node newList = new Node();
head = newList;
Node node;
for(int i = 1; i<=n; i++){
if(list1 != null) {
node = list1.head;
while(node != null) {
newList.value = node.value;
newList.next = new Node();
newList = newList.next;
node = node.next;
}
}
}
//Precondition: n >= 0.
/*Postcondition: this points to the concatenation of n copies of
list1. If n = 0, then the copied list is the empty string represented by a
linked list consisting of only the null pointer.*/
}
public void assign(String list1){
//Precondition: None. (Notice that list1 is a Java string.)
//Postcondition: this points to the link list
//representing the string as list1.
Node newList = new Node();
head = newList;
for(int i=0; i<list1.length() - 1; i++) {
char c = list1.charAt(i);
newList.value = c;
newList.next = new Node();
newList = newList.next;
}
newList.value = list1.charAt(list1.length() - 1);
}
public void substring(StringADT list1, int x, int y){
Node newList = new Node();
head = newList;
if(x>=0){
if(y>=x)
if(list1.leng()>y){
for(int i=x; i<=y; i++){
}
}
}
//Precondition: 0<= x <= y < leng(list1).
/*Postcondition: this points to the link list representing the
substring of list1 that goes from the xth indexed character to the yth indexed
character.*/
}
public int leng()
{
int length = 0;
Node node = head;
while(node != null) {
length++;
node = node.next;
}
return length;
//Precondition: None.
//Postcondition: Returns the number of characters of list1.
}
public String toString(){
//Precondition: list1 exists or is null
//Postcondition: Displays the characters of list1 on the terminal.
String output = "";
Node node = head;
while(node != null) {
output = output + node.value;
node = node.next;
}
return output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment