Thread: Linked list acting as string

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    47

    Linked list acting as string

    I am trying to add characters to each node of a linked list.

    Code:
    #include <stdio.h>            // NULL defined here
    #include <stdlib.h>            // malloc
    
    struct node            
        {
            char data;            
            struct node *next;    
        };
    
    
    
    void main()
    {
    
        struct node *front, *temp;        
        int i = 0;
        char response[] = "Programming";
    
    
    
    
    
        /* setting the first node */
        front = malloc(sizeof(struct node));    
        front->data = response[i++];        
        front->next = NULL;                    
        temp = front;            
    
    
    
    
    
        while(i < response.length)                                                        
        {
            temp->next = malloc(sizeof(struct node));                    
            temp = temp -> next;                                        
            temp->data = response[i++];                                            
            temp->next = NULL;
        }
    
    
    
    
    
        /* Printing */
        temp = front;                                                    
        while(temp!=NULL)                                                
        {
            printf("&#37;c", temp->data);                                    
            temp = temp->next;                                        
        }
    }
    for this line:

    Code:
    while(i < response.length)
    I get the error:
    Code:
    Error    1    error C2224: left of '.length' must have struct/union type

    What can i do to correct my problem?
    Last edited by drater; 04-29-2008 at 07:41 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    response is an array, and therefore doesn't have any member functions. If you mean std::string (which has a length function) then say so. If you need to use a char array, then go with sizeof. EDIT: Not C++, so ignore the std::string thing, and go with sizeof.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    I fixed my previous code, but now when I try and add a separate method it no longer works again.

    Code:
    #include <stdio.h>
    #include <stdlib.h>			
    
    
    struct node {				
    	char data;
    	struct node *next;
    };
    
    int menu();					
    struct node *insert(struct node *, char);	
    
    
    void main()
    {
    	struct node *front;		
    	int optionsResponse;
    	char response[9999];	
    	front = NULL;			
    	do				
    	{				
    		optionsResponse = menu();
    		switch(optionsResponse) 
    		{
    			case 1 : 
    					printf ("Enter a new string:  ");	// insert new datum
    					scanf("&#37;s", &response);
    					front = insert(front, response);
    					printf("\n");
    					break;
    		}
    	} while(optionsResponse != 8);
    }
    
    int menu()	
    {
    	int temp;
    	printf("Please select from one of the following String menu options: \n\n");
    	printf("1.\tCreate a new string\n");
    	printf("8.\tQuit\n\n");
    	printf("I choose option number: ");
    	scanf("%d", &temp);
    	return temp;
    }
    
    
    struct node *insert(struct node *f, char *response[])
    { 
    	struct node *front, *temp;		
    	int i = 0;
    	
    
    	front = malloc(sizeof(struct node));							
    	front->data = response[i++];											
    	front->next = NULL;					
    	temp = front;													
    
    	
    	while(response[i] != NULL)													
    	{
    		temp->next = malloc(sizeof(struct node));					
    		temp = temp -> next;										
    		temp->data = response[i++];									
    		temp->next = NULL;
    	}
    
    	printf("List currently contains:  ");
    	while(f != NULL)
    	{
    		printf("%c", f->data);
    		f=f->next;
    	}
    	printf("\n");
    }
    What am i doing wrong?

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    A 'f=front;' is missing between the insertion part and the final printf part, I think.

    It won't be sufficient to correctly return the result though (as the
    front in main won't be modified).

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    After adding f = front, I get the first node to print correctly but all/most after do not correctly print.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    main should be int main(void) - read FAQ http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    Code:
    scanf("&#37;s", &response);
    remove &
    and better add width to the format to prevent buffer overrun
    also note that %s format reads string till the first whitespace, probably - you need other format, like
    %9998[^\n]

    Code:
    struct node *insert(struct node *f, char *response[])
    should be
    Code:
    struct node *insert(struct node *f, const char *response)
    you pass a string not an array of strings


    Code:
    response[i] != NULL
    should be
    Code:
    response[i] != '\0'
    because response[i] is one char


    while(f != NULL)
    you have never connected the old list of chars with the new created list of chars

    And of cource - you have no return in your function - you should increase the warning level of your compiler and pay attention to the warnigns
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    I am not sure what you mean about the buffer overrun. I added a print method but for some reason, now my program crashes when I want to print out the linked list. I also think my previous code was destroying my nodes when I entered a new string instead of tacking it on to the end of my first string. How can I fix these errors?

    Also - thank you to everyone who has helped me despite my annoying newbie problems.

    Code:
    #include <stdio.h>
    #include <stdlib.h>			
    
    
    struct node {				
    	char data;
    	struct node *next;
    };
    
    int menu();					
    struct node *insert(struct node *, char);
    void print(struct node *);
    
    
    void main(void)
    {
    	struct node *front;		// front points to the front of our list
    	int optionsResponse;
    	char response[25];	// user inputs
    	int index = 0;
    	int index2 = 0;
    	front = NULL;			// initialize list to NULL
    	do				// display menu, do command, quit when user inputs choice 8
    	{				
    		optionsResponse = menu();
    		switch(optionsResponse) 
    		{
    			case 1 : 
    					printf ("Enter a new string:  ");	// insert new datum
    					scanf("%s", response);
    					front = insert(front, response);
    					printf("\n");
    					break;
    			case 2 :
    					print(front);
    					break;
    		}
    	} while(optionsResponse != 8);
    }
    
    int menu()	// display menu and get user's choice
    {
    	int temp;
    	printf("Please select from one of the following String menu options: \n\n");
    	printf("1.\tCreate a new string\n");
    	printf("2.\tPrint the linked list\n");
    	printf("8.\tQuit\n\n");
    	printf("I choose option number: ");
    	scanf("%d", &temp);
    	return temp;
    }
    
    
    struct node *insert(struct node *f, char *response)
    { 
    	struct node *front, *temp;		
    	int i = 0;
    
    		front = malloc(sizeof(struct node));							
    		front->data = response[i++];
    		front->next = NULL;					
    		temp = front;
    	
    	while(response[i] != '\0')													
    	{
    		temp->next = malloc(sizeof(struct node));					
    		temp = temp -> next;										
    		temp->data = response[i++];
    		temp->next = NULL;
    	}
    
    	return temp->data;
    }
    
    void print(struct node *f)
    {
    	printf("List currently contains:  ");
    	while(f != NULL)
    	{
    		printf("%c", f->data);
    		f=f->next;
    	}
    	printf("\n");
    }

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    I am trying a new approach since my last post, and I am not sure if the logic is right or why my syntax is incorrect.
    Code:
    #include <stdio.h>
    #include <stdlib.h>			
    
    
    struct node {				
    	char data;
    	struct node *next;
    };
    
    int menu();					
    struct node *insert(struct node *, char);
    void print(struct node *);
    
    
    void main(void)
    {
    	struct node *front;	
    	int optionsResponse;
    	char response[25];	
    	front = NULL;			
    	do				
    	{				
    		optionsResponse = menu();
    		switch(optionsResponse) 
    		{
    			case 1 : 
    					printf ("Enter a new string:  ");	
    					scanf("%s", response);
    					front = insert(front, response);
    					printf("\n");
    					break;
    			case 2 :
    					print(front);
    					break;
    		}
    	} while(optionsResponse != 8);
    }
    
    int menu()	
    {
    	int temp;
    	printf("Please select from one of the following String menu options: \n\n");
    	printf("1.\tCreate a new string\n");
    	printf("2.\tPrint the linked list\n");
    	printf("8.\tQuit\n\n");
    	printf("I choose option number: ");
    	scanf("%d", &temp);
    	return temp;
    }
    
    
    struct node *insert(struct node *f, char response)
    { 
    			
    	int i = 0;
    	struct node *temp = (struct node *)malloc(sizeof(struct node)), *previous, *current;
    	temp->data=response[i++];
    	temp->next=NULL;
    
    	while (response[i] != '\0')
    	{
    		if (f == NULL)
    		{
    			return temp;
    		}
    		else {
    
    				current = f;
    				while(current != NULL)													
    				{
    					previous=current;
    					current=current->next;
    				}
    				previous->next=temp;	
    				temp->next=current;
    				temp->data = response[i++];
    				return temp;
    			}
    	}
    
    	
    }
    
    void print(struct node *f)
    {
    	printf("List currently contains:  ");
    	while(f != NULL)
    	{
    		printf("%c", f->data);
    		f=f->next;
    	}
    	printf("\n");
    }
    My errors are on lines:
    Code:
    temp->data=response[i++];
    while (response[i] != '\0')
    temp->data = response[i++];
    and I get the error:
    subscript requires array or pointer type

    Was I better off the way I had it? What do I need to correct?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    struct node *insert(struct node *f, char response)
    response is not an array or pointer.

    Coding style:
    Code:
    	struct node *temp = (struct node *)malloc(sizeof(struct node)), *previous, *current;
    Do not declare variables on the same line as you are calling functions to initialize other variables. Too easy to get confused.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    Code:
    struct node *insert(struct node *f, char response)
    needed to be:
    Code:
    struct node *insert(struct node *f, char *response)
    And it fixed that error, but I still have a logic error where it only puts the first character of my char array into the first node, and doesn't put my next character in the array in my next node(which it's supposed to do until my char array points at '\0')

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suspect it's because you return in the loop that loops through the string:
    Code:
    	while (response[i] != '\0')
    	{
    		if (f == NULL)
    		{
    			return temp;
    		}
    		else {
    
    				current = f;
    				while(current != NULL)													
    				{
    					previous=current;
    					current=current->next;
    				}
    				previous->next=temp;	
    				temp->next=current;
    				temp->data = response[i++];
    				return temp;
    			}
    	}
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    How do i return everything in the linked list rather than having to return each individual node?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by drater View Post
    How do i return everything in the linked list rather than having to return each individual node?
    Well, I presume you actually want to return (for example) the head of the list, rather than returning all the elements in the list - you can't store all elements in one node pointer, which is what receives the return value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    I was able to fix my prior questions, but I am trying to create 3 char arrays and place each character into a node with a null node to separate each word.

    Code:
    #include <stdio.h>
    #include <stdlib.h>			
    
    
    struct node 
    {				
    	char data;
    	struct node *next;
    };
    
    int menu();					
    struct node *addString(struct node *, char *);
    void print(struct node *);
    
    
    void main(void)
    {
    	struct node *front;	
    	int optionsResponse;
    	char response[25];	
    	front = NULL;			
    	do				
    	{				
    		optionsResponse = menu();
    		switch(optionsResponse) 
    		{
    			case 1 : 
    					printf ("Enter a new string:  ");
    					scanf("%s", response);
    					front = addString(front, response);
    					printf("\n");
    					break;
    			case 2 :
    					print(front);
    					break;
    		}
    	} while(optionsResponse != 8);
    }
    
    int menu()
    {
    	int temp;
    	printf("Please select from one of the following String menu options: \n\n");
    	printf("1.\tCreate a new string\n");
    	printf("2.\tPrint the linked list\n");
    	printf("8.\tQuit\n\n");
    	printf("I choose option number: ");
    	scanf("%d", &temp);
    	return temp;
    }
    
    
    struct node *addString(struct node *f, char *response)
    { 	
    	int i = 0;
    	struct node *temp = (struct node *)malloc(sizeof(struct node));
    	struct node *front = temp;
    
    	temp->data=response[i++];
    	temp->next=NULL;
    	
    	while(response[i] != '\0')													
    	{
    		temp->next = (struct node *)malloc(sizeof(struct node));					
    		temp = temp -> next;										
    		temp->data = response[i++];
    		temp->next = NULL;
    	}
    
    	return front;
    
    }
    
    void print(struct node *f)
    {
    	printf("List currently contains:  ");
    	while(f != NULL)
    	{
    		printf("%c", f->data);
    		f=f->next;
    	}
    	printf("\n");
    }
    But my code only prints the last created char array and not all the char arrays I have added to the linked list. Where is my logic error?

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Code:
    ...
    front = addString(front, response);
    ...
    You only record the head of the last linked list.
    Try to initialize 'front' only once to get everything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Adding To The Middle Of A Linked List
    By LostNotFound in forum C++ Programming
    Replies: 1
    Last Post: 02-23-2003, 06:02 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM