uNreQuiteD

February 20, 2007

stack: using singly link list

Filed under: data structure — anai lem @ 8:15 am

import javax.swing.*;
class nodeStack
{
Object info;
nodeStack link;
static nodeStack head, next, tail, last;

public nodeStack(Object i, nodeStack l) //CONSTRUCTOR
{ info = i;
link = l; }

//add node
static void doAdd()
{
int i = Integer.parseInt(JOptionPane.showInputDialog(“Add Node\nEnter a number”));
if(head == null)
{
head = new nodeStack(i,null);
tail = head;
}
else
{
next = new nodeStack(i,null);
tail.link = next;
tail=next;
}
}

//display
static void doDisplay()
{
String output=””;
last = tail;
if (head != null)
{
if (head == last)
output += head.info + ” “;
else
{
while(head != last)
{
next = head;
while(next.link != last)
{
next = next.link;
}
output += last.info + ” “;
last = next;
}
output += head.info + ” “;
}
JOptionPane.showMessageDialog(null, “Array List Content\n” + output);
}

else
JOptionPane.showMessageDialog(null, “List is empty”);
}

//delete
static void doDelete()
{
if (head != null)
{
if (head == tail)
head = null;
else
{
next = head;
while(next.link != tail)
{
next = next.link;
}
next.link = null;
tail = null;
tail = next;
}
JOptionPane.showMessageDialog(null, “Tail node deleted”);
}

else
JOptionPane.showMessageDialog(null, “List is empty”);
}

public static void main(String[] args)
{
String menu = “MAIN MENU\n” +
“[1] Add node\n”+
“[2] Display list\n” +
“[3] Delete node\n” +
“[4] Exit”;
int choice=0;
do
{
String str = JOptionPane.showInputDialog(menu);
if(str.equals(“”))
JOptionPane.showMessageDialog(null, “enter a number”);
else
{
choice = Integer.parseInt(str);
if (choice == 1)
doAdd();
else if (choice == 2)
doDisplay();
else if (choice == 3)
doDelete();
else if (choice == 4)
JOptionPane.showMessageDialog(null, “Programmer: eugene”);
}
} while(choice != 4);
} }

sample output:
stackto.jpg

stack: using 1-di array

Filed under: data structure — anai lem @ 7:21 am

class stackArr
{
public static void main(String args[])
{
String str = “3-5*2-6*2+7/2”;

char stck[] = {‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘};
int top = 0, x;
char c;

System.out.print(“STATEMENT : ” + str + “\nPOSTFIX : “);
for(x=0; x<str.length(); x++)
{
c = str.charAt(x);
switch(c)
{

// operators: multiplication division
case ‘/’:
case ‘*’:
{
if(top == 0)
{
stck[top]= c;
top++;
}
else
if(stck[top-1] == ‘/’ || stck[top-1] == ‘*’)
{
top–;
System.out.print(stck[top]);
stck[top]=c;
top++;
}
else
{
stck[top] = c;
top++;
}

break;
} //end case

// operators: addition subtraction
case ‘-‘:
case ‘+’:
{
if(top == 0)
{
stck[top]= c;
top++;
}
else
{
for(int ctr=top-1; ctr>=0; ctr–)
{
if(stck[ctr] == ‘-‘ || stck[ctr] == ‘+’ ||
stck[ctr] == ‘/’ || stck[ctr] == ‘*’)
{
top–;
System.out.print(stck[top]);
stck[top]=’ ‘;
}
} // end loop

stck[top] = c;
top++;
} // end else
break;
} // end case

//operand
default: System.out.print(c);

} //end switch
} //end for

for(int y=top-1; y>=0; y–)
System.out.print(stck[y]);
System.out.println();
}
}
//PROGRAMMED BY: EUGENE

output:
STATEMENT : 3-5*2-6*2+7/2
POSTFIX : 352*-62*-72/+

February 13, 2007

menu: QUEUE manipulation

Filed under: data structure — anai lem @ 3:28 am

import javax.swing.*;
class nodeMenu
{
  Object info;
  nodeMenu link;
  static nodeMenu head, next, tail;

  public nodeMenu(Object i, nodeMenu l) //CONSTRUCTOR
  { info = i;
    link = l;  }

  static void doAdd()
  {
    int i = Integer.parseInt(JOptionPane.showInputDialog(“Add Node\nEnter a number”));
    if(head == null)
      {
       head = new nodeMenu(i,null);
       tail = head;
      }
    else
      {
       next = new nodeMenu(i,null);
       tail.link = next;
       tail=next;
      }
  }

  static void doDisplay()
  {
    String output=””;
   if (head != null)
   {
    next = head;
    while(next!=null)
    {
      output = output + (next  + ” -> ” + next.info + ” ” + next.link + “\n”);
      next = next.link;
    }
    JOptionPane.showMessageDialog(null, “Display List\n” + output);
   }
   else
      JOptionPane.showMessageDialog(null, “List is empty”);
  }

  static void doDelete()
  {
  if(head!=null)
   {
    next = head;
    head = next.link;
    next = null;
    JOptionPane.showMessageDialog(null,”Delete Node\nHEAD node deleted”);
   }
  else
    JOptionPane.showMessageDialog(null,”List is empty”);
  }

  public static void main(String[] args)
  {
    String menu = “MAIN MENU\n” +
                   “[1] Add node\n”+
                   “[2] Display list\n” +
                   “[3] Delete node\n” +
                   “[4] Exit”;
   int choice=0;
 do
 {
  String str = JOptionPane.showInputDialog(menu);
  if(str.equals(“”))
        JOptionPane.showMessageDialog(null, “enter a number”);
  else
  {
   choice = Integer.parseInt(str);
   if (choice == 1)
        doAdd();
   else if (choice == 2)
        doDisplay();
   else if (choice == 3)
        doDelete();
   else if (choice == 4)
      JOptionPane.showMessageDialog(null, “Programmer: eugene”);
  }
 } while(choice != 4);
} }

sample output

queue

January 16, 2007

link list

Filed under: data structure — anai lem @ 11:00 am

import javax.swing.*;
class Node
{
  int no;
  Node ptr;

  //constructor of class Node
  Node(int n, Node p)
  {
    no = n;
    ptr = p;
  }
//display list
static void printList(Node list){
  Node node =list;
  String out=””;
  while (node !=null)
   {
    out += node + ” –>  [” + node.no + “] ” + node.ptr + “\n” ;
    node =node.ptr;
   }
   JOptionPane.showMessageDialog(null,”link list \n”+out);
 }
//main method
public static void main(String args[])
{
int noNodes = Integer.parseInt(JOptionPane.showInputDialog(“number of nodes: “));
if (noNodes>=2 && noNodes<=5)
{
  int ctr = 1;
  Node head, dummy, node;
  int value = Integer.parseInt(JOptionPane.showInputDialog(“number ” + ctr));

//1st node created
    node = new Node(value, null);
    head = node;

  ctr++;
//2nd upto last node created
  while(ctr<=noNodes)
  {
    value = Integer.parseInt(JOptionPane.showInputDialog(“number ” + ctr));
    dummy = new Node(value,null);
    node.ptr = dummy;
    node = dummy;
    ctr++;
  }

//call static method printList
  printList(head);
 }
}}

 output

put.jpg

December 7, 2006

2-di array: zeroDown

Filed under: data structure — anai lem @ 7:55 am

/*
2-di array.
test if the values below the diagonal of the 2-di array (3×3) are all equal to zero.

 programmed by: iana mel
*/

import javax.swing.JOptionPane;
class zeroDown
{
  public static void main(String args[])
  {
    int ar[][] = {{4,6,3},
                  {0,5,7},
                  {0,0,6}};
    int a,b, valid = 1;
    for(a=0; a<=2; a++)
    {
      for(b=0; b<=2; b++)
      {
       if(a==b)
          break;
       if(ar[a][b] != 0)
         valid = 0;
      }
      if (valid == 0)
        break;
    }

    String s=””;
    for(a=0; a<=2; a++)
    {
      for(b=0; b<=2; b++)
          s += ar[a][b] +”  “;
      s+=”\n”;
    }

    if(valid == 1)
       JOptionPane.showMessageDialog(null,s + “\n\n VALID”);
    else
       JOptionPane.showMessageDialog(null,s + “\n\n INVALID”);
  }
}

OUTPUT

out12.jpg

2-di array: zeroUp

Filed under: data structure — anai lem @ 7:51 am

/*
2-di array.
test if the values above the diagonal of the 2-di array (3×3) are all equal to zero.

programmed by: iana mel
*/

import javax.swing.JOptionPane;
class zeroUp
{
  public static void main(String args[])
  {
    int ar[][] = {{4,0,0},
                  {5,1,0},
                  {3,4,6}};
    int a,b, valid = 1;
    for(a=0; a<=2; a++)
    {
      for(b=0; b<=2; b++)
      {
       if(a==b || a>=b)
          continue;
       if(ar[a][b] != 0)
         valid = 0;
      }
      if (valid == 0)
        break;
    }

    String s=””;
    for(a=0; a<=2; a++)
    {
      for(b=0; b<=2; b++)
          s += ar[a][b] +”  “;
      s+=”\n”;
    }

    if(valid == 1)
       JOptionPane.showMessageDialog(null,s + “\n\n VALID”);
    else
       JOptionPane.showMessageDialog(null,s + “\n\n INVALID”);
  }
}

 OUTPUT

out2.jpg

HARRY POTTER & THE AMAZING WIZARD

Filed under: data structure — anai lem @ 6:29 am

/*
This lab exercise was downloaded from a page which i forgot. my apology.  The downloaded file was edited by the programmer.

HARRY POTTER & THE AMAZING WIZARD.
Declare and initialize a 20-element array. In each element place one of the following characters:
‘W’ – Wizard
‘S’ — Sword
‘D’ — Demon
‘B’ — Blank – Save spot.

Example an array can look like this:
W S S D B D W B B D W S W D B D S W B S

You ask the player to select a location in Hogarth Tower. (Choose a number from 0 – 19) That places the player at a position in the array. In the example if the user enters a 4 as a starting position (starting at 0). So, the entry at that position is a B (Blank).

Based on the rules below the player can then move North (add 1 to the array index) or South (subtract 1 from the array index). Here are the rules:

(1) For each Blank (B) spot, output the message ‘Safe Place’; 2 points is awarded.
(2) For each Wizard (W) spot, output the message ‘You picked up a Wizard’; 3 Points is Awarded.
(3) For each Sword (S) spot, output the message ‘You picked up a Sword’; 4 Points is awarded.
(4) For each Demon (D) encountered, display the message ‘A demon has attacked’; subtract 5 points.

The game is over when (a) the player types in 99 this ends the game. Print out the accumulated points. Or (b) when the points are below 0 – the player is killed. Display ‘You are killed’ if the point garnered by the player is below zero.

Make sure the player does not go beyond the array boundary. The boundary has is limited by the size of the array 19 elements.
*/
/*programmed by: iana mel – school of information technology, dwcl*/
import javax.swing.JOptionPane;
class harryPot
{
   static int score(char c)
    {
        switch(c)
        {
        case 66: // ‘B’
            JOptionPane.showMessageDialog(null,”SAFE PLACE. +2 pts”);
            return 2;

        case 87: // ‘W’
            JOptionPane.showMessageDialog(null,”YOU PICKED UP A WIZARD. +3 pts”);
            return 3;

        case 83: // ‘S’
            JOptionPane.showMessageDialog(null,”YOU PICKED UP A SWORD. +4 pts”);
            return 4;

        case 68: // ‘D’
            JOptionPane.showMessageDialog(null,”A DEMON HAS ATTACKED. -5 pts”);
            return 5;
        }
        return 0;
    }

    public static void main(String args[])
    {

        char ac[] = { ‘W’, ‘S’, ‘S’, ‘D’, ‘B’, ‘D’, ‘W’, ‘B’, ‘B’, ‘D’,
                               ‘W’, ‘S’, ‘W’, ‘D’, ‘B’, ‘D’, ‘S’, ‘W’, ‘B’, ‘S’};
        int i = 0, j=0, k, l;

        String s = JOptionPane.showInputDialog(“Choose a number from 0 – 19″);
        k = Integer.parseInt(s);

        if(k < 0 || k > 19)
           JOptionPane.showMessageDialog(null,”INVALID POSITION. \nPROGRAM RUN TERMINATED.”);
        else
         do
         {
           if(k<0)
           {
               JOptionPane.showMessageDialog(null,”You’re already at index 0. \nYou can’t move South anymore.”);
               k=0;
           }
           else if(k>19)
           {
               JOptionPane.showMessageDialog(null,”You’re already at index 19. \nYou can’t move North anymore.”);
               k=19;
           }
           else
           {
            l = score(ac[k]);
            if(l < 5)
                i += l;
            else
                i -= l;
            if(i < 1)
            {
                JOptionPane.showMessageDialog(null,”YOU ARE KILLED!”);
                break;
            }
           }
                s =JOptionPane.showInputDialog(“Score: ” + i + “\n\n[1] north \n[2] south \n[99]exit \n\n   Choose :  “);
                j = Integer.parseInt(s);
                if(j == 1)
                    k++;
                else if(j == 2)
                    k–;
        } while(j != 99);

        if(i > 0)
            JOptionPane.showMessageDialog(null,”TOTAL SCORE = ” + i);
    }
}

OUTPUT:

out.JPG

November 23, 2006

lab exer1: 1-di array

Filed under: data structure — anai lem @ 5:54 am

class array13579
{
public static void main(String args[])
{
int A[] = new int[5];
for (int i=0; i<5; i++)
{
A[i] = i*2 + 1;
System.out.print(A[i] + ” “);
}
}
}

//******************************************

class array10203040
{
public static void main(String args[])
{
int A[] = new int[4];
for (int i=0; i<4; i++)
{
A[i] = (i + 1) * 10;
System.out.print(A[i] + ” “);
}
}
}

Create a free website or blog at WordPress.com.