Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

INA240 Note 05: Stacks and Queues, Lecture notes of Data Structures and Algorithms

An overview of stacks and queues, their characteristics, and their implementation using arrays. It also covers exceptions that can occur when using stacks and queues, and the difference between fixed and dynamic front designs.

Typology: Lecture notes

2017/2018

Uploaded on 10/23/2018

keehwan
keehwan 🇨🇦

20 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
자료구조 실습(INA240) NOTE 05
©copyright 2005
스택, 큐
한국기술교육대학교
한국기술교육대학교 인터넷미디어공학부
인터넷미디어공학부 김상진
김상진
2
2/20
/20
교육목표
스택과 큐의 특성을 살펴보고,
배열을 이용한 스택과 큐의 구현방법을 학습한다.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download INA240 Note 05: Stacks and Queues and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

©copyright 2005 자료구조 및 실습( INA240) NOTE 05

3/20 3 /

스택 – 논리적 단계

가장 최근에 추가된 요소가 맨 위(top)에 있고, 가장 오래 전에 추가된 요소가 맨 밑(bottom)에 있다. LIFO(Last- LIFO(Last-InIn--FirstFirst--Out)Out) (^) 구조구조: 요소의 제거 또는 추가는 스택의 top에서만 이루어진다. 연산 push: 요소를 추가하는 연산 pop: 요소를 스택에서 제거하고, 맨 위 요소를 반환해주는 연산 top: 스택의 변화 없이 맨 위 요소를 반환해주는 연산

스택 – 논리적 단계

push: 배열을 이용하여 구현할 경우에는 더 이상 추가할 수 없는 상황이 발생할 수 있다. pop, top: 스택에 요소가 하나도 없을 수 있다.

stack =new Stack() stack.push(2) stack.push(3) stack.top() stack.pop()

7/20 7 /

ArrayStack ADT

int topindex=-1; Object[] elements; 참고 참고. size. 가 필요 없음 생성자 ArrayStack() ArrayStack(int capacity); 상태 boolean isFull(); 조건: topindex==elements.length- boolean isEmpty(); 조건: topindex==-

void push(Object item); isFull()이 참이면 StackOverflowException 발생 void pop(); isEmpty()가 참이면 StackUnderflowException 발생 Object top(); isEmpty()가 참이면 StackUnderflowException 발생

push

public void push(Object item) throws StackOverflowException{ if(isFull()) throw new StackOverflowException(“Push attempted on a full stack.”); Listable x = (Listable)item; topindex++; elements[topindex] = x.clone(); }

topindex = -

2

topindex = 0

push(2)

topindex = 1

push(3) 3 2

topindex = 2

push(5) 3

9/20 9 /

push – 계속

topindex = 3

push(1)

3

topindex = 4

push(7)

3

topindex = 3

push(8) 실패

pop()

pop, top

public void pop() throws StackUnderflowException{ if(isEmpty()) throw new StackUnderflowException(“Pop attempted on an empty stack”); elements[topindex] = null; // 불필요 topindex--; }

public Object top() throws StackUnderflowException{ if(isEmpty()) throw new StackUnderflowException(“Top attempted on an empty stack”); Listable x = (Listable)elements[topindex]; return x.clone(); }

13 13/20/

큐 – 논리적 단계

enqueue: 배열을 이용하여 구현할 경우에는 더 이상 추가할 수 없는 상황이 발생할 수 있다. dequeue: 큐에 요소가 하나도 없을 수 있다.

queue =new Queue() queue.enq(2) queue.enq(3) queue.enq(1) queue.deq()

고정된 Front 설계 방법

front로 고정해서 사용하는 방법 실제 front 멤버 변수가 필요 없음 enq는 항상 size-1 슬롯에 추가하면 된다. rear 멤버 변수도 필요 없음 front, rear, size 중 size 하나만 사용하여 구현 가능 deq의 경우에는 최악의 경우 n-1개의 요소를 하나씩 왼쪽으로 이동해야 한다.

enq(10); 10 front=0,rear=

enq(20); 10 20 front=0,rear=

enq(30); 10 20 30 front=0,rear=

deq(); 20 30

front=0, rear=-

20 30 front=0,rear=

15 15/20/

front=1, rear=

front=1, rear=

유동 Front 설계 방법

front가 고정되어 있지 않다. enq, deq 모두 O(1)이다. enq: rear 증가 deq: front 증가 추가적으로 front, rear 멤버 변수를 유지해야 하며, front와 rear 값을 통해 큐에 있는 요소의 개수를 알 수 없으면 추가적으로 size 멤버변수가 필요하다. 예에서 알 수 있듯이 full 상태와 empty 상태에서 front와 rear 값의 차이가 동일함

enq(10); 10 front=0,rear=

deq();

enq(20); 20 front=1,rear=

enq(30); 20 30 front=1,rear=

front=0, rear=-

enq(40); 20 30 40 front=1,rear=

enq(50); 20 30 40 50 front=1, rear=

enq(60); 60 20 30 40 50

ArrayQueue ADT

배열을 이용한 유동 front 방식의 큐 구현 int front=0; int rear=-1; int size=0; Object[] elements; 생성자 ArrayQueue() ArrayQueue(int capacity);

boolean isFull(); 조건: size==elements.length boolean isEmpty(); 조건: size== 조작 void enq(Object item); isFull()이 참이면 QueueOverflowException 발생 Object deq(); isEmpty()가 참이면 QueueUnderflowException 발생

19 19/20/

효율적인 유동 front 구현

front와 rear 정보만을 이용하여 현재 큐의 상태(full 또는 empty)를 판별할 수 있는가? 현재 구현의 경우에는 두 조건이 같다. 두 조건이 같으므로 이 조건을 구분하는 boolean flag을 사용한다. size를 사용하는 것과 차이가 없음

배열의 한 공간을 사용하지 않으면 판별할 수 있다. (How?)

public void enq(Object item) throws QueueOverflowException{ if(isFull()) throw new QueueOverflowException(“…”); Listable x = (Listable)item; rear = (rear + 1) % elements.length; elements[rear] = x.clone(); isFull = ((rear + 1) % elements.length)==front)? true : false; }

front=5, rear=

front=5, rear=

효율적인 유동 front 구현 – 계속

이 방법은 size 정보를 유지하지 않아도 된다. 하지만 항상 배열 한 슬롯이 사용되지 않는다. 공간 측면에서는 기존 size를 사용하는 것과 차이는 없다. 시간 측면에서는 size를 유지하는 비용이 없으므로 효율적이다.

front=5, (^10) rear=

10 20 30 40 50

20 front=0, (^30 40 50) rear=

front=1, (^30 40 50) rear=

front=1, (^30 40 50 60) rear=

deq();

deq();

enq(60);