Write a method switchPairs that takes a stack of integers as a parameter and tha
ID: 3650362 • Letter: W
Question
Write a method switchPairs that takes a stack of integers as a parameter and that switches successive pairs of numbers starting at the bottom of the stack. For example, if the stack initially stores these values:bottom [3, 8, 17, 9, 99, 9, 17, 8, 3, 1, 2, 3, 4, 14] top
Your method should switch the first pair (3, 8), the second pair (17, 9), the third pair (99, 9), and so on, yielding this sequence:
bottom [8, 3, 9, 17, 9, 99, 8, 17, 1, 3, 3, 2, 14, 4] top
If there are an odd number of values in the stack, the value at the top of the stack is not moved. For example, if the original stack had stored:
bottom [3, 8, 17, 9, 99, 9, 17, 8, 3, 1, 2, 3, 4, 14, 42] top
It would again switch pairs of values, but the value at the top of the stack (42) would not be moved, yielding this sequence:
bottom [8, 3, 9, 17, 9, 99, 8, 17, 1, 3, 3, 2, 14, 4, 42] top
Do not make assumptions about how many elements are in the stack. Use one queue as auxiliary storage.
Explanation / Answer
#include #include using namespace std; struct node { int value; int b; char c; struct node * next; }; void traverseList(node *); void pairReverse(node **); int main(){ node * head = NULL; // This loop will only create the list of 11 nodes for ( int i = 11; i > 0; i--) { node * temp = new node; temp->value = i; temp->next = head; head = temp; } coutnext; walker->next = walkerb->next; walkerb->next = walker; if (prevwalkerb != NULL) prevwalkerb->next = walkerb; if (prevwalkerb== NULL) first = walkerb; prevwalkerb = walker; walker = walker->next; } *head = first; } void traverseList (node * temp) { coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.