In: Computer Science
LINKLIST CLASS BELOW
/**
* Builds a singly linked list of size 5 and prints it to the
console.
*
* @author Jochen Lang
*/
class LinkList {
DNode llist;
LinkList( int sz ) {
if ( sz <= 0 ) {
llist = null;
}
else {
// start with list of size 1
llist = new DNode( "0", null, null );
DNode current = llist; // temp node for loop
// add further nodes
for ( int i=1; i //
create node and attach it to the list
DNode node2Add = new DNode(
Integer.toString(i), null, null );
current.setNext(node2Add); // add
first node
current=node2Add;
}
}
}
/**
* Print all the elements of the list assuming that they are
Strings
*/
public void print() {
/* Print the list */
DNode current = llist; // point to the first
node
while (current != null) {
System.out.print((String)current.getElement() + "
");
current = current.getNext(); // move to the next
}
System.out.println();
}
public void deleteFirst() {
if ( llist != null ) {
llist = llist.getNext();
}
}
public void deleteLast() {
}
// create and display a linked list
public static void main(String [] args){
/* Create the list */
LinkList llist = new LinkList( 5 );
/* Print the list */
llist.print();
/* delete first and print */
llist.deleteFirst();
llist.print();
/* delete last and print 5 times */
for ( int i=0; i< 5; ++i ) {
llist.deleteLast();
llist.print();
}
}
}
DNODE CLASS BELOW
public class DNode { private Object element; private Node next; private Node prev; Node() { this(null, null, null); } Node(Object e, Node n, Node p) { element = e; next = n; prev = p; } public void setElement(Object newElem) { element = newElem; } public void setPrev(Node newPrev) { prev = newPrev; } public void setNext(Node newNext) { next = newNext; } public Object getElement() { return element; } public Node getNext() { return next; } public Node getPrev() { return prev; } }
Working fine now with DNode
/**
* Builds a singly linked list of size 5 and prints it to the console.
*
* @author Jochen Lang
*/
public class LinkList {
DNode llist;
LinkList(int sz) {
if (sz <= 0) {
llist = null;
} else {
// start with list of size 1
llist = new DNode("0", null, null);
DNode current = llist; // temp node for loop
// add further nodes
for (int i = 1; i <= 5; ++i) { // create node and attach it to the list
DNode node2Add = new DNode(Integer.toString(i), null, null);
current.setNext(node2Add); // add first node
current = node2Add;
}
}
}
/**
* Print all the elements of the list assuming that they are Strings
*/
public void print() {
/* Print the list */
DNode current = llist; // point to the first node
while (current != null) {
System.out.print((String) current.getElement() + " ");
current = current.getNext(); // move to the next
}
System.out.println();
}
public void deleteFirst() {
if (llist != null) {
llist = llist.getNext();
}
}
public void deleteLast() {
if (llist == null)
return;
else {
DNode temp = llist;
while (temp.getNext().getNext() != null)
temp = temp.getNext();
temp.setNext(null);
}
}
// create and display a linked list
public static void main(String[] args) {
/* Create the list */
LinkList llist = new LinkList(5);
/* Print the list */
llist.print();
/* delete first and print */
llist.deleteFirst();
llist.print();
/* delete last and print 5 times */
for (int i = 0; i < 5; ++i) {
llist.deleteLast();
llist.print();
}
}
}
===============================================================================
// DNODE CLASS BELOW, Modified DNODE Class as well
public class DNode {
private Object element;
private DNode next;
private DNode prev;
DNode() {
this(null, null, null);
}
DNode(Object e, DNode n, DNode p) {
element = e;
next = n;
prev = p;
}
public void setElement(Object newElem) {
element = newElem;
}
public void setPrev(DNode newPrev) {
prev = newPrev;
}
public void setNext(DNode newNext) {
next = newNext;
}
public Object getElement() {
return element;
}
public DNode getNext() {
return next;
}
public DNode getPrev() {
return prev;
}
}
======================================================================
Thanks, PLEASE COMMENT if there is any concern.