1.Enqueue
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Helloworld1
{
class Queue
{
public Node tail;
public Node head;
public Queue()
{
}
public void Enqueue(int data)
{
if(head == null)
{
head = new Node(data);
tail = head;
return;
}
else
{
tail.next = new Node(data);
tail = tail.next;
}
}
}
}
2.Dequeue
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Helloworld1
{
class Queue
{
public Node tail;
public Node head;
public int data;
public Node temp;
public Queue()
{
}
public void Enqueue(int data)
{
if(head == null)
{
head = new Node(data);
tail = head;
return;
}
else
{
tail.next = new Node(data);
tail = tail.next;
}
}
public int Dequeue()
{
if (head == null)
{
throw new InvalidOperationException();
}
else
{
data = head.data;
head = head.next;
if (head == null)
{
tail = null;
head = null;
}
return data;
}
}
}
}
3.Peek
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Helloworld1
{
class Queue
{
public Node tail;
public Node head;
public int data;
public Node temp;
public Queue()
{
}
public void Enqueue(int data)
{
if(head == null)
{
head = new Node(data);
tail = head;
return;
}
else
{
tail.next = new Node(data);
tail = tail.next;
}
}
public int Dequeue()
{
if (head == null)
{
throw new InvalidOperationException();
}
else
{
data = head.data;
head = head.next;
if (head == null)
{
tail = null;
head = null;
}
return data;
}
}
public int Peek()
{
if (head == null)
{
throw new InvalidOperationException();
}
else
{
return head.data;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Helloworld1
{
class Node
{
public int data;
public Node next;
//생성자
public Node(int data)
{
this.data = data;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Helloworld1
{
class App
{
public App()
{
Queue queue = new Queue();
queue.Enqueue(3);
queue.Enqueue(5);
queue.Enqueue(7);
Console.WriteLine(queue.Dequeue());
Console.WriteLine(queue.Dequeue());
Console.WriteLine(queue.Peek());
}
}
}
4.Count
'c# > 수업 내용' 카테고리의 다른 글
자료구조 트리 순회LCRS (Level Order) (0) | 2021.09.27 |
---|---|
자료구조 트리(Tree) 정의, LCRS구현 (0) | 2021.09.24 |
자료구조 Stack 복습(push, pop, peek,count) (0) | 2021.09.23 |
자료구조 스택(stack) - 추상자료형 (0) | 2021.09.17 |
재귀 함수 연습 (0) | 2021.09.16 |