Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

c - Singly linked list program error while using malloc function(updated one)

I am having trouble inserting the second element the program just waits and exits, the first element is printing correctly, the problem occurs when I try to enter 2nd element using function Insert. please help case 7: uses the function Insert which passes a "d" which is data,and Display prints the list.

#include <stdio.h>
#include <stdlib.h>
int count=0;
struct node{
  int data;
  struct node *next;
};
struct node *head=NULL;


void Insert(int d){
  struct node *temp,*newnode;
  newnode= (struct node*) malloc(sizeof(struct node));
  if(newnode==NULL)
  {
    printf("not there
");
  }
else{newnode->data= d;
      newnode->next=NULL;  }

  if(head==NULL)
  {
    head=newnode;temp=newnode; count++;

  }
  else
  {
     temp->next=newnode;
      temp=newnode;
    count++;
  }

}

void Display(){
struct node*temp;

temp=head;
 while(temp!=NULL){
 printf("%d",temp->data);
 temp=temp->next;

 }

}



void main()
{  int c=0;int d;

    do{
     printf("choose an option: 1. Insert at begining 2. Insert at end 3. Insert at specified position
 4.Delete from begining 5.Delete from end 6. Delete from specified position 7.Insert
 8. Exit
");
     scanf("%d",&c);
    switch (c) {

      case 7: printf("enter element
");
              scanf("%d",&d);Insert(d);Display();break;

      default  : c=8; break;
    }

  }while(c!=8);

}
question from:https://stackoverflow.com/questions/65842618/singly-linked-list-program-error-while-using-malloc-functionupdated-one

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
#include <stdio.h>
#include <stdlib.h>
int count=0;
struct node{
  int data;
  struct node *next;
};
struct node *head=NULL,*temp;


void Insert(int d){
  struct node *newnode;
  newnode= (struct node*) malloc(sizeof(struct node));

  if(newnode==NULL)
  {
    printf("not there
");
  }
else{newnode->data= d;
      newnode->next=NULL;  }

  if(head==NULL)
  {
    head=newnode;temp=newnode; count++;

  }
  else
  {
     temp->next=newnode;
      temp=newnode;
    count++;
  }

}

void Display(){
struct node*temp;

temp=head;
 while(temp!=NULL){
 printf("%d,",temp->data);
 temp=temp->next;

 }
 printf("
", );
}



void main()
{  int c=0;int d;

    do{
     printf("choose an option: 1. Insert at begining 2. Insert at end 3. Insert at specified position
 4.Delete from begining 5.Delete from end 6. Delete from specified position 7.Insert
 8. Exit
");
     scanf("%d",&c);
    switch (c) {

      case 7: printf("enter element
");
              scanf("%d",&d);Insert(d);Display();break;

      default  : c=8; break;
    }

  }while(c!=8);

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...