Thread: array to linked list

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    Question array to linked list

    hi,
    I have to write a few functions that do a few things with two linked list, but the problem is that i have to test them with linked lists. Does anybody have a quick code that can convert an array of integers to a linked list??

    I defined the list:
    Code:
    typedef struct node{
    	int data;
    	struct node* next;
    }NODE;
    Thanks

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Why not write it by yourself. It's not too hard. Write function to form linked list by inserting new elements.
    And then traverse through the arrray and insert it's elements into linked list.
    If you don't want to write it yourself do a board search, you'll find a lot of useful code.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Heres something to start you off:

    Code:
    #include <stdio.h>
    
    struct node {
    	int data;
    	struct node* next;
    };
    
    void insert(struct node** head, int key);
    void print_list(struct node* head);
    
    int main(int argc, char **argv)
    {
    	struct node* list = NULL;
    
    	insert(&list,5);
    	insert(&list,7);
    	insert(&list,2);
    	print_list(list);
    	return 0;
    }
    
    void insert(struct node** head, int key)
    {
    	struct node** current = head;
    
    	while (*current != NULL)
    	{
    		/* traverse the list */
    		current = &((*current)->next);
    	}
    
    	/* make new node and re-link */
    	struct node* new = (struct node*)malloc(sizeof(struct node));
    	new->data = key;
    	new->next = *current;
    	*current = new;
    }		
    
    void print_list(struct node* head)
    {
    	while (head != NULL)
    	{
    		/* print and traverse the list */
    		printf("%d ",head->data);
    		head = head->next;
    	}
    	return;
    }
    This basically demonstrates the basic functions of a linked list, obviously you can create more functions such as delete() etc. I've also added in a print function so you can verify that it works properly. Now you'll probably want to create another function that takes both an array and a linked list pointer (struct node*) and copies from the array to the linked list using the insert() function I have above.

    Hope that helps you point to the right direction.

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. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM