为什么我的结构没有变化? (链接列表)

时间:2019-07-08 18:36:20

标签: c

请注意insert_beg函数,我将结构地址传递给了函数,并对其进行了操作(在开头插入),因此当我遍历列表时。它没有更新。如果我将函数的(insert_beg)返回类型更改为struct node *,并将其分配给start(在main内部)。它得到反映。 问题是,即使我已将开始的地址传递给函数(按引用调用),为什么它仍然没有首先体现出来?

P.S:我是指针和结构的新手,帮帮我!谢谢!

#include<stdio.h>
#include<stdlib.h>
struct node {
    int val;
    struct node *link;
};

//Operations
void traverse(struct node *START);
struct node* search(struct node *START,int);
void insert_beg(struct node *START);

int main(){

    struct node *start;
    start = NULL;

    int choice = 1;
    while(choice){
        printf("\n\n****Linked List****\n");
        printf("1.Create Node\n2.Traverse LL\n3.Search\n4.Insert At Beginning\nEnter Your Choice : ");
        scanf("%d",&choice);
        switch(choice){
            case 1:{
                //create node
                struct node *new_node = (struct node*)malloc(sizeof(struct node));
                printf("Enter Data : ");
                scanf("%d",&new_node->val);
                new_node->link = NULL;

                if(start == NULL){
                    //This means that the linked list is empty
                    start = new_node;
                }else{
                    //This means the the ll has more than 1 node
                    struct node *iter = start;
                    while(iter->link != NULL){
                        iter = iter->link;
                    }
                    //iter points to the last node now
                    iter->link = new_node;
                }
                break;
            }
            case 2:{
                //Traverse
                traverse(start);
                break;
            }
            case 3:{
                printf("\nEnter the data to be searched : ");
                int data;
                scanf("%d",&data);
                struct node *pos = search(start,data);
                if(pos != NULL)
                    printf("Data : %d is present | Address : %p",pos->val,pos->link);
                else
                    printf("-1");
                break;
            }
            //Insertion
            case 4:{
                //1.Insert Beginning
                insert_beg(start);
               //start = insert_beg(start);
                break;
            }
            default:{
                choice = 0;
                break;
            }
        }
    } 
    return 7;
}

void insert_beg(struct node *START){
    struct node *new_node = (struct node*) malloc(sizeof(struct node));
    printf("Enter the data to be stored : ");
    scanf("%d",&new_node->val);
    new_node->link = START;
    START = new_node;
    // return START;
}


void traverse(struct node *START){
    struct node* iter = START;
    int node_count = 0;
    while(iter != NULL){
        printf("Data : %d | Address : %p\n",iter->val,iter);
        iter = iter->link;
        node_count++;
        }
    printf("LL has %d node(s)",node_count);
}

struct node* search(struct node *START,int data){
    struct node* pos = NULL;
    struct node* iter = START;
    if(START != NULL){
    while(iter->link != NULL){
        if(data == iter->val){
            pos = iter;
            break;
        }
        else
            iter = iter->link;
        }
    }
    return pos;
}

1 个答案:

答案 0 :(得分:0)

以下更改应可解决问题。

下面的内容传递列表头(开始)的地址,以便可以修改列表头的内容。

我还提供了适当的错误检查

在原型中替换:

public class Customer
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid CustomerID { get; set; }
    public string Name { get; set; }
    public string PrimaryContact { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
    public Guid ResellerId { get; set; }
    [ForeignKey("ResellerId")]
    public Reseller Reseller { get; set; }  
}

具有:

void insert_beg(struct node *START);

void insert_beg(struct node **START); 函数替换为以下内容:

insert_beg()

调用void insert_beg(struct node **START) { struct node *new_node = malloc(sizeof(struct node)); if( !new_node ) { perror( "malloc failed" ); // free all allocated memory, then exit( EXIT_FAILURE ); } printf("Enter the data to be stored : "); if( scanf("%d",&new_node->val) != 1 ) { fprintf( stderr, "scanf for data failed\n" ); free( new_node ); } if( !(*START) ) { // list is empty *START = new_node; new_node->link = NULL; } else { // list already contains (at least) one node new_node->link = *START; *START = new_node; } // return START; } 时,使用以下语句:

insert_beg()

注意,该参数是列表开头的地址,而不是列表开头的内容