CSC212 Lab 6 Solutions 2014

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 13:04, 1 October 2014 (EDT)



Problem 1


class IntSLLNode {
	public int info;
	public IntSLLNode next;
	public IntSLLNode( int el ) { this( el, null ); }
	public IntSLLNode( int el, IntSLLNode n ) { info = el; next = n; }
}

public class Lab6_1 {

	public static void main(String[] args) {
		IntSLLNode head, tail;
		head = null;
		tail = null;
		
		head = new IntSLLNode( 5, head );
		tail = head;
		
		head = new IntSLLNode( 10, head );
		
		tail.next =  new IntSLLNode( 3, null );
		tail = tail.next;

		for ( IntSLLNode it = head; it  != null; it = it.next ) {
			System.out.println( it.info );
		}
	}
}


Problem 2


class IntSLLNode {
		public int info;
		public IntSLLNode next;
		public IntSLLNode( int el ) { this( el, null ); }
		public IntSLLNode( int el, IntSLLNode n ) { info = el; next = n; }
}


public class MyLinkedList {
	IntSLLNode head, tail;
	int length;
	
	public MyLinkedList() {
		head = tail = null;
		length = 0;
	}

	public boolean isEmpty() {
		return head == null;
	}
	
	public void addToHead( int el ) {
		head = new IntSLLNode( el, head );
		if ( tail==null ) 
			tail = head;
		length++;
	}
	
	public void addToTail( int el ) {
		if ( tail == null ) 
			addToHead( el );
		else {
			tail.next = new IntSLLNode( el );
			tail = tail.next;
                        length++;
		}
		
	}
	
	public void printAll() {
		for ( IntSLLNode it=head; it != null; it = it.next  ) 
			System.out.print( it.info + " " );
		System.out.println( );
	}
	
	public int deleteFromHead() {
		int el = head.info;
		if ( tail==head ) 
			head = tail = null;
		else 
			head = head.next;
		length--;
		return el;
	}
	
	public void printStatus( String caption ) {
		System.out.println( "+===========================================\n|" + caption );
		System.out.println( "+===========================================\n| List:");
		System.out.println( isEmpty()? "| is empty": "| is not empty" );
		System.out.println( "| contains " + length + " element" + ((length!=1)? "s":"" ) );
		System.out.print( "| elements: " );
		printAll();
		System.out.println( "+===========================================" );
	}
	
	public static void main(String[] args) {
		MyLinkedList L = new MyLinkedList();
		
		L.printStatus( "Brand new list" );
		
		for ( int i=10; i<50; i+= 10 ) 
			L.addToTail( i );
		
		L.printStatus( "After adding 10, 20, ...  to tail...");
		
		L = new MyLinkedList();
		L.printStatus( "Brand new list" );
		
		for ( int i=5; i<50; i+= 10 ) 
			L.addToHead( i );
		
		L.printStatus( "After adding 5, 15, ... to head...");
		
		while ( ! L.isEmpty() ) {
			int el = L.deleteFromHead();
			L.printStatus( "After removing " + el );
		}
		
	}

}


Problem 3


import java.util.Iterator;
import java.util.LinkedList;


public class Lab6_3 {

 	
	public static void printStatus( LinkedList L, String caption ) {
		System.out.print( caption + "\nL = " );
		Iterator it = L.iterator();
		while ( it.hasNext() ) {
			int el = (int) it.next();
			System.out.print( el + " " );
		}
		System.out.println( );

	}
	public static void main(String[] args) {
		LinkedList L = new LinkedList();
		
		for ( int i=10; i<=50; i+= 10 ) 
			L.addFirst( i );
		
		printStatus( L, "After adding 10, 20, 30, 40, 50..." );
		
		L = new LinkedList();
		
		printStatus( L, "Brand new list" );
		
		for ( int i=10; i<=50; i+= 10 ) 
			L.addLast( i );
		
		printStatus( L, "After adding 10, 20, ...50  to tail...");
		
		L = new LinkedList();
		printStatus( L, "Brand new list" );
		
		for ( int i=5; i<50; i+= 10 ) 
			L.addFirst( i );
		
		printStatus( L, "After adding 5, 15, ... to head...");
		
		while ( ! L.isEmpty() ) {
			int el = (int) L.removeFirst();
			printStatus( L, "After removing " + el );
		}
	}

}


Problem 4



...