20-CS-4003-001 Organization of Programming Languages Fall 2017
Data Structures

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures        Code    

List and Operations

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;

class PQ implements Comparator  {
   public int compare (Integer x, Integer y) {
      return (x.compareTo(y));
   }
}

class DataStorage {
   PriorityQueue  pq;
   LinkedList  q;
   Stack  s;
   int a[];
   Integer b[];

   public DataStorage () {
      pq = new PriorityQueue  (100, new PQ ());
      q = new LinkedList  ();
      s = new Stack  ();
      a = new int[6];
      b = new Integer[6];
   }
}

class java_1Frame
   extends JFrame implements ActionListener {
   JTextArea text;
   JButton priq, link, stck, iarr, sort;
   DataStorage ds;

   public java_1Frame () {
      setLayout(new BorderLayout());
      add("Center",
          new JScrollPane(text = new JTextArea()));
      JPanel p = new JPanel();
      p.setLayout(new FlowLayout());
      p.add(priq = new JButton("PQueue"));
      p.add(link = new JButton("LinkList"));
      p.add(stck = new JButton("Stack"));
      p.add(iarr = new JButton("Array"));
      p.add(sort = new JButton("Sort"));
      add("South", p);
      stck.addActionListener(this);
      link.addActionListener(this);
      priq.addActionListener(this);
      iarr.addActionListener(this);
      sort.addActionListener(this);
      ds = new DataStorage();
      ds.pq.add(new Integer(10));
      ds.pq.add(new Integer(12));
      ds.pq.add(new Integer(9));
      ds.pq.add(new Integer(6));
      ds.pq.add(new Integer(16));
      ds.pq.add(new Integer(13));
      ds.q.add("One");
      ds.q.add("Two");
      ds.q.add("Three");
      ds.q.add("Four");
      ds.q.add("Five");
      ds.q.add("Six");
      ds.s.add(new Integer(1));
      ds.s.add(new Integer(2));
      ds.s.add(new Integer(3));
      ds.s.add(new Integer(4));
      ds.s.add(new Integer(5));
      ds.s.add(new Integer(6));
      ds.a[0] = 10;
      ds.a[1] = 12;
      ds.a[2] = 9;
      ds.a[3] = 6;
      ds.a[4] = 16;
      ds.a[5] = 13;
      ds.b[0] = new Integer(10);
      ds.b[1] = new Integer(12);
      ds.b[2] = new Integer(9);
      ds.b[3] = new Integer(6);
      ds.b[4] = new Integer(16);
      ds.b[5] = new Integer(13);
      text.append("stack: "+ds.s.toString()+"\n");
      text.append("  (order added: as above)\n");
      text.append("linked list: "+
                  ds.q.toString()+"\n");
      text.append("  (order added: as above)\n");
      text.append("priority queue: "+
                  ds.pq.toString()+"\n");
      text.append("  (order added: "+
                  "10,12,9,6,16,13)\n");
      text.append("Integer array: ");
      text.append("[");
      try {
         for (int i=0 ; ; i++) 
            text.append(ds.b[i]+" ");
      } catch (Exception e) { }
      text.append("]\n");
      text.append("int array: ");
      text.append("[");
      try {
         for (int i=0 ; ; i++) 
            text.append(ds.a[i]+" ");
      } catch (Exception e) { }
      text.append("]\n");
   }

   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == priq) {
         try {
             text.append(ds.pq.poll().toString()+"\n");
         } catch (NullPointerException e) {
         text.append("priority queue is empty\n");
      }
         int p = text.getDocument().getLength();
         text.setCaretPosition(p);
      } else if (evt.getSource() == link) {
         try {
            text.append(ds.q.poll().toString()+"\n");
         } catch (NullPointerException e) {
            text.append("linked list is empty\n");
         }
         int p = text.getDocument().getLength();
         text.setCaretPosition(p);
      } else if (evt.getSource() == stck) {
         try {
            text.append(ds.s.pop().toString()+"\n");
         } catch (EmptyStackException e) {
            text.append("linked list is empty\n");
         }
         int p = text.getDocument().getLength();
         text.setCaretPosition(p);
      } else if (evt.getSource() == iarr) {
         text.append("[");
         try {
            for (int i=0 ; ; i++)
               text.append(ds.a[i]+" ");
         } catch (Exception e) { }
         text.append("]\n");
         int p = text.getDocument().getLength();
         text.setCaretPosition(p);
      } else if (evt.getSource() == sort) {
         Arrays.sort(ds.b, new PQ());
         text.append("[");
         try {
            for (int i=0 ; ; i++) 
               text.append(ds.b[i]+" ");
         } catch (Exception e) { }
         text.append("]\n");
         int p = text.getDocument().getLength();
         text.setCaretPosition(p);
      }
   }
}

public class java_1
   extends Applet implements ActionListener {
   JButton go;

   public void init () {
      setLayout(new FlowLayout());
      add(go = new JButton("Applet"));
      go.addActionListener(this);
   }

   public void actionPerformed (ActionEvent evt) {
      java_1Frame jf = new java_1Frame();
      jf.setSize(450,300);
      jf.setVisible(true);
   }
}
 -  This example shows just a few of the structures available in Java to organize data. Class DataStorage declares five containers, including one of primitive type. The non-primitive container types exist to provide import services for manipulating data structures. For example, the Arrays class has a static procedure that will sort any type of non-primitive type array using a class that implements the Comparator interface. This is applied to an array of Integers, where the Comparator used is PQ. Click on the Sort button to see the array sorted. This could not be applied to the int array.

The PriorityQueue class also requires a Comparator object to order the elements it contains. The PriorityQueue declared in this example assumes Integer objects and the Comparator, which is PQ, implements a compare method that uses compareTo of the Integer class. Click on the PQueue button to remove top objects and verify that the order is in increasing integer value.

Click on the LinkList button to remove strings from the linked list in index order.

Click on the Stack button to remove top objects from the stack and verify they are removed in reverse order of arrival on the stack.

Click on the Array button to display the int array. Applying toString() to the array would only have printed the reference.