Thread: List within a list

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    67

    List within a list

    I wrote a code that uses list structure to take an input and print out a reverse as soon as a '.' character is entered.
    Next thing i wanted to do was to make the function print out the words in reverse; e.g.
    Sample
    Code
    Test
    .
    output:
    Test
    Code
    Sample.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    typedef struct { char k; struct List *next; } List;
    
    Sergey *insertList(char head, List*tail)
    {
    	List *t = calloc (1,sizeof (List));
    	t->k = head;
    	t->next = tail;
    	return t;
    }
    	
    int backward_print(List *j)
    {
    	while (j != NULL){
    		printf("%c", j -> k);
    		j = j -> next;
    		}
    		printf("\n");
    		return 0;
    }
    
    int main(void)
    {
    	char s;
    	List *S = NULL;
    	while (s != '.'){
    		s = getchar();
    		S = insertList(s, S);
    		}
    		backward_print(S);
    		return 0;
    }
    this is my code so far. i thought of inserting list within a list, but i am not sure how to modify the present code to achieve that. thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Just make all your 'char' into char arrays, and (for something simple), read the input with scanf("&#37;s

    What's "Sergey" ?
    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. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM