Thread: Linked List Display Error

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    1

    Post Linked List Display Error

    My linked list display get error..please help

    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<stdbool.h>
    
    
    struct test_struct
    {
        //int val;
        char val2[10];
        struct test_struct *next;
    };
    
    
    struct test_struct *head = NULL;
    struct test_struct *curr = NULL;
    
    
    struct test_struct* create_list(char val2[10])
    {
        printf("\n creating list with headnode as [%s]\n",val2);
        struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
        if(NULL == ptr)
        {
            printf("\n Node creation failed \n");
            return NULL;
        }
        ptr->val2[10] = val2[10];
        ptr->next = NULL;
    
    
        head = curr = ptr;
        return ptr;
    }
    
    
    struct test_struct* print_list(char val2[10])
    {
        struct test_struct* ptr = head;
    
    
        printf("\n -------Printing list Start------- \n");
        while(ptr != NULL)
        {
            printf("\n [%s] \n",ptr->val2);
            ptr = ptr->next;
        }
        printf("\n -------Printing list End------- \n");
    
    
        return ptr;
    }
    
    
    struct test_struct* add_to_list(char val2[10], bool add_to_end)
    {
        if(NULL == head)
        {
            return (create_list(val2));
        }
    
    
        if(add_to_end)
            printf("\n Adding node to end of list with value [%s]\n",val2);
        else
            printf("\n Adding node to beginning of list with value [%s]\n",val2);
    
    
        struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
        if(NULL == ptr)
        {
            printf("\n Node creation failed \n");
            return NULL;
        }
        ptr->val2[10] = val2[10];
        ptr->next = NULL;
    
    
        if(add_to_end)
        {
            curr->next = ptr;
            curr = ptr;
        }
        else
        {
            ptr->next = head;
            head = ptr;
        }
        return ptr;
    }
    
    
    struct test_struct* search_in_list(char val2[10], struct test_struct **prev)
    {
        struct test_struct *ptr = head;
        struct test_struct *tmp = NULL;
        bool found = false;
    
    
        printf("\n Searching the list for value [%s] \n",val2);
    
    
        while(ptr != NULL)
        {
            if(ptr->val2 == val2)
            {
                found = true;
                break;
            }
            else
            {
                tmp = ptr;
                ptr = ptr->next;
            }
        }
    
    
        if(true == found)
        {
            if(prev)
                *prev = tmp;
            return ptr;
        }
        else
        {
            return NULL;
        }
    }
    
    
    void delete_begin(void)
    {
        if(head==NULL)
        {
            printf("\nLIST IS EMPTY\n");
        }
        else
        {
            test_struct *ptr=head;
            head=head->next;
            free(ptr);
        }
        //print_list();
    }
    
    
    void delete_end(void)
    {
        test_struct *ptr;
    
    
        //if list is empty.
        if(head==NULL)
        {
            printf("\nEMPTY LIST\n");
        }
    
    
        //if list has only one node.
        if(head->next==NULL)
        {
            ptr=head;
            head=NULL;
            free(ptr);
        }
        //Traversing the list.
        else
        {
            test_struct *prev;
            ptr=head;
            while(ptr->next!=NULL)
            {
            prev=ptr;
            ptr=ptr->next;
            }
            prev->next=NULL;
            free(ptr);
        }
        //print_list();
    }
    
    
    int delete_from_list(char val2[10])
    {
        struct test_struct *prev = NULL;
        struct test_struct *del = NULL;
    
    
        printf("\n Deleting value [%s] from list\n",val2);
    
    
        del = search_in_list(val2,&prev);
        if(del == NULL)
        {
            return -1;
        }
        else
        {
            if(prev != NULL)
                prev->next = del->next;
    
    
            if(del == curr)
            {
                curr = prev;
            }
            else if(del == head)
            {
                head = del->next;
            }
        }
    
    
        free(del);
        del = NULL;
    
    
        return 0;
    }
    
    
    void menu()
    {
        int i = 0, ret = 0, select=0;
        char y[10];
        struct test_struct *ptr = NULL;
    
    
        printf("\n\n----- Menu -----\n");
    	printf(" 1-Add begin\n 2-Add end\n 3-Delete begin\n 4-Delete end\n 5-Delete anywhere\n 6-End\n");
    	printf("\n\nPlease select an action: ");
    	scanf("%d", &select);
    
    
    	switch(select)
    	{
    		case 1:
    				/* Add at Beginning */
                    printf("\n Add at Beginning \n");
                    printf(" Enter the input : ");
                    scanf("%s",&y);
                    add_to_list(y,false);
                    //print_list();
                    /* Add at Beginning */
    				break;
    
    
    		case 2:
                    /* Add at End */
                    printf("\n Add at End \n");
                    printf(" Enter the input : ");
                    scanf("%s",&y);
                    add_to_list(y,true);
                    print_list(y);
                    /* Add at End */
    				break;
    
    
    		case 3:
                    /* Delete Begin */
                    delete_begin();
                    /* Delete Begin */
    				break;
    
    
    		case 4:
                    /* Delete End */
                    delete_end();
                    /* Delete End */
    				break;
    
    
    		case 5:
                    /* Delete at Everywhere */
                    printf("\n Delete at Everywhere \n");
                    printf(" Enter the input : ");
                    scanf("%s",&y);
                    ret = delete_from_list(y);
                    if(ret != 0)
                    {
                        printf("\n delete [val = %d] failed, no such element found\n",y);
                    }
                    else
                    {
                        printf("\n delete [val = %d]  passed \n",y);
                    }
                    //print_list();
                    /* Delete at Everywhere */
    				break;
    
    
            default:
                    printf("\nThanks For Using The System !\n Have a Nice Day.");
    	}
    }
    
    
    int main(void)
    {
        int i = 0, ret = 0, select=0;
        char y[10];
        struct test_struct *ptr = NULL;
    
    
        /* Create Linked List */
        printf("\n Create Linked List of 5 Name \n");
        printf(" Enter the input : ");
        for(i = 0; i<5; i++)
        {
            scanf("%s",&y);
            add_to_list(y,true);
        }
        print_list(y);
        /* Create Linked List */
    
    
        /* Menu */
        menu();
        /* Menu */
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Run-time or build error?

    If build error post the error.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ptr->val2[10] = val2[10];
    Perhaps you need to figure out strcpy before doing any thing else.
    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. Replies: 13
    Last Post: 05-02-2012, 04:17 PM
  2. Linked List Error
    By eleceng16 in forum C Programming
    Replies: 7
    Last Post: 04-13-2012, 06:59 AM
  3. Help with some error...(linked list)
    By google@ in forum C Programming
    Replies: 5
    Last Post: 11-08-2006, 08:29 AM
  4. Replies: 1
    Last Post: 04-01-2002, 03:08 PM

Tags for this Thread