Question

1. Use diagrams to explain the logic to return a reversed Queue. Provide solutions for both...

1. Use diagrams to explain the logic to return a reversed Queue. Provide solutions for both array Queue and linked list stack. Explain the steps then use pseudocode to explain the implementation. Then use C++, C# or Java to implement the method and test it. Use diagrams to explain the logic to return a reversed Queue. Provide solutions for both array Queue and linked list stack. Explain the steps then use pseudocode to explain the implementation. Then use C++ to implement the method and test it.

2.  Use template to make changes on the code to implement ArrayStack and LinklistQueue, so that the element could be in other data types. Please test the code using characters and strings instead of int numbers. Use C++ to implement the method and test it. Please submit a copy of your source code and screenshots for your test cases. [

0 0
Add a comment Improve this question Transcribed image text
Answer #1

stack linked list:

Ans. #include<iostream>

using namespace std;

template<class t>

struct node

{

          int info;

          node<t>* next;

};

template<class t>

class stacktype

{

          node<t>* top;

public:

          stacktype()

          {

                   top = NULL;

          }

          node<t>* createnewnode(t add);

          void push(node<t>*);

          node<t>* pop();

          int isempty();

          void display();

          void clear();

};

int main()

{

          int choice, e;

          float add;

          node<float>* nptr;

          char ch = 'y';

          stacktype<float>s;

          while (ch == 'y' || ch == 'Y')

          {

                   cout << "main menu" << endl;

                   cout << "1.push" << endl;

                   cout << "2.pop" << endl;

                   cout << "3.isempty" << endl;

                   cout << "4.to display" << endl;

                   cout<<"5.to clear"<<endl;

                   cout << "input choice" << endl;

                   cin >> choice;

                   switch (choice)

{

                   case 1:

                             cout << "input" << endl;

                             cin >> add;

                             nptr = s.createnewnode(add);

                             s.push(nptr);

                             s.display();

                             break;

                   case 2:

                             e = s.isempty();

                             if (e == 1)

                                      cout << "deletion is not possible";

                             else

                             {

                                      nptr = s.pop();

                                      cout << "deleted element is" << nptr->info << endl;

                                      delete nptr;

                             }

                             break;

                   case 3:

                             e = s.isempty();

                             if (e == 1)

                             {

                                      cout << "underflow";

                             }

                             else

                                      cout << "not empty";

                             break;

                   case 4:

                             e = s.isempty();

                             if (e == 1)

                                      cout << "empty stack";

                             else

                                      s.display();

                             break;

                             case 5:

                             e = s.isempty();

                             if (e == 1)

                                      cout << "empty stack";

                             else

                             s.clear();

                             break;

                   default:

                             cout << "invalid";

                   }

                   cout << "do u want to continue" << endl;

                   cin >> ch;

          }

}

template<class t>

int stacktype<t>::isempty()

{

          if (top == NULL)

                   return 1;

          else

                   return 0;

}

template<class t>

node<t>* stacktype<t>::createnewnode(t add)

{

          node<t>* ptr = new(node<t>);

          ptr->info = add;

          ptr->next = NULL;

          return ptr;

}

template<class t>

void stacktype<t>::push(node<t>* nptr)

{

          if (top == NULL)

                   top = nptr;

          else

          {

                   nptr->next = top;

                   top = nptr;

          }

}

template<class t>

node<t>* stacktype<t>::pop()

{

          node<t>*temp;

          temp = top;

          top = top->next;

          return temp;

}

template<class t>

void stacktype<t>::display()

{

          node<t>* temp;

          if (top == NULL)

                   cout << " \nstack is empty" << endl;

          else

                   temp = top;

          do

          {

                   cout << temp->info << "\t";

                   temp = temp->next;

          }

          while (temp != NULL);

}

template<class t>

void stacktype<t>::clear()

{

while(top!=NULL)

{

pop();

}

}

Output: main menu

1.push

2.pop

3.isempty

4.to display

5.to clear

input choice

1

input

45

45      do u want to continue

y

main menu

1.push

2.pop

3.isempty

4.to display

5.to clear

input choice

1

input

78

78      45      do u want to continue

y

main menu

1.push

2.pop

3.isempty

4.to display

5.to clear

input choice

2

deleted element is78

do u want to continue

y

main menu

1.push

2.pop

3.isempty

4.to display

5.to clear

input choice

2

deleted element is45

do u want to continue

y

main menu

1.push

2.pop

3.isempty

4.to display

5.to clear

input choice

3

underflowdo u want to continue

n

>>Stack Operation using Array implementation using template

Ans.

#include<iostream>

#include<iostream>

using namespace std;

const int size = 5;

template <class t>

class stacktype

{

          t st[size];

          int top;

public:

          int isempty();

          int isfull();

          void push(t);

          t pop();

          void display();

          void clear();

          stacktype()

          {

                   top = -1;

          }

};

int main()

{

          stacktype<float>s;

          int f, e;

          float add, del;

          int choice;

          char ch = 'y';

          while (ch == 'y' || ch == 'Y')

          {

                   cout << "\n main menu";

                   cout << "\n1.TO PUSH";

                   cout << "\n2.POP";

                   cout << "\n3.IF EMPTY";

                   cout << "\n4.IF FULL";

                   cout << "\n5.TO DISPLAY";

                   cout<<"\n6.TO CLEAR";

                   cout << "\nenter choice";

                   cin >> choice;

                   switch (choice)

                   {

                   case 1:

                             f = s.isfull();

                             if (f == 1)

                             {

                                      cout << "\n stack is full.Insetion not possible";

                                      break;

                             }

                             else

                             {

                                      cout << "\ninput element to be added";

                                      cin >> add;

                                      s.push(add);

                             }

                             break;

                   case 2:

                             e = s.isempty();

                             if (e == 1)

                             {

                                      cout << "\n stack is empty";

                                      break;

                             }

                             else

                             {

                                      del = s.pop();

                             }

                             cout <<del<< "\n element is popped";

                             break;

                   case 3:

                             e = s.isempty();

                             if (e == 1)

                                      cout << "\n stack is empty";

                             else

                                      cout << "\n not empty";

                             break;

                   case 4:

                             f = s.isfull();

                             if (f == 1)

                             {

                                      cout << "\n stack is full";

                             }

                             else

                             {

                                      cout << "\n not full";

                             }

                             break;

                   case 5:

                             s.display();

                             break;

                   case 6:

                   e=s.isempty();

                  

                             if (e == 1)

                             {

                                      cout << "\n stack is empty";

                                       break;}

                                      else

                                      s.clear();

                                      break;         

                   default:

                             cout << "\n invalid choice";

                   }

                   cout << "\n do u want to continue (y/n)";

                   cin >> ch;

          }

}

template <class t>

int stacktype<t>::isempty()

{

          if (top == -1)

                   return 1;

          else

                   return 0;

}

template <class t>

int stacktype<t>::isfull()

{

          if (top == size - 1)

                   return 1;

          else

                   return 0;

}

template <class t>

void stacktype<t>::push(t add)

{

          top++;

          st[top] = add;

          cout << add << "has been pushed onto the stack";

}

template <class t>

t stacktype<t>::pop()

{

          int d;

          d = st[top];

          top--;

          return d;

}

template <class t>

void stacktype<t>::display()

{

          if (top !=-1)

          {

                   cout << "\n contents of stck are";

                   for (int i = 0;i <= top;i++)

                             cout << "\t" << st[i];

          }

          else

          {

                   cout << "\n stack is empty";

          }

}

template <class t>

void stacktype<t>::clear()

{

top=-1;

cout<<"stack is now cleared"<<endl;

}

Output: main menu

1.TO PUSH

2.POP

3.IF EMPTY

4.IF FULL

5.TO DISPLAY

6.TO CLEAR

enter choice1

input element to be added23

23has been pushed onto the stack

do u want to continue (y/n)y

main menu

1.TO PUSH

2.POP

3.IF EMPTY

4.IF FULL

5.TO DISPLAY

6.TO CLEAR

enter choice1

input element to be added24

24has been pushed onto the stack

do u want to continue (y/n)n

>>Operations on Queues using Linked List implementations?

Ans. #include<iostream>

using namespace std;

template<class t>

struct node

{

          t info;

          node<t>* next;

};

template<class t>

class queuetype

{

          node<t>* f;

          node<t> *r;

public:

          queuetype()

          {

                   f = NULL;

                   r = NULL;

          }

          void insertion(node<t>*);

          node<t>* deletion();

          int isempty();

          void display();

          node<t>* createnewnode(t);

};

int main()

{

          int choice, empty;

          float add;

          node<float>* nptr;

          char ch = 'y';

          queuetype<float> q;

          while (ch == 'y' || ch == 'Y')

          {

                   cout << "main menu" << endl << "1-insertion" << endl << "2-deletion" << endl << "3-is empty" << endl << "4-display" << endl << "enter choice" << endl;

                   cin >> choice;

                   switch (choice)

                   {

                   case 1:

                             cout << "input an element " << endl;

                             cin >> add;

                             nptr = q.createnewnode(add);

                             q.insertion(nptr);

                             q.display();

                             break;

                   case 2:

                             empty = q.isempty();

                             if (empty == 1)

                             {

                                      cout << "underflow deletion is not possible" << endl;

                             }

                             else

                             {

                                      nptr = q.deletion();

                                      cout << nptr->info << " has been deleted" << endl;

                                      delete nptr;

                             }

                             break;

                   case 3:

                             empty = q.isempty();

                             if (empty == 1)

                             {

                                      cout << "queue is empty" << endl;

                             }

                             else

                                      cout << "queue is not empty" << endl;

                             break;

                   case 4:

                             empty = q.isempty();

                             if (empty == 1)

                             {

                                      cout << "queue is empty" << endl;

                             }

                             else

                             {

                                      cout << "contents of queue from frontmost node are" << endl;

                                      q.display();

                             }

                             break;

                   default:

                             cout << "invalid choice";

                   }

                   cout << "Do you want to continue(y/n)" << endl;

                   cin >> ch;

          }

}

template<class t>

int queuetype<t>::isempty()

{

          if (f == NULL)

                   return 1;

          else

                   return 0;

}

template<class t>

void queuetype<t>::insertion(node<t>* nptr)

{

          if (r == NULL)

          {

                   r = nptr;

                   f = nptr;

          }

          else

          {

                   r->next = nptr;

                   r = nptr;

          }

}

template<class t>

node<t>* queuetype<t>::deletion()

{

          node<t>* temp;

          if (f == r)

          {

                   temp = f;

                   f = r = NULL;

          }

          else

          {

                   node<t>* temp;

                   temp = f;

                   f = f->next;

          }

          return temp;

}

template<class t>

void queuetype<t>::display()

{

          node<t>* temp;

          for (temp = f;temp != NULL;temp = temp->next)

          {

                   cout << temp->info << " ";

          }

}

template<class t>

node<t>* queuetype<t>::createnewnode(t add)

{

          node<t>* ptr;

          ptr = new(node<t>);

          ptr->info = add;

          ptr->next = NULL;

          return ptr;

}

Output:

main menu

1-insertion

2-deletion

3-is empty

4-display

enter choice

1

input an element

34

34 Do you want to continue(y/n)

y

main menu

1-insertion

2-deletion

3-is empty

4-display

enter choice

23

invalid choiceDo you want to continue(y/n)

y

main menu

1-insertion

2-deletion

3-is empty

4-display

enter choice

1

input an element

67

34 67 Do you want to continue(y/n)

y

main menu

1-insertion

Add a comment
Know the answer?
Add Answer to:
1. Use diagrams to explain the logic to return a reversed Queue. Provide solutions for both...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and...

    In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and the Stack ADT. In addition, you will be using two different implementations for each ADT: Array Based Linked You will also be writing a driver to test your Queue and Stack implementations and you will be measuring the run times and memory use of each test case. You will also be adding some functionality to the TestTimes class that you created for Homework 1....

  • It is C++ problems, please explain your each line of code as well. Task 1: Implement/...

    It is C++ problems, please explain your each line of code as well. Task 1: Implement/ Hard Code a Doubly Linked Lists and make it a Stack. Do not use ::list:: class from the STD library for this example. The user will input a string and the program will output the string backwards. Displaying correct Stack implementation. Utilize the conventional static methods as needed. push() pop() empty() peek() peek() Sample Execution Please input String: happy yppah Task 2: Implement /...

  • Implement the stack queue data structure with a linked list implementation to get the given test...

    Implement the stack queue data structure with a linked list implementation to get the given test code in driver.cpp to work properly: driver.cpp code: #include <iostream> #include "stackLL.h" using namespace std; int main() { /////////////Test code for stack /////////////// stackLL stk; stk.push(5); stk.push(13); stk.push(7); stk.push(3); stk.push(2); stk.push(11); cout << "Popping: " << stk.pop() << endl; cout << "Popping: " << stk.pop() << endl; stk.push(17); stk.push(19); stk.push(23); while( ! stk.empty() ) { cout << "Popping: " << stk.pop() << endl; }...

  • 1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push(...

    1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push( "Jane" ); push( "Jess" ); push( "Jill" ); push( pop() ); push( "Jim" ); String name = pop(); push( peek() ); Write separate programs for each of the data structures Stack, Queue, PriorityQueue, and List. Use the appropriate push(), pop(), peek() for each of the respective ADT's. Use the Java class library of the ADT's as opposed to the author's implementation. What is in...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return t...

    e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } } f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex...

  • This lab will give you a practice with both queue and stack ADTs. In this work,...

    This lab will give you a practice with both queue and stack ADTs. In this work, you are to determine if an input string is a palindrome. A string of characters is a palindrome if and only if it reads the same forward and backward. Examples: eye, abba, civic, radar, and so on. Sample Output: Please enter a string of characters: abba The given string is a palindrome. Want to examine another string? (y/n): y Please enter a string of...

  • Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

    Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write...

    Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write the pseudocode for removing and returning only the second element from the Stack. The top element of the Stack will remain the top element after this operation. You may assume that the Stack contains at least two items before this operation. (a) Write the algorithm as a provider implementing a Stack using a contiguous memory implementation. You can refer to the implementation given in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT