实验 线性表的有关操作

1、随机产生或键盘输入一组元素,建立一个带头结点的单向链表(无序)。

2、遍历单向链表(显示)。

3、把单向链表中元素逆置(不允许申请新的结点空间)。

4、在单向链表中删除所有的偶数元素(值为偶数)结点。

5、编写在非递减有序链表中插入一个元素使链表元素仍有序的函数,并利用该函数建立一个非递减有序单向链表。

6、利用算法5建立两个非递减有序单向链表,然后合并成一个非递增链表。

7、利用算法5建立两个非递减有序单向链表,然后合并成一个非递减链表。

8、编写一个主函数,调试上述算法。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef struct Node{
    int val;
    Node *next;
    Node(int _v=0,Node *_n=NULL){
        val=_v;next=_n;
    }
}Node;
typedef struct ilist{
    Node *head;
    ilist(){
        head=(Node*)malloc(sizeof(Node));
    }
public:
    Node* newnode(){//......................生成新节点
        Node* p=(Node*)malloc(sizeof(Node));
        p->val=rand();
        p->next=NULL;
        return p;
    }
    void randini(int num,int order){//......随机生成数字
        Node *now=head;
        while(num--){
            now->next=newnode();
            now=now->next;
        }
        if(order) sort();
        if(order==2) reverse();
    }
    void traval(){//........................遍历链表
        Node *now=head->next;
        while(now!=NULL){
            printf("%d\n",now->val);
            now=now->next;
        }
        puts("");
    }
    void reverse(){//.......................翻转链表
        Node *now=head->next;
        Node *next=NULL,*last=NULL;
        while(now!=NULL){
            next=now->next;
            now->next=last;
            last=now;
            now=next;
        }
        head->next=last;
    }
    void delete_even(){//...................删除偶数
        Node *now=head->next;
        Node *last=head;
        while(now!=NULL){
            if(now->val%2==0) last->next=now->next;
            else last=last->next;
            now=now->next;
        }
    }
    void sort(){//..........................链表排序
        for(Node *i=head->next;i!=NULL;i=i->next){
            for(Node *j=head->next;j!=NULL;j=j->next){
                if(i->val<j->val) swap(i->val,j->val);
            }
        }
    }
    void insert(int val){//.................非递减插入
        Node *now=head->next,*last=head,*p=newnode();
        p->val=val;
        while(1){
            if(now==NULL||now->val>=val){
                last->next=p;
                p->next=now;
                return ;
            }
            now=now->next;
            last=last->next;
        }
    }
    void UDunion(ilist a){//................非递减合并
        for(Node *i=a.head->next;i!=NULL;i=i->next){
            insert(i->val);
        }
    }
    void UIunion(ilist a){//................非递增合并
        for(Node *i=a.head->next;i!=NULL;i=i->next){
            insert(i->val);
        }
        reverse();
    }
}ilist;

int main()
{
    srand((unsigned)time(0));
    ilist a,b;
    a.randini(10,0);
    b.randini(5,0);a.traval();
    a.reverse();a.traval();
    a.sort();a.traval();
    a.delete_even();a.traval();
    a.insert(4523);a.traval();
    a.UIunion(b);a.traval();
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值