1. 程式人生 > 其它 >vue封裝元素拖拽指令

vue封裝元素拖拽指令

思路(非遞迴):

  • 合併成一個新的連結串列
  • 用兩個指標指向原來兩個連結串列,用一個指標指向合併的新連結串列的尾部
  • 比較兩個連結串列元素的大小
  • 核心程式碼
p3->next=p1;
p3=p1;
p1=p1->next;

1.插入元素

struct data* insert(struct data* head,struct data* p0)//有序
{
    struct data* p1=head,*p2;
    if(head==NULL)
    {
        head=p0;
        p0->next=NULL;
    }
    else
    {
            
while(p0->data>p1->data&&p1->next!=NULL) { p2=p1; p1=p1->next; } if(p0->data<=p1->data)//插在頭前或中間 { if(p1==head) head=p0; else p2->next=p0; p0->next=p1; }
else//插在尾後 { p1->next=p0; p0->next=NULL; } } n++; return head; }

2.建立單向有序連結串列

struct data* creat()
{
    struct data* head=NULL,*p;
    p=(struct data*)malloc(LEN);
    printf("Please input data:(end with 0)\n");
    scanf("%d",&p->data);
    
while(p->data) { head=insert(head,p);//插入後要賦值給head p=(struct data*)malloc(LEN); scanf("%d",&p->data); } free(p); return head; }

3.列印連結串列

void print(struct data* head)
{
    struct data* p=head;
    if(head==NULL)
        printf("List is NULL.\n");
    else
    {
        printf("Now,there are datas:\n");
        do
        {
            printf("%d->",p->data);
            p=p->next;
        }while(p->next!=NULL);
        printf("%d\n",p->data);
    }
}

4.合併兩個有序連結串列

struct data* merge(struct data* head1,struct data* head2,struct data* head3)
{
    struct data* p1=head1,*p2=head2,*p3=head3;
    if(head1==NULL) return head2;
    if(head2==NULL) return head1;
    while(p1!=NULL&&p2!=NULL)
    {
        if(p1->data<p2->data)
        {
            p3->next=p1;
            p3=p1;
            p1=p1->next;
        }
        else
        {
            p3->next=p2;
            p3=p2;
            p2=p2->next;
        }
    }
    p3->next=(p1!=NULL)?p1:p2;
    head3=head3->next;
    return head3;
}

完整程式碼

#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct data)
struct data{
    int data;
    struct data* next;
};
int n=0;
struct data* creat();
void print(struct data* head);
struct data* insert(struct data* head,struct data* p);
struct data* merge(struct data* head1,struct data* head2,struct data* head3);
int main()
{
    struct data* head1,*head2,*head3;
    head3=(struct data*)malloc(20*LEN);//一定要給heads開闢空間
    head1=creat();
    printf("List1:\n");
    print(head1);
    head2=creat();
    printf("List2:\n");
    print(head2);
    printf("After merging:\n");
    head3=merge(head1,head2,head3);
    print(head3);
    return 0;
}

struct data* insert(struct data* head,struct data* p0)//有序
{
    struct data* p1=head,*p2;
    if(head==NULL)
    {
        head=p0;
        p0->next=NULL;
    }
    else
    {
            while(p0->data>p1->data&&p1->next!=NULL)
        {
            p2=p1;
            p1=p1->next;
        }
        if(p0->data<=p1->data)//插在頭前或中間
        {
            if(p1==head)
                head=p0;
            else
                p2->next=p0;
            p0->next=p1;
        }
        else//插在尾後
        {
            p1->next=p0;
            p0->next=NULL;
        }
    }
    n++;
    return head;
}

struct data* creat()
{
    struct data* head=NULL,*p;
    p=(struct data*)malloc(LEN);
    printf("Please input data:(end with 0)\n");
    scanf("%d",&p->data);
    while(p->data)
    {
        head=insert(head,p);//插入後要賦值給head
        p=(struct data*)malloc(LEN);
        scanf("%d",&p->data);
    }
    free(p);
    return head;
}

void print(struct data* head)
{
    struct data* p=head;
    if(head==NULL)
        printf("List is NULL.\n");
    else
    {
        printf("Now,there are datas:\n");
        do
        {
            printf("%d->",p->data);
            p=p->next;
        }while(p->next!=NULL);
        printf("%d\n",p->data);
    }
}

struct data* merge(struct data* head1,struct data* head2,struct data* head3)
{
    struct data* p1=head1,*p2=head2,*p3=head3;
    if(head1==NULL) return head2;
    if(head2==NULL) return head1;
    while(p1!=NULL&&p2!=NULL)
    {
        if(p1->data<p2->data)
        {
            p3->next=p1;
            p3=p1;
            p1=p1->next;
        }
        else
        {
            p3->next=p2;
            p3=p2;
            p2=p2->next;
        }
    }
    p3->next=(p1!=NULL)?p1:p2;
    head3=head3->next;
    return head3;
}
View Code

寫連結串列會碰到的問題

  • 呼叫函式的返回值是指標,需要賦給頭指標
  • 動態建立連結串列需要開闢新空間,結束要free沒用了的指標