数据结构实验一:线性表的基本操作

顺序表—线性表的顺序实现删除多余元素
#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);
}
复制代码

博主ma6174对本博客文章(除转载的)享有版权,未经许可不得用于商业用途。转载请注明出处http://www.cnblogs.com/ma6174/

对文章有啥看法或建议,可以评论或发电子邮件到ma6174@163.com


本文转自ma6174博客园博客,原文链接:http://www.cnblogs.com/ma6174/archive/2012/01/05/2313297.html ,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值