Linked List
Description
This applet demonstrates the basic operations on a linked list data structure.
A linked stack, and a linked queue are special cases of this linked list.
- Ins_Front: Inserts an element at the beginning of the list. The applet inserts a randomly generated value,
when this button is clicked. This operation takes O(1) time.
- Ins_Rear: Inserts an element at the rear end of the list. The applet inserts a randomly generated value,
when this button is clicked. This operation takes only O(1) time, if a separate pointer to the last node is stored.
- Del_Front: Deletes the element at the beginning of the list. This operation takes only O(1) time.
- Search: To search for a value in the list, type the value in the text field, and click the "Search" button.
A linear search through the list takes O(n) time.
Code
(A list node)
public class Node{
int data;
Node next;
}
|
Code
(List operations)
public void insertEnd(int val){
Node n = new Node();
if(first==null) first=n;
else last.next=n;
n.data=val;
last=n;
}
public void insertBegin(int val){
Node n = new Node();
n.next=first;
if(first==null)last=n;
first=n;
n.data=val;
}
public void deleteBegin(){
if(first==null)return;
first=first.next;
}
public boolean isEmpty(){
return (first==null)? true: false;
}
|
Data Structures and Algorithms
Java Applets Centre
R. Mukundan
Department of Computer Science
University of Canterbury
Private Bag 4800, Christchurch
New Zealand.