July First
-
Upload
omotayo-fakinlede -
Category
Documents
-
view
216 -
download
0
Transcript of July First
-
7/31/2019 July First
1/33
Click to edit Master subtitle style
7/5/12
Containers & Generics
OA FakinledeUniversity of LagosMay 2011
-
7/31/2019 July First
2/33
7/5/12
The Windows Forms Exercise of last week
allowed us to do some things with the Arraytype.
We used an array of Strings and we were ableto do the following:
Insert elements
Reverse Array Contents
Sort Array Contents
Display Current Contents
The Array Type
-
7/31/2019 July First
3/33
-
7/31/2019 July First
4/33
7/5/12
As you have seen over the course of the
previous chapters, C# arrays allow you todefine a set of identically typed items(including an array of System.Objects, whichessentially 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
Student Class
Arrays & Generics?
-
7/31/2019 July First
5/33
7/5/12
Arrays are limited by size which we must
state at the outset and cannot easily alterthereafter
Linear Data structures are implemented tosolve this problem and give the flexibility to
add new objects anywhere
A linear structure generalizes such conceptsas LIFO, FIFO and mixtures of these
It is automatically extensible
Arrays vs Lists
-
7/31/2019 July First
6/33
7/5/12
Options for implementing an Generic List
Array has a fixed size
Data must be shifted during insertions anddeletions
Generic Linked list is able to grow in size asneeded
Does not require the shifting of items duringinsertions and deletions
Arrays vs Generic Lists
-
7/31/2019 July First
7/33
7/5/12
Insertion into a List ofIntegers
-
7/31/2019 July First
8/33
7/5/12
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
Reference to the NextObject
-
7/31/2019 July First
9/33
7/5/12
Deletion of a Node
-
7/31/2019 July First
10/33
7/5/12
Delete First Node
-
7/31/2019 July First
11/33
7/5/12
Size
Increasing the size of a resizable array canwaste storage and time
Storage requirements
Array-based implementations require lessmemory than a Linked lists
Arrays & Lists
-
7/31/2019 July First
12/33
7/5/12
Access time
Array-based: constant access time
List-based: the time to access the ith nodedepends 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 Sharpim lementation allows a list to be converted
Compare
-
7/31/2019 July First
13/33
7/5/12
Last node references the first node
Every node has a successor
No node in a circular linked list contains NULL
Singly Linked Circular List
-
7/31/2019 July First
14/33
7/5/12
Double Link Circular List
-
7/31/2019 July First
15/33
7/5/12
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, HostelWater, etc)
Examples
-
7/31/2019 July First
16/33
7/5/12
The LinkedList generic class is located in
the Systems Generic namespace
LinkedList ) is a general-purposelinked list. It supports enumerators andimplements the ICollection interface,
consistent with other collection classes inthe .NET Framework.
Creation is very easy as, unlike when we usedan array, we need not specify the size.
Several properties and methods are given to
C Sharp Library Linked List
-
7/31/2019 July First
17/33
7/5/12
The LinkedList Class implements the
ICollection and IEnumerable Interfaces ingeneric and non-generic forms.
ICollection allows us to easily convert thelinked list to an array, if necessary
IEnumerable allows us foreach iteration overeach contained item.
Linked List Class
-
7/31/2019 July First
18/33
7/5/12
Public LinkedList();// non parametrized
constructor
public LinkedList(IEnumerable collection);
Allows another enumerable object such as apredefined array to initialize the linked list
Public int Count { get; }
// It can tell you how many items it contains
public LinkedListNode First { get; }
public LinkedListNode Last { get; }
LinkedList Methods andProperties
-
7/31/2019 July First
19/33
7/5/12
public void AddAfter(LinkedListNode
node, LinkedListNode newNode)
public void AddBefore(LinkedListNodenode, LinkedListNode newNode);
public void AddFirst(LinkedListNodenode);
public void AddLast(LinkedListNodenode);
More Methods
-
7/31/2019 July First
20/33
7/5/12
public void Clear();
public bool Contains(T value);
public void CopyTo(T[] array, int index); //starting from the index location
public LinkedListNode Find(T value);
public LinkedListNode FindLast(T value);
public LinkedList.EnumeratorGetEnumerator();
public void Remove(LinkedListNode
node);
More Methods
-
7/31/2019 July First
21/33
7/5/12
// Create the link list.
string[] words = { "the", "fox", "jumped", "over", "the","dog" };
LinkedList sentence = newLinkedList(words);
Display(sentence, "The linked list values:");
Console.WriteLine("sentence.Contains(\"jumped\") ={0}",
sentence.Contains("jumped"));
A Simple Example
-
7/31/2019 July First
22/33
7/5/12
// Move the first node to be the last node.LinkedListNode 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'.
Example Continued
-
7/31/2019 July First
23/33
7/5/12
// 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 befirst node:");
// Indicate, by using parentheisis, the lastoccurence of 'the'.
Example Continued
-
7/31/2019 July First
24/33
7/5/12
// 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");
" ' '
Example
-
7/31/2019 July First
25/33
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/31/2019 July First
26/33
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/31/2019 July First
27/33
7/5/12
LinkedList list = new
LinkedList();
int Current=0; // No longer necessary
Add to Partial Form1 Class
-
7/31/2019 July First
28/33
7/5/12
-
7/31/2019 July First
29/33
7/5/12
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;
Add to InitializeComponents
-
7/31/2019 July First
30/33
7/5/12
list.AddFirst(textBox1.Text.ToString());
textBox1.Clear();
Current++; button1.Enabled = false;
button1.BackColor = Color.Aqua;
AddFirst
-
7/31/2019 July First
31/33
7/5/12
Form1.ActiveForm.Text = "Last Item removed;
Click Display to see it";
list.RemoveLast();
RemoveLast()
-
7/31/2019 July First
32/33
7/5/12
Pp 699-758 Bonus Material Chapter 21
HomeWork:
Ayo Game
Learn How it is played
Or Learn another game you can implementwith the data structures we have learned
Begin to define relevant classes
Forms Tutorial/HW
-
7/31/2019 July First
33/33
7/5/12
Require your program to obtain a password
before starting. Let the password itself bemasked by dots or asterisks
Quiz