上一篇的头插法书写有误,在此更正

//create by yu

#include "stdio.h"
#include "stdlib.h"

typedef struct node{
    int element;
    struct node *next;
}node,*linklist;


linklist insertfromhead(int number){
    linklist head=(linklist)malloc(sizeof(node));
    head->next=NULL;
    for(int i=0;i<number;i++){
        linklist n=(linklist)malloc(sizeof(node));
        printf("please input the %dth number",(i+1));
        scanf("%d",&(n->element));
        n->next=head->next;
        head->next=n;
    }

    return head;
}

void display(linklist head){
    linklist temp=head;
    while(temp->next){
        temp=temp->next;
        printf("%d\n",temp->element);
    }
}
int main(){
    linklist head=insertfromhead(5);
    display(head);
    return 0;
}

 

相关文章:

  • 2021-09-05
  • 2022-12-23
  • 2021-11-27
  • 2021-12-14
  • 2021-10-06
  • 2021-11-21
  • 2021-12-29
  • 2022-02-13
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-13
  • 2021-12-22
  • 2022-12-23
相关资源
相似解决方案