zoukankan      html  css  js  c++  java
  • 数据结构实验一:线性表的基本操作

    顺序表—线性表的顺序实现删除多余元素
    #include<iostream>
    #include<stdlib.h>
    using namespace std;
    struct node
    {
        int n[100];
        int length;
        int tag;
    }a;
    void init()
    {
        int i;
        for(i=0;i<30;i++)
        {
            a.n[i]=rand()%10;
            a.length++;
        }
    }
    void display(int n)
    {
        int i;
        for(i=0;i<n;i++)
        cout<<a.n[i]<<" ";
        cout<<endl;
    }
    int find(int num)
    {
        int i;
        for(i=0;i<a.tag;i++)
        if(a.n[i]==num)
        return 1;
        return 0;
    }
    void del()
    {
        int i;
        for(i=0;i<30;i++)
        {
            if(!find(a.n[i]))
            a.n[a.tag++]=a.n[i];
        }
    }
    int main()
    {
        init();
        display(30);
        del();
        display(a.tag);
        return 0;
    }





    链表——链表实现删除多余元素

    #include<iostream>
    #include<stdlib.h>
    using namespace std;
    typedef struct s
    {
        int data;
        struct s *next;
    }list;
    list *head2,*tail;
    void insert(list *l,int n)
    {
        list *p;
        p=new list;
        p->data=n;
        p->next=l->next;
        l->next=p;
    }
    void display(list *l)
    {
        list *p;
        p=l->next;
        while(p!=NULL)
        {
            cout<<p->data<<" ";
            p=p->next;
        }
        cout<<endl;
    }
    int find(int n)
    {
        list *p;
        p=head2->next;
        while(p!=NULL)
        {
            if(p->data==n)
            return 1;
            p=p->next;
        }
        return 0;
    }
    void del(list *l)
    {
        list *p,*q;
        p=l;
        while(p->next->next!=NULL)
        {
            if(!find(p->next->data))
            {
                q=p->next;
                p->next=q->next;
                q->next=NULL;
                tail->next=q;
                tail=q;
            }
            else
            p=p->next;
        }
        if(!find(p->next->data))
        {
            q=p->next;
            q->next=NULL;
            tail->next=q;
            tail=q;
        }
    }
    int main()
    {
        int i;
        list *head1;
        head1=new list;
        head2=new list;
        head1->next=NULL;
        head2->next=NULL;
        tail=head2;
        for(i=1;i<=30;i++)
        insert(head1,rand()%10);
        display(head1);
        del(head1);
        display(head2);
    }
  • 相关阅读:
    剑指offer分块总结----------字符串
    sizeWithFont 不是线程安全。
    《创业之路,败给了……》欠大家一个回复,及项目转让:高频彩票通、仓库管理软件
    搞懂.NET Framework 历史版本(2017年)
    DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践(3)
    DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践(2)
    MVC+EF 理解和实现仓储模式和工作单元模式
    (译文)MVC通用仓储类
    【开源】T430s光驱位支架
    【开源】讯飞VBOX改装蓝牙5.0(aptX HD)音箱
  • 原文地址:https://www.cnblogs.com/ma6174/p/2313297.html
Copyright © 2011-2022 走看看