1. Trang chủ
  2. » Thể loại khác

Com Session 6 Collections

17 74 0
Tài liệu được quét OCR, nội dung có thể không chính xác

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 17
Dung lượng 1,22 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

- Stack là một cấu trúc theo kiểu LIFO Last In First Out, phần tử vào sau cùng sẽ được lấy ra trước.. - Hai thao tác cơ bản trên Stack — Chèn phần tử: Luôn chèn vào đỉnh Stack push — L

Trang 1

I Xây dựng cấu trúc dữ liệu trong C#

1 Stack

2 Queue

ll Collections

1 Các lớp và giao diện thường sử dụng trong

namespace: System.Collections va

System.Collections.Generic

2 Sử dụng các phương thức và thuộc tính của lớp:

ArrayList

Trang 2

- Stack là một cấu trúc theo kiểu LIFO (Last In First Out),

phần tử vào sau cùng sẽ được lấy ra trước

- Hai thao tác cơ bản trên Stack

— Chèn phần tử: Luôn chèn vào đỉnh Stack (push)

— Lãy ra phần tử: Luôn lấy ra từ đỉnh Stack (pop)

- Lớp Stack thuộc namespace System.Collections, biểu

diễn ngăn xếp (LIFO) các đối tượng non — generic Để

sử dụng kiểu generic dùng

System.Collections.Generic.Stack<T>

- L6p Stack hién thuc cac giao dién |Collection,

lEnumerable, |Cloneable.

Trang 3

namespace DataStructures

{

class Program

{

Static void Main(string[] args)

{

int [] a = {10,20,30};

Stack s = new Stack(a);

s.Push(1);

s.Push(ˆabccd');

Console.WriteLine(s.Count);

Console.ReadLine();

Trang 4

- Capacity la s6 luong cac phan tur ma stack có thể chứa

Stack có thể chứa các phần tử có kiểu dữ liệu khác

nhau Thuộc tính Count để chỉ số phần tử hiện trong

Stack

- Khi 1 phần tử được thêm vào (Push), capacity tự động

tăng, và tổ chức lại bộ nhớ Nếu Count< Capacity thì

Push có số phép toán là O(1), ngược lại là O(n) (n =

Count) Pop có số phép toán là O(1)

- Stack cho phép chèn phần tử null, hoặc các phần tử có

giá trị băng nhau

Trang 5

- Contructor

StackQ Khởi tạo Stack trống, capacity ban đầu

mặc định

Stack(ICollection) Khởi tạo Stack chứa các phần tử copy tỪ

một tập hợp (mảng), capacity ban đầu bằng số phần tử được copy

Stack(Int32)

Khởi tạo Stack trống, capacity ban đầu

bang giá trị truyền vào (Dùng Contructor này tốt nhật) WHY?

Trang 6

Clear Removes tất cả các đối tượng trong Stack

Clone Tạo bản sao của Stack

Contains | Xác định xem phần tử có trong Stack

CopyTo | Copy stack ra mảng 1 chiều, bắt đầu từ vị trí chỉ

định (Nếu mảng chứa các KDL khác nhau ?)

Peek Trả về đối tượng trên đỉnh Stack không remove nó

khói stack

Pop Remove và trả về đối tượng trên đỉnh stack

Push Chèn một đối tượng vào đỉnh stack

ToArray | Copy stack ra mỘột mảng mới

Trang 7

- Queue (Hàng đợi) là cấu trúc theo kiểu FIFO (First In First Out), phần tử vào trước sẽ được lấy ra trước

- Hai thao tác cơ bản trên hàng đợi

+ Chèn phần tử: Luôn chèn vào cuối hàng đợi

(enqueue)

+ Lấy ra phần tử: Lấy ra từ đầu hàng đợi (dequeue)

- Lớp Queue thuộc namespace System.Collections, biểu

diễn ngăn xếp (FIFO) các đối tượng non — generic Dé

sử dụng kiểu generic dùng

System.Collections.Generic.Queue<T>

- L6p Queue hiện thực các giao diện ICollection,

IEnumerable, ICloneable.

Trang 8

class Program

{

Static void Main(string[] args)

{

int [] a = {10,20,30,10};

Queue q = new Queue(a);

q-Enqueue(22);

while (q.Count > 0) Console.WriteLine(q.Dequeue()):

Console.ReadLine();

Trang 9

Queue rất hữu ích trong việc lưu và xử lý các thông điệp

theo thứ tự được gửi đến Trong queue các phần tử

được chèn ở một đầu, lấy ra ở đầu khác

capacity của queue là số phần tử nó có thể chứa, count

là số các phần tử hiện có trong queue Khi Insert 1 phần

tỬ vào, capacity tự động tăng, capacity có thể giảm khi

gọi phương thức TrimToSize()

growth factor là hệ số nhân của capacity khi tăng kích

thước, giá trị mặc định là 2.0 Capacity tăng tối thiểu là

42

Queue cho phép chèn giá trị null, và các phần tử có giá

trị bằng nhau

Trang 10

Tên Mô tả

Queue() Initializes a new instance of the Queue class

that is empty, has the default initial capacity, and uses the default growth factor

Queue(ICollection) Initializes a new instance of the Queue class

that contains elements copied from the specified

collection, has the same initial capacity as the

number of elements copied, and uses the default growth factor

Queue(Int32) Initializes a new instance of the Queue class that

is empty, has the specified initial capacity, and

uses the default growth factor

Queue(Int32, Single) Initializes a new instance of the Queue class that

is empty, has the specified initial capacity, and uses the specified growth factor

Trang 11

Name Description

Clone Creates a shallow copy of the Queue

Contains Determines whether an element is in the Queue

CopyTo Copies the Queue elements to an existing one-

dimensional Array, starting at the specified array

H>olax

Dequeue - Removes and returns the object at the beginning of

the Queue

without removing it

11

Trang 12

1 ArrayList

2 Hastable

3 Dictionary

Trang 13

- Hiện thực giao diện IList, sử dụng 1 mảng có kích thước

có thể thay đổi nếu cần

- Thuộc namespace: System.Collections

- Capacity của ArrayList là số lượng các phần tử mà

ArrayList có thể chứa, khi 1 phần tử được thêm vào,

Capacity có thể tăng nếu cần Giá trị Capacity có thể

giảm khi sử dụng phương thức TrimToSize() hoặc gán

giá trị Thuộc tính Count là số phần tử hiện có trong

ArrayList

- Có thể sử dụng index để truy cập các phần tử của

ArrayList, index cơ sở là 0

- _ Các phần tử có thé có kiểu dữ liệu khác nhau, cho phép

gia tri null.

Trang 14

using System;

using System.Collections;

public class SamplesArrayList {

public static void Main() {

// Creates and initializes a new ArrayList

ArrayList myAL = new ArrayList(;

myAL.Add("Hello");

myAL.Add(ˆWorld');

myAL.Add("!");

// Displays the properties and values of the ArrayList

Console.WriteLine( "myAL" );

Console.WriteLine(" Count: {0}", myAL.Count );

Console.WriteLine(" Capacity: {0}", myAL.Capacity );

Console.Write(" Values:" );

PrintValues( myAL );

public static void PrintValues( IEnumerable myList ) {

foreach ( Object obj in myList )

Console.Write(“ {O}", obj );

Console.WriteLine();

Trang 15

ArrayList0 Initializes a new instance of the

ArrayList class that is empty and has the default initial capacity

ArrayList(ICollect

) on Initializes a new instance of the ArrayList class that contains elements

copied from the specified collection and that has the same initial capacity as the number of elements copied

ArrayList(Int32)

Initializes a new instance of the

ArrayList class that is empty and has the specified initial capacity

Trang 16

Name Description

Add Adds an object to the end of the ArrayList

AddRange Adds the elements of an !Collection to the end of the

ArrayList

BinarySearch Overloaded Uses a binary search algorithm to locate a

specific element in the sorted ArrayList or a portion of It

Clear Clone Creates a shallow copy of the ArrayList Removes all elements from the ArrayList

Contains Determines whether an element is in the ArrayList

CopyTo Overloaded Copies the ArrayList or a portion of it to a one-

dimensional array

IndexOf occurrence of a value in the ArrayList or in a portion of it Overloaded Returns the zero-based index of the first

Trang 17

Name Description Insert Inserts an element into the ArrayList at the specified index

InsertRange Inserts the elements of a collection into the ArrayList at the

specified index

LastIndexOf Overloaded Returns the zero-based index of the last occurrence of

a value in the ArrayList or in a portion of it

Remove Removes the first occurrence of a specific object from the ArrayList

RemoveAt Removes the element at the specified index of the ArrayList

RemoveRange Removes a range of elements from the ArrayList

Sort Overloaded Sorts the elements in the ArrayList or a portion of it

ToArray Overloaded Copies the elements of the ArrayList to a new array

TrimToSize Sets the capacity to the actual number of elements in the ArrayList

)

Ngày đăng: 18/01/2018, 13:04

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN