/// <summary> /// add an entry at a specified position /// </summary> /// <param name="index">the position</param> /// <param name="o">the entry to add</param> public void Add(int index, Object o) { Node newNode = new Node(); Node probe; newNode.dataItem = o; if (head == null) { newNode.nextNode = null; head = newNode; } else { if (index == 0) { newNode.nextNode = head; head = newNode; } else { probe = head; int i = 0; while ((i < index - 1) && (probe.nextNode != null)) { probe = probe.nextNode; i++; } newNode.nextNode = probe.nextNode; probe.nextNode = newNode; } } }
/// <summary> /// dequeues the item at the head of the queue /// </summary> /// <returns>the item dequeued</returns> public Object Dequeue() { if (head == null) return null; else { Node temp = new Node(); temp = head; head = head.nextNode; if (head == null) tail = null; return temp.dataItem; } }
/// <summary> /// sets all queue entries to null /// </summary> public void Destroy() { Node temp = new Node(); Node setNull = new Node(); temp = head; while (temp != null) { setNull = temp; temp = temp.nextNode; setNull = null; } head = null; tail = null; }
/// <summary> /// enqueues an item /// </summary> /// <param name="o">the item to enqueue</param> public void Enqueue(Object o) { Node newNode = new Node(); newNode.dataItem = o; if (tail == null) { head = newNode; tail = newNode; } else { tail.nextNode = newNode; tail = newNode; } }
/// <summary> /// default constructor /// </summary> public SimpleQueue() { // initialise head and tail references head = null; tail = null; }
/// <summary> /// default constructor /// </summary> public SimpleLinkedList() { // initialise head reference head = null; }
/// <summary> /// sets the entry at the specified position /// </summary> /// <param name="index">the position</param> /// <param name="o">the entry previously at that position</param> /// <returns></returns> public Object Set(int index, Object o) { Node probe; Node oldNode = new Node(); if (head == null) return null; else { if (index == 0) { oldNode.dataItem = head.dataItem; head.dataItem = o; return oldNode.dataItem; } else { probe = head; int i = 0; while ((i < index - 1) && (probe.nextNode != null)) { probe = probe.nextNode; i++; } if (probe.nextNode != null) { oldNode.dataItem = probe.nextNode.dataItem; probe.nextNode.dataItem = o; return oldNode.dataItem; } else return null; } } }
/// <summary> /// removes the first entry which matches the target object /// </summary> /// <param name="o">the target object</param> /// <returns></returns> public bool Remove(Object o) { Node probe = new Node(); Node previous = new Node(); if (head == null) return false; else { if (head.dataItem.Equals(o)) { head = head.nextNode; return true; } else { probe = head; previous = head; while (!probe.dataItem.Equals(o) && probe.nextNode != null) { previous = probe; probe = probe.nextNode; } if (probe.dataItem.Equals(o)) { previous.nextNode = probe.nextNode; probe = null; return true; } else return false; } } }