Sort the data stored in 1) stack 2) queue and 3) linked list entered in part 1 (using arrays)
// 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;
}
Comments
Leave a comment