为什么不显示输出?

时间:2015-07-30 13:01:33

标签: c

我正在尝试编写一个程序,我在其中创建新节点并插入项目。 我的问题是当我想在它没有显示它的所有节点中显示项目时。

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0

struct node{
    int item;
    struct node *next;
}lol;

struct node *head = NULL;
void insertfirst(int ele);
void insertnext(int ele);
void displayelement();

int main()
{
    clrscr();
    int ele,choice;
    printf("Enter element in first node:");
    scanf("%d",&ele);
    do{
        printf("Please select any of the other options\n1.Add element to new node\n2.Display elements\n");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                    printf("Enter element in node:");
                    scanf("%d",&ele);
                    insertnext(ele);
                    break;
            case 2:
                    displayelement();
                    break;
            default:
                    break;
        }
        getch();
        clrscr();
        displayelement();
    }while(choice < 3);
    getch();
    return 0;
}

void insertfirst(int ele)
{
    struct node *p;
    p = (struct node*)malloc(sizeof(struct node));
    p -> item = ele;
    p -> next = head;
    head = p;

}

void insertnext(int ele)
{
    struct node *temp;
    struct node *p;
    temp = head;
    p = (struct node*)malloc(sizeof(struct node));
    temp = head;
    p->item = ele;
    p->next = temp ->next;
    temp -> next = p;
}

void displayelement()
{
    struct node *temp;
    temp = head;
    printf("\n The nodes are");
    while(temp!=NULL)
    {
        printf("%d",temp -> item);
        temp = temp -> next;
    }

}

当我直接使用开关盒

时,程序似乎有效
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0

struct node{
            int item;
            struct node *next;
            }lol;

struct node *head = NULL;
void insertfirst(int ele);
void insertnext(int ele);
void displayelement();

int main()
{
clrscr();
int ele;
printf("Enter element in first node:");
scanf("%d",&ele);
insertfirst(ele);
insertnext(ele);
displayelement();
getch();
 return 0;
}

void insertfirst(int ele)
{
    struct node *p;
    p = (struct node*)malloc(sizeof(struct node));
    p -> item = ele;
    p -> next = head;
    head = p;

}

void insertnext(int ele)
{
    struct node *temp;
    struct node *p;
    temp = head;
    p = (struct node*)malloc(sizeof(struct node));
    temp = head;
    p->item = ele;
    p->next = temp ->next;
    temp -> next = p;
}

void displayelement()
{
    struct node *temp;
    temp = head;
    printf("\n The nodes are");
    while(temp!=NULL)
    {
        printf("%d",temp -> item);
        temp = temp -> next;
    }

}

2 个答案:

答案 0 :(得分:2)

在取消引用NULL指针时,您忘记从新代码中调用insertfirst导致insertnext中的未定义行为。

通过添加

解决问题
insertfirst(ele);

printf("Enter element in first node:");
scanf("%d",&ele);

答案 1 :(得分:0)

看起来更好:

void insertfirst(int ele)
{
    head = (struct node*)malloc(sizeof(struct node));
    head->item = ele;
    head->next = head;
}

void insertnext(int ele)
{
    struct node *p;
    p = (struct node*)malloc(sizeof(struct node));
    p->item = ele;
    p->next = head->next;
    head->next = p;
}