Thread: Linked List with Two Pointers

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    Linked List with Two Pointers

    So I need some help, I'm creating a program that keeps track of weekly gas mileage. It reads a list of cars from a file and creates a linked list, the only thing I'm having trouble with is that each entry in the list needs to point to the next car as well as another linked list of average mileage for each day the car was driven. There wont be anymore than 7 entries for gas mileage. I'd like to initilize the list of gas mileage to be NULL and then prompt to enter the mileage for each car and insert into the list that way. Then I'm going to generate the average for each car. Here's what I have for getting the cars from the file into the first linked list, what I don't know how to do is link to the second linked list.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct node {
     	char name[25];
    	double val;
    	struct node *link;
    };
    
    int populateName () {
    	struct node **q;
    	q = NULL;
    	FILE *names;
    	int size;
    	int i;
    	names = fopen("names.dat", "r");
    	fscanf(names, "%d", &size);
    	for(i = 0; i < size; i++) {
    		fscanf(names, "%s", &name[i].name);
    		struct node *temp, *r;
    		temp = *q;
    		if (i == 0) {
    			temp = (struct node *)malloc(sizeof(struct node));
    			strcpy(temp->name, fscanf(names, "%s", name[i]);
    			temp->val = val;
    			temp->link = NULL;
    			*q = temp;
    		} else {
    			temp = *q;
    			while (temp->link != NULL) 
    				temp = temp->link;
    			r = (struct node *)malloc(sizeof(struct node));
    			strcpy(r->name,fscanf(names, "%s", name[i]);
    			r->val = val;
    			r->link = NULL;
    			temp->link = r;
    		}
    	}
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If there won't be more than seven gas entries, just use an array. Otherwise, if you wish to make a list of lists, that's pretty easy to do once you figure out how to make a list of whatever in the first place.
    Code:
    struct foo
    {
        ...everything else...
        struct foo *next;
    };
    All you need to do is figure out how to make a linked list, using just the next pointer, and ignore everything else. Then you do the same thing for your other type of data, and just stick an instance of it (pointer to type) in the structure:
    Code:
    struct foo
    {
        struct bar *days; /* list of bar (days) */
        struct foo *next; /* list of foo... */
    };
    Make functions for each list--or reuse the same function, if you have two of the same type of list in a function--to handle finding and adding, creating and deleting.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    Quote Originally Posted by quzah View Post
    If there won't be more than seven gas entries, just use an array. Otherwise, if you wish to make a list of lists, that's pretty easy to do once you figure out how to make a list of whatever in the first place.
    Code:
    struct foo
    {
        ...everything else...
        struct foo *next;
    };
    All you need to do is figure out how to make a linked list, using just the next pointer, and ignore everything else. Then you do the same thing for your other type of data, and just stick an instance of it (pointer to type) in the structure:
    Code:
    struct foo
    {
        struct bar *days; /* list of bar (days) */
        struct foo *next; /* list of foo... */
    };
    Make functions for each list--or reuse the same function, if you have two of the same type of list in a function--to handle finding and adding, creating and deleting.


    Quzah.
    Okay, so I'd need two structs then, right? One for the days list and one for the car list? I don't know what I get so hung up on when trying to understand these. Could you give me a simple implementation of just adding like a single car and a single mileage, or some way to help me understand it easier?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void addnode( struct node **n, ...whatever... )
    {
        if( n )
        {
            x = ...allocate a node, or use some argument you've passed...
    
            /* no existing list */
            if( *n == NULL )
            {
                *n = x;
            }
            else
            {
                /* be lazy and prepend */
                x->next = *n;
                *n = x;
            }
        }
    }
    
    
    /* elsewhere... */
    struct node *n = NULL;
    
    /* still elsewhere... */
    addnode( &n, ...whatever... );
    Something to that effect.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    Okay, I think I'm getting somewhere, would something like this work:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct NodeRec
    {
    	void *pd ;
    	struct NodeRec *pn ;
    } NodeRecT ;
    
    typedef struct
    {
    	NodeRecT *pFirst ;
    	NodeRecT *pLast ; // Optional, but convenient.
    	size_t count ;  // Optional but sometimes convenient.
    } ListT ;
     
    
    NodeRecT* createCar( ListT *pList, NodeRecT *pNode ) ;
    NodeRecT* createMileage( ListT *pList, NodeRecT *pNode ) ;
    ListT cars = { NULL, NULL, 0 } ;
    NodeRecT current = insertNode( &cars, createCar( "name of car" ) ) ;
    
    if ( current != NULL )
    {
    NodeRecT pm = insertNode( (ListT*)current.pd, createMileage( 16 ) ) ;
    
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, 'current' can't ever be NULL, because it's not a pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM