Question about linked lists

This is a discussion on Question about linked lists within the C Programming forums, part of the General Programming Boards category; I am writing a program that is like a survey. It prompts the user to enter his favorite meal. Then ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    44

    Question about linked lists

    I am writing a program that is like a survey. It prompts the user to enter his favorite meal. Then after all users have submitted their choices, the program should display the results.

    The user can enter any type of food , and hence a linked list must be used , with a counter, so that if some users choose the same food , the count is incremented. The program should then display the food chosen in alphabetical order. The program should print something like this finally(the foods are examples);

    ----Food Chosen----

    Fish 2
    Meat 5
    Pasta 3


    etc.

    I created a structure that will store the food name entered:

    Code:
    typedef struct Food_List
    {
    
    char food [50];
    
    
    } Food_List;
    This is my attempt in creating the linked list :


    Code:
    typedef struct listNode
    {
    
    const char *food;
    struct listNode *nextPtr;
    }
    ListNode;
    
    
    
    void addFood(const Food_List *food_List, listNode** startPtr)
    {
    prinf("Enter food\n");
    scanf("%s",food_List->food);
    
    
    addFood(startPtr, food_List->food);
    
    };
    
    void insertFood(listNode **startPtr, const char *food)
    {
    listNode *prevNode = NULL, *curNode = *startPtr;
    
    listNode *newNode = (listNode*)malloc(sizeof(listNode));
    
    
    memcpy(newNode, food, sizeof(Food_List));
    
    newNode->nextPtr = NULL;
    
    while ((curNode != NULL)&&( strcmp(newNode->food , curNode->food) > 0 ) )
    {
    prevNode = curNode;
    curNode = prevNode->nextPtr;
    };
    
    
    if (prevNode==NULL)
    {
    *startPtr=newNode;
    } 
    
    
    else
    {
    prevNode->nextPtr = newNode;
    };
    
    
    newNode->nextPtr = curNode;
    
    };
    
    void printList(listNode* startPtr){
    listNode* curNode = startPtr;
    
    
    printf(" ----Food Chosen---- \n");
    
    
    while (curNode != NULL)
    {
    
    curNode = curNode->nextPtr;
    };
    };
    I know that it does not work and it has a lot of errors , and I don't know how it can then show the results and print them alphabetically , so can anybody help me please?

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Sorted out your formatting for you.

    Code:
    typedef struct listNode
    {
    	const char *food;
    	struct listNode *nextPtr;
    }
    ListNode;
    
    
    void addFood(const Food_List *food_List, listNode** startPtr)
    {
    	prinf("Enter food\n");
    	scanf("%s",food_List->food);
    
    	addFood(startPtr, food_List->food);
    
    };
    
    void insertFood(listNode **startPtr, const char *food)
    {
    	listNode *prevNode = NULL, *curNode = *startPtr;
    
    	listNode *newNode = (listNode*)malloc(sizeof(listNode));
    
    
    	memcpy(newNode, food, sizeof(Food_List));
    
    	newNode->nextPtr = NULL;
    
    	while ((curNode != NULL)&&( strcmp(newNode->food , curNode->food) > 0 ) )
    	{
    		prevNode = curNode;
    		curNode = prevNode->nextPtr;
    	};
    
    
    	if (prevNode==NULL)
    	{
    		*startPtr=newNode;
    	} 
    
    
    	else
    	{
    		prevNode->nextPtr = newNode;
    	};
    
    
    		newNode->nextPtr = curNode;
    
    	};
    
    void printList(listNode* startPtr){
    	listNode* curNode = startPtr;
    
    
    	printf(" ----Food Chosen---- \n");
    
    
    	while (curNode != NULL)
    	{
    		curNode = curNode->nextPtr;
    	};
    };
    So... let me make sure I understand what you're trying to do. Food_List is just a string, and just stores 1 input from the user. You'll need to put each new "food" input into the linked list listNodes. You have a const char*, but that won't do! Use a char* and malloc, or put the array in the listNode.

    I don't think you should worry about sorting or showing the results yet. One thing at a time! Add a main() and get it to compile for a start, write some simple test code. Add some printf diagnostic output in your functions, get acquainted with a debugger....

    Whatever it takes to come back with more specific questions -- I really wouldn't know where to start helping with the code you posted. It's in pieces! Random statements drifting around outside of functions. Really hard to see what's meant to be going on. Sorry.

  3. #3
    Registered User Alan.Brown's Avatar
    Join Date
    Jul 2010
    Location
    NSW, Australia
    Posts
    9
    In your [B]listNode[B] structure you will need a counter initialised to 1 each time a node is created. Then each time a food item is entered you need to scan your list to see if it has been entered already. If it has, increment the counter in the existing node - if not, add the new node to the list with a colunter of 1.

    As [B]smokeyangel[B] says 'come back with more specific questions'. The code is far from complete.

    I realise I am an 'old bastard from way back' but we always used to set out our problems as a flowchart (flow/logic diagram) so that we could get a good visual representation of the problem. [http://en.wikipedia.org/wiki/Flowchart]

    When this had been carefully and thoroughly, and only then, did we start to code. I suggest you try the same.

    Alan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists Question
    By panfilero in forum C Programming
    Replies: 4
    Last Post: 11-22-2005, 12:33 AM
  2. Linked Lists Question
    By SlyMaelstrom in forum C++ Programming
    Replies: 12
    Last Post: 11-12-2005, 11:03 AM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 01:49 AM
  4. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM
  5. One more question about linked lists?
    By shaeng in forum C Programming
    Replies: 2
    Last Post: 06-24-2003, 07:36 AM

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