TestSinglyLinkedList

TestSinglyLinkedList - /* * Class implements the...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
/* * Class implements the Singly-linked List * CIS3023 Summer 2010 * * @author: CIS3023 UF - Summer 2010 * @creation: 22nd July 2010 */ import java.util.Scanner; i /* Node */ class Node{ int data; Node nextNode; N public Node(int d){ data=d; nextNode=null;} p public String toString(){ String s="{ " + data + " ->}"; return s; } } /* Linked List */ class SinglyLinkedList{ Node head; Node tail; N public SinglyLinkedList(){ this.head=null; this.tail=null; } public boolean isEmpty(){ return head==null; } public Node getHead(){ return head; } public Node getTail(){ return tail; } p public void addToHead(int data) { p Node tmp = new Node(data); if (head==null) {//list is empty head=tmp; tail=tmp; } else { tmp.nextNode=head; head=tmp; } } public void addToTail(int data) { p Node tmp = new Node(data); if(tail==null) {//list is empty tail=tmp; head=tmp; } else {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
tail.nextNode=tmp; tail=tmp; } } public void add(int index, int data){ p Node tmp = new Node(data); N if(head==null && index>0){ System.out.println("Empty list!"); // throw new ListEmptyException("Empty list!"); return; } else if(index == 0){ //insert into empty stack at position 0 System.out.println("Inserting into head. .."); addToHead(data); } else { Node previous = findPrevious(index); tmp.nextNode=previous.nextNode;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/27/2011 for the course CIS 3023 taught by Professor Dobbins during the Summer '08 term at University of Florida.

Page1 / 5

TestSinglyLinkedList - /* * Class implements the...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online