Create a program that contains the following functions:
-create two dynamic stacks with integer data contained in an external file.
-Sort items in stacks by the selection sort method
-Record outputs in an external file.
Main function main ()-menu selection functions and check the status of the data
I dont know if this is right and i cant do the input function/it is wrong/ can somebody help me and if i have something wrong with this code /sorry for bad english/-Example: input: s1: 1,3,5,9,10 / s2:2,3,4,6,7,11 sorting with selection sort-> output: 1,2,3,3,4,5,6,7,8,9,10,11
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include<fstream>
using namespace std;
typedef struct elem {
int key;
elem *next;
} *stack;
void push(stack& S, int n) {
stack p = S;
S = new elem;
S->key = n;
S->next = p;
}
int pop(stack& S) {
int n;
stack p = S;
n = S->key;
S = S->next;
delete p;
return n;
}
void file_input()
{
int a;
ifstream file_1;
file_1.open("file.txt", ios::in);
if (file_1)
{
while (!file_1.eof())
{
file_1 >> a;
push(s1, a);
}
file_1.close();
cout << endl << "Done" << endl << endl;
}
else
cout << "Error" << endl;
int sort2(stack& s1, stack& s2) {
int m;
m = pop(s1);
while (s1)
if (m<s1->key) push(s2, pop(s1));
else { push(s2, m); m = pop(s1); }
return m;
}
void sort(stack& s1) {
stack s2 = NULL;
ofstream file;
file.open("output.txt", ios::out);
if (file) {
while (s1) {
file << sort2(s1, s2) << " ";
if (s2) file << sort2(s2, s1) << " ";
}
file << endl;
file.close();
cout << "Saved in output.txt";
}
else {
cout << "output.txt can't open";
while (s1) {
cout << sort2(s1, s2) << " ";
if (s2) cout << sort2(s2, s1) << " ";
}
}
cout << endl;
}
int main() {
stack Stack = NULL;
int n, t;
srand(static_cast<unsigned int>(time(NULL)));
while (1) {
cout << "0 for EXIT, N = ";
cin >> n;
if (n<1) return 0;
for (int i = 0; i<n; i++) {
t = rand() % 1000;
cout << t << " ";
push(Stack, t);
}
cout << "\n\nSorting...\n";
sort(Stack);
cout << endl << endl;
}
}