Question

In: Computer Science

C++ Please Fill in for the functions for the code below. The functions will implement an...

C++ Please

Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below:

Stack *ptr = new Stack();

ptr->push(value);

int pop1 = ptr->pop();

int pop2 = ptr->pop();

bool isEmpty = ptr->empty();

class Stack{

    public:

// Default Constructor

Stack() {// ... }

// Push integer n onto top of stack

void push(int n) {// ... }

// Pop element on top of stack

int pop() {// ... }

// Get the top element but do not pop it (peek at top of stack)

int top() {// ... }

// Return whether the stack is empty or not

bool empty() {// ... }

};

Solutions

Expert Solution

A stack is a linear structure implemented in LIFO(last in first out) manner where insertions and deletions are restricted to occure only at one end ie, stack's top.The stack is also a dynamic data structure as it can grow(with increas in number of elements or shrink(with decrease in number of elements).

A Queue is also a linear dynamic structure implemented in FIFO (first in first out) manner where insertions can occure at the"rear" end ,and deletions occur only from "front" end.

Stack:

Logically ,a stack is a LIFO structure and physically it can be implemented as an array or as a linked list.A stack implemented as an array inherits all the properties of an array and if implemented as a linked list ,all characteristics of a linked list are possessed by it.But whatever way a stack may be implemented ,insertions and deletions occure at the top only.An insertion in a stack is called pushing and a deletion from a stack is called popping.

Insertion in a stack as an array(PUSHING):

Pushing an element in the stack involves shifting of elements as the new elements will be inserted at the top only.

  

      

a       
s
d
g

fig(.a)

w
a
s
d
g

fig(b)

e
w
a
s
d
g

fig(c)

for instance ,at any point of time we have stack(as an array) like fig(a).

After pushing W,stack becomes as fig(b).After pushing another element E,the stack becomes fig(c).Every time a push occures,elements are shifted down wards to accommodate the new entrant.

In case the array is full and no new element can be accommodated ,it is called STACK-FULL condition.This condition is also called an overflow.

Algorithm for a push in a stack:

1.ctr=N,top=0

2.repeat steps 3 and 4 until ctr=top

3.stack[ctr]=stack[ctr-1]

4.ctr=ctr-1

5.stack[top]=ITEM.

Deletion in a stack (as an array) POPPING:

Popping ie,deletion of an element from a stack also involves re shifting of elements .For instance ,from the stack shown in fig(a),the top most element which is E,can be popped and after popping E,the stack looks as shown in fig(d),after popping another element W,the stack looks as fig(e).In case ` the last element is popped ,the stack becomes empty.

w
a
s
d
g

fig(d)

a
s
d
g

fig(e)

Algorithm for a pop in a stack:

1.ctr=top.

/*print top most element*/

2.print stack[top].

/*move the elements upward to fill the top */

3.repeat steps 4 and 5 until ctr=N.

4.stack[ctr]=stack[ctr+1].

5.ctr==ctr+1.1``

template <class T>

class stack

{

private:

int top;

T*nodes;

public:

stack();

int empty(void);

void push(T&);

T pop (void);

T pop(int &);

~stack ();

};

program:

template <class T> int stack<T>:: empty(void)

{

return top>=0;

};

template<class T> void stack<T>::push(T &j)

{

if(top==STACKSIZE)

{

count<<"stack overflow"<< end1;

return;

}

nodes[++top]=j;

}

template<class T> T stack <T>::pop(void)

{

T p;

if(empty())

{

count<<"stack underflow"<< end1;

return p;

}

p=nodes[top--];

return p;

};

template <class T>T stack<T>::pop(int & und)

{

Tp;

if (empty())

{

und=1;

return p;

}

und=0;

p=nodes[top--];

return p;

};


Related Solutions

C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments string at the bottom displaying your output. Using sets (described in Deitel chapter 6) will simplify your work. Below is the starter template for the code: def overlap(user1, user2, interests): """ Return the number of interests that user1 and user2 have in common """ return 0    def most_similar(user, interests): """ Determine the name of user who is most similar to the input user...
Problem: Write a C++ program that will implement and test the five functions described below that...
Problem: Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions: You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness. 1. minimum: takes an int array and the array's size as arguments. It should return the minimum value of the array elements. Do not use square brackets anywhere in the function, not even the parameter...
[Hash Tables] Given the following code in C++, implement the functions in table2.cpp. The first 2...
[Hash Tables] Given the following code in C++, implement the functions in table2.cpp. The first 2 files (table2.h, short story text file) are all fully coded and commented for convenience. *****I have given references that I've completed previously for the table2.cpp file, I just need help applying them. Will provide good rating, thanks****** -------------------------------------------------------------------------------------------------------------------------- table2.h (given file, do not edit): // FILE: table2.h // // ABSTRACT BASE CLASS: Table //    1. The number of records in the Table is...
Fill in the blanks in the MATLAB code below.
Fill in the blanks in the MATLAB code below. (Do not type unnecessary words or blank spaces in your response. The correct answers are case-sensitive.) % Consider a row vector v. % Complete the lines of code below to find the average and standard deviation of the elements of vector v such that these two values are assigned to variables M and S, respectively. E = G =
PLEASE DO NOT CHANGE THE CODE BELOW! In this problem, we will implement an nth root...
PLEASE DO NOT CHANGE THE CODE BELOW! In this problem, we will implement an nth root finder. In particular, please fill in the findNthRoot(int number, int n, int precision) method in the class NthRootFinder. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should fill in the answer with decimal places (i.e. with input 41 and precision 5, we should return 41.00000.) You may...
USE ONLY THE BELOW FUNCTIONS AND IMPLEMENT THE MISSING PART implement the following missing functions from...
USE ONLY THE BELOW FUNCTIONS AND IMPLEMENT THE MISSING PART implement the following missing functions from the implementation: * reset * intersection * difference Set Set::intersection(Set& s){ Set r; // find intersection return r; } Set Set::difference(Set& s){ Set r; // find difference return r; } void Set::reset(int c){ // increase the capacity and clear the data } driver program int a1[] = {10,5,7,3,9}; Set s1(5); s1.insert(a1,5); s1.print("s1"); int a2[] = {2,9,6}; Set s2(3); s2.insert(a2,3); s2.print("s2"); Set s3 = s1.unionset(s2);...
IN C ONLY (follow instruction please) Implement the following functions: Queue * initQueue( ) // Return...
IN C ONLY (follow instruction please) Implement the following functions: Queue * initQueue( ) // Return empty queue // enqueue and dequeue each return an int error code int enqueue(int, Queue *) int dequeue(Queue *, int *) int getQsize(Queue *) // returns # of items in queue void freeQueue(Queue *) // Free *all* space Implement the above using a circular-linked list. Again, all operations except freeQueue should take O(1) time. You will need to document each of your functions to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT