Академический Документы
Профессиональный Документы
Культура Документы
7/5/12
The Windows Forms Exercise of last week allowed us to do some things with the Array type. We used an array of Strings and we were able to do the following:
Insert elements Reverse Array Contents Sort Array Contents Display Current Contents
7/5/12
What changes do we require to make in order to do the same an array of bigger objects? For example, an array of students? Home work
Create, Insert objects to, Reverse, Sort and Display and array of Student Objects from last term work Due May 26, 2011
7/5/12
As you have seen over the course of the previous chapters, C# arrays allow you to define a set of identically typed items (including an array of System.Objects, which essentially represents an array of any types) of a fixed upper limit. These could be Strings as our simple examples show They could be complex Objects as the 7/5/12 Student Class
Arrays vs Lists
Arrays are limited by size which we must state at the outset and cannot easily alter thereafter Linear Data structures are implemented to solve this problem and give the flexibility to add new objects anywhere A linear structure generalizes such concepts as LIFO, FIFO and mixtures of these It is automatically extensible 7/5/12
7/5/12
7/5/12
Deletion of a Node
7/5/12
7/5/12
Size
Increasing the size of a resizable array can waste storage and time
Storage requirements
Array-based implementations require less memory than a Linked lists
7/5/12
Compare
Access time
Array-based: constant access time List-based: the time to access the ith node depends on i
These standard rules are only true in theory because as we shall see, C 7/5/12 Sharp implementation allows a list to be converted
Last node references the first node Every node has a successor No node in a circular linked list contains NULL
7/5/12
7/5/12
Examples
Inventory Items Stacks of Cements Bags Airline Queue Locations of the seeds played in an Ayo Game The University Bus Stop Waiting For any service (Petrol Station, Hostel Water, etc)
7/5/12
The LinkedList<> generic class is located in the Systems Generic namespace LinkedList <(Of <(T >)>) is a general-purpose linked list. It supports enumerators and implements the ICollection interface, consistent with other collection classes in the .NET Framework. Creation is very easy as, unlike when we used an array, we need not specify the size. Several properties and methods are given to
7/5/12
The LinkedList<> Class implements the ICollection and IEnumerable Interfaces in generic and non-generic forms. ICollection allows us to easily convert the linked list to an array, if necessary IEnumerable allows us foreach iteration over each contained item.
7/5/12
More Methods
public void AddAfter(LinkedListNode<T> node, LinkedListNode<T> newNode) public void AddBefore(LinkedListNode<T> node, LinkedListNode<T> newNode); public void AddFirst(LinkedListNode<T> node); public void AddLast(LinkedListNode<T> node);
7/5/12
More Methods
public void Clear(); public bool Contains(T value); public void CopyTo(T[] array, int index); // starting from the index location public LinkedListNode<T> Find(T value); public LinkedListNode<T> FindLast(T value); public LinkedList<T>.Enumerator GetEnumerator(); public void Remove(LinkedListNode<T> node); 7/5/12
A Simple Example
// Create the link list. string[] words = { "the", "fox", "jumped", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); Display(sentence, "The linked list values:"); Console.WriteLine("sentence.Contains(\"jumped\") = {0}", sentence.Contains("jumped"));
7/5/12
Example Continued
// Move the first node to be the last node. LinkedListNode<string> mark1 = sentence.First; sentence.RemoveFirst(); sentence.AddLast(mark1); Display(sentence, "Test 2: Move first node to be last node:"); // Change the last node be 'yesterday'.
7/5/12
Example Continued
// Move the last node to be the first node. mark1 = sentence.Last; sentence.RemoveLast(); sentence.AddFirst(mark1); Display(sentence, "Test 4: Move last node to be first node:"); // Indicate, by using parentheisis, the last occurence of 'the'. 7/5/12
Example
// Add 'lazy' and 'old' after 'the' (the LinkedListNode named current). sentence.AddAfter(current, "old"); sentence.AddAfter(current, "lazy"); IndicateNode(current, "Test 6: Add 'lazy' and 'old' after 'the':"); // Indicate 'fox' node. current = sentence.Find("fox"); 7/5/12
// Add 'quick' and 'brown' before 'fox': sentence.AddBefore(current, "quick"); sentence.AddBefore(current, "brown"); IndicateNode(current, "Test 8: Add 'quick' and 'brown' before 'fox':"); // Keep a reference to the current node, 'fox', // and to the previous node in the list. Indicate the 'dog' node. 7/5/12
Console.WriteLine("Test 16: Copy the list to an array:"); // Create an array with the same number of // elements as the inked list. string[] sArray = new string[sentence.Count]; sentence.CopyTo(sArray, 0);
7/5/12
7/5/12
7/5/12
Add to InitializeComponents
Text = "A Simple I/O with Window Forms"; list.AddFirst("First Entry"); list.AddLast("Last Entry"); if (textBox1.Text == "") button1.Enabled = false; else button1.Enabled = true;
7/5/12
AddFirst
7/5/12
RemoveLast()
7/5/12
Forms Tutorial/HW
Pp 699-758 Bonus Material Chapter 21 HomeWork: Ayo Game Learn How it is played Or Learn another game you can implement with the data structures we have learned Begin to define relevant classes
7/5/12
Quiz
Require your program to obtain a password before starting. Let the password itself be masked by dots or asterisks
7/5/12