need help... array of linked list

This is a discussion on need help... array of linked list within the C Programming forums, part of the General Programming Boards category; i'm trying to add a node in a linked list.. the problem is like i don't get access to the ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    8

    need help... array of linked list

    i'm trying to add a node in a linked list..

    the problem is like i don't get access to the head of the list (this head declared in a slot of the array) the array consist of a word and a pointer to the position node(linked list)

    it gives me segmentation fault..

    this is the part to add a new node..

    Code:
    head = idxADT->words[keyValue].posNumPtr;
    
    if(!strcmp(idxADT->words[keyValue].word," "))
             {  
                strcpy(idxADT->words[keyValue].word, token[j]);
                
                posNode = malloc(sizeof(PositionNode));
                if(posNode == NULL)
                {
                   printf("\n Memory Allocated Failure \n");
                   return FAILURE;
                }
                
                posNode->positionNum = j;
                /* problem here */
                posNode->nextPosition = head->nextPosition;
                head->nextPosition = posNode;
             }
    this is the initialization part

    Code:
    /* initialise indexADT instance, " " indicates empty slot */
       for(i = 0; i < tableSize; i++)
       {
          strcpy(idxADT->words[i].word," ");
          idxADT->words[i].posNumPtr = NULL;
       }
    can anyone help me??

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your array of structures shouldn't initially contain anything, so you shouldn't be trying to initialize it at all. It should just be an array of pointers, which get set to NULL. You don't actually fill them up with allocated instances right off.

    Can you put together a few sentences on what you're actually trying to do?


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

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    8
    is that means i don't need to initialize the pointer ( idxADT->words[i].posNumPtr ) to NULL??

    Code:
    /* inserting word into hash table */
          for(j = 0; j < i; j++)
          {
             keyValue = Hash(token[j], tableSize);
             
             printf("\n insert %d %s %d", j, token[j], keyValue);
             
             /* check whether the hash table with hash value given are null or not */
             /* and check whether the value of the given hash table same with the word to be inserted */
             while(strcmp(idxADT->words[keyValue].word," ") && strcmp(idxADT->words[keyValue].word,token[j]))
             {
                keyValue = (keyValue + 1) % tableSize;
             }
             
             head = idxADT->words[keyValue].posNumPtr;
             
             /* if it's a new key */
             if(!strcmp(idxADT->words[keyValue].word," "))
             {  
                strcpy(idxADT->words[keyValue].word, token[j]);
                
                /* posNode = addPosNode(j); */
                posNode = malloc(sizeof(PositionNode));
                if(posNode == NULL)
                {
                   printf("\n Memory Allocated Failure \n");
                   return FAILURE;
                }
                
                posNode->positionNum = j;
                /* problem here */
                posNode->nextPosition = head->nextPosition;
                head->nextPosition = posNode;
             }
             
             /* if the key already there, insert position number */
             else if(!strcmp((idxADT->words[keyValue].word),token[j]))
             {
                /* posNode = addPosNode(j); */
                posNode = malloc(sizeof(PositionNode));
                if(posNode == NULL)
                {
                   printf("\n Memory Allocated Failure \n");
                   return FAILURE;
                }
                
                while(head != NULL)
                {
                   head = head->nextPosition;
                }
                
                posNode->positionNum = j;
                /* problem here */
                posNode->nextPosition = head->nextPosition;
                head->nextPosition = posNode;
             }
          }
    the code up there is what i'm trying to do..

    i'm required to add a new word into an array and its position on the text file to the linked list (can be multiple occurrence / more than 1 node)

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't suppose you could show us what your structure and variable declarations look like there?


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

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, 08: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, 02: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, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21