Commit f4de9c9f authored by Anya Helene Bagge's avatar Anya Helene Bagge 🦆

Legg til lister

parent b1887593
package inf101.v16.datastructures;
public interface IList {
/**
* Legg til et element på slutten av listen.
*
* @param s Elementet som skal legges til
*
* Etterpå vil size() øke med én, og get(size()-1) vil returnere elementet.
*/
void add(String s);
/**
* Fjern et element fra listen
*
* @param i Indeksen til elementet
* @return elementet som ble fjernet
*
* Etterpå vil alle senere indekser i listen flyttes én posisjon frem.
*/
String remove(int i);
/**
* Returner elementet på posisjon i
* @param i
* @return
*/
String get(int i);
boolean isEmpty();
int size();
void set(int i, String s);
}
......@@ -3,25 +3,25 @@ package inf101.v16.datastructures;
public class Main {
public static void main(String[] args) {
IStack stack = new MyStack();
IList list = new MyList();
stack.push("Eple");
stack.push("Banan");
stack.push("Kaffe");
list.add("Eple");
list.add("Banan");
list.add("Kaffe");
while (stack.size() < stack.maxSize())
stack.push("Eple");
while (list.size() < 20)
list.add("Eple");
while( ! stack.isEmpty())
System.out.println(stack.pop());
String kaffe = stack.pop();
String banan = stack.pop();
String eple = stack.pop();
while( ! list.isEmpty())
System.out.println(list.remove(list.size()-1));
String kaffe = list.remove(list.size()-1);
String banan = list.remove(list.size()-1);
String eple = list.remove(list.size()-1);
// // String ingenting = stack.pop();
//
// System.out.println("Kaffe=" + kaffe);
// System.out.println("Banan=" + banan);
// System.out.println("Eple=" + eple);
System.out.println("Kaffe=" + kaffe);
System.out.println("Banan=" + banan);
System.out.println("Eple=" + eple);
}
}
package inf101.v16.datastructures;
import java.util.Arrays;
public class MyList implements IList, IStack {
private int length = 0;
private String data[] = new String[10];
@Override
public void add(String s) {
if(length == data.length) {
System.out.println("Dobler størrelsen av listen!");
//String newData[] = new String[data.length*2];
//for(....) { .... }
data = Arrays.copyOf(data, data.length*2);
}
data[length] = s;
length = length + 1;
}
@Override
public String remove(int i) {
String element = data[i];
for(int x = i; x < length-1; x++) {
data[x] = data[x+1];
}
length = length -1;
return element;
}
@Override
public String get(int i) {
return data[i];
}
@Override
public boolean isEmpty() {
return length == 0;
}
@Override
public int size() {
return length;
}
@Override
public void set(int i, String s) {
data[i] = s;
}
@Override
public void push(String s) {
add(s);
}
@Override
public String pop() {
return remove(size()-1);
}
@Override
public String top() {
return get(size()-1);
}
@Override
public int maxSize() {
return Integer.MAX_VALUE;
}
@Override
public void clear() {
length = 0;
}
}
package inf101.v16.datastructures;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class MyListStackTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void pushPopTest() {
IStack stack = new MyList();
stack.push("Hei");
assertEquals("Hei", stack.pop());
}
@Test
public void pushPopTest2() {
IStack stack = new MyList();
stack.push("Hei");
stack.push("Kaffe");
assertEquals("Kaffe", stack.pop());
assertEquals("Hei", stack.pop());
}
@Test
public void clearTest() {
IStack stack = new MyList();
stack.push("Hei");
stack.push("Kaffe");
stack.clear();
// test
assertTrue(stack.isEmpty());
assertEquals(0, stack.size());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment