강의자료8
Upcoming SlideShare
Loading in...5
×
 

강의자료8

on

  • 644 views

 

Statistics

Views

Total Views
644
Views on SlideShare
644
Embed Views
0

Actions

Likes
0
Downloads
3
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

강의자료8 강의자료8 Presentation Transcript

  • 스택과 큐 ( 연결구조에 의한 구현 )
  • Definition of Stack
    • 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.
    • A stack is a LIFO “last in, first out” structure.
  • Stack ADT Operations
    • IsEmpty -- Determines whether the stack is currently empty.
    • IsFull -- Determines whether the stack is currently full.
    • Push (ItemType newItem) -- Adds newItem to the top of the stack.
    • Pop (ItemType &item) -- Removes the item at the top of the stack.
  • Another Stack Implementation ( 스택의 다른 구현방법 )
    • One advantage of an ADT is that the kind of implementation used can be changed.
    • The dynamic array implementation of the stack has a weakness -- the maximum size of the stack is passed to the constructor as parameter. ( 스택의 동적 배열의 단점 : 스택의 최대 크기가 생성자에게 전달되어야 됨 )
    • Instead we can dynamically allocate the space for each stack element as it is pushed onto the stack. ( 스택에 원소가 삽입될 때 이 원소에 대한 메모리 공간을 할당 )
  • Using operator new
    • 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.
    • ( 연산자 new 는 힙 영역의 메모리에 여유공간이 있으면 요청된 객체에 대한 메모리를 할당하고 이의 주소를 반환한다 .)
    • The dynamically allocated object exists until the delete operator destroys it.
  • ItemType is char class StackType Top Pop Push IsFull IsEmpty Private data: topPtr ~StackType ‘ C’ ‘V’ 노드 (node) info next StackType
  • ItemType is float class StackType Top Pop Push IsFull IsEmpty Private data: topPtr ~StackType 23.4 -7.9 StackType
    • // DYNAMICALLY LINKED IMPLEMENTATION OF STACK
    • typedef char ItemType;
    • struct NodeType; //Forward declaration
    • class StackType
    • {
    • public:
    • //Identical to previous implementation
    • private:
    • NodeType* topPtr;
    • };
    • .
    • .
    • .
    • s truct NodeType
    • {
    • ItemType info;
    • NodeType* next;
    • };
  • 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’);
  • Adding newItem to the stack
    • NodeType* location;
    • location = new NodeType<ItemType>;
    • location->info = newItem;
    • location->next = topPtr;
    • topPtr = location;
    topPtr ‘ X’ ‘C’ ‘L’ ‘ B’ newItem
    • // DYNAMICALLY LINKED IMPLEMENTATION OF STACK
    • template<class ItemType>
    • s truct NodeType
    • {
    • ItemType info;
    • NodeType* next;
    • };
    • template<class ItemType>
    • class StackType
    • {
    • public:
    • //Identical to previous implementation
    • void Push ( ItemType newItem );
    • void Pop ( ItemType &item );
    • private:
    • NodeType <ItemType> * topPtr;
    • };
    • template<class ItemType>
    • void StackType <ItemType> ::Push ( ItemType newItem )
    • // Adds newItem to the top of the stack.
    • {
    • if (IsFull())
    • {
    • cout << “Stack is full n”; // throw FullStack();
    • return;
    • }
    • NodeType* location;
    • location = new NodeType <ItemType>
    • location->info = newItem;
    • location->next = topPtr;
    • topPtr = location;
    • }
    Implementing Push
    • The object currently pointed to by the pointer is deallocated, and the pointer is considered unassigned. The memory is returned to the free store.
    Using operator delete
  • Deleting item from the stack
    • NodeType* tempPtr;
    • item = topPtr->info;
    • tempPtr = topPtr;
    • topPtr = topPtr->next;
    • delete tempPtr;
    topPtr ‘ B’ ‘X’ ‘C’ ‘L’ tempPtr item
  • Deleting item from the stack
    • NodeType<ItemType>* tempPtr;
    • item = topPtr->info;
    • tempPtr = topPtr;
    • topPtr = topPtr->next;
    • delete tempPtr;
    topPtr item ‘ X’ ‘C’ ‘L’ tempPtr ‘ B’
    • template<class ItemType>
    • void StackType <ItemType> ::Pop ( ItemType &item )
    • // Adds newItem to the top of the stack.
    • {
    • if (IsEmpty())
    • {
    • cout << “Stack is empty n”; // throw EmptyStack();
    • return;
    • }
    • NodeType <ItemType> * tempPtr;
    • item = topPtr->info;
    • tempPtr = topPtr;
    • topPtr = topPtr->next;
    • delete tempPtr;
    • }
    Implementing Pop
  • Implementing IsFull
    • template<class ItemType>
    • bool StackType <ItemType> ::IsFull() const
    • // Returns true if there is no room for another
    • // ItemType on the free store; false otherwise
    • {
    • NodeType <ItemType> * location;
    • try
    • {
    • location = new NodeType<ItemType>;
    • delete location;
    • return false;
    • }
    • catch(std::bad_alloc exception)
    • {
    • return true;
    • }
    • }
  • Why is a destructor needed?
    • 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 이 가리키는 노드에 대한 메모리 공간은 해제되지 않는다 .)
    • A class destructor is used to deallocate the dynamic memory pointed to by the data member. ( 소멸자에서 데이터 멤버가 가리키는 동적 메모리를 해제하여야 한다 .)
  • Implementing the Destructor ( 소멸자 구현 )
    • template<class ItemType>
    • StackType <class ItemType> ::~StackType()
    • // Post: stack is empty;
    • // All items have been deallocated.
    • {
    • NodeType* tempPtr;
    • while (topPtr != NULL)
    • {
    • tempPtr = topPtr;
    • topPtr = topPtr-> next;
    • delete tempPtr;
    • }
    • }
  • What is a Queue?
    • 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 ).
    • A queue is a FIFO “first in, first out” structure.
  • Queue ADT Operations
    • MakeEmpty -- Sets queue to an empty state.
    • IsEmpty -- Determines whether the queue is currently empty.
    • IsFull -- Determines whether the queue is currently full.
    • Enqueue (ItemType newItem) -- Adds newItem to the rear of the queue.
    • Dequeue (ItemType& item) -- Removes the item at the front of the queue and returns it in item.
  • class QueType<char> QueType ~QueType Enqueue Dequeue . . . Private Data: qFront qRear ‘ C’ ‘ Z’ ‘T’
    • // DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE
    • template<class ItemType>
    • s truct NodeType
    • {
    • ItemType info;
    • NodeType* next;
    • };
    • template<class ItemType>
    • class QueType {
    • public:
    • QueType( ); // CONSTRUCTOR
    • ~QueType( ) ; // DESTRUCTOR
    • bool IsEmpty( ) const;
    • bool IsFull( ) const;
    • void Enqueue( ItemType item );
    • void Dequeue( ItemType& item );
    • void MakeEmpty( );
    • private:
    • NodeType <ItemType> * qFront;
    • NodeType <ItemType> * qRear;
    • };
    • // DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE continued
    • // member function definitions for class QueType
    • template<class ItemType>
    • QueType <ItemType> ::QueType( ) // CONSTRUCTOR
    • {
    • qFront = NULL;
    • qRear = NULL;
    • }
    • template<class ItemType>
    • bool QueType <ItemType> ::IsEmpty( ) const
    • {
    • return ( qFront == NULL )
    • }
    • // DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE continued
    • // member function definitions for class QueType
    • template<class ItemType>
    • QueType <ItemType> ::QueType( ) // CONSTRUCTOR
    • {
    • qFront = NULL;
    • qRear = NULL;
    • }
    • template<class ItemType>
    • bool QueType <ItemType> ::IsEmpty( ) const
    • {
    • return ( qFront == NULL )
    • }
    • template<class ItemType>
    • void QueType <ItemType> ::Enqueue( ItemType newItem )
    • // Adds newItem to the rear of the queue.
    • // Pre: Queue has been initialized.
    • / Queue is not full.
    • // Post: newItem is at rear of queue.
    • {
    • NodeType <ItemType> * ptr;
    • ptr = new NodeType <ItemType> ;
    • ptr->info = newItem;
    • ptr->next = NULL;
    • if ( qRear == NULL )
    • qFront = ptr;
    • else
    • qRear->next = ptr;
    • qRear = ptr;
    • }
    • template<class ItemType>
    • void QueType <ItemType> ::Dequeue( ItemType& item )
    • // Removes element from from front of queue
    • // and returns it in item.
    • // Pre: Queue has been initialized.
    • // Queue is not empty.
    • // Post: Front element has been removed from queue.
    • // item is a copy of removed element.
    • {
    • NodeType <ItemType> * tempPtr;
    • tempPtr = qFront;
    • item = qFront->info;
    • qFront = qFornt->next;
    • if ( qFront == NULL )
    • qRear = NULL;
    • delete tempPtr;
    • }