The following example shows how you can append member functions to add an element to the end of the queue and get the element from the front of the queue. To create a queue collectionÄerive a new list class from one of the predefined list classes provided with the Microsoft Foundation Class Library and add more member functions to support the semantics of queue operations. The list collection member functions AddTail and RemoveHead can be used to add and remove elements specifically from the head or tail of the list thus, the most recently added element is always the last to be removed. As more people come, they go to the end of the line to wait their turn. The first person in line is the first to be served. A queue is like a line of people in a cafeteria. QueuesÄ«ecause the standard list collection has both a head and a tail, it is also easy to create a derived list collection that mimics the behavior of a first-in-first-out queue. Adaptor patterns implement a class by using methods of another class In general, adaptor classes specialize general classes Two such applications: - Specialize a general class by changing some. The user can call any CObList member function, whether it makes sense for a stack or not. Linked list implementation of stack data structure must support basic stack operations like push, pop, peek and isEmpty. Stacks, Queues, and Linked Lists 22 The Adaptor Pattern Using a deque to implement a stack or queue is an example of the adaptor pattern. Note that this approach exposes the underlying CObList class. The following example shows how to add member functions to push elements on to the stack, peek at the top element of the stack, and pop the top element from the stack: class CTray : public CObject To create a stack collectionÄerive a new list class from one of the existing MFC list classes and add more member functions to support the functionality of stack operations. The list collection member functions AddHead and RemoveHead can be used to add and remove elements specifically from the head of the list thus, the most recently added element is the first to be removed. The last tray added is the first to be removed. 1) Insert element to queue 2) Delete element from queue 3) Display all the elements of queue 4) Exit Enter your choice : 1 Insert the element in queue : 4 Enter your choice : 1 Insert the element in queue : 3 Enter your choice : 1 Insert the element in queue : 5 Enter your choice : 2 Element. As trays are added to the stack, they go on top of the stack. The output of the above program is as follows. Convert a + b c + (d e + f) g the infix expression into postfix form. A stack is like a stack of trays in a cafeteria. Source code for stack operations, using linked list: include .StacksÄ«ecause the standard list collection has both a head and a tail, it is easy to create a derived list collection that mimics the behavior of a last-in-first-out stack. The examples use classes derived from CList, but you can use CList directly unless you need to add functionality. This article explains how to create other data structures, such as stacks and queues, from MFC list classes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |