链接列表不适用于插入

时间:2014-04-07 16:59:35

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

我编写了一个链表代码,用于在节点中插入元素。但问题是当我想使用函数插入第一个元素时,输出变为空。但是当我在main函数中插入第一个元素时(参见注释行),它会给出正确的输出。怎么解决? 这是我的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;
}

由于

2 个答案:

答案 0 :(得分:2)

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

insert(&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),以便对头部所做的更改也反映在函数外部。干杯

答案 1 :(得分:0)

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

#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();
    }

}