BCA Linked List Implementation & O Notation

studied byStudied by 2 people
0.0(0)
get a hint
hint

// inserts the specified element at the specified position in this list. existing elements in the list are shifted right. If the specified index is at the end of list, the specified element is appended to the end of the list.

//@throws IndexOutOfBoundsException if index is invalid.

public void add(int index, E e){

1 / 11

Tags and Description

12 Terms

1

// inserts the specified element at the specified position in this list. existing elements in the list are shifted right. If the specified index is at the end of list, the specified element is appended to the end of the list.

//@throws IndexOutOfBoundsException if index is invalid.

public void add(int index, E e){

 public void add(int index, E e) {
if(index > listSize || index < 0){
throw new IndexOutOfBoundsException();
}
Node<E> n = new Node<E>(e);

if(index == 0){
n.next = head;
head = n;

if(listSize == 0){
tail = n;
}
}
else if(index == listSize){
tail.next = n;
tail = n;
}

else{
Node<E> s = head;

for(int i = 0; i<index-1; i++){
s = s.next;
}

n.next = s.next;
s.next = n;
}

listSize++;


}
New cards
2

//clear the entire list

public void clear(){

 public void clear() {
tail = null;
head = null;
listSize = 0;

}
New cards
3

//check whether or not something is contained in the list

public boolean contains(E e){

public boolean contains(E e) {
return indexOf(e) != -1;
}
New cards
4

// return the element at a specified position in a list

public E get(int index){

public E get(int index) {
if(index<0 || index>=listSize){
throw new IndexOutOfBoundsException();
}

Node<E> n = head;
for(int i = 0; i<index; i++){
n = n.next;
}
return n.data;
}
New cards
5

//find the index of a specific element in the list

public int indexOf(E e){

public int indexOf(E e) {
Node<E> n = head;
int pos = 0;
while(n!= null){
if(n.data.equals(e)){
return pos;
}
pos++;
n = n.next;
}
return -1;
}
New cards
6

//check if the list is empty

public boolean is Empty(){

public boolean isEmpty() {
return listSize ==0;
}
New cards
7

//find the last index of an element in a list

public int lastIndexOf(E e){

public int lastIndexOf(E e) {
Node<E> n = head;
int pos = 0;
int ret = -1;
while(n!=null){
if(n.data.equals(e)){
ret = pos;
}
pos++;
n=n.next;
}
return ret;
}
New cards
8

//remove the element at a specific position of a list

public E remove (int index){

public E remove(int index) {
if (index < 0 || index >= listSize) {
throw new IndexOutOfBoundsException();
}
E returnVal = null;

// deleting the only element in the list
if (listSize == 1) {
returnVal = head.data;
head = null;
tail = null;
}

// deleting first element of list having at least 2 elements
else if (index == 0) {
returnVal = head.data;
head = head.next;
} else {
Node<E> n = head;
for (int i = 0; i < index - 1; i++) {
n = n.next;
}
returnVal = n.next.data;
n.next = n.next.next;

// If deleting last node in list, change tail reference.
if (index == listSize - 1) {
tail = n;
}
}

listSize--;
return returnVal;
}
New cards
9

//check if you need to remove an element in a list and remove it

public boolean remove(E e){

public boolean remove(E e) {
int index = indexOf(e);
if(index>=0){
remove(index);
return true;
}
else{
return false;
}
}
New cards
10

//set a specific value to a specific index

public E set(int index, E e){

public E set(int index, E e) {
if(index<0 || index>= listSize){
throw new IndexOutOfBoundsException();
}
Node<E> n = new Node(e);
for (int i = 0; i < index; i++) {
n=n.next;
}
E ret = n.data;
n.data = e;
return ret;
}
New cards
11

//return the size of the list

public int size(){

public int size() {
return listSize;
}
New cards
12

public void add(E e)

public void add(E e) {
add(listSize, e);
}
New cards

Explore top notes

note Note
studied byStudied by 5 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 11 people
Updated ... ago
5.0 Stars(2)
note Note
studied byStudied by 13 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 8 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 4 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 15 people
Updated ... ago
5.0 Stars(3)
note Note
studied byStudied by 8 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 3224 people
Updated ... ago
4.6 Stars(22)

Explore top flashcards

flashcards Flashcard57 terms
studied byStudied by 9 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard66 terms
studied byStudied by 5 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard129 terms
studied byStudied by 5 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard52 terms
studied byStudied by 2 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard39 terms
studied byStudied by 7 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard37 terms
studied byStudied by 31 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard58 terms
studied byStudied by 1335 people
Updated ... ago
5.0 Stars(10)
flashcards Flashcard48 terms
studied byStudied by 31 people
Updated ... ago
5.0 Stars(1)