简体   繁体   中英

Why can't I delete the first node (head node) directly?

I have created a simple linked list using c where we can insert and delete elements at any place in the list. The code worked properly until i tried to delete the first node using the following code.

typedef struct l_list
{
    int data,index;
    struct l_list *next_node;
}node;
static int total_node=0;
node *search(node *,int,node **);

int main()
{
int choice,key;
char ans;
node *new_node,*head, *p_node,*index_change,*cur;
node *get_node();
head=NULL;


printf("Program for Linked List.\n");
do
{
    printf("\n1. Create Node");
    printf("\n2. Delete Node");
    printf("\n3. Traverse the List");
    printf("\n4. Exit");
    printf("\nEnter your choice: ");
    scanf("%d",&choice);

    switch(choice)
    {
        case 1:
            do
            {
                total_node++;
                new_node=get_node();
                printf("\nEnter the data you want to insert: ");
                scanf("%d",&new_node->data);

                if(head==NULL)
                {
                    head=new_node;
                    head->index=1;
                }
                else
                {
                printf("\nWhich node you want to insert it as:\n");
                for(int i=1;i<=total_node;i++)
                {
                    printf("%d ",i);
                }
                printf("==) ");
                scanf("%d",&key);
                //printf("\b\b-|-");
                if(key==1)
                {
                    new_node->next_node=head;
                    head=new_node;
                }
                else
                {
                    p_node=search(head,key,&cur);
                    new_node->next_node=p_node->next_node;
                    p_node->next_node=new_node;
                //p_node=NULL;
                }
                new_node->index=key;
                index_change=new_node->next_node;
                while(index_change!=NULL)
                {
                    index_change->index=++key;
                    index_change=index_change->next_node;
                }

                }

                printf("\nDo you want to insert more node in the linked list: [y/n]");
                //ans=getch();
            }while((ans=getch())=='y');

            break;

//Deletion code.
case 2:
            do
            {
                if(head==NULL)//head is first node of the list
                {
                    printf("\nUNDERFLOW!\nThe linked list is already empty.\n");
                }
                else
                {
                    printf("Which node you want to delete:\n");
                    for(inti=1;i<=total_node;i++)
                        printf("%d ",i);  //total_node=variable taken
                    printf("==) ");      //to track the total no of node
                    scanf("%d",&key); //key=node index to be deleted
                    //printf("\b\b-|-");
                    if(key==1)
                    {
        //If we need to delete the first node when only one node is left                
                            if(total_node==1)
                            {
                            //p_node=head;
                            head=NULL;

                            }
                //If we need to delete the first node when more than one node are there
                        else
                            {
                            //p_node=head;
                            head=head->next_node;
                            }
                        total_node--;
                    }
                    else
                    {
                        p_node=search(head,key,&cur);//returns node just before the node to be deleted
                        p_node->next_node=cur->next_node;//cur gets the value of the node that is to be deleted.
                        total_node--;
                    }
                    index_change=p_node->next_node;
                    while(index_change!=NULL)//to change the index of following nodes.
                    {
                        index_change->index=key++;
                        index_change=index_change->next_node;
                    }
                }
                printf("\nDo you want to delete more nodes: [y/n]\n");
            }while((ans=getch())=='y');
case 3:
        if(head==NULL)
            printf("\nThe linked list is empty.\n");
        else
        {
            printf("\nThe elements of linked lists are as follows:\n\n");
            p_node=head;
            while(p_node!=NULL)
            {
                printf("[%d]->%d  ",p_node->index,p_node->data);
                p_node=p_node->next_node;
            }
        }
        break;


    }
}while(choice!=4);



return 0;
}

    node *get_node()
    {
        node *temp1;
        temp1= new node;
        temp1->next_node=NULL;
        return temp1;
    }

    node *search(node *head,int key,node **cur)
    {
        node *current,*prev;
        current=head;
        while(current!=NULL)
        {                
            if(current->index==key)
            {
                return prev;
            }
            prev=current;
            current=current->next_node;
            *cur=current;
        }
        return prev;
    }

using this code if i try to delete the first node the program gets crashed. And when i use a temporary variable such as

if(key==1)
                    {
                        if(total_node==1)
                            {
                            p_node=head;
                            head=NULL;
                            }
                        else
                            {
                            p_node=head;
                            head=p_node->next_node;
                            }
                        total_node--;
                    }

the program works properly. So what i want to ask is can we delete the head node directly or we always require another temporary structure pointer to delete the head node.

In this line :

index_change=p_node->next_node;

you dereference p_node . But in the cases where you delete the first node, you don't set a value for p_node . The crash you observe is likely because p_node does not hold a valid memory address.

It is difficult to say what is happening without the exact error you are getting, and the complete program.

One thing that immediately looks wrong is that you assign nodes (your struct Linked_List ), which includes the data. This is not what you want with a linked list. In a linked list, you just want to modify pointers, and prevent copying the data.

Also, you program is very badly structured. Consider moving the specific operations into separate functions, and write tests for each of these. This will also allow you to post a concise, complete code sample with your question.

In order to close in on the error, you can either use a debugger, or you can add print statements to your code that tell you what is happening.

In this line:

p_node=search(head,key,&cur);//returns node just before the node to be deleted

you pass head pointer which is already set to NULL if you try to delete 1st node.

So you can not dereference the head pointer inside the search function which your code must be doing (as I believe) since you do not have any other way to get to the start of linked list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM