第三周-删除线性表的元素
2015-10-04 17:49
330 查看
#include <stdio.h> #include <malloc.h> typedef int ElemType; #define MaxSize 50 typedef struct { ElemType data[MaxSize]; int length; } SqList; //用数组创建线性表 void CreateList(SqList *&L, ElemType a[], int n) { int i; L=(SqList *)malloc(sizeof(SqList)); for (i=0; i<n; i++) L->data[i]=a[i]; L->length=n; } bool ListEmpty(SqList *L) { return(L->length==0); } void DispList(SqList *L) { int i; if (ListEmpty(L)) return; for (i=0; i<L->length; i++) printf("%d ",L->data[i]); printf("\n"); } void delx2y(SqList *&L, ElemType x, ElemType y) { int k=0,i; //k记录非x的元素个数 ElemType t; if(x>y) { t=x; x=y; y=t; } for (i=0; i<L->length; i++) if (L->data[i]<x || L->data[i]>y ) //复制不在[x, y]之间的元素 { L->data[k]=L->data[i]; k++; } L->length=k; } //用main写测试代码 int main() { SqList *sq; ElemType a[10]= {5,8,7,0,2,4,9,6,7,3}; CreateList(sq, a, 10); printf("删除前 "); DispList(sq); delx2y(sq, 4, 7); printf("删除后 "); DispList(sq); return 0; }
相关文章推荐
- NOIP四校联训Round2小结
- CentOS7部署LAMP之phpMyAdmin、wordpress、Discuz
- 表达式求值
- SQL Server解决分页问题(三种方法)
- 《测试驱动开发―31重构》
- SpringMVC 整合 hibernate 报错:No Session found for current thread
- 第三周-求集合并集
- 程序员面试智力题集锦
- 才发现,温度能左右我的心情。
- Android自定义权限(从一个Demo主界面跳转到另外一个Demo的主界面)、及四大组件的声明
- C++引用
- Java线程小结
- 排序
- centos7没有安装ifconfig命令的解决方法
- Android:Material Design详解
- Junit测试框架 Tips
- [java学习笔记]java语言基础概述之函数的定义和使用&函数传值问题
- (数据挖掘-入门-4)基于物品的协同过滤
- 读取ip比如“192.168.1.2”并且提取出里面各个字段的数字
- Java Evaluate Reverse Polish Notation(逆波兰式)