Linked Lists - CSU
[Pages:20]Linked Lists
Walls and Mirrors Chapter 5
Linked Lists
public class Node { private Object item; private Node next; public Node(int item) {...} public Node(int item, Node next) {...} // getters and setters
}
A linked list is a collection of Nodes:
item next 42
item next -3
item next 17
item next 9 null
The list interface
Method object get(index) index indexOf(object)
add (object) add (index, object)
object remove(index)
object remove(object)
int size() boolean isEmpty() clear()
Returns the element at the given position
Returns the index of the first occurrence of the specified element Appends an element to the list
inserts given value at given index, shifting subsequent values right Removes the element at the specified position (and returns it) Removes the element that corresponds to the given object (and returns it) returns the size of the list
indicates if the list is empty
removes all elements from the list
index is an int, and object is of type Object
Linked List version 1
public class LinkedList { private Node head; private int size;
public LinkedList() { head = null; size = 0;
}
methods go here
}
LinkedList
front = size = 0
Implementing add
How do we add to a linked list?
add at the end of the list add at a given index
item next 42
item next -3
item next 17
item next 9 null
The add method
public boolean add(int index, Object item){ if (indexsize) return false; if (index == 0) { head = new Node(item, head); } else { // find predecessor of node Node curr = head; for (int i=0; i ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- linked lists bu
- linked lists colorado state university
- singly linked list class carnegie mellon university
- linked list example snode class edu
- learning exercise c linked list baumann
- linked list basics stanford university
- linked lists csu
- lecture notes on linked lists carnegie mellon university
- linked lists edu
- linked list classes linked list example virginia tech