All pastes #537086 Raw Edit

Stuff

public text v1 · immutable
·published 2007-06-04 16:46 UTC
rendered paste body
using System;

public interface ListCollection {
	void Add(object o);
	void Clear();
	bool Remove(object o);
	int Size();
	bool Contains(object o);
	bool IsEmpty();
}

public abstract class LinkedList: ListCollection {
	public Node first = null;
	public int size;
	
	public abstract void Add(object o);
	
	public void Clear() {
		first = null;
		size = 0;
	}
	
	public bool Remove(object o) {
		Node current = first;
		
		while (current != null) {
			if (current.data == o) {
				if (current.next != null) current.next.prev = current.prev;
				if (current.prev != null) current.prev.next = current.next;
				size--;
				return true;
			}
			
			current = current.next;
		}
		
		return false;
	}
	
	public int Size() {
		return size;
	}
	
	public bool Contains(object o) {
		Node current = first;
		
		while (current != null) {
			if (current.data == o) return true;
			current = current.next;
		}
		
		return false;
	}
	
	public bool IsEmpty() {
		if (first == null) return false;
		return true;
	}
	
	public class Node {
		public Node next, prev;
		public object data;
		
		public Node(Node p, Node n, object d) {
			prev = p; next = n; data = d;
		}
	}
}

public class UnorderedList: LinkedList {
	public override void Add(object o) {
		first = new Node(null, first, o);
		size++;
	}
}

public class Test {
	UnorderedList l = new UnorderedList();
	
	public static void Main(string[] args) {
		string t1 = "test", t2 = "test!";
		l.Add(t1); l.Add(t2);
		
		Console.WriteLine("List is {0} items contained.", l.Size());
	}
}