Основной тестовый файл:
Код: Выделить всё
import java.util.Iterator;
class Test
{
// Pushing element on the top of the stack
static void stack_push(Stack stack)
{
for(int i = 0; i < 5; i++)
{
stack.push(i);
}
}
// Popping element from the top of the stack
static void stack_pop(Stack stack)
{
System.out.println("Pop Operation:");
for(int i = 0; i < 5; i++)
{
Integer y = (Integer) stack.pop();
System.out.println(y);
}
}
// Displaying element on the top of the stack
static void stack_peek(Stack stack)
{
Integer element = (Integer) stack.peek();
System.out.println("Element on stack top: " + element);
}
// Searching element in the stack
static void stack_search(Stack stack, int element)
{
Integer pos = (Integer) stack.search(element);
if(pos == -1)
System.out.println("Element not found");
else
System.out.println("Element is found at position: " + pos);
}
public static void main (String[] args)
{
Stack stack = new Stack();
stack_push(stack);
stack_pop(stack);
stack_push(stack);
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack, 6);
// Displaying the Stack
System.out.println("Stack: " + stack);
// Creating an iterator
Iterator value = stack.iterator();
// Displaying the values
// after iterating through the stack
System.out.println("The iterator values are: ");
while (value.hasNext()) {
System.out.println(value.next());
}
for (Integer i : stack){
// for (Integer i = stack.peek(); stack.hasNext(); stack.next()) { //this works
System.out.println( i);
}
}
}
Код: Выделить всё
import java.util.Iterator;
public class Stack implements Iterable, Iterator{
T obj;
LinkedList s;
T current = null;
ListIterator iter;
/**
* Creates a new instance of the stack object with the data type T specified.
*
* @param none the data that will be added to the stack
*/
public Stack(){
s = new LinkedList();
}
/**
* Addes and item to the top of the stack object.
*
* @param data the data that will be added to the stack
* @return none
*/
public void push(T data){
s.add(0,data);
}
/**
* Removes the item at the top of the stack, and returns it to the calling method.
*
* @param none the data that will be added to the stack
* @return The data in the top of the stack
*/
public T pop() {
T d = s.get(0);
s.remove(0);
return d;
}
/**
* Returns the top item of the stack to the calling method, without removing the item.
*
* @param none the data that will be added to the stack
* @return The data in the top of the stack
*/
public T peek(){
return s.get(0);
}
/**
* Returns the index of the first instance of the data passes in
* parameter elem.
*
* @param elem the data search will search for
* @return The index of the first match
*/
public int search(T elem) {
return s.search(elem);
}
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public T next() {
return iter.next();
}
@Override
public ListIterator iterator() {
iter = s.iterator();
return iter;
}
}
Код: Выделить всё
import java.util.Iterator;
// import java.util.ListIterator;
public class LinkedList implements Iterable {
private node head;
private node tail;
private int length = 0;
public LinkedList () {
head = null;
tail = head;
}
public void add(T data){
node temp = new node(data);
if (this.head == null){
head.next = temp;
length = 1;
return;
}
tail.next = temp;
length++;
}
public void add(int position, T data){
node temp = new node(data);
temp.next = head;
head = temp;
}
public T get(int i){
if (i>length) {
// error, how do I return an error?
}
int counter = 0;
node tmp = head;
while (tmp != null && counter < i){
tmp = tmp.next;
}
return tmp.data;
}
public void remove(int index) {
if (index > length) {
// error, how do I return an error?
}
int counter = 0;
node tmp = head;
node prev = null;
while (tmp != null && counter < index){
prev = tmp;
tmp = tmp.next;
}
if (prev != null){
// this is the case when removing from the middle
prev.next = tmp.next;
}
else{
// This is the case when removing the head
head = head.next;
}
}
public void remove(T key) {
node tmp = head;
node prev = null;
while (tmp != null && tmp.data != key){
prev = tmp;
tmp = tmp.next;
}
prev.next = tmp.next;
}
public void clear(){
head = null;
tail = null;
length = 0;
}
public boolean isEmpty(){
return length == 0;
}
public int length(){
return length;
}
public int search(T elem){
int counter = 0;
node tmp = head;
while (tmp != null ){
if (tmp.data.equals(elem)) {
return counter;
}
tmp = tmp.next;
counter++;
}
return -1;
}
public node getHead() {
return head;
}
public node getTail() {
return tail;
}
public int getLength() {
return length;
}
@Override
public ListIterator iterator() {
return new ListIterator(this);
}
}
class ListIterator implements Iterator {
node current;
// initialize pointer to head of the list for iteration
public ListIterator(LinkedList list)
{
current = list.getHead();
}
// returns false if next element does not exist
public boolean hasNext()
{
return current != null;
}
// return current data and update pointer
public T next()
{
T data = current.data;
current = current.next;
return data;
}
// implement if needed
public void remove()
{
throw new UnsupportedOperationException();
}
}
Код: Выделить всё
public class node {
T data;
node next;
public node(T data){
this.data = data;
this.next = null;
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... to-integer