第三周项目1 顺序表的基本运算
2015-09-21 16:30
453 查看
#ifndef LIST_H_INCLUDED #define LIST_H_INCLUDED #include <stdio.h> #include <malloc.h> #define MaxSize 50 typedef int ElemType; typedef struct { ElemType data[MaxSize]; int length; } SqList; void CreateList(SqList *&L, ElemType a[], int n); bool ListEmpty(SqList *L); void DispList(SqList *L); #endif int main() { SqList *sq; ElemType x[8]= {1,9,9,6,0,6,0,1}; CreateList(sq, x, 8); if((ListEmpty(sq))>0) printf("是空表\n"); else printf("不是空表\n"); DispList(sq); return 0; } 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"); }
#ifndef LIST_H_INCLUDED #define LIST_H_INCLUDED #include <stdio.h> #include <malloc.h> #define MaxSize 50 typedef int ElemType; typedef struct { ElemType data[MaxSize]; int length; } SqList; void CreateList(SqList *&L, ElemType a[], int n); bool ListEmpty(SqList *L); void DispList(SqList *L); int ListLength(SqList *L); bool GetElem(SqList *L,int i,ElemType &e); int LocateElem(SqList *L, ElemType e); #endif int main() { SqList *sq; int k; ElemType E; ElemType x[8]= {1,9,9,6,0,5,0,1}; CreateList(sq, x, 8); printf("表长度:%d\n", ListLength(sq)); if(GetElem(sq, 3, E)) printf("找到了第3个元素值为:%d\n", E); else printf("第3个元素超出范围!\n"); if(GetElem(sq, 15, E)) printf("找到了第15个元素值为:%d\n", E); else printf("第15个元素超出范围!\n"); if((k=LocateElem(sq, 1))>0) printf("找到了,值为1的元素是第 %d 个\n", k); else printf("值为1的元素木有找到!\n"); if((k=LocateElem(sq, 16))>0) printf("找到了,值为16的元素是第 %d 个\n", k); else printf("值为16的元素木有找到!\n"); return 0; } 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); } int ListLength(SqList *L) { return(L->length); } void DispList(SqList *L) { int i; if (ListEmpty(L)) return; for (i=0; i<L->length; i++) printf("%d ",L->data[i]); printf("\n"); } bool GetElem(SqList *L,int i,ElemType &e) { if (i<1 || i>L->length) return false; e=L->data[i-1]; return true; } int LocateElem(SqList *L, ElemType e) { int i=0; while (i<L->length && L->data[i]!=e) i++; if (i>=L->length) return 0; else return i+1; }
#ifndef LIST_H_INCLUDED #define LIST_H_INCLUDED #include <stdio.h> #include <malloc.h> #define MaxSize 50 typedef int ElemType; typedef struct { ElemType data[MaxSize]; int length; } SqList; void DispList(SqList *L); bool ListEmpty(SqList *L); void InitList(SqList *&L); void DestroyList(SqList *&L); bool ListInsert(SqList *&L,int i,ElemType e); bool ListDelete(SqList *&L,int i,ElemType &e); #endif int main() { SqList *sq; ElemType E; printf("初始化线性表\n"); InitList(sq); printf("在第1个位置插入元素1\n"); ListInsert(sq, 1, 1); DispList(sq); printf("在第2个位置插入元素6\n"); ListInsert(sq, 2, 6); DispList(sq); printf("在第1个位置插入元素9\n"); ListInsert(sq, 1, 9); DispList(sq); printf("删除第2个位置的元素\n"); ListDelete(sq,2,E); DispList(sq); printf("销毁线性表\n"); DestroyList(sq); DispList(sq); return 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"); } bool ListEmpty(SqList *L) { return(L->length==0); } void InitList(SqList *&L) { L=(SqList *)malloc(sizeof(SqList)); L->length=0; } void DestroyList(SqList *&L) { free(L); } bool ListInsert(SqList *&L,int i,ElemType e) { int j; if (i<1 || i>L->length+1) return false; i--; for (j=L->length; j>i; j--) L->data[j]=L->data[j-1]; L->data[i]=e; L->length++; return true; } bool ListDelete(SqList *&L,int i,ElemType &e) { int j; if (i<1 || i>L->length) return false; i--; e=L->data[i]; for (j=i; j<L->length-1; j++) L->data[j]=L->data[j+1]; L->length--; return true; }
相关文章推荐
- 第四周项目一
- Spring 事务管理
- 【Android笔记】执行命令行语句
- 开源中国源码学习UI篇(一)之FragmentTabHost的使用分析
- mysql编译问题集
- 常见SQL注入函数
- 【Android笔记】执行命令行语句
- javascript中的prototype和constructor
- 【碰撞检测】
- 第3周实践项目-3
- H5学习之旅-xhtml语法(15)
- 第三周项目3-求集合并集
- Android新开发语言Sky:取代Java 告别卡顿
- 第四周--建设“单链表”算法库
- 第四周—项目1 建立单链表
- H5学习之旅-xhtml语法(15)
- H5学习之旅-xhtml语法(15)
- 分布式学习的故事
- 数据结构顺序查找与二分法
- select、poll、epoll之间的区别总结[整理]