954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Linked list of structs

I'm going crazy. I've been trying to a linked list of a struct (which stores some strings and a character) however I've had no luck. It's just not working and I've been working on this for hours on end.

Note: StorePtr is a pointer to the Store struct (which stores the head category) and cPtr is an item (with values already set) that I want to add to the end of the list.

The insert code is as follows:

void insert(StorePtr *store, CategoryPtr *cPtr)
{
    CategoryPtr previousPtr;
    CategoryPtr currentPtr;

    (*cPtr)->nextCategory = NULL;

    currentPtr = (*store).headCategory;

    if (cPtr != NULL)
    {
        while (currentPtr != NULL)
        {
            previousPtr = currentPtr;
            currentPtr = currentPtr->nextCategory;
        }

        if ((*store).headCategory == NULL)
        {
            (*store).headCategory = *cPtr;

            currentPtr = *cPtr;
        }
        else
        {
            currentPtr = *cPtr;
            previousPtr->nextCategory = currentPtr;
        }


    }
NRaf
Newbie Poster
16 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Could you post the code for your structs by any chance?

nexocentric
Junior Poster in Training
72 posts since Mar 2009
Reputation Points: 26
Solved Threads: 4
 

Of course, it's a wrong code. You assign a pointer (?) value to the local variable currentPtr. In other words, do nothing.
However it's unclear what is cPtr type. You declare it as a pointer to CategoryPtr. Why CategoryPtr? Is CategoryPtr type a pointer? If so, cPtr is a pointer to pointer. Why?

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 
Of course, it's a wrong code. You assign a pointer (?) value to the local variable currentPtr. In other words, do nothing. However it's unclear what is cPtr type. You declare it as a pointer to CategoryPtr. Why CategoryPtr? Is CategoryPtr type a pointer? If so, cPtr is a pointer to pointer. Why?

CategoryPtr is a pointer to a struct. It's the node that I want to add to the list.

Anyway, seeing as I'm assigning *cPtr to headCategory (as the first assignment) and then in the previousPtr (which is a reference to the last valid node, I believe), I assign the nextCategory (which is a CategoryPtr) to *cPtr, shouldn't it be working?

NRaf
Newbie Poster
16 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Modifying little bit:

void insert(StorePtr *store, CategoryPtr *cPtr)
{
    CategoryPtr previousPtr;
    CategoryPtr currentPtr;

    (*cPtr)->nextCategory = NULL;

    currentPtr = (*store).headCategory;

    if (cPtr != NULL)
    {
        if ((*store).headCategory == NULL)
        {
            (*store).headCategory = *cPtr;
        } else {
             while (currentPtr-> nextCategory != NULL)
            {
                 currentPtr = currentPtr->nextCategory;
            }

            currentPtr-> nextCategory = *cPtr;
       }
    }
}


may this work!

Luckychap
Posting Pro in Training
444 posts since Aug 2006
Reputation Points: 83
Solved Threads: 61
 

Thanks for the suggestion however I've already fixed the problem.

The issue didn't turn out to be in the code I pasted. While, in another function, I was malloc'ing some space for the Category object, when I passed that pointer to another object (to initialize based on a tokenized string), rather than allocating the values to the malloc'ed object, I created a new Category type and did something like:

CategoryPtr = &category;

Once category went out of scope, it's data became corrupted, which, I believe, is where my issues were stemming from.

NRaf
Newbie Poster
16 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

I'm not really sure if this'll work; but it's
worth giving this a try...
I hope this'll help...
Thanks...

void insert(StorePtr *store, CategoryPtr *cPtr)
{
    CategoryPtr *previousPtr;
    CategoryPtr *currentPtr;

    cPtr->nextCategory = NULL;

    currentPtr = store->headCategory;

    if (cPtr != NULL)
    {
        while (currentPtr != NULL)
        {
            previousPtr = currentPtr;
            currentPtr = currentPtr->nextCategory;
        }

        if (store->headCategory == NULL)
        {
            store->headCategory = cPtr;

            currentPtr = cPtr;
        }
        else
        {
            currentPtr = cPtr;
            previousPtr->nextCategory = currentPtr;
        }


    }
}
neigyl_noval
Light Poster
32 posts since May 2009
Reputation Points: 12
Solved Threads: 5
 

OR this instead....

void insert(StorePtr *store, CategoryPtr *cPtr)
{
    CategoryPtr *previousPtr;
    CategoryPtr *currentPtr;

    cPtr->nextCategory = NULL;

    currentPtr = (CategoryPtr* )malloc(sizeof(CategoryPtr));
    currentPtr = store->headCategory;

    if (cPtr != NULL)
    {
        while (currentPtr != NULL)
        {
            previousPtr = currentPtr;
	    currentPtr->nextCategory = (CategoryPtr* )malloc(sizeof(CategoryPtr));
            currentPtr = currentPtr->nextCategory;
        }

        if (store->headCategory == NULL)
        {
            store->headCategory = cPtr;

            currentPtr = cPtr;
        }
        else
        {
            currentPtr = cPtr;
	    previousPtr->nextCategory = (CategoryPtr* )malloc(sizeof(CategoryPtr));
            previousPtr->nextCategory = currentPtr;
        }
    }
}
neigyl_noval
Light Poster
32 posts since May 2009
Reputation Points: 12
Solved Threads: 5
 
OR this instead.... ...bla-bla-bla...


1. Never send senseless illiterate codes like that.
2. Read this forum rules; next time use code tag for your terrible masterpieces...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

I can't get my code to run right at all. This is C programming. I need to also figure out where to plug in to prompt user for words and make it store them. Help please. This is the assignment guidelines:

Write a program that will do the following:

Continuously prompt the user for words and store the words in a linked list, keeping the words in alphabetical order. Do not use an array to store the words or in any way pre-allocate any memory. You can, and should, use an array when initially reading the word from the user. Use malloc() to request memory for each word after it is read and then store the node into the linked list in its proper location.

Use a function GetWord to read a single word from the user.

As each word is entered into the list, print the contents of the list using a function PrintList. If the list is empty the function should print “List is empty”. The output of the PrintList function should be on the right side of the screen as shown on the sample output.

When the user indicates that there are no more words to enter, print the final contents of the list.

Then delete the last node in the list repeatedly, until the list is empty, calling the PrintList function after each word is removed from the list.

Your program should allow for the possible storage of any number of words. Each word should allow a maximum size of 15 letters. Your program should make certain that this word length is not exceeded so that the program does not crash if longer words are entered by the user. If a longer word is entered it should be discarded.

A sample run is shown below. Your data entry prompts should be on the left side of the screen as shown. Your output should look EXACTLY like the example, except for the actual words entered.


Sample Run

List is empty

Enter a word: four

four


Do you have more words? yes or no: y
Enter a word: score

four
score


Do you have more words? yes or no: y
Enter a word: and

and
four
score


Do you have more words? yes or no: y
Enter a word: seven

and
four
score
seven


Do you have more words? yes or no: y
Enter a word: years

and
four
score
seven
years


Do you have more words? yes or no: y
Enter a word: ago

ago
and
four
score
seven
years


Do you have more words? yes or no: n
Final list is:

ago
and
four
score
seven
years


Deleting nodes one at a time

ago
and
four
score
seven


ago
and
four
score


ago
and
four


ago
and


ago


List is empty

Press any key to continue


This is the code I have so far:

#include "stdafx.h"
#include
#include "stdlib.h"
#include "ctype.h"
#include "stdio.h"

#define MAX_WORD 16

/*function prototype*/
void GetWord (char *word,FILE *ffptr);
void PrintList (struct node *head);

struct node
{
char word[MAX_WORD];
struct node *next;
};


int main(void)
{
struct node *list = NULL; /*points to the first word in the list */
struct node *tmpPtr = NULL; /*used to scan through the list*/
struct node *prevPtr = NULL; /*points to previous word while scanning*/
struct node *newPtr = NULL; /*points to new node*/
char tempWord[MAX_WORD * 2];
FILE *ffptr;

if((ffptr = fopen("File Data.txt", "r")) == NULL)
{
printf("File could not be opened\n");
return 1;
}

PrintList (list);


GetWord(tempWord,ffptr);
list = (struct node *)malloc(sizeof(struct node));
strcpy(list->word, tempWord);
list->next = NULL;

PrintList (list);



while(!feof(ffptr))
{ /* get next word and build node*/
GetWord(tempWord,ffptr);
newPtr = (struct node *)malloc(sizeof(struct node));
strcpy(newPtr->word, tempWord);
newPtr->next = NULL;

tmpPtr = list;
prevPtr = NULL;

while (strcmp(newPtr->word, tmpPtr->word) > 0)
{
prevPtr = tmpPtr;
tmpPtr = tmpPtr->next;
if (tmpPtr == NULL)
break;
}


if (tmpPtr == NULL)
{
prevPtr->next = tmpPtr;
}
else if (prevPtr == NULL)
{
newPtr->next = tmpPtr;
list = newPtr;
}
else
{
prevPtr->next = newPtr;
newPtr->next = tmpPtr;
}

PrintList(list);
fclose(ffptr);


}


printf("\t\t\t\t\t\tFinal list is:\n\n");
PrintList(list);


printf("\t\t\t\t\t\tDeleting nodes one a a time");
while (list !=NULL)
{
if (list->next ==NULL)
{
free (list);
list = NULL;
}
else
{

tmpPtr = list;
while (tmpPtr->next != NULL)
{
prevPtr = tmpPtr;
tmpPtr = tmpPtr->next;
}
prevPtr->next = NULL;
free (tmpPtr);
}
PrintList(list);
}

getch();

return 0;
}

void GetWord(char *word, FILE *ffptr)
{

fscanf(ffptr,"%s", word);
while (strlen(word) > MAX_WORD-1 )
{

fscanf(ffptr,"%s", word);
}
}

void PrintList (struct node *head)
{
struct node *curPtr;

printf("\n\n");

if (head == NULL)
printf("\t\t\t\t\t\tList is empty");
else
{
curPtr = head;

printf("\t\t\t\t\t\t%s\n", curPtr->word);
while (curPtr->next != NULL)
{
curPtr = curPtr->next;
printf("\t\t\t\t\t\t%s\n", curPtr->word);
}
}

printf("\n\n");
}

Newbie78
Newbie Poster
2 posts since Apr 2010
Reputation Points: 17
Solved Threads: 0
 

thread hijack. this thread is a year old.

read the rules.

make your own thread.

use code tags.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

thread hijack. this thread is a year old.

read the rules.

make your own thread.

use code tags.


Uh ... I'm new to this. Thanks for the kindness. I couldn't figure out where to make a new thread, punk.

Newbie78
Newbie Poster
2 posts since Apr 2010
Reputation Points: 17
Solved Threads: 0
 

lol

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You