Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SingLList {
- private Node head;
- private static class Node {
- public int data;
- public Node next;
- }
- public static void main(String[] args) {
- int[] arr = {1,2,3,4,5,6,7};
- SingLList list = SingLList.arrayToList(arr);
- list.showList();
- System.out.println();
- list.addBack(8);
- // list.removeOdd();
- // list.showList();
- // list.addFront(1);
- // System.out.println();
- list.showList();
- // System.out.println("contains 3? "+list.contains(3));
- // System.out.println("contains 8? "+list.contains(8));
- // list.clear();
- // list.showList();
- }
- public boolean empty() {
- return this.head == null;
- }
- public void addFront(int d) {
- if ( this.head.data == 0) {
- this.head.data = d;
- this.head.next = null;
- }
- else {
- SingLList list = new SingLList();
- list.head = new Node();
- list.head.data = d;
- list.head.next = this.head;
- this.head = list.head;
- }
- }
- public void addBack(int d) {
- if( this.empty() ) {
- this.head = new Node();
- this.head.data = d;
- System.out.print("*");
- }
- else {
- SingLList list = new SingLList();
- list.head = this.head.next;
- list.addBack(d);
- }
- }
- public static SingLList arrayToList(int[] arr) {
- SingLList list = new SingLList();
- list.head = new Node();
- for ( int i = arr.length; i > 0; i-- ) {
- list.addFront(arr[i-1]);
- }
- return list;
- }
- public void removeOdd() {
- SingLList list = new SingLList();
- list.head= new Node();
- while(! this.empty()) {
- if ( this.head.data % 2 == 0) {
- list.addFront(this.head.data);
- }
- this.head = this.head.next;
- }
- this.head = list.head;
- }
- // public boolean contains(int d)
- public void showList() {
- SingLList temp = new SingLList();
- System.out.print(this.head.data+" ");
- if ( this.head.next != null ) {
- temp.head = this.head.next;
- temp.showList();
- }
- }
- //public void clear()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement