链表

#include <stdio.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    struct node *p, *q, *head, *t;//p是临时指针 表示当前节点
    int n, i, a;
    scanf("%d",&n);
    head = NULL;//刚开始将头指针设为空
    for(i = 1; i <= n; i++)
    {
        scanf("%d",&a);
        p = (struct node *)malloc(sizeof(struct node));//临时申请空间
        p->data= a;
        p->next = NULL;
        if(head == NULL)//如果head是头指针则head指向第一个节点
            head = p;
        else
            q->next = p;//第一个节点的后继指针指向第二个节点
        q = p;//q刚开始指向第一个节点 第二次循环
    }
    t = head;
    scanf("%d",&a);
    while(t!=NULL)
    {
        if(t->next->data>a)
        {
            p = (struct node*)malloc(sizeof(struct node));
                p->data= a;
            p->next = t->next;
            t->next = p;
            break;
        }    
        //printf("%d ",t->data);
        t = t->next;    
    }
    t = head;
    while(t!=NULL)
    {
        printf("%d ",t->data);
        t = t->next;
    }
return 0;
}

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 13:47
机械打工仔:你自己匿名可以,这么好的公司就别给它匿名了
点赞 评论 收藏
分享
码农索隆:单休一个月少休息4天,一年就是48天,平时节假日,别人3天假期,单休的两天
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务