-
03/17 stack 연습C#/수업내용 2021. 3. 17. 17:04728x90
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study06 { class Program { static void Main(string[] args) { Console.WriteLine("Main"); new App(); } } }
App.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study06 { public class App { public App() { Console.WriteLine("App"); //Stack<Item> 변수 선언 Stack<Item> items; //Stack<Item> 인스턴스 생성후 변수 할당 items = new Stack<Item>(); //Item 객체 생성 Item item1 = new Item() { Name = "첫번째 아이템" }; Item item2 = new Item() { Name = "두번째 아이템" }; Item item3 = new Item() { Name = "세번째 아이템" }; //Stack에 값 추가 Push items.Push(item1); items.Push(item2); items.Push(item3); //Stack의 요소 출력 (foreach) foreach(Item item in items) { Console.WriteLine(item.Name); } Console.WriteLine("요소의 수 출력 : {0}", items.Count); //Stack에서 값 꺼내오기 Pop (출력 : 아이템의 이름) Console.WriteLine("--------- pop ----------"); Console.WriteLine("아이템의 이름 : {0}", items.Pop().Name); //Stack의 요소의 수 출력 Console.WriteLine("요소의 수 출력 : {0}", items.Count); //Stack의 요소 보기 Peek (출력: 아이템 이름) Console.WriteLine("--------- peek ----------"); Console.WriteLine("아이템의 이름 : {0}", items.Peek().Name); Console.WriteLine("요소의 수 출력 : {0}", items.Count); } } }
Item.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study06 { public class Item { public string Name { get; set; } } }
728x90'C# > 수업내용' 카테고리의 다른 글
03/17 수업내용 메모 (0) 2021.03.17 03/17 queue 연습 (0) 2021.03.17 03/17 abtract 연습 (0) 2021.03.17 03/17 인터페이스연습 2 (0) 2021.03.17 03/17 인터페이스 연습 1 (0) 2021.03.17