Thread: Creating Linked List From Array

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    47

    Creating Linked List From Array

    Tough One Here:

    I'm trying to, as the subject says, create a linked list from an array but I can't quite visualize it. Here's what I've got: I've created an array of nodes, each with next pointers. If this sounds confusing, hopefully you'll see what I mean by looking at the code. Anyway, I want to create seperate linked lists from each array node. Below is the code.

    Class:
    Code:
    struct lettertype					
    {					
       keytype  key;	//string			
    };
    
    struct binType					
    {					
      lettertype  letter;				
      lettertype  word;			
      binType*   next;				
    };
    
    typedef  binType*  subnodeptr;	
    
    class Something
    {
     public:
        insertSub(binType letterBin[index], keytype key);
        // keytype here is data to be entered into new node
    
     private:
        subnodeptr	subLoc;
        binType letterBin[MAX_ITEMS];
    };
    And here's the class implementation file:
    Code:
    void listType::insertSub(binType letterBin[index], keytype key)
    {
      subnodeptr  newel;             //create new pointer
      sublen = sublen + 1;			
      newel = new binType;         // create new node	
      newel->word.key = key;      // insert data 
    
      if (letterBin[index]->next == NULL) 	// no list created
      {
        letterBin[index]->next = newel;
        subLoc = letterBin[index]->next;
       }			
       else	//  new node goes on the end of list
       {
         int loop = 0;
         while (loop < 1)
         {
           subLoc = letterBin[index]->next;
           if (subLoc->next == NULL)
           {
             subLoc->next = newel;
             loop++;
            } 
            else
             subLoc = subLoc->next;          
         }
       }
    	
    return;
    }
    Any idea's of what I'm doing wrong here?

    Thanks,
    bob2509
    Last edited by bob2509; 12-09-2002 at 11:56 PM.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Little help please, anyone?

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by bob2509
    Little help please, anyone?
    Please dont bump threads. Continue to do so and I'll close it.

  4. #4
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    To make a list out of an array just build the list like normal and copy each part of the array into it.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct list{int stuff; struct list *next;}LIST;
    
    main(){
        int x;
        int array[]={1,2,3,4,5,6,7,8,9};
        LIST *head, *walk, *save;
    
        if (!(head=malloc(sizeof(LIST))))
            exit(1);
    
        walk=head;
    
        for (x=0; x<9; x++){
            if (!(walk->next=malloc(sizeof(LIST))))
                break;
            walk=walk->next;
            walk->next=0;
            walk->stuff=array[x];
        }
    
        for (walk=head->next; walk; walk=walk->next)
            printf("%d\n", walk->stuff);
    
        for (walk=head; walk; walk=save){
            save=walk->next;
            free(walk);
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  3. linked list from array problem
    By dcs in forum C++ Programming
    Replies: 5
    Last Post: 05-21-2004, 11:37 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM