Java Data Structure - Programming Examples
Learn how to play with data structure in Java programming. Here are most commonly used examples:
1. How to print summation of n numbers?
2. How to get the first and the last element of a linked list?
. Page 61
3. How to add an element at first and last position of a linked list?
4. How to convert an infix expression to postfix expression?
5. How to implement Queue?
6. How to reverse a string using stack?
7. How to search an element inside a linked list?
8. How to implement stack?
9. How to swap two elements in a vector?
10. How to update a linked list?
11. How to get the maximum element from a vector?
12. How to execute binary search on a vector?
13. How to get elements of a LinkedList?
14. How to delete many elements from a linkedList?
How to print summation of n numbers?
Solution:
Following example demonstrates how to add first n natural numbers by using the concept of stack .
import [Link];
public class AdditionStack {
static int num;
static int ans;
static Stack theStack;
public static void main(String[] args)
throws IOException {
num = 50;
stackAddition();
[Link]("Sum=" + ans);
}
public static void stackAddition() {
theStack = new Stack(10000);
ans = 0;
while (num > 0) {
[Link](num);
--num;
}
while (![Link]()) {
int newN = [Link]();
ans += newN;
}
}
}
class Stack {
private int maxSize;
private int[] data;
private int top;
public Stack(int s) {
maxSize = s;
data = new int[maxSize];
top = -1;
}
public void push(int p) {
data[++top] = p;
}
Page 62
public int pop() {
return data[top--];
}
public int peek() {
return data[top];
}
public boolean isEmpty() {
return (top == -1);
}
}
Result:
The above code sample will produce the following result.
Sum=1225
How to get the first and the last element of a linked list ?
Solution:
Following example shows how to get the first and last element of a linked list with the help
of [Link]() and [Link]() of LinkedList class.
import [Link];
public class Main {
public static void main(String[] args) {
LinkedList lList = new LinkedList();
[Link]("100");
[Link]("200");
[Link]("300");
[Link]("400");
[Link]("500");
[Link]("First element of LinkedList is :
" + [Link]());
[Link]("Last element of LinkedList is :
" + [Link]());
}
}
Result:
The above code sample will produce the following result.
First element of LinkedList is :100
Last element of LinkedList is :500
How to add an element at first and last position of a linked list?
Page 63
Solution:
Following example shows how to add an element at the first and last position of a linked list by using
addFirst() and addLast() method of Linked List class.
import [Link];
public class Main {
public static void main(String[] args) {
LinkedList lList = new LinkedList();
[Link]("1");
[Link]("2");
[Link]("3");
[Link]("4");
[Link]("5");
[Link](lList);
[Link]("0");
[Link](lList);
[Link]("6");
[Link](lList);
}
}
Result:
The above code sample will produce the following result.
1, 2, 3, 4, 5
0, 1, 2, 3, 4, 5
0, 1, 2, 3, 4, 5, 6
How to convert an infix expression to postfix expression?
Solution:
Following example demonstrates how to convert an infix to postfix expression by using the concept of stack.
import [Link];
public class InToPost {
private Stack theStack;
private String input;
private String output = "";
public InToPost(String in) {
input = in;
int stackSize = [Link]();
theStack = new Stack(stackSize);
}
public String doTrans() {
for (int j = 0; j < [Link](); j++) {
char ch = [Link](j);
switch (ch) {
case '+':
case '-':
gotOper(ch, 1);
Page 64
break;
case '*':
case '/':
gotOper(ch, 2);
break;
case '(':
[Link](ch);
break;
case ')':
gotParen(ch);
break;
default:
output = output + ch;
break;
}
}
while (![Link]()) {
output = output + [Link]();
}
[Link](output);
return output;
}
public void gotOper(char opThis, int prec1) {
while (![Link]()) {
char opTop = [Link]();
if (opTop == '(') {
[Link](opTop);
break;
}
else {
int prec2;
if (opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) {
[Link](opTop);
break;
}
else
output = output + opTop;
}
}
[Link](opThis);
}
public void gotParen(char ch){
while (![Link]()) {
char chx = [Link]();
if (chx == '(')
break;
else
output = output + chx;
}
}
public static void main(String[] args)
throws IOException {
String input = "1+2*4/5-7+3/6";
String output;
InToPost theTrans = new InToPost(input); output =
[Link](); [Link]("Postfix
is " + output + '\n');
}
class Stack {
private int maxSize;
private char[] stackArray;
Page 65
private int top;
public Stack(int max) {
maxSize = max;
stackArray = new char[maxSize];
top = -1;
}
public void push(char j) {
stackArray[++top] = j;
}
public char pop() {
return stackArray[top--];
}
public char peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
}
}
Result:
The above code sample will produce the following result.
124*5/+7-36/+
Postfix is 124*5/+7-36/+
How to implement Queue ?
Solution:
Following example shows how to implement a queue in an employee structure.
import [Link];
class GenQueue {
private LinkedList list = new LinkedList();
public void enqueue(E item) {
[Link](item);
}
public E dequeue() {
return [Link]();
}
public boolean hasItems() {
return ![Link]();
}
public int size() {
return [Link]();
}
public void addItems(GenQueue q) {
while ([Link]())
[Link]([Link]());
}
}
public class GenQueueTest {
Page 66
public static void main(String[] args) {
GenQueue empList;
empList = new GenQueue();
GenQueue hList;
hList = new GenQueue();
[Link](new HourlyEmployee("T", "D"));
[Link](new HourlyEmployee("G", "B"));
[Link](new HourlyEmployee("F", "S"));
[Link](hList);
[Link]("The employees' names are:");
while ([Link]()) {
Employee emp = [Link]();
[Link]([Link] + " "
+ [Link]);
}
}
}
class Employee {
public String lastName;
public String firstName;
public Employee() {
}
public Employee(String last, String first) {
[Link] = last;
[Link] = first;
}
public String toString() {
return firstName + " " + lastName;
}
}
class HourlyEmployee extends Employee {
public double hourlyRate; public
HourlyEmployee(String last, String first) {
super(last, first);
}
}
Result:
The above code sample will produce the following result.
The employees' name are:
T D
G B
F S
How to reverse a string using stack ?
Solution:
Following example shows how to reverse a string using stack with the help of user defined
method StringReverserThroughStack().
import [Link];
Page 67
public class StringReverserThroughStack {
private String input;
private String output;
public StringReverserThroughStack(String in)
{ input = in;
}
public String doRev() {
int stackSize = [Link]();
Stack theStack = new Stack(stackSize);
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
[Link](ch);
}
output = "";
while (![Link]()) {
char ch = [Link]();
output = output + ch;
}
return output;
}
public static void main(String[] args)
throws IOException {
String input = "Java Source and Support";
String output;
StringReverserThroughStack theReverser =
new StringReverserThroughStack(input);
output = [Link]();
[Link]("Reversed: " + output);
}
class Stack {
private int maxSize;
private char[] stackArray;
private int top;
public Stack(int max) {
maxSize = max;
stackArray = new char[maxSize];
top = -1;
}
public void push(char j) {
stackArray[++top] = j;
}
public char pop() {
return stackArray[top--];
}
public char peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
}
}
Result:
The above code sample will produce the following result.
JavaStringReversal
Reversed:lasreveRgnirtSavaJ
Page 68
How to search an element inside a linked list ?
Solution:
Following example demonstrates how to search an element inside a linked list using
[Link](element) to get the first position of the element and
[Link](elementname) to get the last position of the element inside the linked list.
import [Link];
public class Main {
public static void main(String[] args) {
LinkedList lList = new LinkedList();
[Link]("1");
[Link]("2");
[Link]("3");
[Link]("4");
[Link]("5");
[Link]("2");
[Link]("First index of 2 is:"+
[Link]("2"));
[Link]("Last index of 2 is:"+
[Link]("2"));
}
}
Result:
The above code sample will produce the following result.
First index of 2 is: 1
Last index of 2 is: 5
How to implement stack?
Solution:
Following example shows how to implement stack by creating user defined push() method for
entering elements and pop() method for retriving elements from the stack.
public class MyStack {
private int maxSize;
private long[] stackArray;
private int top;
public MyStack(int s) {
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
public void push(long j) {
stackArray[++top] = j;
Page 69
}
public long pop() {
return stackArray[top--];
}
public long peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
public static void main(String[] args) {
MyStack theStack = new MyStack(10);
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);
while (![Link]()) {
long value = [Link]();
[Link](value);
[Link](" ");
}
[Link]("");
}
}
Result:
The above code sample will produce the following result.
50 40 30 20 10
How to swap two elements in a vector ?
Solution:
Following example .
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
Vector v = new Vector();
[Link]("1");
[Link]("2");
[Link]("3");
[Link]("4");
[Link]("5");
[Link](v);
[Link](v, 0, 4);
[Link]("After swapping");
[Link](v);
}
Page 70
}
Result:
The above code sample will produce the following result.
1 2 34 5
After swapping
5 2 34 1
How to update a linked list ?
Solution:
Following example demonstrates how to update a linked list by using [Link]() and [Link]()
methods of LinkedList class.
import [Link];
public class MainClass {
public static void main(String[] a) {
LinkedList officers = new LinkedList();
[Link]("B");
[Link]("B");
[Link]("T");
[Link]("H");
[Link]("P");
[Link](officers);
[Link](2, "M");
[Link](officers);
}
}
Result:
The above code sample will produce the following result.
BBTHP
BBMHP
How to get the maximum element from a vector ?
Solution:
Following example demonstrates how to get the maximum element of a vector by using [Link]() method of
Vector class and [Link]() method of Collection class.
import [Link];
Page 71
import [Link];
public class Main {
public static void main(String[] args) {
Vector v = new Vector();
[Link](new Double("3.4324"));
[Link](new Double("3.3532"));
[Link](new Double("3.342"));
[Link](new Double("3.349"));
[Link](new Double("2.3"));
Object obj = [Link](v);
[Link]("The max element is:"+obj);
}
}
Result:
The above code sample will produce the following result.
The max element is: 3.4324
How to execute binary search on a vector ?
Solution:
Following example how to execute binary search on a vector with the help of [Link]() method of Vector
class and [Link]() method of Collection class.
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
Vector v = new Vector();
[Link]("X");
[Link]("M");
[Link]("D");
[Link]("A");
[Link]("O");
[Link](v);
[Link](v);
int index = [Link](v, "D");
[Link]("Element found at : " + index);
}
}
Result:
The above code sample will produce the following result.
[A, D, M, O, X]
Element found at : 1
Page 72
How to get elements of a LinkedList?
Solution:
Following example demonstrates how to get elements of LinkedList using top() & pop() methods.
import [Link].*;
public class Main {
private LinkedList list = new LinkedList();
public void push(Object v) {
[Link](v);
}
public Object top() {
return [Link]();
}
public Object pop() {
return [Link]();
}
public static void main(String[] args) {
Main stack = new Main();
for (int i = 30; i < 40; i++)
[Link](new Integer(i));
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
}
}
Result:
The above code sample will produce the following result.
39
39
38
37
How to delete many elements from a linkedList?
Solution:
Following example demonstrates how to delete many elements of linkedList using Clear() method.
import [Link].*;
public class Main {
public static void main(String[] args) {
LinkedList lList = new LinkedList();
[Link]("1");
[Link]("8");
Page 73
[Link]("6");
[Link]("4");
[Link]("5");
[Link](lList);
[Link](2, 4).clear();
[Link](lList);
}
}
Result:
The above code sample will produce the following result.
[one, two, three, four, five]
[one, two, three, Replaced, five]