0% found this document useful (0 votes)
112 views

Final Code Compilation IN Data Structure: SUBMITTED BY: JP Parungao & Renz Hidalgo BSCS201

The document contains code submissions from students JP Parungao & Renz Hidalgo for their BSCS201 data structures course. It includes Java code for several programs and algorithms including a "Hello World" program, a magic square program, an array program, a bubble sort program, and more. Each code submission is followed by its output.

Uploaded by

Scented
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views

Final Code Compilation IN Data Structure: SUBMITTED BY: JP Parungao & Renz Hidalgo BSCS201

The document contains code submissions from students JP Parungao & Renz Hidalgo for their BSCS201 data structures course. It includes Java code for several programs and algorithms including a "Hello World" program, a magic square program, an array program, a bubble sort program, and more. Each code submission is followed by its output.

Uploaded by

Scented
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 29

FINAL CODE COMPILATION

IN
DATA STRUCTURE

SUBMITTED BY: JP Parungao & Renz Hidalgo

BSCS201

First program

class hello {
public static void main(String[] args) {

System.out.println("Please enter your Name:");

System.out.println("Hello JayPee!This is my first Program in JAVA.");

Output:

Magic square

import java.io.*;

class MagicSquare

{
static final int maxsize = 50;

public static void main (String [] args) throws IOException

int i, j, k, l, n, key;

boolean n_ok;

String line;

int [] [] square = new int [maxsize] [maxsize];

BufferedReader KeybIn = new BufferedReader

(new InputStreamReader(System.in));

try

System.out.print("Size of square? ");

line = KeybIn.readLine();

n = Integer.parseInt(line);

n_ok = (1<=n) & (n<=maxsize+1) & (n%2==1);

if ( n_ok )

for (i=0;i<n;i++)

for (j=0;j<n;j++) square[i][j] = 0;

square[0][(int)(n-1)/2] = 1;

key = 2;
i = 0;

j = (int)(n-1)/2;

while ( key <= n*n )

k = i - 1;

if ( k < 0 ) k += n;

l = j - 1;

if ( l < 0 ) l += n;

if ( square[k][l] != 0 ) i = (i+1) % n;

else { i = k; j = l; }

square[i][j] = key;

key++;

System.out.println("Magic square of size " + n);

for (i=0;i<n;i++)

for (j=0;j<n;j++) System.out.print("\t"+square[i][j]);

System.out.println();

else

if ( n % 2 == 0)

System.out.println("Error!!! n is even");

else System.out.println("Error!!! n out of range");

}
catch (NumberFormatException e)

System.out.println("Error in number, try again.");

Output:

arrays

class array {

public static void main(String[] args) {

long[] array;

array = new long[10];

int nElems = 0;

int j;

long searchKey;

array[0] = 77;

array[1] = 99;

array[2] = 44;
array[3] = 55;

array[4] = 22;

array[5] = 88;

array[6] = 11;

array[7] = 0;

array[8] = 66;

array[9] = 33;

nElems = 10;

for(j=0; j<nElems; j++)

System.out.print(array[j] + " ");

System.out.println("");

searchKey = 66;

for(j=0; j<nElems; j++)

if(array[j] == searchKey)

break;

if(j == nElems)

System.out.println("Can’t find " + searchKey);

else

System.out.println("Found " + searchKey);

searchKey = 55;

for(j=0; j<nElems; j++)

if(array[j] == searchKey)

break;

for(int k=j; k<nElems-1; k++)

array[k] = array[k+1];

nElems--;

for(j=0; j<nElems; j++)

System.out.print( array[j] + " ");


System.out.println("");

Output:

bubblesort

class bubblesort {

public static void main(String[] args) {

int unsortedArray[] = {3,2,1};

int i;

bubbleSort(unsortedArray, unsortedArray.length);

System.out.println("After sorting, the list elements are: ");

for(i=0; i<unsortedArray.length; i++) {

System.out.print(unsortedArray[i] + " ");

private static void bubbleSort(int[] unsortedArray, int length) {

int temp, counter, index;

for(counter=0; counter<length-1; counter++) {

for(index=0; index<length-1-counter; index++) {


if(unsortedArray[index] > unsortedArray[index+1]) {

temp = unsortedArray[index];

unsortedArray[index] = unsortedArray[index+1];

unsortedArray[index+1] = temp;

Output:

Selection arraybubble

class ArrayBub

private long[] a;

private int nElems;

public ArrayBub(int max)

a = new long[max];

nElems = 0;

public void insert(long value)

a[nElems] = value;
nElems++;

public void display()

for(int j=0; j<nElems; j++)

System.out.print(a[j] + " ");

System.out.println("");

public void bubbleSort()

int out, in;

for(out=nElems-1; out>1; out--)

for(in=0; in<out; in++)

if( a[in] > a[in+1] )

swap(in, in+1);

private void swap(int one, int two)

long temp = a[one];

a[one] = a[two];

a[two] = temp;

class BubbleSortApp

public static void main(String[] args)

{
int maxSize = 100;

ArrayBub arr;

arr = new ArrayBub(maxSize);

arr.insert(77);

arr.insert(99);

arr.insert(44);

arr.insert(55);

arr.insert(22);

arr.insert(88);

arr.insert(11);

arr.insert(00);

arr.insert(66);

arr.insert(33);

arr.display();

arr.bubbleSort();

arr.display();

Output:

triangle

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;
public class jaypee{

public static void main(String[] args) throws NumberFormatException, IOException {

System.out.print("How many Triangular Numbers are required:");

InputStreamReader ir = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(ir);

int j = 1;

int num = Integer.parseInt(br.readLine());

for(int i = 1; i<= num; i++)

j = i * (i+1)/2;

System.out.println("Returning:" + j);

Output:

Deck of cards

class lab3{

public static void main(String a[]){

int i;
int array[] = {1,2,3,4,6,7,8,9,10,5};

System.out.println("Deck of Cards\n");

for(i = 0; i < array.length; i++)

System.out.print( array[i]+" ");

System.out.println();

insertion_srt(array, array.length);

System.out.print("Values after the sort:\n");

for(i = 0; i <array.length; i++)

System.out.print(array[i]+" ");

System.out.println();

public static void insertion_srt(int array[], int n){

for (int i = 1; i < n; i++){

int j = i;

int B = array[i];

while ((j > 0) && (array[j-1] > B)){

array[j] = array[j-1];

j--;

array[j] = B;

Output:
Triangular

import java.io.*

class triangular {

public static void main(String[] args) throws IOException {

BufferedReader object = new BufferdReader(new InputStreamReader(system.in));

System.out.println("Input"):

int a;

int b;

int s;

int d;

int p;

a = Integer.parse.Int(br.readLine());

s = 0;

s = s+b;

System.out.println("sum +"+s);

p=1;

for

Output:
Linkedlist

import java.io.*;

import java.util.List;

import java.util.ArrayList;

class LinkedList

public static DataInputStream a = new DataInputStream(System.in);

public static void main (String arg[])throws Exception

int choice;

System.out.println("ABC Company List of Employees");

System.out.println(" ");

List<String> emplist = new ArrayList<String>();

emplist.add( "Allenon, Dianne 235");

emplist.add("Baranek, Marsha 568");

emplist.add( "Bearrance, Susan 987 ");

emplist.add( "Bears, Linda 456");

emplist.add( "Bloomhuff, Deborah 753");

emplist.add( "Drabant, Paige 159");

emplist.add( "Felcyn, Kenneth 645");

emplist.add( "Foster, Catherine 387");

emplist.add( "Kardynal, Vicki 268");

emplist.add( "Koch, Christine 657");

for(int b=0;b<emplist.size();b++)
{

System.out.println(emplist.get(b));

System.out.println();

do

System.out.print("Choose from: 1-Insert, 2-Delete, 3-Display, 4-Find, 5 -Quit: ");

choice = Integer.parseInt(a.readLine());

switch(choice)

case 1:

System.out.print("Enter Name & Id No.# to be inserted: ");

String NL = a.readLine();

emplist.add(NL);

break;

case 2:

System.out.print("Enter Name & Id No.# to be deleted: ");

String RL = a.readLine();

int i = emplist.indexOf(RL);

if(i != -1)

emplist.remove(i);

System.out.println("Successfully Deleted!" + RL);

}else

System.out.println("Can not find "+ RL + " to Delete!!");

break;
case 3:

for(int b=0;b<emplist.size();b++)

System.out.println(emplist.get(b));

System.out.println();

break;

case 4:

System.out.print("Enter Name & Id No.# to be find: ");

String FL = a.readLine();

int indexOf = emplist.indexOf(FL);

if(indexOf != -1)

String foundName = (String)emplist.get(indexOf);

System.out.println("Found! " + foundName);

}else

System.out.println("Not Found! " + FL);

break;

case 5:

System.out.println("Thank you! End of the program..");

break;

} while(choice !=5);

Output:
Node

import java.io.*;

import java.util.*;

class Node

public int iData;

public double dData;

public Node leftChild;

public Node rightChild;

public void displayNode()

System.out.print('{');

System.out.print(iData);

System.out.print(", ");

System.out.print(dData);

System.out.print("} ");

class Tree

private Node root;


public Tree()

{ root = null; }

public Node find(int key)

Node current = root;

while(current.iData != key)

if(key < current.iData)

current = current.leftChild;

else

current = current.rightChild;

if(current == null)

return null;

return current;

public void insert(int id, double dd)

Node newNode = new Node();

newNode.iData = id;

newNode.dData = dd;

if(root==null)

root = newNode;

else

Node current = root;


Node parent;

while(true)

parent = current;

if(id < current.iData)

current = current.leftChild;

if(current == null)

parent.leftChild = newNode;

return;

else

current = current.rightChild;

if(current == null)

parent.rightChild = newNode;

return;

public boolean delete(int key)

{
Node current = root;

Node parent = root;

boolean isLeftChild = true;

while(current.iData != key)

parent = current;

if(key < current.iData)

isLeftChild = true;

current = current.leftChild;

else

isLeftChild = false;

current = current.rightChild;

if(current == null)

return false;

if(current.leftChild==null &&

current.rightChild==null)

if(current == root)

root = null;

else if(isLeftChild)

parent.leftChild = null;

else

parent.rightChild = null;
}

else if(current.rightChild==null)

if(current == root)

root = current.leftChild;

else if(isLeftChild)

parent.leftChild = current.leftChild;

else

parent.rightChild = current.leftChild;

else if(current.leftChild==null)

if(current == root)

root = current.rightChild;

else if(isLeftChild)

parent.leftChild = current.rightChild;

else

parent.rightChild = current.rightChild;

else

Node successor = getSuccessor(current);

if(current == root)

root = successor;

else if(isLeftChild)

parent.leftChild = successor;

else

parent.rightChild = successor;
successor.leftChild = current.leftChild;

return true;

private Node getSuccessor(Node delNode)

Node successorParent = delNode;

Node successor = delNode;

Node current = delNode.rightChild;

while(current != null)

successorParent = successor;

successor = current;

current = current.leftChild;

if(successor != delNode.rightChild)

successorParent.leftChild = successor.rightChild;

successor.rightChild = delNode.rightChild;

return successor;

public void traverse(int traverseType)


{

switch(traverseType)

case 1: System.out.print("\nPreorder traversal: ");

preOrder(root);

break;

case 2: System.out.print("\nInorder traversal: ");

inOrder(root);

break;

case 3: System.out.print("\nPostorder traversal: ");

postOrder(root);

break;

System.out.println();

private void preOrder(Node localRoot)

if(localRoot != null)

System.out.print(localRoot.iData + " ");

preOrder(localRoot.leftChild);

preOrder(localRoot.rightChild);

private void inOrder(Node localRoot)

{
if(localRoot != null)

inOrder(localRoot.leftChild);

System.out.print(localRoot.iData + " ");

inOrder(localRoot.rightChild);

private void postOrder(Node localRoot)

if(localRoot != null)

postOrder(localRoot.leftChild);

postOrder(localRoot.rightChild);

System.out.print(localRoot.iData + " ");

public void displayTree()

Stack globalStack = new Stack();

globalStack.push(root);

int nBlanks = 32;

boolean isRowEmpty = false;

System.out.println(

"......................................................");

while(isRowEmpty==false)

{
Stack localStack = new Stack();

isRowEmpty = true;

for(int j=0; j<nBlanks; j++)

System.out.print(' ');

while(globalStack.isEmpty()==false)

Node temp = (Node)globalStack.pop();

if(temp != null)

System.out.print(temp.iData);

localStack.push(temp.leftChild);

localStack.push(temp.rightChild);

if(temp.leftChild != null ||

temp.rightChild != null)

isRowEmpty = false;

else

System.out.print("--");

localStack.push(null);

localStack.push(null);

for(int j=0; j<nBlanks*2-2; j++)

System.out.print(' ');

System.out.println();

nBlanks /= 2;

while(localStack.isEmpty()==false)
globalStack.push( localStack.pop() );

System.out.println(

"......................................................");

class TreeApp

public static void main(String[] args) throws IOException

int value;

Tree theTree = new Tree();

theTree.insert(50, 1.5);

theTree.insert(25, 1.2);

theTree.insert(75, 1.7);

theTree.insert(12, 1.5);

theTree.insert(37, 1.2);

theTree.insert(43, 1.7);

theTree.insert(30, 1.5);

theTree.insert(33, 1.2);

theTree.insert(87, 1.7);

theTree.insert(93, 1.5);

theTree.insert(97, 1.5);

while(true)

System.out.print("Enter first letter of show, ");

System.out.print("insert, find, delete, or traverse: ");


int choice = getChar();

switch(choice)

case 's':

theTree.displayTree();

break;

case 'i':

System.out.print("Enter value to insert: ");

value = getInt();

theTree.insert(value, value + 0.9);

break;

case 'f':

System.out.print("Enter value to find: ");

value = getInt();

Node found = theTree.find(value);

if(found != null)

System.out.print("Found: ");

found.displayNode();

System.out.print("\n");

else

System.out.print("Could not find ");

System.out.print(value + '\n');

break;

case 'd':

System.out.print("Enter value to delete: ");

value = getInt();
boolean didDelete = theTree.delete(value);

if(didDelete)

System.out.print("Deleted " + value + '\n');

else

System.out.print("Could not delete ");

System.out.print(value + '\n');

break;

case 't':

System.out.print("Enter type 1, 2 or 3: ");

value = getInt();

theTree.traverse(value);

break;

default:

System.out.print("Invalid entry\n");

public static String getString() throws IOException

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

String s = br.readLine();

return s;

public static char getChar() throws IOException

{
String s = getString();

return s.charAt(0);

public static int getInt() throws IOException

String s = getString();

return Integer.parseInt(s);

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy