线性集合A-线性集合B

需要注意的地方:删除一个元素后,下一次遍历的下标值不变,另外程序使用了动态线性表,在插入操作时可以扩容,使用p=realloc(p,size)
#include <iostream> using namespace std; typedef struct { int *p; int length; int size; }sqListEx; void initList(sqListEx& sqlist,int m){ sqlist.p=(int *)malloc(m*sizeof(int)); sqlist.length=0; sqlist.size=m; } void destroyList(sqListEx& sqlist){ free(sqlist.p); sqlist.length=0; sqlist.size=0; } int deleteList(sqListEx& sqlist,int n){ if ((n+1)==sqlist.length) { sqlist.length--; return 1; } for (int i=n;i<sqlist.length;i++) { sqlist.p[i]=sqlist.p[i+1]; } sqlist.length--; return 1; } void main(){ sqListEx sqListA,sqListB; initList(sqListA,30); initList(sqListB,10); sqListA.p[0]=1; sqListA.p[1]=2; sqListA.p[2]=3; sqListA.p[3]=4; sqListA.p[4]=5; sqListA.p[5]=6; sqListB.p[0]=10; sqListB.p[1]=60; sqListB.p[2]=50; sqListA.length=6; sqListB.length=3; for (int i=0;i<sqListA.length;) { int flag=0; for (int j=0;j<sqListB.length;j++) { if (sqListA.p[i]==sqListB.p[j]) { flag=1; deleteList(sqListA,i); break; } } if (flag==0) i++; } for (i=0;i<sqListA.length;i++) { cout<<sqListA.p[i]<<endl; } destroyList(sqListA); destroyList(sqListB); }

转载于:https://www.cnblogs.com/GoAhead/archive/2012/08/23/2652014.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值