Skip to content
Open

Done #32

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 198 additions & 0 deletions src/ArrayIntList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;

public class ArrayIntList implements IntList {

private int size;
private int [] buffer;
public ArrayIntList(){
size=0;
buffer=new int[10];
}
/**
* Prepends (inserts) the specified value at the front of the list (at index 0).
* Shifts the value currently at the front of the list (if any) and any
* subsequent values to the right.
*
* @param value value to be inserted
*/
@Override
public void addFront(int value) {
//shift the present value to right
for(int i= size; i>0; i--){
buffer[i]=buffer[i-1];
}
//add new value at front
buffer[0]=value;
size++;
}

/**
* Appends (inserts) the specified value at the back of the list (at index size()-1).
*
* @param value value to be inserted
*/
@Override
public void addBack(int value) {
//add the value to the end of list
buffer[size]=value;
size++;
}

/**
* Inserts the specified value at the specified position in this list.
* Shifts the value currently at that position (if any) and any subsequent
* values to the right.
*
* @param index index at which the specified value is to be inserted
* @param value value to be inserted
* @throws IndexOutOfBoundsException if the index is out of range
*/
@Override
public void add(int index, int value) {
if(index<0 || index>size){
throw new NoSuchElementException("index out of range ");
}
//shift present elements to right to make space for new
for(int i = size; i>index; i--){
buffer[i]= buffer[i-1];
}
buffer[index]=value;
size++;
}

/**
* Removes the value located at the front of the list
* (at index 0), if it is present.
* Shifts any subsequent values to the left.
*/
@Override
public void removeFront() {
if(size==0){
return; // empty list
}
//move to left ,remove front elemtns
for(int i = 0; i<size-1; i++){
buffer[i]=buffer[i+1];
}
size--;
}

/**
* Removes the value located at the back of the list
* (at index size()-1), if it is present.
*/
@Override
public void removeBack() {
if(size==0){
return;//list empty
}
size--;
}

/**
* Removes the value at the specified position in this list.
* Shifts any subsequent values to the left. Returns the value
* that was removed from the list.
*
* @param index the index of the value to be removed
* @return the value previously at the specified position
* @throws IndexOutOfBoundsException if the index is out of range
*/
@Override
public int remove(int index) {
if (index<0||index>=size){
throw new IndexOutOfBoundsException("index out range");
}
int removedValue = buffer[index];
for(int i=index; i<size-1; i++){
buffer[i]=buffer[i+1];
}
size--;
return removedValue;
}

/**
* Returns the value at the specified position in the list.
*
* @param index index of the value to return
* @return the value at the specified position in this list
* @throws IndexOutOfBoundsException if the index is out of range
*/
@Override
public int get(int index) {
if(index<0|| index>= size){
throw new IndexOutOfBoundsException("index i out of range");
}
return buffer[index];
}

/**
* Returns true if this list contains the specified value.
*
* @param value value whose presence in this list is to be searched for
* @return true if this list contains the specified value
*/
@Override
public boolean contains(int value) {
for(int i=0; i <size; i++){
if(buffer[i]== value){
return true;
}
}
return false;
}

/**
* Returns the index of the first occurrence of the specified value
* in this list, or -1 if this list does not contain the value.
*
* @param value value to search for
* @return the index of the first occurrence of the specified value in this list
* or -1 if this list does not contain the value
*/
@Override
public int indexOf(int value) {
return 0;
}

/**
* Returns true if this list contains no values.
*
* @return true if this list contains no values
*/
@Override
public boolean isEmpty() {
return size==0;
}

/**
* Returns the number of values in this list.
*
* @return the number of values in this list
*/
@Override
public int size() {
return size;
}

/**
* Removes all the values from this list.
* The list will be empty after this call returns.
*/
@Override
public void clear() {
size=0;
}

/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
@Override
public Iterator<Integer> iterator() {
return null;
}
}
191 changes: 191 additions & 0 deletions src/DoublyLinkedIntList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import java.util.Iterator;

public class DoublyLinkedIntList implements IntList {
// private fields

private class Node {
int data;
Node next; // address of the node "after" this one in line
Node prev; // address of the node "before" this one in line

public Node() {
next = null;
prev = null;
}
}

private Node pre;
private Node post;
private int size;

// constructor
public DoublyLinkedIntList() {
// an empty list has 2 sentinel (dummy) nodes that serve as bookends
pre = new Node();
post = new Node();
pre.next = post;
post.prev = pre;
size = 0;
}


@Override
public void addFront(int value) {
Node firstNode=pre.next;

Node newNode = new Node();
newNode.data=value;
newNode.next =firstNode;
newNode.prev=pre;

pre.next=newNode;
firstNode.prev=newNode;
size++;
}

@Override
public void addBack(int value) {
Node theLastOne = post.prev;

// set up my new node and fill it out (data, prev, next)
Node theNewOne = new Node();
theNewOne.data = value;
theNewOne.next = post;
theNewOne.prev = theLastOne;

// go to the end of the list's sentinel, and update it's prev
post.prev = theNewOne;

// go to the node before the new one, and update it's next
theLastOne.next = theNewOne;

size++;

}

@Override
public void add(int index, int value) {
if (index<0||index>size){
throw new IndexOutOfBoundsException("index out of range");
}
Node current = pre;
for(int i =0; i<index;i++){
current=current.next;
}
Node theNewNode = new Node();
theNewNode.data=value;
theNewNode.next=current.next;
theNewNode.prev=current;
//update
current.next.prev=theNewNode;
current.next=theNewNode;
size++;
}

@Override
public void removeFront() {
if (size == 0) {
return;//empty
}
//skip first node
pre.next=pre.next.next;
pre.next.prev=pre;


size--;
}
@Override
public void removeBack() {
if (size > 0) {
// set up a temp variable for convenience
Node theOneToRemove = post.prev;

theOneToRemove.prev.next = post;
post.prev = theOneToRemove.prev;

// optional, but strongly recommended to clean up
theOneToRemove.next = null;
theOneToRemove.prev = null;
theOneToRemove.data = 0;

size--;
}
}

@Override
public int remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("index out range");
}
Node current = pre;
for (int i = 0; i < index; i++) {
current = current.next;
}
int removedValue = current.next.data;
current.next = current.next.next;
current.next.prev = current;
size--;
return removedValue;
}

@Override
public int get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("index out of range");
}
Node current = pre.next;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current.data;
}
@Override
public boolean contains(int value) {
Node current = pre.next;
while (current!=post){
if(current.data==value){
return true;
}
current=current.next;
}
return false;
}

@Override
public int indexOf(int value) {
Node current = pre.next;
int index=0;
while (current!=post){
if (current.data==value){
return index;
}
current=current.next;
index++;
}
return -1;
}

@Override
public boolean isEmpty() {
return size==0;
}

@Override
public int size() {
return size;
}

@Override
public void clear() {
pre.next=null;
post.prev=null;
pre=null;
post=null;
size=0;
}

@Override
public Iterator<Integer> iterator() {
return null;
}
}
Loading