강의자료8

698 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
698
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

강의자료8

  1. 1. 스택과 큐 ( 연결구조에 의한 구현 )
  2. 2. Definition of Stack <ul><li>Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of stack items can take place only at the top of the stack. </li></ul><ul><li>A stack is a LIFO “last in, first out” structure. </li></ul>
  3. 3. Stack ADT Operations <ul><li>IsEmpty -- Determines whether the stack is currently empty. </li></ul><ul><li> </li></ul><ul><li>IsFull -- Determines whether the stack is currently full. </li></ul><ul><li>Push (ItemType newItem) -- Adds newItem to the top of the stack. </li></ul><ul><li>Pop (ItemType &item) -- Removes the item at the top of the stack. </li></ul>
  4. 4. Another Stack Implementation ( 스택의 다른 구현방법 ) <ul><li>One advantage of an ADT is that the kind of implementation used can be changed. </li></ul><ul><li>The dynamic array implementation of the stack has a weakness -- the maximum size of the stack is passed to the constructor as parameter. ( 스택의 동적 배열의 단점 : 스택의 최대 크기가 생성자에게 전달되어야 됨 ) </li></ul><ul><li>Instead we can dynamically allocate the space for each stack element as it is pushed onto the stack. ( 스택에 원소가 삽입될 때 이 원소에 대한 메모리 공간을 할당 ) </li></ul>
  5. 5. Using operator new <ul><li>If memory is available in an area called the free store (or heap), operator new allocates th e requested object, and returns a pointer to the memory allocated. </li></ul><ul><li>( 연산자 new 는 힙 영역의 메모리에 여유공간이 있으면 요청된 객체에 대한 메모리를 할당하고 이의 주소를 반환한다 .) </li></ul><ul><li>The dynamically allocated object exists until the delete operator destroys it. </li></ul>
  6. 6. ItemType is char class StackType Top Pop Push IsFull IsEmpty Private data: topPtr ~StackType ‘ C’ ‘V’ 노드 (node) info next StackType
  7. 7. ItemType is float class StackType Top Pop Push IsFull IsEmpty Private data: topPtr ~StackType 23.4 -7.9 StackType
  8. 8. <ul><li>// DYNAMICALLY LINKED IMPLEMENTATION OF STACK </li></ul><ul><li>typedef char ItemType; </li></ul><ul><li>struct NodeType; //Forward declaration </li></ul><ul><li>class StackType </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>//Identical to previous implementation </li></ul><ul><li>private: </li></ul><ul><li>NodeType* topPtr; </li></ul><ul><li>}; </li></ul><ul><li>. </li></ul><ul><li>. </li></ul><ul><li>. </li></ul><ul><li>s truct NodeType </li></ul><ul><li>{ </li></ul><ul><li>ItemType info; </li></ul><ul><li>NodeType* next; </li></ul><ul><li>}; </li></ul>
  9. 9. Tracing Client Code letter ‘ V’ char letter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);
  10. 10. Adding newItem to the stack <ul><li>NodeType* location; </li></ul><ul><li>location = new NodeType<ItemType>; </li></ul><ul><li>location->info = newItem; </li></ul><ul><li>location->next = topPtr; </li></ul><ul><li>topPtr = location; </li></ul>topPtr ‘ X’ ‘C’ ‘L’ ‘ B’ newItem
  11. 11. <ul><li>// DYNAMICALLY LINKED IMPLEMENTATION OF STACK </li></ul><ul><li>template<class ItemType> </li></ul><ul><li>s truct NodeType </li></ul><ul><li>{ </li></ul><ul><li>ItemType info; </li></ul><ul><li>NodeType* next; </li></ul><ul><li>}; </li></ul><ul><li>template<class ItemType> </li></ul><ul><li>class StackType </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>//Identical to previous implementation </li></ul><ul><li>… </li></ul><ul><li>void Push ( ItemType newItem ); </li></ul><ul><li>void Pop ( ItemType &item ); </li></ul><ul><li>private: </li></ul><ul><li>NodeType <ItemType> * topPtr; </li></ul><ul><li>}; </li></ul>
  12. 12. <ul><li>template<class ItemType> </li></ul><ul><li>void StackType <ItemType> ::Push ( ItemType newItem ) </li></ul><ul><li>// Adds newItem to the top of the stack. </li></ul><ul><li>{ </li></ul><ul><li>if (IsFull()) </li></ul><ul><li>{ </li></ul><ul><li>cout << “Stack is full n”; // throw FullStack(); </li></ul><ul><li>return; </li></ul><ul><li>} </li></ul><ul><li>NodeType* location; </li></ul><ul><li>location = new NodeType <ItemType> </li></ul><ul><li>location->info = newItem; </li></ul><ul><li>location->next = topPtr; </li></ul><ul><li>topPtr = location; </li></ul><ul><li>} </li></ul>Implementing Push
  13. 13. <ul><li>The object currently pointed to by the pointer is deallocated, and the pointer is considered unassigned. The memory is returned to the free store. </li></ul>Using operator delete
  14. 14. Deleting item from the stack <ul><li>NodeType* tempPtr; </li></ul><ul><li>item = topPtr->info; </li></ul><ul><li>tempPtr = topPtr; </li></ul><ul><li>topPtr = topPtr->next; </li></ul><ul><li>delete tempPtr; </li></ul>topPtr ‘ B’ ‘X’ ‘C’ ‘L’ tempPtr item
  15. 15. Deleting item from the stack <ul><li>NodeType<ItemType>* tempPtr; </li></ul><ul><li>item = topPtr->info; </li></ul><ul><li>tempPtr = topPtr; </li></ul><ul><li>topPtr = topPtr->next; </li></ul><ul><li>delete tempPtr; </li></ul>topPtr item ‘ X’ ‘C’ ‘L’ tempPtr ‘ B’
  16. 16. <ul><li>template<class ItemType> </li></ul><ul><li>void StackType <ItemType> ::Pop ( ItemType &item ) </li></ul><ul><li>// Adds newItem to the top of the stack. </li></ul><ul><li>{ </li></ul><ul><li>if (IsEmpty()) </li></ul><ul><li>{ </li></ul><ul><li>cout << “Stack is empty n”; // throw EmptyStack(); </li></ul><ul><li>return; </li></ul><ul><li>} </li></ul><ul><li>NodeType <ItemType> * tempPtr; </li></ul><ul><li>item = topPtr->info; </li></ul><ul><li>tempPtr = topPtr; </li></ul><ul><li>topPtr = topPtr->next; </li></ul><ul><li>delete tempPtr; </li></ul><ul><li>} </li></ul>Implementing Pop
  17. 17. Implementing IsFull <ul><li>template<class ItemType> </li></ul><ul><li>bool StackType <ItemType> ::IsFull() const </li></ul><ul><li>// Returns true if there is no room for another </li></ul><ul><li>// ItemType on the free store; false otherwise </li></ul><ul><li>{ </li></ul><ul><li>NodeType <ItemType> * location; </li></ul><ul><li>try </li></ul><ul><li>{ </li></ul><ul><li> location = new NodeType<ItemType>; </li></ul><ul><li> delete location; </li></ul><ul><li> return false; </li></ul><ul><li>} </li></ul><ul><li>catch(std::bad_alloc exception) </li></ul><ul><li>{ </li></ul><ul><li> return true; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  18. 18. Why is a destructor needed? <ul><li>When a local stack variable goes out of scope, the memory space for data member topPtr is deallocated. But the nodes that topPtr points to are not automatically deallocated. ( 지역 스택 변수가 범위를 벗어나면 topPtr 에 대한 메모리 공간이 해제되지만 topPtr 이 가리키는 노드에 대한 메모리 공간은 해제되지 않는다 .) </li></ul><ul><li>A class destructor is used to deallocate the dynamic memory pointed to by the data member. ( 소멸자에서 데이터 멤버가 가리키는 동적 메모리를 해제하여야 한다 .) </li></ul>
  19. 19. Implementing the Destructor ( 소멸자 구현 ) <ul><li>template<class ItemType> </li></ul><ul><li>StackType <class ItemType> ::~StackType() </li></ul><ul><li>// Post: stack is empty; </li></ul><ul><li>// All items have been deallocated. </li></ul><ul><li>{ </li></ul><ul><li>NodeType* tempPtr; </li></ul><ul><li>while (topPtr != NULL) </li></ul><ul><li>{ </li></ul><ul><li> tempPtr = topPtr; </li></ul><ul><li> topPtr = topPtr-> next; </li></ul><ul><li> delete tempPtr; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  20. 20. What is a Queue? <ul><li>Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear ) , and elements are removed from the other end (the front ). </li></ul><ul><li>A queue is a FIFO “first in, first out” structure. </li></ul>
  21. 21. Queue ADT Operations <ul><li>MakeEmpty -- Sets queue to an empty state. </li></ul><ul><li>IsEmpty -- Determines whether the queue is currently empty. </li></ul><ul><li>IsFull -- Determines whether the queue is currently full. </li></ul><ul><li>Enqueue (ItemType newItem) -- Adds newItem to the rear of the queue. </li></ul><ul><li>Dequeue (ItemType& item) -- Removes the item at the front of the queue and returns it in item. </li></ul>
  22. 22. class QueType<char> QueType ~QueType Enqueue Dequeue . . . Private Data: qFront qRear ‘ C’ ‘ Z’ ‘T’
  23. 23. <ul><li>// DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE </li></ul><ul><li>template<class ItemType> </li></ul><ul><li>s truct NodeType </li></ul><ul><li>{ </li></ul><ul><li>ItemType info; </li></ul><ul><li>NodeType* next; </li></ul><ul><li>}; </li></ul><ul><li>template<class ItemType> </li></ul><ul><li>class QueType { </li></ul><ul><li>public: </li></ul><ul><li>QueType( ); // CONSTRUCTOR </li></ul><ul><li>~QueType( ) ; // DESTRUCTOR </li></ul><ul><li>bool IsEmpty( ) const; </li></ul><ul><li>bool IsFull( ) const; </li></ul><ul><li>void Enqueue( ItemType item ); </li></ul><ul><li>void Dequeue( ItemType& item ); </li></ul><ul><li>void MakeEmpty( ); </li></ul><ul><li>private: </li></ul><ul><li>NodeType <ItemType> * qFront; </li></ul><ul><li>NodeType <ItemType> * qRear; </li></ul><ul><li>}; </li></ul>
  24. 24. <ul><li>// DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE continued </li></ul><ul><li>// member function definitions for class QueType </li></ul><ul><li>template<class ItemType> </li></ul><ul><li>QueType <ItemType> ::QueType( ) // CONSTRUCTOR </li></ul><ul><li>{ </li></ul><ul><li>qFront = NULL; </li></ul><ul><li>qRear = NULL; </li></ul><ul><li>} </li></ul><ul><li>template<class ItemType> </li></ul><ul><li>bool QueType <ItemType> ::IsEmpty( ) const </li></ul><ul><li>{ </li></ul><ul><li>return ( qFront == NULL ) </li></ul><ul><li>} </li></ul>
  25. 25. <ul><li>// DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE continued </li></ul><ul><li>// member function definitions for class QueType </li></ul><ul><li>template<class ItemType> </li></ul><ul><li>QueType <ItemType> ::QueType( ) // CONSTRUCTOR </li></ul><ul><li>{ </li></ul><ul><li>qFront = NULL; </li></ul><ul><li>qRear = NULL; </li></ul><ul><li>} </li></ul><ul><li>template<class ItemType> </li></ul><ul><li>bool QueType <ItemType> ::IsEmpty( ) const </li></ul><ul><li>{ </li></ul><ul><li>return ( qFront == NULL ) </li></ul><ul><li>} </li></ul>
  26. 26. <ul><li>template<class ItemType> </li></ul><ul><li>void QueType <ItemType> ::Enqueue( ItemType newItem ) </li></ul><ul><li>// Adds newItem to the rear of the queue. </li></ul><ul><li>// Pre: Queue has been initialized. </li></ul><ul><li>/ Queue is not full. </li></ul><ul><li>// Post: newItem is at rear of queue. </li></ul><ul><li>{ </li></ul><ul><li>NodeType <ItemType> * ptr; </li></ul><ul><li>ptr = new NodeType <ItemType> ; </li></ul><ul><li>ptr->info = newItem; </li></ul><ul><li>ptr->next = NULL; </li></ul><ul><li>if ( qRear == NULL ) </li></ul><ul><li>qFront = ptr; </li></ul><ul><li>else </li></ul><ul><li>qRear->next = ptr; </li></ul><ul><li>qRear = ptr; </li></ul><ul><li>} </li></ul>
  27. 27. <ul><li>template<class ItemType> </li></ul><ul><li>void QueType <ItemType> ::Dequeue( ItemType& item ) </li></ul><ul><li>// Removes element from from front of queue </li></ul><ul><li>// and returns it in item. </li></ul><ul><li>// Pre: Queue has been initialized. </li></ul><ul><li>// Queue is not empty. </li></ul><ul><li>// Post: Front element has been removed from queue. </li></ul><ul><li>// item is a copy of removed element. </li></ul><ul><li>{ </li></ul><ul><li>NodeType <ItemType> * tempPtr; </li></ul><ul><li>tempPtr = qFront; </li></ul><ul><li>item = qFront->info; </li></ul><ul><li>qFront = qFornt->next; </li></ul><ul><li>if ( qFront == NULL ) </li></ul><ul><li>qRear = NULL; </li></ul><ul><li>delete tempPtr; </li></ul><ul><li>} </li></ul>

×