您的位置:首页 > 其它

顺序表应用1:多余元素删除之移位算法

2016-07-27 14:54 393 查看
链表做法

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

typedef struct no

{

    int data;

    struct no *next;

} node;

node *creat(int n)

{

    node *head,*p,*tail;

    head=(node *)malloc(sizeof(node));

    head->next=NULL;

    tail=head;

    for(int i=1; i<=n; i++)

    {

        p=(node *)malloc(sizeof(node));

        scanf("%d",&p->data);

        p->next=NULL;

        tail->next=p;

        tail=p;

    }

    return head;

}

node *del(node *head)

{

    node *p,*q,*k;

    p=head->next;

    while(p)

    {

        k=p;

        q=p->next;

        while(q)

        {

            if(p->data==q->data)

            {

                k->next=q->next;

                free(q);

                q=k->next;

            }

            else

            {

                k=q;

                q=q->next;

            }

        }

        p=p->next;

    }

    return head;

}

node *print(node *p)

{

    node*q;

    q=p->next;

    while(q)

    {

        if(q->next==NULL)

        {

            printf("%d\n",q->data);

        }

        else

        {

            printf("%d ",q->data);

        }

        q=q->next;

    }

    return 0;

}

int main()

{

    node *p;

    int n,m;

    scanf("%d",&n);

    while(n--)

    {

        scanf("%d",&m);

        p=creat(m);

        del(p);

        print(p);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: