You are on page 1of 5

Пример задача за имплементација на магацин со помош на листа

package ListStack;
/** Cell models one cell used to assemble a linked list. */
public class Cell
{ private Object val; // value in the cell
private Cell next; // the address of the next cell in the list

/** Constructor Cell builds a new cell


* @param value - the value inserted in the cell
* @param link - the cell that is chained to this new cell */
public Cell(Object value, Cell link)
{ val = value;
next = link;
}

/** getVal returns the value held in the cell */


public Object getVal()
{ return val; }

/** getNext returns the address of the cell chained to this one */
public Cell getNext()
{ return next; }

/** setNext resets the address of the cell chained to this one
* @param link - the address of the Cell that is chained to this one */
public void setNext(Cell link)
{ next = link; }
}

package ListStack;
/** Stack models a stack data structure (using a linked list) */
public class Stack
{ private Cell top; // the top cell of the stack, which is modelled by
// a linked list of Cells.
// invariant: top == null means the stack is empty

/** Constructor Stack creates an empty stack. */


public Stack()
{ top = null; }

/** push inserts a new element onto the stack


* @param ob - the element to be added */
public void push(Object ob)
{ // reset the top of the stack to be a new Cell that holds ob:
top = new Cell(ob, top);
}

/** pop removes the most recently added element


* @return the element removed from the stack
* @exception RuntimeException if stack is empty */
public Object pop()
{ if ( top == null )
{ throw new RuntimeException("Stack error: stack empty"); }
// else, return the value in the top stack cell:
Object answer = top.getVal();
top = top.getNext();
return answer;
}
/** top returns the identity of the most recently added element
* @return the element
* @exception RuntimeException if stack is empty */
public Object top()
{ if ( top == null )
{ throw new RuntimeException("Stack error: stack empty"); }
// else:
return top.getVal();
}

/** isEmpty states whether the stack has 0 elements.


* @return whether the stack has no elements */
public boolean isEmpty()
{ return ( top == null ); }
}

import ListStack.*;
/** TestStack does simple tests of linked-list stacks. */
public class TestStack
{ public static void main(String[] args)
{ // push and pop some integers:
Stack s1 = new Stack();
// wrap the integers:
Integer i1 = new Integer(1);
Integer i2 = new Integer(2);
// push them:
s1.push(i1);
s1.push(i2);
// pop one:
Object ob = s1.pop();
int v = ((Integer)ob).intValue();
System.out.println(v); // prints 2
System.out.println(s1.isEmpty()); // prints false
s1.push(new Integer(3));

// push some strings:


Stack s2 = new Stack();
s2.push("A"); // in Java, strings are already objects
// and don't require wrappers
s2.push("B");
s2.push("C");
String w = (String)(s2.pop());

// push some other objects:


Stack s3 = new Stack();
s3.push(s1); // any object can be pushed onto a stack
s3.push(s2);
Stack x = (Stack)(s3.pop());
Object y = x.pop();
// perhaps we forgot what form of objects are in stack x:
if ( y instanceof Integer )
{ System.out.println( ((Integer)y).intValue() ); }
else if ( y instanceof String )
{ System.out.println( (String)y ); }
else { System.out.println( y.toString() ); } // if you are desperate,
// try this trick!
}
}
Пример задача за имплементација на ред со помош на листа

package Queue;
/** Cell models a cell of a linked list. */
public class Cell
{ private Object val; // value in the cell
private Cell next; // the address of the next cell in the list

/** Constructor Cell builds a new cell


* @param value - the value inserted in the cell
* @param link - the cell that is chained to this new cell */
public Cell(Object value, Cell link)
{ val = value;
next = link;
}

/** getVal returns the value held in the cell */


public Object getVal()
{ return val; }

/** getNext returns the address of the cell chained to this one */
public Cell getNext()
{ return next; }

/** setNext resets the address of the cell chained to this one
* @param link - the address of the Cell that is chained to this one */
public void setNext(Cell link)
{ next = link; }
}

package Queue;
/** Queue implements a queue */
public class Queue
{ private Cell front; // marks the first Cell of the queue
private Cell rear; // marks the last Cell of the queue

/** Constructor Queue creates an empty queue */


public Queue()
{ front = null;
rear = null;
}

/** enqueue inserts a new element to the end of the queue


* @param ob - the element to be added */
public void enqueue(Object ob)
{ Cell c = new Cell(ob, null);
if ( front == null ) // is the queue empty?
{ front = c; } // if true, then set front to c
else { rear.setNext(c); } // else, attach c to the rear of the queue
rear = c;
}

/** dequeue removes the element from the front of the queue
* @return the element removed
* @exception RuntimeException if the queue is empty */
public Object dequeue()
{ if ( front == null )
{ throw new RuntimeException("Queue error: queue empty"); }
Object answer = front.getVal();
if ( front == rear ) // queue contains just this one element?
{ rear = null; } // if true, then set queue to empty
front = front.getNext();
return answer;
}

/** getFront returns the identity of the queue's front element


* @return the element
* @exception RuntimeException if stack is empty */
public Object getFront()
{ if ( front == null )
{ throw new RuntimeException("Queue error: queue empty"); }
return front.getVal();
}

/** isEmpty states whether the queue has 0 elements.


* @return whether the stack has no elements */
public boolean isEmpty()
{ return (front == null); }
}

package Queue;
public class TestCell
{
/** lengthOf calculates the number of cells in a linked list
* @param l - the leading cell of the list
* @return the length of the linked list */
public int lengthOf(Cell l)
{ int length = 0;
if ( l == null )
{ length = 0; }
else { length = 1 + lengthOf(l.getNext());}
return length;
}

/** append constructs a list that is its two arguments connected together.
* @param l1 - the first list
* @param l2 - the second list
* @return the address of the concatenated list */
public Cell append(Cell l1, Cell l2)
{ Cell answer;
if ( l1 == null )
{ answer = l2; }
else { Cell c = append(l1.getNext(), l2);
answer = new Cell( l1.getVal(), c);
}
return answer;
}

You might also like