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

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

posted on   ma6174  阅读(1472)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

统计

点击右上角即可分享
微信分享提示