I\'m not sure how to start with this assignment. Can someone help me please? I a
ID: 3696271 • Letter: I
Question
I'm not sure how to start with this assignment. Can someone help me please? I am still trying to get a grasp of the C++ programming.
1.Create an input data file using name “pa12input.dat,” using the following data as a reference file:
5 11 21 18 3 19 9 3 2 7 11 3
this file is for commonality testing, and other files may be used for grading. The program must work with a variety of files.
2.Read the file into a Linked List, Close the input file.
3.Write the loop through the list and print the contents to the screen.
4.Write a “Search” function, to allow user provide a number from keyboard, then return if the list contain the number.
5.Write another “Frequency” function, to allow user provide a number from keyboard, then return how many time the number occur in the list.
Explanation / Answer
in the two hours i could write only this much
loading the content of the file into the linked list and display the content on to the screen
save the file using list.cpp
run using g++ list.cpp
----------------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
struct listNode {
int val;
struct listNode *next;
bool operator<(const struct listNode &n2) const {
return this->val < n2.val;
}
bool operator>(const struct listNode &n2) const {
return this->val > n2.val;
}
};
struct listNode *newListNode(int val);
void deleteList(struct listNode *);
void printList(struct listNode *);
int main(int argc, char *argv[]) {
struct listNode *head,*p;
string filename,in;
stringstream ss;
int n;
cout << "Enter filename: ";
getline(cin,filename);
ifstream inFile(filename.c_str());
if (inFile.is_open()) {
p = head = newListNode(-1);
while (inFile) {
getline(inFile,in);
ss.clear(); ss.str(in);
while (ss >> n) {
p->next = newListNode(n);
p = p->next;
}
}
inFile.close();
cout << endl;
printList(head);
} else {
cout << filename << " not found." << endl;
}
return 0;
}
//
// newListNode: create new struct listNode
//
struct listNode *newListNode(int val) {
struct listNode *n = new listNode;
n->val = val;
n->next = NULL;
return n;
}
//
// printList: display the list
//
void printList(struct listNode *head) {
struct listNode *p = head->next;
cout << "List: ";
while (p != NULL) {
cout << p->val << ' ';
p = p->next;
}
cout << endl << endl;
}