Answer to Question #203445 in C++ for Ayesha

Question #203445

Sort the data stored in 1) stack 2) queue and 3) linked list entered in part 1 (using arrays)


1
Expert's answer
2021-06-05T02:14:09-0400
// sorting stack
stack<int> sortedStack(stack<int> Stack) {
    int tmpStack[Stack.size()];
    int i=0;
    while (!Stack.empty()) {
        tmpStack[i++]=Stack.top();
        Stack.top();
    }
    sort(tmpStack, tmpStack+i);
    int j=0;
    while (j<i) {
        Stack.push(tmpStack[j++]);
    }
    return Stack;
}
// sorting queue
queue<int> sortedQueue(queue<int> Queue) {
    int tmpQueue[Queue.size()];
    int i=0;
    while (!Queue.empty()) {
        tmpQueue[i++]=Queue.front();
        Queue.pop();
    }
    sort(tmpQueue, tmpQueue+i);
    int j=0;
    while (j<i) {
        Queue.push(tmpQueue[j++]);
    }
    return Queue;
}
// sorting list
list<int> sortedList(list<int> List) {
    int tmpList[List.size()];
    int i=0;
    while (!List.empty()) {
        tmpList[i++]=List.back();
        List.pop_back();
    }
    sort(tmpList, tmpList+i);
    int j=0;
    while (j<i) {
        List.push_back(tmpList[j++]);
    }
    return List;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog