כל מבני הנתונים המותרים לשימוש בשפת C#:
public class Node<T>
{
private T value;
private Node<T> next;
public Node(T value)
{
this.value = value;
this.next = null;
}
public Node(T value, Node<T> next)
{
this.value = value;
this.next = next;
}
public T GetValue()
{
return this.value;
}
public Node<T> GetNext()
{
return this.next;
}
public bool HasNext()
{
return (this.next != null);
}
public void SetValue(T value)
{
this.value = value;
}
public void SetNext(Node<T> next)
{
this.next = next;
}
public override string ToString()
{
return value+"";
}
// This method was added to use for printing list
public string ToPrint()
{
if (this.HasNext())
return value + "=>" + this.next.ToPrint();
return value + "=>null";
}
}
public class Queue<T>
{
private Node<T> first;
private Node<T> last;
public Queue()
{
this.first = null;
this.last = null;
}
public void Insert(T x)
{
Node<T> temp = new Node<T>(x);
if (first == null)
first = temp;
else
last.SetNext( temp);
last = temp;
}
public T Remove()
{
T x = first.GetValue();
first = first.GetNext();
if (first == null)
last = null;
return x;
}
public T Head()
{
return first.GetValue();
}
public bool IsEmpty()
{
return first == null;
}
// This method was added to use for printing Queue
public override string ToString()
{
Node<T> current = first;
string st = "Queue[";
while (current != null) {
st += current.ToString();
current = current.GetNext();
if (current != null)
st += ",";
}
return st + "]";
}
}
public class Stack<T>
{
private Node<T> head;
public Stack()
{
this.head = null;
}
public void Push(T x)
{
Node<T> temp = new Node<T>(x);
temp.SetNext( head);
head = temp;
}
public T Pop()
{
T x = head.GetValue();
head = head.GetNext();
return x;
}
public T Top()
{
return head.GetValue();
}
public bool IsEmpty()
{
return head == null;
}
// This method was added to use for printing Stack
public override string ToString()
{
Node<T> pos = head;
string st = "Stack[";
while (pos!=null)
{
st += pos.ToString();
pos = pos.GetNext();
if (pos!=null)
st += ",";
}
return st + "]";
}
}
public class BinNode<T>
{
private BinNode<T> left;
private T value;
private BinNode<T> right;
public BinNode(T value)
{
this.value = value;
this.left = null;
this.right = null;
}
public BinNode(BinNode<T> left, T value, BinNode<T> right)
{
this.left = left;
this.value = value;
this.right = right;
}
public T GetInfo()
{
return value;
}
public T GetValue()
{
return value;
}
public BinNode<T> GetLeft()
{
return left;
}
public BinNode<T> GetRight()
{
return right;
}
public bool HasLeft()
{
return (left != null);
}
public bool HasRight()
{
return (right != null);
}
public void SetValue(T value)
{
this.value = value;
}
public void SetLeft(BinNode<T> left)
{
this.left = left;
}
public void SetRight(BinNode<T> right)
{
this.right= right;
}
// This method was added to use for printing Tree
public override string ToString ()
{
return "(" + this.left + "," +this.value + "," + this.right + ")";
}
}
כל מבני הנתונים המותרים לשימוש בבגרות בשפת Java:
public class Node<T>
{
private T info;
private Node<T> next;
public Node(final T x) {
this.info = x;
this.next = null;
}
public Node(final T x, final Node<T> next) {
this.info = x;
this.next = next;
}
public T getValue() {
return this.info;
}
public T getInfo() {
return this.info;
}
public void setInfo(final T x) {
this.info = x;
}
public void setValue(final T x) {
this.info = x;
}
public Node<T> getNext() {
return this.next;
}
public void setNext(final Node<T> next) {
this.next = next;
}
public boolean hasNext() {
return this.next != null;
}
@Override
public String toString() {
return "" + this.info;
}
}
public class BinNode<T>
{
private BinNode<T> left;
private T info;
private BinNode<T> right;
public BinNode(final T x) {
this.left = null;
this.info = x;
this.right = null;
}
public BinNode(final BinNode<T> left, final T x, final BinNode<T> right) {
this.left = left;
this.info = x;
this.right = right;
}
public T getInfo() {
return this.info;
}
public T getValue() {
return this.info;
}
public void setInfo(final T x) {
this.info = x;
}
public void setValue(final T x) {
this.info = x;
}
public BinNode<T> getLeft() {
return this.left;
}
public BinNode<T> getRight() {
return this.right;
}
public void setLeft(final BinNode<T> left) {
this.left = left;
}
public void setRight(final BinNode<T> right) {
this.right = right;
}
public boolean hasLeft() {
return this.left != null;
}
public boolean hasRight() {
return this.right != null;
}
@Override
public String toString() {
return this.info.toString();
}
}
public class Stack<T>
{
private List<T> data;
public Stack() {
this.data = new List<T>();
}
public boolean isEmpty() {
return this.data.isEmpty();
}
public void push(final T x) {
this.data.insert(null, x);
}
public T pop() {
final T x = this.data.getFirst().getInfo();
this.data.remove(this.data.getFirst());
return x;
}
public T top() {
return this.data.getFirst().getInfo();
}
@Override
public String toString() {
Node<T> pos = this.data.getFirst();
String str = "[";
while (pos != null) {
str += pos.getInfo().toString();
if (pos.getNext() != null) {
str += ",";
}
pos = pos.getNext();
}
str += "]";
return str;
}
}
public class Queue<T>
{
private List<T> data;
private Node<T> lastPos;
public Queue() {
this.lastPos = null;
this.data = new List<T>();
}
public boolean isEmpty() {
return this.data.isEmpty();
}
public void insert(final T x) {
this.lastPos = this.data.insert(this.lastPos, x);
}
public T remove() {
if (this.lastPos == this.data.getFirst()) {
this.lastPos = null;
}
final T x = this.data.getFirst().getInfo();
this.data.remove(this.data.getFirst());
return x;
}
public T head() {
return this.data.getFirst().getInfo();
}
@Override
public String toString() {
Node<T> pos = this.data.getFirst();
String str = "[";
while (pos != null) {
str += pos.getInfo().toString();
if (pos.getNext() != null) {
str += ",";
}
pos = pos.getNext();
}
str += "]";
return str;
}
}