Final Code Compilation IN Data Structure: SUBMITTED BY: JP Parungao & Renz Hidalgo BSCS201
Final Code Compilation IN Data Structure: SUBMITTED BY: JP Parungao & Renz Hidalgo BSCS201
Final Code Compilation IN Data Structure: SUBMITTED BY: JP Parungao & Renz Hidalgo BSCS201
IN
DATA STRUCTURE
BSCS201
First program
class hello {
public static void main(String[] args) {
Output:
Magic square
import java.io.*;
class MagicSquare
{
static final int maxsize = 50;
int i, j, k, l, n, key;
boolean n_ok;
String line;
(new InputStreamReader(System.in));
try
line = KeybIn.readLine();
n = Integer.parseInt(line);
if ( n_ok )
for (i=0;i<n;i++)
square[0][(int)(n-1)/2] = 1;
key = 2;
i = 0;
j = (int)(n-1)/2;
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++;
for (i=0;i<n;i++)
System.out.println();
else
if ( n % 2 == 0)
System.out.println("Error!!! n is even");
}
catch (NumberFormatException e)
Output:
arrays
class array {
long[] array;
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;
System.out.println("");
searchKey = 66;
if(array[j] == searchKey)
break;
if(j == nElems)
else
searchKey = 55;
if(array[j] == searchKey)
break;
array[k] = array[k+1];
nElems--;
Output:
bubblesort
class bubblesort {
int i;
bubbleSort(unsortedArray, unsortedArray.length);
temp = unsortedArray[index];
unsortedArray[index] = unsortedArray[index+1];
unsortedArray[index+1] = temp;
Output:
Selection arraybubble
class ArrayBub
private long[] a;
a = new long[max];
nElems = 0;
a[nElems] = value;
nElems++;
System.out.println("");
swap(in, in+1);
a[one] = a[two];
a[two] = temp;
class BubbleSortApp
{
int maxSize = 100;
ArrayBub arr;
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{
int j = 1;
j = i * (i+1)/2;
System.out.println("Returning:" + j);
Output:
Deck of cards
class lab3{
int i;
int array[] = {1,2,3,4,6,7,8,9,10,5};
System.out.println("Deck of Cards\n");
System.out.println();
insertion_srt(array, array.length);
System.out.print(array[i]+" ");
System.out.println();
int j = i;
int B = array[i];
array[j] = array[j-1];
j--;
array[j] = B;
Output:
Triangular
import java.io.*
class triangular {
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
int choice;
System.out.println(" ");
for(int b=0;b<emplist.size();b++)
{
System.out.println(emplist.get(b));
System.out.println();
do
choice = Integer.parseInt(a.readLine());
switch(choice)
case 1:
String NL = a.readLine();
emplist.add(NL);
break;
case 2:
String RL = a.readLine();
int i = emplist.indexOf(RL);
if(i != -1)
emplist.remove(i);
}else
break;
case 3:
for(int b=0;b<emplist.size();b++)
System.out.println(emplist.get(b));
System.out.println();
break;
case 4:
String FL = a.readLine();
if(indexOf != -1)
}else
break;
case 5:
break;
} while(choice !=5);
Output:
Node
import java.io.*;
import java.util.*;
class Node
System.out.print('{');
System.out.print(iData);
System.out.print(", ");
System.out.print(dData);
System.out.print("} ");
class Tree
{ root = null; }
while(current.iData != key)
current = current.leftChild;
else
current = current.rightChild;
if(current == null)
return null;
return current;
newNode.iData = id;
newNode.dData = dd;
if(root==null)
root = newNode;
else
while(true)
parent = current;
current = current.leftChild;
if(current == null)
parent.leftChild = newNode;
return;
else
current = current.rightChild;
if(current == null)
parent.rightChild = newNode;
return;
{
Node current = root;
while(current.iData != key)
parent = current;
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
if(current == root)
root = successor;
else if(isLeftChild)
parent.leftChild = successor;
else
parent.rightChild = successor;
successor.leftChild = current.leftChild;
return true;
while(current != null)
successorParent = successor;
successor = current;
current = current.leftChild;
if(successor != delNode.rightChild)
successorParent.leftChild = successor.rightChild;
successor.rightChild = delNode.rightChild;
return successor;
switch(traverseType)
preOrder(root);
break;
inOrder(root);
break;
postOrder(root);
break;
System.out.println();
if(localRoot != null)
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
{
if(localRoot != null)
inOrder(localRoot.leftChild);
inOrder(localRoot.rightChild);
if(localRoot != null)
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
globalStack.push(root);
System.out.println(
"......................................................");
while(isRowEmpty==false)
{
Stack localStack = new Stack();
isRowEmpty = true;
System.out.print(' ');
while(globalStack.isEmpty()==false)
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);
System.out.print(' ');
System.out.println();
nBlanks /= 2;
while(localStack.isEmpty()==false)
globalStack.push( localStack.pop() );
System.out.println(
"......................................................");
class TreeApp
int value;
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)
switch(choice)
case 's':
theTree.displayTree();
break;
case 'i':
value = getInt();
break;
case 'f':
value = getInt();
if(found != null)
System.out.print("Found: ");
found.displayNode();
System.out.print("\n");
else
System.out.print(value + '\n');
break;
case 'd':
value = getInt();
boolean didDelete = theTree.delete(value);
if(didDelete)
else
System.out.print(value + '\n');
break;
case 't':
value = getInt();
theTree.traverse(value);
break;
default:
System.out.print("Invalid entry\n");
String s = br.readLine();
return s;
{
String s = getString();
return s.charAt(0);
String s = getString();
return Integer.parseInt(s);
Output: