static void Main(string[] args) { Stack<int> s = new Stack<int>(); Queue<int> q = new Queue<int>(); for (int i = 0; i < 10; ++i) { s.Push(i); q.Enqueue(i); } while (s.Count > 0) Console.WriteLine(s.Pop()); //Writes them out in reverse order (9-0). 'Cause FILO while (q.Count > 0) Console.WriteLine(q.Dequeue()); //Writes them in order (FIFO). //New list LinkedList<Student> TwoKay = new LinkedList<Student>(); //New node with a new student LinkedListNode<Student> Current = new LinkedListNode<Student>(new Student("AJ", 123432)); //Take that node and add it to my list. TwoKay.AddFirst(Current); //Add a student without creating a node first TwoKay.AddBefore(TwoKay.First, new Student("Caleb", 123456)); //Show it PrintList(TwoKay); //Change AJ TwoKay.First.Next.Value = new Student("AJ the Mighty", 333); //Print the list again PrintList(TwoKay); //Now, note that the value of current changed, too, because reference. Console.WriteLine(Current.Value); }
static void Main(string[] args) { Console.WriteLine("Do you want queue or stack?"); string choice = Console.ReadLine(); if (choice.ToLower() == "queue") { Queue queue = new Queue(QueueType.FIFO); // Add some stuff queue.Enqueue(10); queue.Enqueue(15); queue.Enqueue(20); // Print the stuff. Console.WriteLine("{0}", queue.Dequeue()); Console.WriteLine("{0}", queue.Dequeue()); Console.WriteLine("{0}", queue.Dequeue()); } else { Queue queue = new Queue(QueueType.FILO); // Add some stuff queue.Enqueue(10); queue.Enqueue(15); queue.Enqueue(20); // Print the stuff. Console.WriteLine("{0}", queue.Dequeue()); Console.WriteLine("{0}", queue.Dequeue()); Console.WriteLine("{0}", queue.Dequeue()); } Console.ReadLine(); }
/// <summary> /// Performs broad-first search for a graph given by its adjacence matrix. /// </summary> /// <param name="adjacence_matrix">A[i,j] = 0 or +infty, if there is no edge from i to j; any non-zero value otherwise.</param> /// <param name="root">The vertex to begin the search.</param> /// <returns>Adjacence matrix of the computed spanning tree.</returns> public static ArrayMatrix BFS(ArrayMatrix adjacence_matrix, int root) { if (!adjacence_matrix.IsSquare()) throw new ArgumentException("Adjacence matrices are expected to be square."); else if (!adjacence_matrix.IsReal()) throw new ArgumentException("Adjacence matrices are expected to be real."); var n = adjacence_matrix.RowCount; if (root < 1 || root > n) throw new ArgumentException("Root must be a vertex of the graph, e.i. in {1, ..., n}."); var spanTree = new ArrayMatrix(n); var marked = new bool[n + 1]; var todo = new Queue(); todo.Enqueue(root); marked[root] = true; // adajacence lists for each vertex var A = new ArrayList[n + 1]; for (var i = 1; i <= n; i++) { A[i] = new ArrayList(); for (var j = 1; j <= n; j++) if (adjacence_matrix[i, j].Real != 0 && adjacence_matrix[i, j].Real != double.PositiveInfinity) A[i].Add(j); } int v, w; while (todo.Count > 0) { v = (int) todo.Peek(); if (A[v].Count > 0) { w = (int) A[v][0]; if (!marked[w]) { marked[w] = true; // mark w spanTree[v, w].Real = 1; // mark vw todo.Enqueue(w); // one more to search } A[v].RemoveAt(0); } else todo.Dequeue(); } return spanTree; }
private static void Queue() { //Queue Console.WriteLine("FIFO with Queue"); Queue<Employee> queue = new Queue<Employee>(); queue.Enqueue(new Employee { Name = "Bob" }); queue.Enqueue(new Employee { Name = "Joe" }); queue.Enqueue(new Employee { Name = "Mark" }); queue.Enqueue(new Employee { Name = "Ned" }); while (queue.Count > 0) { var employee1 = queue.Dequeue(); Console.WriteLine(employee1.Name); Console.WriteLine("Length of Queue After Dequeue(): {0}", queue.Count); } }
static void Main(string[] args) { //Arraylists Console.WriteLine("arraylists"); ArrayList a1 = new ArrayList(); a1.Add(5); a1.Add("raju"); a1.Add(2.33); a1.Add('d'); foreach (var val in a1) { Console.WriteLine(val); } a1.Remove(2.33); foreach (var val in a1) { Console.WriteLine(val); } Console.WriteLine("***********"); //non generic Queue Console.WriteLine("Queue"); Queue qlist = new Queue(); qlist.Enqueue(1); qlist.Enqueue("raj"); qlist.Enqueue('r'); Console.WriteLine("removed element is" + qlist.Dequeue()); foreach (var val in qlist) { Console.WriteLine(val); } Console.WriteLine("***********"); Console.WriteLine("Stack"); Stack slist = new Stack(); slist.Push(23); slist.Push("hello"); slist.Push(23.122); Console.WriteLine(slist.Pop()); Console.WriteLine(slist.Peek()); foreach (var s in slist) { Console.WriteLine(s); } Console.WriteLine("***********"); Console.WriteLine("Hash table"); Hashtable ht = new Hashtable(); ht.Add(1, "raj"); ht.Add("a", 1000); ht.Add("t1", 30); foreach (var h in ht.Keys) { Console.WriteLine(h); Console.WriteLine(ht[h]); } Console.WriteLine("***********"); Console.WriteLine("List"); List <int> list = new List <int>(); list.Add(10); list.Add(10); list.Add('a'); list.Add(34); foreach (int val in list) { Console.WriteLine(val); } Console.WriteLine("***********"); HashSet <int> hs = new HashSet <int>(); Console.WriteLine("Hash set"); hs.Add(10); hs.Add(20); hs.Add(47); foreach (int val in hs) { Console.WriteLine(val); } Console.WriteLine("***********"); Console.WriteLine(" generic queue"); Queue <string> gqlist = new Queue <string>(); gqlist.Enqueue("1"); gqlist.Enqueue("arun"); gqlist.Enqueue("c"); Console.WriteLine(gqlist.Dequeue()); foreach (var val in gqlist) { Console.WriteLine(val); } Stack <float> gslist = new Stack <float>(); gslist.Push(23); gslist.Push(12.234f); gslist.Push(23.123f); Console.WriteLine(gslist.Pop()); Console.WriteLine(gslist.Peek()); foreach (var s in gslist) { Console.WriteLine(s); } Console.WriteLine("***********"); Console.WriteLine("Dictionary"); Dictionary <int, string> dt = new Dictionary <int, string>(); dt.Add(1, "Raj"); dt.Add(2, "radha"); dt.Add(3, "lakshmi"); foreach (KeyValuePair <int, string> kl in dt) { Console.WriteLine(kl.Key); Console.WriteLine(kl.Value); } Console.ReadLine(); Console.ReadLine(); }