Thread: Linked lists

  1. #1
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275

    Linked lists

    Hi there,

    I have a simple linked list composed of an int a pointer.

    The code below compiles and runs fine, but I wonder whether I am wasting memory (or implementing the list in an inappropriate way) by keeping one nullified structure at the end of the list.

    Thanks in advance...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <math.h>
    
    struct my_node{
    	int num;
    	float sqr;
    	struct my_node *next;
    };
    
    void list_print(struct my_node *l){
    	struct my_node *head = l;
    
    	while (head->next != NULL){
    		printf("%d\t->\t%.0f\n",head->num , head->sqr);
    		head = head->next;
    	}
    }
    
    int list_count(struct my_node *l){
        int c=0;
        struct my_node *head = l;
    
        while (head->next != NULL){
            head = head->next;
            c++;
        }
    
        return c;
    }
    
    int main(){
    	int r, i;
    	float p;
    	time_t t;
    	struct my_node *head, *new_node;
    
    	srand((unsigned) time(&t));
    
    	head = (struct my_node*)malloc(sizeof(struct my_node));
    	new_node = head;
    
    	i = 0;
    	while (i<3) {
    		r = rand() % 50;
    		p = pow(r,2);
    		printf("%d\t->\t%.0f\n",r , p);
    
    		new_node->num = r;
    		new_node->sqr = p;
    		new_node->next = (struct my_node*)malloc(sizeof(struct my_node));
    		memset(new_node->next, 0, sizeof(struct my_node));
    		new_node = new_node->next;
    
    		i++;
    	}
    
    	printf("\n----------------------------\n\n");
    
    	list_print(head);
    
    	printf("list count = %d\n", list_count(head));
    
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by fnoyan
    The code below compiles and runs fine, but I wonder whether I am wasting memory (or implementing the list in an inappropriate way) by keeping one nullified structure at the end of the list.
    It looks like a logic error: if you have a node memset to zeroes at the end, it might help you to append to the list more easily since you have a ready-made new node at the end to populate, but it also means the typical head->next != NULL check no longer works as on the conceptual last node, its next pointer would not be a null pointer, but rather will point to this nullified node.

    A better method would be to keep track of both head and tail, so when you want to append, you just append to the tail, e.g.,
    Code:
    struct my_node *head = NULL;
    struct my_node *tail = NULL;
    struct my_node *new_node = NULL;
    
    for (i = 0; i < 3; i++) {
        new_node = malloc(sizeof(*new_node));
        if (new_node) {
            r = rand() % 50;
            p = pow(r, 2);
            printf("%d\t->\t%.0f\n", r, p);
    
            new_node->num = r;
            new_node->sqr = p;
            new_node->next = NULL;
    
            if (head) {
                tail->next = new_node;
            } else {
                head = new_node;
            }
            tail = new_node;
        } else {
            /* report an allocation error and end the loop/program? */
        }
    }
    Incidentally, you probably should just write r * r instead of calling pow.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  4. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM
  5. Linked List of Linked lists Revisited.
    By Qui in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2004, 09:45 PM

Tags for this Thread