CS210 Data Structures & Algorithms I
LAB 4
w/b 22/11/99
Mr. Tom Lysaght
Assignment due Friday, December 10th

 

  1. Using functional decomposition design a program which implements a stack data structure. Operations on the stack will include:
  2. -initialisation(a class constructor), Push(), Pop()
    -a function to test if the stack is empty or full (IsEmpty(), IsFull())
    -a function to empty the stack (MakeEmpty).
    -A special index TopOfStack gives the index of the current top of the stack.
    -A function Top() should return the top element on the stack

    Implement the stack program using an array-based stack class. The array should be declared as a private member of the class and should be dynamically allocated with a constructor. Use the following class definition for the program.

    class stack
    {
        public:
                stack();
                ~stack() { // delete the array}
                void Push(int S);
                void Pop();
                int Top();
                int IsEmpty() {return TopOfStack == -1;}
                int IsFull(){return TopOfStack == Maxsize-1;}
                void MakeEmpty() {TopOfStack = -1;}

        private:
                int Maxsize;
                int TopOfStack;
                int *SArray;
    };

    Declare an object of the class stack using a pointer with new.
    Test the program with calls to Push(), Pop() and Top(). Use the method MakeEmpty() to Empty the stack and then push one element onto the stack and call Top() to show the element on the top of the stack.
     

  3. Using functional decomposition design a program which implements a queue data structure. Operations on the queue will include
  4. -initialisation(a class constructor), Enqueue(), Dequeue(),
    -a function to test if the queue is empty or full (IsEmpty(), IsFull())
    -a function to empty the queue (MakeEmpty).
    -A special index Back is used to add elements to the queue and an index Front
    keeps the index of the first element in the queue.
    -A function GetFront() should return the first(front) element on the queue.

    Implement the queue program using an array-based queue class. The array should be declared as a private member of the class and should be dynamically allocated with a constructor.

    class queue
    {
        public:
                queue();
                ~queue() {delete [] QArray;}
                void Enqueue(int X);
                void Dequeue();
                int GetFront();
                int IsEmpty() {return Currentsize == 0;}
                int IsFull(){return Currentsize == Maxsize;}
                void MakeEmpty();

        private:
                int Maxsize;
                int Currentsize;
                int Front;
                int Back;
                int *QArray;
    };

    Amend the queue program to use a circular array. Include a method void Increment(int X); as a private member of the class to allow use of the circular array.
     

  5. Give a linked-list implementation of the stack program in part 1 above.
        Please hand up:
        Stepwise refinement for Part 1. and Part 2.
        A printout of all code and a disk containing the programs.