Question #198798

  1. Sort the data stored in 1) stack 2) queue and 3) linked list
  2. Take user input and insert the data at the respective position in the sorted 1) stack 2) queue and 3) linked list.

Expert's answer

// 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;
}
stack<int> Stack;
// insertion to the sorted stack
Stack.push(item);
Stack=sortedStack(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;
}
queue<int> Queue;
// insertion to the sorted queue
Queue.push(item);
Queue=sortedQueue(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;
}
list<int> List;
// insertion to the sorted list
List.push_back(item);
List=sortedList(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!

LATEST TUTORIALS
APPROVED BY CLIENTS