1. Using an array implementation, the code and the results are as follows:
Requirements:
Write Stack.java, implement the Stack function, use an array of int to save the data features: Advanced backward First Out
Write Queue.java, implement the queue function, use an array of int to save the data features: Advanced first out backward out using Test.java to test stacks and queues
As shown below, the implementation method is:
First implement the Stack.java class, whose code looks like this:
Package ex.md05;
public class Stack
{
private int[] a=new int[1000];
private int i=0;
public void push (int m)
{
a[++i]=m;
}
public int POPs ()
{
if (i>0)
{
return a[i--];
}
else return -1;
}
}
Then implement the queue Queue.java code, as shown below:
Package ex.md05;
public class Queue
{
int[] a=new int[1000];
private int i=1;
public void in (int m)
{
a[i++]=m;
}
public int Out ()
{
int k=1;
int index=0;
int temp=a[k];
for (int j=k;j<i;j++)
{
a[j-1]=a[j];
index++;
}
I=index;
return temp;
}
Finally, we implement the test class Test.java to see if the logic is correct
Package ex.md05;
public class Test {public static void main (string[] args) {Stack stack = new Stack ();
Stack.pop ();
SYSTEM.OUT.PRINTLN ("Stack push ()---1-200--------");
for (int i=1; i<=200; i++) {Stack.push (i);
} System.out.println ("Stack POPs ()---1-100--------");
for (int i=1; i<=100; i++) {System.out.println ("pop:" + stack.pop ());
} System.out.println ("Stack push ()---201-300--------");
for (int i=201; i<=300; i++) {Stack.push (i);
} System.out.println ("Stack POPs ()---1-200--------");
for (int i=1; i<=200; i++) {System.out.println ("pop:" + stack.pop ());
Queue queue = new Queue ();
Queue.out ();
System.out.println ("Queue in ()---1-200--------");
for (int i=1; i<=200; i++) {queue.in (i);
} System.out.println ("Queue out ()---1-100--------");
for (int i=1; i<=100; i++) {System.out.println ("out:" + queue.out ());
} System.out.println ("Queue in ()---201-300--------"); for (int i=201; i<=300; i++) {queue.in (i);
} System.out.println ("Queue out ()---1-200--------");
for (int i=1; i<=200; i++) {System.out.println ("out:" + queue.out ()); }
}
}
After the compilation runs, we can see that the results of the run are as follows:
According to the analysis, our results are correct (the above is only part of the results, the following is the results of the operation, please download the view http://download.csdn.net/my)
2. Using the list to implement the function, the code looks like this:
Write Mystack class, implement Stack function. Use ArrayList to save data in a class.
Write Myqueue class to implement the queue function. Use ArrayList to save data in a class.
Using Test.java test stacks and queues
First implement Mystack.java code:
Package sample;
Import java.util.*;
public class Mystack
{
list<integer> List = new arraylist<integer> ();
Iterator It=list.iterator ();
int index=0;
Public Mystack () {} public
void push (Integer i)
{
list.add (i);
index++;
}
Public Integer pop ()
{
if (! List.isEmpty ())
{
index--;
Return (Integer) list.remove (index);
}
return null;
}
}
Implement Myqueue.java class:
Package sample;
Import java.util.*;
public class Myqueue
{
list<integer> List = new arraylist<integer> ();
Iterator It=list.iterator ();
int index=0;
Public Myqueue () {} public
void in (Integer i)
{
list.add (i);
index++;
}
Public Integer out ()
{
if (! List.isEmpty ())
{
Integer temp=0;
temp= (Integer) list.get (0);
List.remove (0);
index--;
return temp;
}
return null;
}
}
Test using the test class, as shown in the following code:
Package sample;
public class Test {public static void main (string[] args) {Mystack stack = new Mystack ();
Stack.push (New Integer (1));
Stack.push (New Integer (2));
Stack.push (New Integer (3));
System.out.println (Stack.pop ());
Stack.push (New Integer (4));
System.out.println (Stack.pop ());
System.out.println (Stack.pop ());
System.out.println (Stack.pop ());
System.out.println (Stack.pop ());
Stack.push (New Integer (5));
System.out.println (Stack.pop ());
System.out.println (Stack.pop ());
System.out.println ("------------------------");
Myqueue queue = new Myqueue ();
Queue.in (New Integer (1));
Queue.in (New Integer (2));
Queue.in (New Integer (3));
System.out.println (Queue.out ());
Queue.in (New Integer (4));
System.out.println (Queue.out ());
System.out.println (Queue.out ());
System.out.println (Queue.out ());
System.out.println (Queue.out ());
Queue.in (New Integer (5));
System.out.println (Queue.out ());
System.out.println (Queue.out ()); }
}
The results are as follows:
The above is the two ways to implement the stack