






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







ยฉ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);