求帅哥指点,全部re什么情,看着没毛病!
查看原帖
求帅哥指点,全部re什么情,看着没毛病!
640129
Rat_Man楼主2022/8/16 19:52
#include<stdio.h>
#include<stdlib.h>
int flag = 0;
typedef struct link
{
    int data;
    struct link* pr;
    struct link* next;
} node;

node* creatLink(int n){
    node* head,*p,*q,*tail;
    head = NULL;tail = NULL;
    int data;
    for(int i = 0;i < n; ++i){
        scanf("%d",&data);
        p = (node*)malloc(sizeof(node));
        p->data = data;
        p->next = NULL;
        p->pr = NULL;
        if(head == NULL) head = p;
        else {
            q->next = p;
            p->pr = q;
        }
        q = p;
        if(i == n-1) tail = p;
    }
    return head;
}
node* insert(node**tail,node* head,int data,int n){
    if(flag&&data>=(*tail)->data) return head;
    node* p = (node*)malloc(sizeof(node));
    node* h = head;
    p->data = data;
    p->next = NULL;
    p->pr = NULL;
    int k = 1;
    if(data<head->data){
        head->pr = p;
        p->next = head;
        *tail = (*tail)->pr;
        return p;
        }
        
    else{
        while(k<=n){
        if(data<h->data){
            p->pr = h->pr;
            h->pr->next = p;
            h->pr = p;
            p->next = h;
            if(k==n) *tail = p;
            else *tail = (*tail)->pr;
            return head;
            }
        if(h->next!=NULL)
            h = h->next;
        else {
            h->next = p;
            p->pr = h;
            *tail = p;
            return head;
        }
        ++k;
    }
    flag = 1;
        free(p);
}
	return head;
}

int main(){
    int n,m;scanf("%d %d",&n,&m);
    node* head = creatLink(1);
    node* tail = head;
    for(int i = 1;i<n;++i){
        int num;scanf("%d",&num);
        head = insert(&tail,head,num,m);
    }
    for(int i = 1;i<=m;++i) {
        printf("%d\n",head->data);
        head = head->next;
    }
    
    return 0;
}
2022/8/16 19:52
加载中...