import java.awt. *;
import java.applet. *;
import java.lang. *;

class link
{
  String infor;
  link next;
}

public class Linklist extends java.applet.Applet
{
  public int count = 0;
  public int count2 = 0;
  public int count3 = 1;
  public boolean del = false;
  public boolean search = false;
  TextField ele;
  static link head = null;
  static link tail = null;

  public void init ()
  {
    ele = new TextField (4);
    setLayout (new BorderLayout ());

    Panel p = new Panel ();
      p.add (new Button ("Reset"));
      p.add (new Button ("Insert"));
      p.add (new Button ("Delete"));
      p.add (new Button ("Search"));
      p.add (new Button ("Sort"));

    Label la = new Label ("Number");
      p.add (la);
      p.add (ele);

      add ("North", p);
  }

  public void paint (Graphics g)
  {
    link p;
    int nl = 0, N = 1, px = 30, py = 60;
      count = 0;
    if (head != null)
      {
	p = head;
	while (p != null)
	  {
	    if (N < 6 || (N < 12 && N > 6))
	      {
		if (p != tail)
		  nodebox (g, p.infor, px, py, 1);
		else  
		  nodebox (g, p.infor, px, py, 3);
	      }
	    else if (N == 6 || N <= 12)
	      {
		if (p != tail) 
		   {
		     if(N == 12)
			nodebox (g, p.infor, px, py, 4);
		     else
		        nodebox (g, p.infor, px, py, 2);
		   }
		else
		  nodebox (g, p.infor, px, py, 3);
		px = 30 - 80;
		py += 60;
	      }
		N++;
	     if(N>12)
		{
		caninsert(g);
		break;
		}
	    p = p.next;
	    px += 80;
	  }
      }
    if (del == false && count == 1)
      notfound (g);
    del = false;

    if (search == true)
      {
	search_print (g);
	count2 = 0;
	count3 = 1;
	search = false;
      }
  }

/*****************************************/
/*  	function create box         	 */
/*****************************************/   
  public void nodebox (Graphics g, String item, int x, int y, int z)
  {
    g.setColor (Color.red);
    g.drawRect (x, y, 50, 30);
    g.drawLine (x + 35, y, x + 35, y + 30);
    g.setColor (Color.blue);
    g.drawString (item, x + 10, y + 20);
    if (z == 1)
      {
	g.setColor (Color.red);
	g.drawLine (x + 45, y + 15, x + 80, y + 15);
        g.drawLine (x + 70, y + 10, x + 80, y + 15);
        g.drawLine (x + 70, y + 20, x + 80, y + 15);
      }
    else if (z == 2)
      {
	g.setColor (Color.red);
	g.drawLine (x + 45, y + 15, x + 75, y + 15);
	g.drawLine (x + 75, y + 15, x + 75, y + 45);
	g.drawLine (20, y + 45, 505, y + 45);
	g.drawLine (20, 105, 20, 135);
	g.drawLine (20, 135, 30, 135);
	g.drawLine (25, 130, 30, 135);
	g.drawLine (25, 140, 30, 135);
      }
    else if (z == 3)
      {
	g.setColor (Color.red);
	g.drawLine (x + 35, y, x + 50, y + 30);
      }
    else if(z == 4)
      {
	g.setColor (Color.red);
	g.drawLine (x + 35, y, x + 50, y + 30);
      }
  }

/*****************************************/
/*	check notfound			 */
/*****************************************/
  public void notfound (Graphics g)
  {
    Font font = new Font ("TimeRoman", Font.BOLD, 18); 
    g.setFont (font); 
    g.setColor (Color.red); 
    g.drawString ("Not found", 300, 250);
  }
  
/*****************************************/
/*      check can't insert               */
/*****************************************/   
  public void caninsert(Graphics g)
  {
    Font font = new Font ("TimeRoman", Font.BOLD, 18);
    g.setFont (font);
    g.setColor (Color.red);
    g.drawString ("can't insert", 190, 200);
  }

  public void search_print (Graphics g)
  {
    Font font1 = new Font ("TimeRoman", Font.BOLD, 18); 
      g.setFont (font1); 
      g.setColor (Color.red); 
    if (count2 == 0)
      g.drawString ("not found", 300, 250);
    else if (count2 == 2)
      {
	g.drawString ("Found position : " + count3, 300, 250);
      }
  }

  public boolean action (Event evt, Object arg)
  {
    int a=1;
    Graphics g;
    link node, p,r;
    String tmp;
/*************************************/
/*	function insert 	     */
/*************************************/
    if (arg.equals ("Insert"))
      {
	a++;
	tmp = ele.getText ();
	if (!tmp.equals (""))
	  {
	    node = new link ();
	    node.infor = tmp;
	    node.next = null;
	    if (head == null)
	      {
		head = tail = node;
	      }
	    else
	      {
		if(a>=12)
		    {
			r=tail;
			tail.next = null;
			tail = r;
		    }
		else
		    {
		    tail.next = node;
		    tail = node;
      		    }
	      }
	    printlist ();
	    repaint ();
	  }
	else
	  {
	    System.out.println ("Textfield null ");
	  }
      }

/*****************************************/
/*      function delete                  */
/*****************************************/   
    else if (arg.equals ("Delete"))
      {
	if ((tmp = ele.getText ()) != null)
	  {
	    count2 = 1;
	    search = true;
	    count = 1;
	    del = false;
	    if (head != null)
	      {
		search = true;
		count2 = 0;
		p = node = head;
		if (head.infor.equals (tmp))
		  {
		    head = head.next;
		    count = 0;
		    del = true;
		    search = false;
		  }
		else
		  {
		    while (node != null)
		      {
			if (node.infor.equals (tmp))
			  {
			    p.next = node.next;
			    if (node == tail)
			      tail = p;
			    del = true;
			    search = false;
			    count = 0;
			    break;
			  }
			p = node;
			node = node.next;
		      }
		  }
	      }
	    printlist ();
	    repaint ();
	  }
	else
	  {
	    System.out.println ("Textfield null ");
	  }
      }

/*****************************************/
/*      function search                  */
/*****************************************/   
    else if (arg.equals ("Search"))
      {
	search = true;
	count2 = 0;
	count3 = 1;
	if ((tmp = ele.getText ()) != null)
	  {
	    if (head == null)
	      {
		search = true;
		count2 = 1;
	      }
	    link dumy;
	    dumy = new link ();
	    dumy = head;
	    while (dumy != null)
	      {
		if (dumy.infor.equals (tmp))
		  {
		    search = true;
		    count2 = 2;
		    break;
		  }
		dumy = dumy.next;
		count3++;
	      }
	    repaint ();
	  }
      }

/*****************************************/
/*      function sort                    */
/*****************************************/   
    else if (arg.equals ("Sort"))
      {
	link pp, qq;
	pp = new link ();
	qq = new link ();
	String temp;
	temp = new String();
	pp = head;
	    if (head == null)
	      {
		count2 = 1;
		search = true;
	      }
	    else
	      {
		while (pp != null)
		  {
		    qq = head;
		    while (qq != pp)
		      {
			if ((pp.infor.compareTo ( qq.infor))<0)
			  {
			    temp = pp.infor;
			    pp.infor = qq.infor;
			    qq.infor = temp;
			  }
			qq = qq.next;
		      }
		    pp = pp.next;
		  }
	      }
	repaint ();
      }

/*****************************************/
/*      function resert                  */
/*****************************************/   
    else if (arg.equals ("Reset"))
      {
	head=null;
	tail=null;
	repaint();
      }
    return true;
  }


  public void printlist ()
  {
    link p;
      p = head;
    while (p != null)
      {
	System.out.print (p.infor + " ");
	p = p.next;
      }
    System.out.println ("");
  }
}
