Thread: Easier Way To Do a Linked List?

  1. #1
    Registered User
    Join Date
    Nov 2009

    Easier Way To Do a Linked List?

    This is how my teacher explained to use how to do a linked list.

    #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;  
    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;
    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);
        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?
    Last edited by Sherina; 11-28-2009 at 09:40 PM.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Banks of the River Styx
    If you're learning about the internals of a linked list, and the concept of a linked list, probably not.

    If you're writing normal code, then you should be using std::list
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  3. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM