Easier Way To Do a Linked List?
This is how my teacher explained to use how to do a linked list.
Code:
#include <iostream>
#include <cstdlib>
#include <math.h>
#include <fstream>
#include <string>
using namespace std;
struct Node{
string parts;
int ID;
int date;
float price;
Node *next;
Node();
};
Node::Node(){
next=NULL; }
void Insert(Node *&Front,Node *&Rear, string n_parts, int date, int n_Id, float n_price, int count){
Node *n_Info = new Node(); // dynamically allocation a node
n_Info->parts = n_parts; // enter info onto this node's data fields
n_Info->date = date;
n_Info->price = n_price;
n_Info->ID = n_Id;
if(count ==0){
Front = Rear = n_Info;
}
else if(count > 0){
Rear->next = n_Info;
// Rear = n_Info; // this statement acts the same as next line
Rear = Rear -> next;
}
}
void Flush(Node *&Front,Node *&Rear, int L_size){
Node *temp_node = Front;
for(int i =0; i<L_size; i++){
Front = Front->next;
delete temp_node;
temp_node=Front;
}
}
int main(){
Node *Front = NULL;
Node *Rear = NULL;
int L_size = 0;
ifstream in("a7.txt");
char temp[100];
string temp_parts;
int temp_ID;
int temp_date;
float temp_price;
while(in.peek() != EOF){
in.getline(temp, 100);
temp_parts = temp;
in.getline(temp, 100);
temp_ID = atoi(temp);
in.getline(temp, 100);
temp_date = atoi(temp);
in.getline(temp, 100);
temp_price = atof(temp);
in.getline(temp, 100);
Insert(Front, Rear, temp_parts, temp_date, temp_ID, temp_price, L_size);
L_size++;
}
cout << "Total number of entries: " << L_size << endl;
Flush(Front, Rear, L_size);
return 0;
}
I was wondering if there was an easier way than this that does the same thing? Also how do I access things in the linked list?