Docsity

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

Toggle sidebar
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);