Thread: is this code a list?

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    is this code a list?

    it simply allocates n bytes of the memory but, does this means that these bytes are connected each other and finally make a list? thanks...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Person{
    	int age;
    	struct Person *next;
    }ListPerson;
    
    int main(){
    	int n;
    	ListPerson *A;
    		printf("Type the size of the array:");
    		scanf("%d", &n);
    		A = malloc(n*sizeof(struct Person));
    		if(A == NULL) exit(1);
    		
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    This struct has a pointer to another struct.
    Yes, with this you can have a list. Is up to you to fill it correctly.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Probablly this code is correct right? Can any tell me a way to check if it is a list ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Person{
    	int age;
    	struct Person *next;
    }ListPerson;
    
    int main(){
    	int n;
    	ListPerson *A;
    		printf("Type the size of the array:");
    		scanf("%d", &n);
    		A = malloc(n*sizeof(struct Person));
    		if(A == NULL) exit(1);
    
    		for(i = 0; i < (n-1); i++){
    			A[i].next = &A[i+1]
    		}
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Person{
    	int age;
    	struct Person *next;
    }ListPerson;
    
    int main(){
    	int n;
    	ListPerson *A;
    		printf("Type the size of the array:");
    		scanf("%d", &n);
    		A = malloc(n*sizeof(struct Person));
    		if(A == NULL) exit(1);
    
    		for(i = 0; i < (n-1); i++){
    			A[i].next = &A[i+1]
    		}
                    A[n-1].next=NULL; // end of the list
    return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by brack View Post
    Probablly this code is correct right? Can any tell me a way to check if it is a list ?
    It is not a list, it's an array of struct person. Assigning the address of the next element is not necessary here since you can access the elements directly.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    can anyone tell me for sure what the above code is ? it is a list or not?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you should start with a definition: what is a list?
    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. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  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. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM