Thread: Question about linked list and counter

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

    Question about linked list and counter

    So I am writing a program where the user has to enter various names , and then a list shows the entered names in alphabetical order. My program is working , but if for example the user enters the same name twice , it is showing twice in the list. How can I introduce a counter , so that for example if the user enters Mark,Mark,Alan, Paul , it shows as :

    Alan 1
    Mark 2
    Paul 1

    This is the code I have written so far :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct student Student;
    
    
    struct student
    {
            char name[50];
            int counter;
            Student *next;
    };
    
    
    
    void insertStudent(Student** listHead, Student* pStudent)
    {
            Student *prevNode = NULL, *curNode = *listHead;
    
    
            Student *newNode = (Student*) malloc(sizeof(Student));
            memcpy(newNode, pStudent, sizeof(Student));
            newNode->next = NULL;
    
    
            while ((curNode != NULL) && (strcmp(newNode->name, curNode->name) > 0)) 
            {
              prevNode = curNode;
              curNode = prevNode->next;
            }
    
    
            if (prevNode == NULL) 
              *listHead = newNode; 
            else 
              prevNode->next = newNode;
    
    
            newNode->next = curNode;
    }
    
    
    
    
    void readStudent(Student* pStudent)
    {
            printf("Enter name: ");
            scanf("%s", pStudent->name);
    
    
    }
    
    
    void displayStudent(const Student* pStudent)
    {
            printf("%s\n", pStudent->name);
        
    }
    
    
    void printStudents(Student* listHead)
    {
            Student* curNode = listHead;
    
    
            printf("---------------------------\n");
            while (curNode != NULL)
            {
                    displayStudent(curNode);
            
                    curNode = curNode->next;
                    
            }
    }
    
    
    void destroyList(Student** listHead)
    {
            Student* curNode = *listHead;
            Student* prevNode = NULL;
    
    
            while (curNode != NULL)
            {
                    prevNode = curNode;
                    curNode = curNode->next;
                    free(prevNode);
            }
            *listHead = NULL;
    }
    
    
    int menu()
    {
            int choice = 0;
            do
            {
                    printf("1. Add new student\n");
                    printf("2. Display Student List\n");
                    printf("3. Exit\n");
                    printf("Enter choice: ");
                    scanf("%d", &choice);
            }
            while ((choice < 1) || (choice > 3));
            return choice;
    }
    
    
    void addNewStudent(Student **listHead)
    {
            Student student = {0}; 
            readStudent(&student);
            displayStudent(&student);
            insertStudent(listHead, &student);
    }
    
    
    
    
    int main()
    {
            int choice = 0;
            Student* listHead = NULL;
    
    
            do 
            {
                    choice = menu();
                    switch (choice)
                    {
                    case 1:
                            addNewStudent(&listHead);
                            break;
    
    
                    case 2: 
                          printStudents(listHead);
                            break;
    
    
                    case 3:
                            destroyList(&listHead);
                            break;
    
    
                   
    
    
                    default:
                            break;
                    }
    
    
            }
            while (choice != 4);
    
    
            destroyList(&listHead);
    }
    Last edited by TexasKid; 04-30-2012 at 01:49 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Why is it impossible for you to post code in proper code tags?

    Every line of your code has
    [ COLOR=#333333][ FONT=arial]
    That needs to be removed before you post.

    Try copy as text, or whatever (you figure it out).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    If you make a function that searches all the names and returns the number of matches before saving - After this, save the number in a new variable added to your structure.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    I already added the variable 'counter' in the structure. Can you help me create the search function please?

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Start at the first structure - If the name is equal to the one you are searching for, increment a variable - Now point to the next structure in the linked list via the "next" pointer. If the name is the same, increment the variable, or look at the next structure. The last structure should have a NULL in the "next" pointer, so you'll know when you hit the end of your list

    Start with that; if you are still having trouble, post your code and we'll help you.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    I just edited the insertStudent function , so that the counter is incremented. How can the counter then be displayed ?

    This is the edited insertStudent function :

    Code:
    void insertStudent(Student** listHead, Student* pStudent)
    {
    	int c=0;
    
    
            Student *prevNode = NULL, *curNode = *listHead;
    
    
            Student *newNode = (Student*) malloc(sizeof(Student));
            memcpy(newNode, pStudent, sizeof(Student));
            newNode->next = NULL;
    
    
            while (curNode != NULL) {
    c = strcmp(newNode->name, curNode->name);
    if (c == 0) {
    curNode->counter++;
    return;
    } else if (c > 0) {
    prevNode = curNode;
    curNode = prevNode->next;
    } else {
    break;
    }
    }
    }

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    When you print the list the counter should be updated already as far as I can tell. The code above has a memory leak, if c == 0 you just return, leaving newNode behind. Either free it before you return or create it in the "else if" block and strcmp with "pStudent" instead of "newNode".

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Think about what insertStudent does with an empty list (for example, the first time you call it)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List question
    By ee1215 in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2011, 03:15 AM
  2. c linked list question.
    By bos1234 in forum C Programming
    Replies: 2
    Last Post: 03-21-2011, 10:48 AM
  3. linked list question
    By sumdude in forum C Programming
    Replies: 5
    Last Post: 08-10-2009, 08:38 PM
  4. Linked list question.
    By cdalten in forum C Programming
    Replies: 4
    Last Post: 04-03-2006, 01:49 AM
  5. Linked list question
    By John.H in forum C++ Programming
    Replies: 5
    Last Post: 03-02-2003, 06:24 PM