stack queue

Download Stack queue

Post on 23-Jan-2017

39 views

Category:

Technology

0 download

Embed Size (px)

TRANSCRIPT

  • Stacks and Queues

    *

    struct Node{ double data;Node* next;};

    class List {public:List(); // constructorList(const List& list); // copy constructor~List(); // destructorList& operator=(const List& list); // assignment operator

    bool empty() const; // boolean functionvoid addHead(double x); // add to the headdouble deleteHead(); // delete the head and get the head element// List& rest(); // get the rest of the list with the head removed// double headElement() const; // get the head element

    void addEnd(double x); // add to the enddouble deleteEnd(); // delete the end and get the end element// double endElement(); // get the element at the end

    bool searchNode(double x); // search for a given xvoid insertNode(double x); // insert x in a sorted listvoid deleteNode(double x); // delete x in a sorted list

    void print() const; // outputint length() const; // count the number of elementsprivate:Node* head;};More complete list ADT

    *

    Stack OverviewStack ADTBasic operations of stackPushing, popping etc. Implementations of stacks using array linked list

    *

    StackA stack is a list in which insertion and deletion take place at the same endThis end is called topThe other end is called bottom

    Stacks are known as LIFO (Last In, First Out) lists.The last element inserted will be the first to be retrieved

    *

    Push and PopPrimary operations: Push and PopPushAdd an element to the top of the stackPopRemove the element at the top of the stack

    *

    Implementation of StacksAny list implementation could be used to implement a stackArrays (static: the size of stack is given initially)Linked lists (dynamic: never become full)We will explore implementations based on array and linked list

    *

    Stack ADT

    class Stack {public:Stack(); // constructorStack(const Stack& stack); // copy constructor~Stack(); // destructor

    bool empty() const; void push(const double x); double pop(); // change the stack double top() const; // keep the stack unchanged

    // bool full(); // optional// void print() const; private:};update,logical constructor/destructor, composition/decompositionphysical constructor/destructorCompare with List, see that its operations that define the type!inspection, access

    *

    Using Stackint main(void) {Stack stack;stack.push(5.0);stack.push(6.5);stack.push(-3.0);stack.push(-8.0);stack.print();cout