-1
#include <stdio.h>
#include <stdlib.h>



struct LLNode
{

    char key[10];
    struct LLNode *next;

};


struct LLNode *creatNode(char val[10])
{

    struct LLNode*temp;
    temp=(struct LLNode *)malloc(sizeof(struct LLNode));
    temp->key[10]=val;
    temp->next=NULL;
    return (temp);

};

void push(char val[10], struct LLNode *head)
{
    struct LLNode *temp;
    temp = creatNode(val[10]);
    temp->next = head->next;
    head->next = temp;
}





int main()
{
    int cho,a=0;
    struct LLNode *head=NULL;
    struct LLNode *curr=NULL;

    head=creatNode('/0');
    curr=head;
    curr->next = creatNode("Jan");
    curr = curr->next;
    curr->next = creatNode("Feb");
    curr = curr->next;
    curr->next = creatNode("Mar");
    curr = curr->next;
    curr->next = creatNode("Apr");
    curr = curr->next;
    curr->next = creatNode("May");
    curr = curr->next;
    curr->next = creatNode("Jun");
    curr = curr->next;
    curr->next = creatNode("Jul");
    curr = curr->next;
    curr->next = creatNode("Aug");
    curr = curr->next;
    curr->next = creatNode("Sep");
    curr = curr->next;
    curr->next = creatNode("Oct");
    curr = curr->next;
    curr->next = creatNode("Nov");
    curr = curr->next;
    curr->next = creatNode("Dec");

    curr=head;


    printf("1. Create a new node in the linked list\n");

    printf("2. Search the month of 'May' in the linked list\n");

    printf("3. Display the content of each node\n");

    printf("4. Exit\n");

    printf("\n\nPlease enter the serial number of your choice:");

    scanf("%d",&cho);

    switch(cho)
    {

    case 1:
        push ("newnode", head);
        printf("head->next->key = %s\n",head->next->key) ;
        printf("the new node is insert successfully");
        break;

    case 2:
        curr=head;
        while(a==0)
        {

            if(curr->key[10]="May")a=1;

            else curr=curr->next;

        }
        curr=head;
        if(a==1) printf("\n'May'is in the list.\n");

        else printf("\n'May'is not in the list.\n");
        break;



    case 3:
        curr=head;
        while(curr)
        {



            printf("%s  ",curr->key);

        curr=curr->next;

        }

        break;


    case 4:
        printf("You are exiting");
        break;



    }

    return 0;
}

I'm learning about linked lists, but now every time I use this code, I get garbled in the string output in case1 and case3.

The purpose of this code is to implement the tasks in the image(In the link)

enter image description here The first time I used it, I found that I was asked to add a few more statements, so I copied some nonsense here The first time I used it, I found that I was asked to add a few more statements, so I copied some nonsense here The first time I used it, I found that I was asked to add a few more statements, so I copied some nonsense here The first time I used it, I found that I was asked to add a few more statements, so I copied some nonsense here

K.L.DNNT
  • 1
  • 1
  • 2
    Doesn't the compiler complain about the assignment `temp->key[10]=val`? That's not how you copy an array. Also, `creatNode(val[10])` is wrong. As is `creatNode('/0')`. I think you need to spend a little more time with your text-books to learn more about strings, arrays and pointers. – Some programmer dude May 27 '22 at 07:18
  • 1
    Please read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and don't post noise just to work around the system asking you formulate a complete question. – Filburt May 27 '22 at 07:24

1 Answers1

0

In case 2, please note that you have not checked to determine if curr is NULL. If "May" is not found, this will happily loop until curr is NULL and then you'll attempt to access a member field on NULL, causing an error.

In case 1 you similarly have to checked that head->next is or is not NULL.

I suggest in case 2 you replace your while loop with something like:

Also in case 2 you have a couple of errors:

if(curr->key[10]="May")a=1;
  1. You are assigning to curr->key[10].
  2. You are assigning a char pointer to a char.
  3. key only holds ten chars. Indexing starts at 0, so 10 is out of bounds.
  4. If you're attempting to copy a string, string copying in C doesn't work that way.

Your confusion about C string handling and assigning to arrays appears in other locations in your code.

Recommended reading via the C tag info page.

Chris
  • 26,361
  • 5
  • 21
  • 42