头部插入节点的应用——定义单向链表:输入若干个正整数(输入-1为结束标志),要求按输入数据的逆序并输出。

#include <stdio.h>
#include <stdlib.h>
typedef struct num * list;
struct num{
    int data;
    list next;
};
list create(){
    list p,head,tail;
    head=tail=NULL;
    int x;
    while(1){
        p=(list)malloc(sizeof(struct num));
        scanf("%d",&x);
        if(x==-1)
        break;
        p->data=x;
        p->next=head;
        head=p;

    }
    return head;
}
void print666(list head){
    while((head->next)!=NULL){
        printf("%d ",head->data);
        head=head->next; 
       }
       printf("%d",head->data);	//最后一个节点不带空格

}
int main(){
    list head=create();
    print666(head);
    return 0;
}
发布了42 篇原创文章 · 获赞 13 · 访问量 1906

猜你喜欢

转载自blog.csdn.net/KEVINzzh/article/details/105126873