C# 0 The Complete Reference


PART II C h a p t e r   2 4



Download 4,07 Mb.
Pdf ko'rish
bet974/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   970   971   972   973   974   975   976   977   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART II

C h a p t e r   2 4 :  

C o l l e c t i o n s ,   E n u m e r a t o r s ,   a n d   I t e r a t o r s    

785


Here is an example that demonstrates the 

LinkedList

 class:


// Demonstrate LinkedList.

using System;

using System.Collections.Generic;

class GenLinkedListDemo {

  static void Main() {

    // Create a linked list.

    LinkedList ll = new LinkedList();

    Console.WriteLine("Initial number of elements: " +

                       ll.Count);

    Console.WriteLine();

    Console.WriteLine("Adding 5 elements.");

    // Add elements to the linked list

    ll.AddFirst('A');

    ll.AddFirst('B');

    ll.AddFirst('C');

    ll.AddFirst('D');

    ll.AddFirst('E');

    Console.WriteLine("Number of elements: " +

                       ll.Count);

    // Display the linked list by manually walking

    // through the list.

    LinkedListNode node;

    Console.Write("Display contents by following links: ");

    for(node = ll.First; node != null; node = node.Next)

      Console.Write(node.Value + " ");

    Console.WriteLine("\n");

    //Display the linked list by use of a foreach loop.

    Console.Write("Display contents with foreach loop: ");

    foreach(char ch in ll)

      Console.Write(ch + " ");

    Console.WriteLine("\n");

public void Remove(LinkedList 



n

)

Removes the node that matches 



n.

 Throws an 

InvalidOperationException

 if 


n

 is not in the list.

public void RemoveFirst( )

Removes the first node in the list.

public void RemoveLast( )

Removes the last node in the list.

T

ABLE


 24-15 

A Sampling of Methods Defi ned by 

LinkedList

(continued)

Method


Description

www.freepdf-books.com




786

 

P a r t   I I :  



E x p l o r i n g   t h e   C #   L i b r a r y

    // Display the list backward by manually walking

    // from last to first.

    Console.Write("Follow links backwards: ");

      for(node = ll.Last; node != null; node = node.Previous)

      Console.Write(node.Value + " ");

    Console.WriteLine("\n");

    // Remove two elements.

    Console.WriteLine("Removing 2 elements.");

    // Remove elements from the linked list.

    ll.Remove('C');

    ll.Remove('A');

    Console.WriteLine("Number of elements: " +

                       ll.Count);

    // Use foreach loop to display the modified list.

    Console.Write("Contents after deletion: ");

    foreach(char ch in ll)

      Console.Write(ch + " ");

    Console.WriteLine("\n");

    // Add three elements to the end of the list.

    ll.AddLast('X');

    ll.AddLast('Y');

    ll.AddLast('Z');

    Console.Write("Contents after addition to end: ");

    foreach(char ch in ll)

      Console.Write(ch + " ");

    Console.WriteLine("\n");

  }


}

Here is the output:

Initial number of elements: 0

Adding 5 elements.

Number of elements: 5

Display contents by following links: E D C B A

Display contents with foreach loop: E D C B A

Follow links backwards: A B C D E

Removing 2 elements.

Number of elements: 3

Contents after deletion: E D B

Contents after addition to end: E D B X Y Z

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   970   971   972   973   974   975   976   977   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish