SDUT - 2121 数据结构实验之链表六:有序链表的建立

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    int n, i;
    scanf("%d", &n);
    struct node *h, *q, *p;
    h = (struct node *)malloc(sizeof(struct node ));
    h -> next = NULL;
    for(i = 0; i < n; i++)
    {
        p = (struct node *)malloc(sizeof(struct node ));
        p -> next = NULL;
        printf("%d", &p -> data);
        q = h;
        while(q -> next)
        {
            if(q -> next -> data > p -> data)break;
            q = q -> next;
        }
        p -> next = q -> next;
        q -> next = p;
    }
    p = h -> next;
    printf("%d", p -> data);
    p = p -> next;
    while(p)
    {
        printf(" %d", p -> data);
        p = p -> next;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Miracle_QSH/article/details/81699756