开发者

Singly Linked List - C

开发者 https://www.devze.com 2023-03-23 12:56 出处:网络
I have a really quick question about singly linked lists, which I could not find the answer to in other questions.

I have a really quick question about singly linked lists, which I could not find the answer to in other questions.

This is my code:

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

void add(int data);
void printList();

struct node
{
    int data;
    struct node * link;
};

struct node * head = NULL;

main()
{
    char c; 

    while ((c = getchar()) != 'q')
    {
        if (c == 'a')
        {
            int temp;

            printf("data: ");
            scanf("%d", &temp);

            add(temp);
        }

        if (c == 'p')
            printList();
    }
}

void add(int data)
{
    struct node * temp = (struct node *) malloc(sizeof(struct node));

    if (temp == NULL)
        fprintf(stderr, "error");

    temp->link = head;
    temp->data = data;
    head = temp;
}

void printList()
{
    struct node * temp = (struct node *) malloc(sizeof(struct node));

    if (temp == NULL)
        fp开发者_Python百科rintf(stderr, "error");

    temp = head;

    while (temp != NULL)
    {
        printf("%d", temp->data);
        temp = temp->link;
    }
}

Now, I have been told that I need to create a function or a scenario inside my add function that does something differently if a new list is being created. In other words, when the list is empty, and the first element is being added to it, it needs to be done differently from when a populated list is receiving another node at the front. I found an example of such code online:

# // Adding a Node at the Beginning of the List  
#   
# void addBeg(int num)  
# {  
#    struct Node *temp;  
#   
#    temp=(struct Node *)malloc(sizeof(struct Node));  
#    temp->Data = num;  
#   
#    if (Head == NULL)  
#    {  
#       //List is Empty  
#       Head=temp;  
#       Head->Next=NULL;  
#    }  
#    else  
#    {  
#       temp->Next=Head;  
#       Head=temp;  
#    }  
# } 

As you will notice, if the list is empty, the head node is populated.

My code works fine, but I am wondering if I am overlooking something with regards to handling the null head situation.

Many thanks!


There is nothing fundamentally wrong with your add() function. The case where head == NULL needs no special treatment.

As others have noticed, your error checking is not correct because you need to return from the function in case malloc fails. As it stands your add() function carries on an attempts to assign to *temp when temp is NULL.

The code you showed with such special treatment is bogus. The else clause in that code works perfectly well when head == NULL.


You have a choice between checking if your pointer on head is null every time you use it or you can assume that it isn't null if you treated head node differently.

Good manners presume that you should check it always, but if you don't check and don't dereference null pointers then your code will work anyway.


Since you will always run the line (when adding a node)

head = temp;

You will never have a NULL reference in your head node after you've added the node. This is because you verify above (temp != null) that temp is not a null pointer.

Now when searching for a node, it would be a really good idea to check that the head node is not null before you go accessing fields in whatever it (doesn't) point to. Perhaps that's what was meant in the "special conditions" and the notes were mixed up with this case where it is not generally necessary.


If you consider addBeg if branch:

if (Head == NULL)  
{  
   Head=temp;  
   Head->Next=NULL;  
}  
else  
{  
   Head=temp;  
   temp->Next=Head;  
}  

you see that you are assigning temp to head and then setting the next pointer. Your implementation is, on the other hand:

temp->link = head;
temp->data = data;
head = temp;

and if you skip the data part (and head = temp which is present in both cases), you will readily see that it is equivalent to the first one, because

temp->link = head;

is the same as:

if (Head == NULL)  
{  
   Head->Next=NULL;  
}  
else  
{  
   temp->Next=Head;  
}  
0

精彩评论

暂无评论...
验证码 换一张
取 消