链表不打印任何内容

时间:2018-08-10 08:23:16

标签: c pointers

当我将值添加到列表时,它什么也不打印。

`main中的switch语句?在从用户选择的输入之间切换以调用适当的功能。 放一个会提示您添加一个数字,当键入2时,必须打印该列表。

问题是,即使我已插入,它也不会打印任何内容

  

c

#include<stdlib.h>//header files
#include<stdio.h>

struct node{  //structure containing node
   int data;
   struct node *next;//next link
};

struct node *head = NULL;//head pointer

void print_list(struct node *ptr);//prints list
struct node *add_node( int d);//adds value to list
void push(struct node*m);//creates node

int main(){
    int status;

    while(1)
    {
        printf("****************menue oriented**********************\n");
        printf("Enter 1 for adding node, 2 for printing and rest for exiting\n");
        scanf("%d", &status);//switch statement for picking data

        switch(status)
        {
        case 1:
            printf("you selected the adding part\n");
            push(head);//1 adds item
            break;
        case 2:
            print_list(head);// 2 prints list
            break;
        default:
            printf("Wrong input boss\n");// default call
            break;
        exit(0);// program termination
        }
    }
    return 0;
}

struct node *add_node(int d)
{//add node function
    struct node *extra = (struct node *)malloc(sizeof(struct node));
    //node size allocation with malloc
    extra->data = d;//adds value d
    extra->next = NULL;//sets ponter to null

    return extra;//returns the pointer 
}

void print_list(struct node *ptr)
{
    struct node *x;//new pointer
    x = ptr;//pointer == head
    if(x == NULL)
    {
        printf("list is empty boss\n");//executed if empty
        exit(0);
    }
    while(x->next != NULL)
    {
        printf("********************************************\n");
        printf("%d\n", x->data);//print data
        x = x->next;//move to next node
    }
}

void push(struct node*m)
{
    int x;
    printf("Enter value to add to lost\n");//enter value from console
    scanf("%d",&x);
    struct node *p;
    p = add_node(x);//store pointer in p
    if(m == NULL)
    {//let head point to p if head is empty
        m = p;
    }
    else
    {
        p->next = m;//pointer of p pointers to head and head points to p
        m = p;
    }
}

2 个答案:

答案 0 :(得分:0)

您的问题出在您的push函数中,您在其中将新指针分配给函数参数。 C是按值传递的,因此头指针完全不变。您可以将指针传递到头指针(请参见其他答案),也可以将更新后的指针返回给head = push(head);

进行调用
struct node* push(struct node*m){
int x;
printf("Enter value to add to lost\n");//enter value from console
scanf("%d",&x);
struct node *p;
p = add_node(x);//store pointer in p
if(m == NULL){//let head point to p if head is empty
    m = p;
}
else{
    p->next = m;//pointer of p pointers to head and head points to p
    m = p;
}
return m;
}

答案 1 :(得分:0)

首先,传递给push函数的是指针的副本,而不是指针本身。这就是为什么它不能被替换。就像值一样-发送给函数的是值的副本,而不是实际值,因此,如果在函数中进行更改,则它不会在其外部更改。如果要更改原始值,请将其指针发送到函数。与指针相同-是否要覆盖原始指针? -发送该指针的指针。

您要更改以下内容:

drive() {
    super.drive();
    var unlock = function() {
        console.log("Car is unlocked");
    }
    unlock();
}

主要:

 void push(struct node**m); //take a pointer to a pointer

和您的功能

push(&head); //pass the address of head to the function

第二,如果“ next”项为NULL,则打印将停止,但不会显示尾部的值。在while循环之后,您应该再打印一次数据。