【问题标题】:Linked List not working for insertion链接列表不适用于插入
【发布时间】:2014-04-07 16:59:35
【问题描述】:

我已经编写了一个链表代码来在节点中插入一个元素。但问题是当我想使用函数插入第一个元素时,输出为空。但是当我在主函数中插入第一个元素时(见注释行),它给出了正确的输出。如何解决? 这是我的 C 代码:

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

typedef struct node{
    int val;
    struct node *next;
}node;

void print(node *head){

    if(tem == NULL){
        printf("List is Empty\n");
        return;
    }
    node *tem= head;
    while(tem != NULL){
        printf("%d ", tem->val);
        tem= tem->next;
    }
}

void insert(node *head, int val){

    if(head == NULL){
       node *tem= malloc(sizeof(node*));
       tem->val= val;
       tem->next= NULL;
       head= tem;
       return;
    }

    node *tem= head;

    while(tem->next != NULL){
       tem= tem->next;
    }
    tem->next= malloc(sizeof(node*));
    tem->next->val = val;
    tem->next->next= NULL;
}

int main()
{
    node *head= NULL;
    /*
    head = malloc(sizeof(node*));
    head->val= 5;
    head->next= NULL;
    */

    insert(head, 15);
    print(head);

    return 0;
}

谢谢

【问题讨论】:

    标签: c linked-list singly-linked-list insertion


    【解决方案1】:

    尝试发送head的地址而不是head,如下所示:

    insert(&amp;head, 15);

    void insert(node **head, int val){
    
    if(*head == NULL){
       node *tem= malloc(sizeof(node*));
       tem->val= val;
       tem->next= NULL;
       *head= tem;
       return;
    }
    

    这是因为当您发送头部时,所做的任何更改都将是该函数的本地更改(在本例中为插入),并且不会反映在该函数之外。因此,您必须发送 head 的地址 (&amp;head),以便对 head 所做的更改也反映在函数之外。干杯

    【讨论】:

    • @user3507398 因为在print 函数中声明它之前,您已经使用了tem。它应该引发编译错误。
    【解决方案2】:

    试试这个完全实现的单链表:

    #include <stdio.h>
    
    struct node{
    int data;
    struct node *next;
    };
    
    struct node *head=NULL;
    
    
    void insert(int data, int position)
    {
        struct node *newNode=malloc(sizeof(struct node));
        newNode->data=data;
        if(position<1)
        {
          printf("Invalid Insertion Position \n");
          return;
        }
        if(head==NULL && position==1)
        {
            newNode->next=NULL;
            head=newNode;
        }
        else if(head==NULL && position!=1)
        {
            printf("Invalid Insertion Position \n");
        }
        else if(position==1)
        {
            newNode->next=head;
            head=newNode;
        }
        else
        {
            int i=0;
            struct node *temp=head;
            while(temp->next!=NULL && i<position-2)
            {
                i++;
                temp=temp->next;
            }
            if(i<position-2)
            {
                printf("Invalid Insertion Position \n");
            }
            else
            {
                newNode->next=temp->next;
            temp->next=newNode;
            }
    
        }
    }
    
    
    void delete(int position)
    {
        int i=0;
        if(position<1)
        {
            printf("Invalid Position of Deletion \n");
            return;
        }
        if(head==NULL)
        {
            return;
        }
        if(position==1)
        {
            head=head->next;
        }
        else
        {
            struct node *temp=head;
            while(temp->next->next!=NULL && i<position-2)
            {
                i++;
                temp=temp->next;
            }
             if(i<position-2)
                {
                    printf("Invalid Position of Deletion \n");
                    return;
                }
            else
                {
                    temp->next=temp->next->next;
                }
    
        }
    
    }
    
    
    
    
    void printlist()
    {
        if(head==NULL)
        {
            printf("Empty List!! \n");
            return;
        }
        struct node *temp=head;
    
        while(temp!=NULL)
        {
            printf("%d",temp->data);
            printf("\t");
            temp=temp->next;
        }
        printf("\n");
    }
    
    int main()
    {
        int t;
        printf("Enter number of Test Cases: \t");
        scanf("%d", &t);
        printf("\nEnter Queries in this format: \n");
        printf("For Insertion: \t I data position \n");
        printf("\tEx:\t I 25 5 \n");
        printf("For Deletion: \t D position \n");
        printf("\tEx:\t D 2 \n\n");
    
        while(t--)
        {
            char c;
            int a,b;
            printf("Enter query: \t");
            scanf("%c", &c);
            scanf("%c", &c);
    
            if(c=='I')
            {
                scanf("%d %d", &a,&b);
                insert(a,b);
            }
            else if(c=='D')
            {
               scanf("%d", &a);
               delete(a);
            }
            printlist();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多