Java Data Structure
Java Data Structure
Learn how to play with data structure in Java programming. Here are most commonly used examples:
. 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?
Solution:
Following example demonstrates how to add first n natural numbers by using the concept of stack .
import java.io.IOException;
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:
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 linkedlistname.getFirst() and linkedlistname.getLast() of LinkedList class.
import java.util.LinkedList;
Result:
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 java.util.LinkedList;
Result:
1, 2, 3, 4, 5
0, 1, 2, 3, 4, 5
0, 1, 2, 3, 4, 5, 6
Solution:
Following example demonstrates how to convert an infix to postfix expression by using the concept of stack.
import java.io.IOException;
Page 64
break;
case '*':
case '/':
gotOper(ch, 2);
break;
case '(':
theStack.push(ch);
break;
case ')':
gotParen(ch);
break;
default:
output = output + ch;
break;
}
}
while (!theStack.isEmpty()) {
output = output + theStack.pop();
}
System.out.println(output);
return output;
}
public void gotOper(char opThis, int prec1) {
while (!theStack.isEmpty()) {
char opTop = theStack.pop();
if (opTop == '(') {
theStack.push(opTop);
break;
}
else {
int prec2;
if (opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) {
theStack.push(opTop);
break;
}
else
output = output + opTop;
}
}
theStack.push(opThis);
}
public void gotParen(char ch){
while (!theStack.isEmpty()) {
char chx = theStack.pop();
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 =
theTrans.doTrans(); System.out.println("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:
124*5/+7-36/+
Postfix is 124*5/+7-36/+
Solution:
import java.util.LinkedList;
class GenQueue {
private LinkedList list = new LinkedList();
public void enqueue(E item) {
list.addLast(item);
}
public E dequeue() {
return list.poll();
}
public boolean hasItems() {
return !list.isEmpty();
}
public int size() {
return list.size();
}
public void addItems(GenQueue q) {
while (q.hasItems())
list.addLast(q.dequeue());
}
}
Page 66
public static void main(String[] args) {
GenQueue empList;
empList = new GenQueue();
GenQueue hList;
hList = new GenQueue();
hList.enqueue(new HourlyEmployee("T", "D"));
hList.enqueue(new HourlyEmployee("G", "B"));
hList.enqueue(new HourlyEmployee("F", "S"));
empList.addItems(hList);
System.out.println("The employees' names are:");
while (empList.hasItems()) {
Employee emp = empList.dequeue();
System.out.println(emp.firstName + " "
+ emp.lastName);
}
}
}
class Employee {
public String lastName;
public String firstName;
public Employee() {
}
public Employee(String last, String first) {
this.lastName = last;
this.firstName = first;
}
public String toString() {
return firstName + " " + lastName;
}
}
Result:
Solution:
Following example shows how to reverse a string using stack with the help of user defined
method StringReverserThroughStack().
import java.io.IOException;
Page 67
public class StringReverserThroughStack {
private String input;
private String output;
public StringReverserThroughStack(String in)
{ input = in;
}
public String doRev() {
int stackSize = input.length();
Stack theStack = new Stack(stackSize);
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
theStack.push(ch);
}
output = "";
while (!theStack.isEmpty()) {
char ch = theStack.pop();
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 = theReverser.doRev();
System.out.println("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:
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
linkedlistname.indexof(element) to get the first position of the element and
linkedlistname.Lastindexof(elementname) to get the last position of the element inside the linked list.
import java.util.LinkedList;
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.
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);
theStack.push(10);
theStack.push(20);
theStack.push(30);
theStack.push(40);
theStack.push(50);
while (!theStack.isEmpty()) {
long value = theStack.pop();
System.out.print(value);
System.out.print(" ");
}
System.out.println("");
}
}
Result:
50 40 30 20 10
Solution:
Following example .
import java.util.Collections;
import java.util.Vector;
Page 70
}
Result:
1 2 34 5
After swapping
5 2 34 1
Solution:
Following example demonstrates how to update a linked list by using listname.add() and listname.set()
methods of LinkedList class.
import java.util.LinkedList;
Result:
BBTHP
BBMHP
Solution:
Following example demonstrates how to get the maximum element of a vector by using v.add() method of
Vector class and Collections.max() method of Collection class.
import java.util.Collections;
Page 71
import java.util.Vector;
Result:
Solution:
Following example how to execute binary search on a vector with the help of v.add() method of Vector
class and sort.Collection() method of Collection class.
import java.util.Collections;
import java.util.Vector;
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 java.util.*;
Result:
39
39
38
37
Solution:
Following example demonstrates how to delete many elements of linkedList using Clear() method.
import java.util.*;
Page 73
lList.add("6");
lList.add("4");
lList.add("5");
System.out.println(lList);
lList.subList(2, 4).clear();
System.out.println(lList);
}
}
Result: