In: Computer Science
Code for LinkList below
class LinkList {
Node llist;
LinkList( int sz ) {
if ( sz <= 0 ) {
llist = null;
}
else {
// start with list of size 1
llist = new Node( "0", null );
Node current = llist; // temp node for loop
// add further nodes
for ( int i=1; i<sz; ++i ) {
// create node and attach it to the list
Node node2Add = new Node( Integer.toString(i), 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 */
Node 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; // no node
Node prev = llist;
Node current = prev.getNext();
if ( current == null ) { // only 1 node
llist = null;
return;
}
while ( current.getNext() != null ) { // more than 1 node
prev = current;
current = current.getNext();
}
prev.setNext( null );
return;
}
// 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();
}
}
}
Code for GNODE below
public class GNode<E> {
// Instance variables:
private E element;
private GNode<E> next;
/** Creates a node with null references to its element and next node. */
public GNode() {
this(null, null);
}
/** Creates a node with the given element and next node. */
public GNode(E e, GNode<E> n) {
element = e;
next = n;
}
// Accessor methods:
public E getElement() {
return element;
}
public GNode<E> getNext() {
return next;
}
// Modifier methods:
public void setElement(E newElem) {
element = newElem;
}
public void setNext(GNode<E> newNext) {
next = newNext;
}
}
CODE
class LinkList {
GNode<String> llist;
LinkList( int sz ) {
if ( sz <= 0 ) {
llist = null;
}
else {
// start with list of size
1
llist = new GNode<>(
"0", null );
GNode<String> current =
llist; // temp node for loop
// add further nodes
for ( int i=1; i<sz; ++i )
{
// create node and attach it to the
list
GNode<String> node2Add = new GNode<>(
Integer.toString(i), 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 */
GNode<String> 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; // no node
GNode<String> prev = llist;
GNode<String> current = prev.getNext();
if ( current == null ) { // only 1 node
llist = null;
return;
}
while ( current.getNext() != null ) { // more than 1
node
prev = current;
current =
current.getNext();
}
prev.setNext( null );
return;
}
// 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();
}
}
}