0% found this document useful (0 votes)
56 views

Linked List

This document describes a Linked List class that implements a ListInterface. The Linked List class contains methods like add, remove, display and numberOfElements. It uses Node objects with references to the next Node. The add method adds a new node to the head of the list. The remove method removes a node at a given position. The display method prints all elements by traversing from head to tail. The numberOfElements method counts the nodes and returns the size of the list.

Uploaded by

Vam Si
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Linked List

This document describes a Linked List class that implements a ListInterface. The Linked List class contains methods like add, remove, display and numberOfElements. It uses Node objects with references to the next Node. The add method adds a new node to the head of the list. The remove method removes a node at a given position. The display method prints all elements by traversing from head to tail. The numberOfElements method counts the nodes and returns the size of the list.

Uploaded by

Vam Si
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

JBIET M.Tech.

CSE Subject : APS


Public interface ListInterface {
public void display() ;
public boolean add(Object newEntry) ;
public Object remove(int givenPosition) ;
public void bubbleort();
public int numberOfElements();
!
public class "ode {
private Object item;
private "ode ne#t;
public "ode(Object newItem) {
item $ newItem;
ne#t $ null;
! %% end constructor
public "ode(Object newItem&"ode ne#t"ode) {
item $ newItem;
ne#t $ ne#t"ode;
! %% end constructor
public void setItem(Object newItem) {
item $ newItem;
! %% end setItem
public Object getItem() {
return item;
! %% end getItem
public void set"e#t("ode ne#t"ode) {
ne#t $ ne#t"ode;
! %% end set"e#t
public "ode get"e#t() {
return ne#t;
! %% end get"e#t
! %% end class "ode
public class Lin'edList implements ListInterface{
public "ode (ead;
public void display(){
for ("ode curr $ (ead; curr )$ null; curr $curr*get"e#t())
ystem*out*println(curr*getItem());
!
public boolean add(Object newEntry){
"ode t $new "ode(newEntry)
t*set"e#t((ead);
(ead*set"e#t(t);
!
public int numberOfElements(){
int i $ +;
"ode curr;
for (curr $ (ead; curr )$ null; &i,,)
curr $curr*get"e#t()
return i;
!
KVR Rao Page 1 of2
JBIET M.Tech. CSE Subject : APS
public Object remove(int givenPosition)
t(rows ListOutOf-oundsE#ception{
int i $ .;
"ode curr$(ead& prev;
if(givenPosition$$.) {
(ead$curr*ne#t;
return;
!
for (curr $ (ead; (curr )$ null) // (i0givenPosition); &i,,){
prev$ curr;
curr $curr*ne#t;
!
if (curr$$null)
t(row new ListOutOf-oundsE#ception(1t(at many items are not availble2);
prev*set"e#t(curr*get"e#t);
return curr*getItem();
!
public void bubbleort(){
"ode curr& prev$(ead;
curr*ne#t$prev*ne#t;
do
boolean one3orePass$false;
for (;curr)$null;){
if curr*item0prev*item {
one3orePass$true;
Object tmp$curr*getItem();
curr*setItem(prev*getItem());
prev*setItem(tmp);
!
!
w(ile one3orePass ;
!
KVR Rao Page 2 of2

You might also like