链表

#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;
}
 
posted @ 2016-04-18 22:13  云胡同学  阅读(77)  评论(0编辑  收藏  举报