Thursday, October 21, 2010
Data Structure Lab03 "Stack"
import dataStructures.DerivedLinkedStack;
public class MyStack extends DerivedLinkedStack {
public static void main(String[] args) {
MyStack newStack = new MyStack();
MyStack clonedStack;
newStack.push("c");
newStack.push("s");
newStack.push("2");
newStack.push("0");
newStack.push("4");
newStack.push("b");
newStack.push("a");
newStack.push("l");
System.out.println("newStack:" + newStack.toString());
// newStack:[l, a, b, 4, 0, 2, s, c]
clonedStack = (MyStack) newStack.clone();
System.out.println("clonedStack:" + clonedStack.toString());
// clonedStack:[l, a, b, 4, 0, 2, s, c]
newStack.reverse(3);
newStack.reverse(8);
System.out.println("newStack reversed 3 and 8:" + newStack.toString());
// newStack reversed 3 and 8:[c, s, 2, 0, 4, l, a, b]
clonedStack.pop();
clonedStack.pop();
clonedStack.pop();
System.out.println("clonedStack 3 pop:" + clonedStack.toString());
System.out.println("newStack:" + newStack.toString());
//clonedStack 3 pop:[4, 0, 2, s, c]
newStack.moveTo(clonedStack);
System.out.println("clonedStack moved:" + clonedStack.toString());
// clonedStack moved:[c, s, 2, 0, 4, l, a, b, 4, 0, 2, s, c]
System.out.println("newStack moved:" + newStack.toString());
// newStack moved:[]
newStack.push("ehend orson");
newStack.push("xoert orson");
newStack.push("gurawt orson");
newStack.push("dorovt orson");
System.out.println("newStack:" + newStack.toString());
// newStack:[dorovt orson, gurawt orson, xoert orson, ehend orson]
clonedStack.mixTo(newStack);
System.out.println("clonedStack mixed:" + clonedStack.toString());
// clonedStack mixed:[]
System.out.println("newStack mixed:" + newStack.toString());
// newStack mixed:[dorovt orson, c, gurawt orson, s, xoert orson, 2, ehend orson, 0, 4, l, a, b, 4, 0, 2, s, c]
newStack.clear();
System.out.println("newStack cleared:" + newStack.toString());
// newStack cleared:[]
}
public Object clone() {
MyStack clonedStack1 = new MyStack();
while (!empty())
{
clonedStack1.push(peek());
pop();
}
MyStack clonedStack = new MyStack();
while (!clonedStack1.empty())
{
clonedStack.push(clonedStack1.peek());
push(clonedStack1.peek());
clonedStack1.pop();
}
// this-iig clonedStack ruu huulna
return clonedStack;
}
public void reverse(int elementCount) {
MyStack reversedstack = new MyStack();
MyStack reversedstack1 = new MyStack();
for(int i=1; i<=elementCount; i++)
{
reversedstack.push(pop());
}
while (!reversedstack.empty())
{
reversedstack1.push(reversedstack.pop());
}
while (!reversedstack1.empty())
{
push(reversedstack1.pop());
}
// exnii elementCount toonii elementiin bairiig urvuulna
}
public void moveTo(MyStack targetStack) {
MyStack targetStack1 = new MyStack();
while (!empty() )
{
targetStack1.push(peek());
pop();
}
while (!targetStack1.empty())
{
targetStack.push(targetStack1.pop());
}
// this-iin bux elementiig targetStack ruu bairlaliig n
// aldagduulalgui xiine
}
public void mixTo(MyStack targetStack) {
MyStack targetStack1 = new MyStack();
MyStack targetStack2 = new MyStack();
MyStack targetStack3 = new MyStack();
while(!targetStack.empty())
{
targetStack2.push(targetStack.peek());
targetStack.pop();
targetStack1.push(peek());
pop();
}
while (!empty() ){
targetStack3.push(pop());
}
while (!targetStack3.empty() ){
targetStack.push(targetStack3.pop());
}
while (!targetStack1.empty() && !targetStack2.empty() ){
targetStack.push(targetStack1.pop());
targetStack.push(targetStack2.pop());
}
// this-iin bux elementiig targetStack ruu bairlaliig n
// sooljluulen xiine
}
public void clear() {
// this-iin bux elementiig avch hoosolno
while (!empty())
{
pop();
}
}
}
public class MyStack extends DerivedLinkedStack {
public static void main(String[] args) {
MyStack newStack = new MyStack();
MyStack clonedStack;
newStack.push("c");
newStack.push("s");
newStack.push("2");
newStack.push("0");
newStack.push("4");
newStack.push("b");
newStack.push("a");
newStack.push("l");
System.out.println("newStack:" + newStack.toString());
// newStack:[l, a, b, 4, 0, 2, s, c]
clonedStack = (MyStack) newStack.clone();
System.out.println("clonedStack:" + clonedStack.toString());
// clonedStack:[l, a, b, 4, 0, 2, s, c]
newStack.reverse(3);
newStack.reverse(8);
System.out.println("newStack reversed 3 and 8:" + newStack.toString());
// newStack reversed 3 and 8:[c, s, 2, 0, 4, l, a, b]
clonedStack.pop();
clonedStack.pop();
clonedStack.pop();
System.out.println("clonedStack 3 pop:" + clonedStack.toString());
System.out.println("newStack:" + newStack.toString());
//clonedStack 3 pop:[4, 0, 2, s, c]
newStack.moveTo(clonedStack);
System.out.println("clonedStack moved:" + clonedStack.toString());
// clonedStack moved:[c, s, 2, 0, 4, l, a, b, 4, 0, 2, s, c]
System.out.println("newStack moved:" + newStack.toString());
// newStack moved:[]
newStack.push("ehend orson");
newStack.push("xoert orson");
newStack.push("gurawt orson");
newStack.push("dorovt orson");
System.out.println("newStack:" + newStack.toString());
// newStack:[dorovt orson, gurawt orson, xoert orson, ehend orson]
clonedStack.mixTo(newStack);
System.out.println("clonedStack mixed:" + clonedStack.toString());
// clonedStack mixed:[]
System.out.println("newStack mixed:" + newStack.toString());
// newStack mixed:[dorovt orson, c, gurawt orson, s, xoert orson, 2, ehend orson, 0, 4, l, a, b, 4, 0, 2, s, c]
newStack.clear();
System.out.println("newStack cleared:" + newStack.toString());
// newStack cleared:[]
}
public Object clone() {
MyStack clonedStack1 = new MyStack();
while (!empty())
{
clonedStack1.push(peek());
pop();
}
MyStack clonedStack = new MyStack();
while (!clonedStack1.empty())
{
clonedStack.push(clonedStack1.peek());
push(clonedStack1.peek());
clonedStack1.pop();
}
// this-iig clonedStack ruu huulna
return clonedStack;
}
public void reverse(int elementCount) {
MyStack reversedstack = new MyStack();
MyStack reversedstack1 = new MyStack();
for(int i=1; i<=elementCount; i++)
{
reversedstack.push(pop());
}
while (!reversedstack.empty())
{
reversedstack1.push(reversedstack.pop());
}
while (!reversedstack1.empty())
{
push(reversedstack1.pop());
}
// exnii elementCount toonii elementiin bairiig urvuulna
}
public void moveTo(MyStack targetStack) {
MyStack targetStack1 = new MyStack();
while (!empty() )
{
targetStack1.push(peek());
pop();
}
while (!targetStack1.empty())
{
targetStack.push(targetStack1.pop());
}
// this-iin bux elementiig targetStack ruu bairlaliig n
// aldagduulalgui xiine
}
public void mixTo(MyStack targetStack) {
MyStack targetStack1 = new MyStack();
MyStack targetStack2 = new MyStack();
MyStack targetStack3 = new MyStack();
while(!targetStack.empty())
{
targetStack2.push(targetStack.peek());
targetStack.pop();
targetStack1.push(peek());
pop();
}
while (!empty() ){
targetStack3.push(pop());
}
while (!targetStack3.empty() ){
targetStack.push(targetStack3.pop());
}
while (!targetStack1.empty() && !targetStack2.empty() ){
targetStack.push(targetStack1.pop());
targetStack.push(targetStack2.pop());
}
// this-iin bux elementiig targetStack ruu bairlaliig n
// sooljluulen xiine
}
public void clear() {
// this-iin bux elementiig avch hoosolno
while (!empty())
{
pop();
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment