Change order of list

This is a discussion on Change order of list within the C Programming forums, part of the General Programming Boards category; Heya! Right now my program displays the last element very first and the first entered element last. What would the ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    38

    Change order of list

    Heya!

    Right now my program displays the last element very first and the first entered element last.

    What would the most simple approach to reverse the current order of the list, by either changing insert or display function?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define LAENGE 100
    
    struct data
    {
        char name[LAENGE+1];
        struct data *next;
    };
    
    struct data *start_pointer = NULL;
    struct data *next = NULL;
    
    void insert(void);
    void display(void);
    void deleteSingle(void);
    void deleteAllQuit(void);
    
    
    int main (void)
    {
        for (;;)
        {
            char code;
            printf("1. Insert a name\n2. Delete a name\n3. Display all the names\n4. Exit\nEnter your option: ");
            scanf(" %c", &code);
            while (getchar() != '\n')
                ;
            switch (code)
            {
            case '1':
                insert();
                break;
            case '2':
                deleteSingle();
                break;
            case '3':
                display();
                break;
            case '4':
                deleteAllQuit();
            default:
                printf("Illegal code\n");
            }
            printf("\n");
        }
    
    }
    
    int read_line(char str[], int n)
    {
        int ch, i = 0;
        printf("Enter name: ");
        while (isspace(ch = getchar()))
            ;
        while (ch != '\n' && ch != EOF)
        {
            if (i < n)
                str[i++] = ch;
            ch = getchar();
        }
        str[i] = '\0';
        return i;
    }
    
    
    void insert(void)
    {
        struct data *ptr;
        ptr = malloc(sizeof(struct data));
        read_line(ptr->name, LAENGE);
        ptr->next = start_pointer;
        start_pointer = ptr;
        if (ptr->next == NULL)
        {
            printf("Inserted \"%s\" as the only element of the list.\n\n", ptr->name);
        }
        else
        {
            printf("Inserted \"%s\" at the end of the list.\n\n", ptr->name);
        }
    }
    
    void display(void)
    {
        int count=0;
        struct data *ptr = start_pointer;
        printf ("\nList of names:\n");
        while (ptr != NULL)
        {
            count++;
            printf ("(%d) %s\n", count, ptr->name);
            ptr = ptr->next;
        }
    }
    
    void deleteSingle(void)
    {
        if(start_pointer != NULL)
        {
            struct data *ptr = start_pointer;
            struct data *prev;
            char stringrem[LAENGE+1];
            read_line(stringrem, LAENGE);
    
    
            while (ptr != NULL && strcmp(ptr->name,stringrem))
            {
                prev = ptr;
                ptr = ptr->next;
            }
    
            if (ptr == NULL)
            {
                printf("\"%s\" does not exist in the list.\n\n", stringrem);
                return;
            }
            else if (ptr == start_pointer)
            {
                start_pointer = ptr->next;
            }
            else
            {
                prev->next = ptr->next;
            }
            printf("Deleted \"%s\".\n\n", stringrem);
            free (ptr);
        }
    }
    
    void deleteAllQuit(void)
    {
        if(start_pointer != NULL)
        {
            struct data *ptr = start_pointer;
            while (ptr != NULL)
            {
                free(ptr);
                ptr = ptr->next;
            }
        }
        printf("Deleted all names.\nBye.\n");
        exit(0);
    }
    thx in advance ^^

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Well you could physically reverse the list, but in my opinion, just adding a prev link would be better. It makes insertion and deletion easier, and you can traverse the opposite way.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    thanks ^^ solved the issue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initialization list order
    By MK27 in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2010, 12:07 AM
  2. Best way to have an list of indices which changes order
    By laertius in forum C Programming
    Replies: 5
    Last Post: 09-29-2008, 01:45 AM
  3. Problem with order of list
    By Yannick in forum C Programming
    Replies: 12
    Last Post: 06-16-2008, 08:43 AM
  4. byte order change
    By onebrother in forum C Programming
    Replies: 1
    Last Post: 08-06-2007, 06:40 AM
  5. re-order of linked list
    By nomes in forum C Programming
    Replies: 3
    Last Post: 10-16-2002, 10:04 AM

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