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

TASK#1+2: Package Import Public Class

The document describes tasks to implement stack data structures using ArrayStack and LinkedStack classes. It involves: 1. Implementing toString(), equals(), bottomElement(), removeBottomElement(), secondElement(), removeSecondElement() methods in both classes. 2. Implementing a toLinkedStack() method in ArrayStack that converts it to a LinkedStack. 3. Similarly, implement a toArrayStack() method in LinkedStack that converts it to an ArrayStack. 4. The document also explores the java.util.Stack class and applies 5 of its methods to a stack of Strings.

Uploaded by

Anber Sattar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

TASK#1+2: Package Import Public Class

The document describes tasks to implement stack data structures using ArrayStack and LinkedStack classes. It involves: 1. Implementing toString(), equals(), bottomElement(), removeBottomElement(), secondElement(), removeSecondElement() methods in both classes. 2. Implementing a toLinkedStack() method in ArrayStack that converts it to a LinkedStack. 3. Similarly, implement a toArrayStack() method in LinkedStack that converts it to an ArrayStack. 4. The document also explores the java.util.Stack class and applies 5 of its methods to a stack of Strings.

Uploaded by

Anber Sattar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

TASK#1+2

Write and test the following methods in both ArrayStack


and LinkedStack class:
1. toString ()
2. equals ()
3. bottomElement ()
4. removeBottomElement ()
5. secondElement ()
6. removeSecondElement ()
2. Implement and test LinkedStack toLinkedStack () method
in ArrayStack class.
CODE:
package LABNO6_TASKS;

import java.util.Arrays;
public class ArrayStack {

Object[] stack;
int size;

public ArrayStack(int capacity) {


stack = new Object[capacity];
}

public ArrayStack() {
stack = new Object[5];
}
public void push(Object value) {
if(size==stack.length) resize();
stack[size++] = value;
}

private void resize() {


stack = Arrays.copyOf(stack, stack.length*2);
}
public Object pop() {
return stack[--size];
}

public Object peek() {


return stack[size-1];
}

public String toString() {


String str = "";

if(!isEmpty()) {
for(int i=0; i<size; i++) {
str += stack[i]+",";
}
}
else {
throw new IllegalStateException("Stack is empty!");
}

str = str.substring(0, str.length()-1);


str = "[" + str + "]";
return str;
}

public boolean isEmpty() {


return size()==0;
}

public int size() {


return size;
}

public boolean equals(ArrayStack stack){


if(this.toString().equals(stack.toString())) {
return true;
}
else {
return false;
}
}

public Object bottomElement() {


return stack[0];
}

public Object secondElement() {


return stack[size-1];
}
public Object removeBottomElement() {
Object bottom = stack[0];

for(int i=0,j=1; i<size-1; i++, j++) {


stack[i] = stack[j];
}
size--;
return bottom;
}

public Object removeSecondElement() {


Object secondElement = stack[size-2];
stack[size-2] = stack[size-1];
size--;
return secondElement;
}

public LinkedStack toLinkedStack() {


LinkedStack linkedStack = new LinkedStack();

for(int i=size-1; i>=0; i--) {


linkedStack.push(stack[i]);
}

return linkedStack;
}

public static void main(String args[]) {

System.out.println();
System.out.println("--->> Array Stack");
System.out.println("_____________________________________________");
System.out.println();
System.out.println();

ArrayStack stack = new ArrayStack();


stack.push("Data Structures & Algorithms");
stack.push("Database System");
stack.push("Operational Research");
stack.push("Software Economics and Management");
stack.push("Software Requirements Engineering ");
stack.push("Object Oreinted Programming");

System.out.println("1. stack.toString() = " + stack.toString());


System.out.println("2. stack.removeSecondElement() :
"+stack.removeSecondElement());
System.out.println("3. stack.toString() = " + stack.toString());
System.out.println("4. stack.pop() : " +stack.pop());
System.out.println("5. stack.pop() : " +stack.pop());
System.out.println("6. stack.toString() : " + stack.toString());
System.out.println("7. stack.removeBottomElement() : " +
stack.removeBottomElement());
System.out.println("8. stack.toString() : " + stack.toString());
System.out.println("9. stack.size() : " + stack.size());
System.out.println("10. stack.isEmpty() : " + stack.isEmpty());

ArrayStack stack2 = new ArrayStack();


stack2.push("Object Oriented Programming");
stack2.push("Sotware Economics and Management");

System.out.println("11. stack2.toString() = " + stack2.toString());


System.out.println("12. stack.equals(stack2) : " + stack.equals(stack2));
System.out.print("13. stack2.toLinkedStack().printStack(): "+
stack2.toLinkedStack().toString());
System.out.println();
System.out.println();
}
}

OUTPUT:

TASK#1+3
Write and test the following methods in both ArrayStack
and LinkedStack class:
1. toString ()
2. equals ()
3. bottomElement ()
4. removeBottomElement ()
5. secondElement ()
6. removeSecondElement ()
3- Implement and test ArrayStack toArrayStack() method in
LinkedStack class.
CODE:
package LABNO6_TASKS;

public class LinkedStack {


Node top;
int size;

public class Node{


Object data;
Node next;

Node(Object data, Node next){


this.data = data;
this.next = next;
}
}

public void push(Object obj) {


top = new Node(obj, top);
size++;
}

public Object pop() {


if(size==0) throw new IllegalStateException("Stack is empty!");
Object temp = top.data;
top = top.next;
size--;
return temp;
}

public Object peek() {


if(size==0) throw new IllegalStateException("Stack is empty!");
return top.data;
}

public String toString() {


String str = "";
if(!isEmpty()) {
for(Node i=top; i!=null; i=i.next) {
str += i.data+",";
}
}
else {
throw new IllegalStateException("Stack is empty!");
}

str = str.substring(0, str.length()-1);


str = "[" + str + "]";
return str;
}

public Object bottomElement() {


if(isEmpty()) throw new IllegalStateException("Stack is empty!");
Object bottomElement;
Node p = top;
while(p.next!=null) {
p = p.next;
}

bottomElement = p.data;
return bottomElement;
}

public Object secondElement() {


if(isEmpty()) throw new IllegalStateException("Stack is empty!");
return top.next.data;
}

public Object removeSecondElement() {


if(isEmpty()) throw new IllegalStateException("Stack is empty!");
size--;
Object secondElement = top.next.data;
top.next = top.next.next;
return secondElement;
}

public Object removeBottomElement() {


if(isEmpty()) throw new IllegalStateException("Stack is empty!");
size--;
Object bottomElement=null;
Node p = top;
int counter=0;
while(p.next!=null) {
counter++;
if(counter==size) {
bottomElement = p.next.data;
p.next = null;
break;
}
p = p.next;
}
return bottomElement;
}

public ArrayStack toArrayStack() {


ArrayStack array = new ArrayStack();

for(Node i=top; i.next!=null; i=i.next) {


array.push(i.data);
}
array.push(top.data);
return array;
}

public boolean equals(LinkedStack stack) {


if(this.toString().equals(stack.toString())) {
return true;
}else
return false;
}

public int size() {


return size;
}

public boolean isEmpty() {


return size==0;
}
public static void main(String[] args) {
System.out.println();
System.out.println("--->> Linked Stack");
System.out.println("______________________________________________");
System.out.println();
System.out.println();

LinkedStack stack1 = new LinkedStack();


stack1.push("Data Structures & Algorithms");
stack1.push("Database System");
stack1.push("Operational Research");
stack1.push("Software Economics and Management");
stack1.push("Software Requirements Engineering ");

LinkedStack stack2 = new LinkedStack();


stack2.push(20);
stack2.push(10);
stack2.push("Allen");
stack2.push(30);
stack2.push(5);

System.out.print("stack1.toString() : "+ stack1.toString());


System.out.println();
System.out.println();

System.out.print("stack2.toString() : "+ stack2.toString());


System.out.println();
System.out.println();

System.out.print("stack1.equals(stack2) : "+ stack1.equals(stack2));


System.out.println();
System.out.println();

System.out.println("stack1.removeBottomElement() : "
+stack1.removeBottomElement());
System.out.println();

System.out.println("stack1.bottomElement() : " +stack1.bottomElement());


System.out.println();

System.out.println("stack1.removeSecondElement() : "
+stack1.removeSecondElement());
System.out.println();

System.out.println("stack1.secondElement() : " +stack1.secondElement());


System.out.println();

System.out.println("stack1.size() : " + stack1.size());


System.out.println();

System.out.println("stack1.pop() : " + stack1.pop());


System.out.println();

System.out.print("stack1.toString() : "+ stack1.toString());


System.out.println();
System.out.println();
System.out.println("stack.toArrayStack() : " +
stack1.toArrayStack().toString());
System.out.println();

OUTPUT:

TASK#4
Explore java.util.Stack class; Create a stack of any type
using this class and apply any 5 of its methods.

CODE:
package LABNO6_TASKS;

import java.util.Stack;
public class Task4 {
public static void main(String[] args) {

Stack<String> stack = new Stack<String>();

/** 1. push() method **/

stack.push("Test 1");
stack.push("Test 2");
stack.push("Test 3");
stack.push("Test 4");
stack.push("Test 5");

System.out.println();
System.out.println();
System.out.println("1. Can't show push() method in the output but it has been
used!");
System.out.println();

/** 2. pop() method **/

System.out.println("2. stack.pop() : " + stack.pop());


System.out.println();

/** 3. empty() method **/

System.out.println("4. stack.empty() : " + stack.empty());


System.out.println();

/** 4. toString() method **/

System.out.println("5. stack.toString(): " + stack.toString());


System.out.println();
System.out.println();

/** 5. peek() method **/

System.out.println("3. stack.peek() : " + stack.peek());


System.out.println();

}
}

OUTPUT:

You might also like