Thread: Going backwards in linked list

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    Going backwards in linked list

    Hi all,

    I'm writing a piece of code for one of my classes which is supposed to take in a string from the user. It will store the string into an array and then it has to copy the array elements into a linked list. I then have to print each chars from the linked list that's not a vowel. I was going to just use a conditional if statement for this part but my issue is I don't know how to implement the code to go back to the first node and then cycle forward through them to reprint the chars. My code only prints me the char in the last node. Any pointers (pun?) lol

    Also, I get this warning:

    Undeclared function 'gets'; assuming 'extern' returning 'int'.

    relating to my use of gets(xxx); but I can't figure out how to fix the warning so any ideas?

    My code thus far below:

    Thanks all!


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define maxsize 500
    
    
    struct node
    {
    char data;
    struct node *next;
    };
    
    
    int main (void)
    {
        int nodenum=0;
        char sentence[maxsize];
    
        struct node* head = NULL;
        struct node* temp;
    
        head=(struct node*)malloc(sizeof(struct node));
        temp=head;
        temp->next=NULL;
      
        printf("Enter string\n");
        gets(sentence);
        
        while(head->next!=NULL)
        {
            head=head->next;
        }
        do /*puts each char in the array into a node of the linked list*/
        {
        head->next=(struct node*)malloc(sizeof(struct node));
        head=head->next;
         head->data = sentence[nodenum];
        temp=head;
    
        head->next = NULL;
        nodenum++;
        }
        while(sentence[nodenum]!='\0');
    
        do
        {
            printf("\n%c",temp->data); /*This only prints the last letter in the linked list. How to manipulate to go back through the nodes to print corresponding char into a string?*/
            
        }
        while(head==NULL);
            
        free(head);
        free(head->next);
        free(temp);
        
        getchar();
        getchar();
        return(0);
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Several problems:
    1) Don't use gets(). Read FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com
    2) You start with two pointers "head" and "temp" which both point to the beginning of your list. Then you move "head" and later point "temp" to the new "head". Thus you've broken one very important rule in regard to linked list: Never loose the beginning/head of your list!
    3) Your do-while-loop on lines 45-50 can't work (even if the pointers would point to the correct position) because you never change "head" thus the condition is either always true or always false.
    4) On line 52 you free "head" and on line 53 you want to free "head->next". But since you've already freed "head" "head->next" is undefined behaviour.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Traverse linked list backwards
    By Mentallic in forum C Programming
    Replies: 2
    Last Post: 10-10-2012, 07:00 PM
  2. Linked List - Print Backwards
    By skier21 in forum C Programming
    Replies: 9
    Last Post: 07-27-2012, 06:11 PM
  3. Linked list printing out backwards
    By chickenlittle in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2011, 07:41 AM
  4. Replies: 5
    Last Post: 12-11-2010, 12:21 AM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM