List.java - package baitapCSD_2 import java.util.ArrayList import java.util.Scanner public class List public static ListNode head tail public List head

List.java - package baitapCSD_2 import java.util.ArrayList...

This preview shows page 1 - 3 out of 4 pages.

package baitapCSD_2; import java.util.ArrayList; import java.util.Scanner; public class List { public static ListNode head, tail; public List() { head = tail = null; } public void addToHead(int el) { head = new ListNode(el,head); if (tail == null) tail = head; traverse(); } public static boolean isEmpty() { return head == null; } public static void addToTail(int el) { if (!isEmpty()) { tail.next = new ListNode(el); tail = tail.next; } else head = tail = new ListNode(el); } public static void addMany(int [] a) { for(int i=0;i<a.length;i++) addToTail(a[i]); } public static void visit(ListNode p) { System.out.print(p.info + " "); } public static void traverse() { ListNode p = head; while(p!=null) { visit(p); p=p.next; } System.out.println(); } public static int deleteFromHead() { int el = head.info; if (head == tail) { head = tail = null; traverse();} else head = head.next; traverse();{ return el;} } public static int deleteFromTail() { int el = tail.info;
Image of page 1
if (head == tail) head = tail = null; else { ListNode tmp; for (tmp = head; tmp.next != tail; tmp = tmp.next); tail = tmp; tail.next = null; } traverse(); return el; } public static void count() { ListNode tmp = head.next; int length = 1; while (tmp != null) {
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 4 pages?

  • Summer '18
  • Tung Son
  • Return statement, Void type, public class List, public static ListNode head

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes