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);
    }
  • 相关阅读:
    php发送http请求带json格式数据
    a标签跳转,打开一个新页面
    echarts图例多行显示,并且全部对齐
    原子性,有序性,可见性
    winds消息大全
    C#中的结构体和对象区别
    装饰者模式
    hashMapp
    linux 定时任务
    windsServer2008设置定时重启
  • 原文地址:https://www.cnblogs.com/ma6174/p/2313297.html
Copyright © 2011-2022 走看看