C# Queue Stack SortedList

iamzxf發表於2015-04-23

佇列的基本特點是先進先出(first in first out, FIFO),在C#中用Queue定義。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace queueDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Queue qu = new Queue();
            qu.Enqueue("zxf");
            qu.Enqueue(12);

            while (qu.Count != 0)
            {
                Console.WriteLine(qu.Dequeue());
            }

            Console.ReadLine();
        }
    }
}
</pre><p><span style="font-family:SimHei; font-size:18px">堆疊的特點是先進後出(last in first out, LIFO),在C#中用stack實現。</span></p><p><span style="font-family:SimHei; font-size:18px"></span></p><pre code_snippet_id="651530" snippet_file_name="blog_20150423_3_5975888" name="code" class="csharp">using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StackDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack st = new Stack();
            st.Push("zxf");
            st.Push(234);

            while (st.Count != 0)
                Console.WriteLine(st.Pop());

            Console.ReadLine();
        }
    }
}

有序表的特點是資料自動按照排序,插入時,第一個引數是key,第二個引數是value。

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace SortedListDe
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedList st = new SortedList();
            st.Add("102","zxf");
            st.Add("109","zhang");
            st.Add("103","li");

            foreach (DictionaryEntry dd in st)
            {
                Console.WriteLine("{0},{1}",dd.Key,dd.Value);
            }
            
            foreach(string ss in st.Keys)
                Console.WriteLine(ss);

            foreach(string ss in st.Values)
                Console.WriteLine(ss);
            Console.ReadLine();
        }
    }
}



相關文章