Вы находитесь на странице: 1из 33

Containers & Generics

OA Fakinlede Click to edit Master subtitle style


University of Lagos May 2011

7/5/12

The Array Type

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

Array of Bigger Objects

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

Arrays & Generics?

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

Arrays vs Generic Lists

Options for implementing an Generic List


Array has a fixed size
Data must be shifted during insertions and deletions

Generic Linked list is able to grow in size as needed


Does not require the shifting of items during insertions and deletions
7/5/12

Insertion into a List of Integers

7/5/12

Reference to the Next Object


A reference contains the location, or address in memory, of a memory cell
Undefined Until The Next Object Exists Initially Null The Initialization Process Must Ensure this points to some object May be trivial

7/5/12

Deletion of a Node

7/5/12

Delete First Node

7/5/12

Arrays & Lists

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

Insertion and deletions


Array-based: require shifting of data List-based: require a list traversal

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

Singly Linked Circular List

Last node references the first node Every node has a successor No node in a circular linked list contains NULL

7/5/12

Double Link Circular List

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

C Sharp Library Linked List

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

Linked List Class

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

LinkedList Methods and Properties


Public LinkedList();// non parametrized constructor public LinkedList(IEnumerable<T> collection);
Allows another enumerable object such as a predefined array to initialize the linked list

Public int Count { get; }


// It can tell you how many items it contains

public LinkedListNode<T> First { get; } public LinkedListNode<T> Last { get; }


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

Add to Partial Form1 Class

LinkedList<string> list = new LinkedList<string>(); int Current=0; // No longer necessary

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

list.AddFirst(textBox1.Text.ToString()); textBox1.Clear(); Current++; button1.Enabled = false; button1.BackColor = Color.Aqua;

7/5/12

RemoveLast()

Form1.ActiveForm.Text = "Last Item removed; Click Display to see it"; list.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

Вам также может понравиться