Displaying word problem

This is a discussion on Displaying word problem within the C Programming forums, part of the General Programming Boards category; Hey there, i\m building a linked list and i need help with a display function i have. the display function ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    108

    Displaying word problem

    Hey there,
    i\m building a linked list and i need help with a display function i have.
    the display function displays all the letters of the word entered instead of the word itself.
    appreciate if someone could help me
    below is my struct, one of my functions and the display function.
    Code:
    //-----------struct ------------//
    struct Node
    {
        char data;
        struct Node *next;
    }*Head;
    
    //-----------one of my functions--------//
    
    void insert_beg(char info)       // Inserting a node at the beginning of the list
    {
        struct Node *temp;
        
        temp =(struct Node *)malloc(sizeof(struct Node));       // Allocating size
        temp->data = info;
        
        if (Head == NULL)       // meaning list is empty
        {
            Head = temp;
            Head->next = NULL;
        }
        else
        {
            temp->next = Head;
            Head = temp;
        }
    }
    
    //----MY DISPLAY FUNCTION-----//
    void Display()
    {
        struct Node *cur_ptr;
        
        cur_ptr = Head;
        
        if (cur_ptr == NULL)
        {
            printf("\nList is empty");
        }
        else
        {
            printf("\nElements in the list:\n");
            
            while (cur_ptr != NULL)   // Traversing the entire linked list
            {
                printf("-> %c", cur_ptr->data);
                cur_ptr=cur_ptr->next;
            }
            printf("\n");
        }
    }
    
    //------------in my main-----------//
    
    int main(int argc, const char * argv[])
    {
        int i=0;
        
        Head = NULL;
    
    printf("---------------MAIN MENU---------------\n");
        printf("***************************************\n");
        
        printf("Insert your data:\n");
        printf("\t[1]-At The Beginning.\n");
        printf("\t[2]-At The End.\n");
        printf("\t[3]-At A Particular Location In The List\n");
        printf("\t[4]-Print The Elements In The List\n");
        printf("\t[5]-Print Number Of Elements In The List\n\n");
        
        printf("-------------------------------------------\n");
        
        printf("\t\tDelete A Node In The List\n");
        
        printf("-------------------------------------------\n");
        
        printf("\t[6]-Delete A Node Based By Element\n");
        printf("\t[7]-Delete A Node Based On Location\n");
        printf("\t[8]-EXIT\n");
        
        printf("-------------------------------------------\n");
        
        printf("Press 'x' to display MENU \n\n");
        printf("\n\tChoose An Option:");
    
        
        
        
        while (1)
        {
            
            
            scanf("%d", &i);
            
            switch (i)
            {
                case 1:
                {
                    char info;
                    printf("Enter your word:");
                    scanf(" %c", &info);
                    insert_beg(info);
                    Display();
                    break;
                }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by YannB View Post
    the display function displays all the letters of the word entered instead of the word itself.
    I'm not quite sure I quite understand what the problem is. Isn't "the word itself" simply the collection of "all the letters of the word"?

    It would help if you told us what input you gave it, what output you actually got, and what output you expect to see.

    Also:
    1. Providing complete code, that we can copy-paste, and compile, would help, instead of leaving us to finish off your main function.
    2. Don't cast malloc. Read this: FAQ > Casting malloc - Cprogramming.com.
    3. Your insert_beg function could be simplified as follows. Note that when Head is NULL, the if clause behaves the same as the else clause.
    Code:
    struct Node *temp;
    
    
    temp = malloc(sizeof(*temp));
    if (!temp) {
            // handle malloc error, probably return null
    }
      
    temp->data = info;
    temp->next = Head;
    Head = temp;
    Note, I use *temp inside the sizeof operator. That is, I use the name of the variable you're assigning the malloc result to (dereferenced once), instead of the type name. That way, if you change the type of your list, you only need to change the declaration, all of the malloc calls will still work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  2. displaying problem
    By braddy in forum C++ Programming
    Replies: 6
    Last Post: 09-09-2006, 06:23 PM
  3. Bitmap Displaying Problem
    By MadCow257 in forum Game Programming
    Replies: 0
    Last Post: 01-25-2005, 04:21 PM
  4. Problem displaying bitmaps
    By batman123 in forum Game Programming
    Replies: 2
    Last Post: 01-09-2005, 01:01 AM
  5. 1 Problem on displaying a picture
    By SuperNewbie in forum Windows Programming
    Replies: 0
    Last Post: 01-25-2002, 03:38 AM

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