数据结构第2章上机实验题2.1
来源:互联网 发布:唐青枫捏脸数据 编辑:程序博客网 时间:2024/06/05 00:22
问题描述:实现顺序表的基本运算(1-12具体要求见课本P62),作为对已经学过的顺序表的小revision~
源代码:
list.h:
#include <stdio.h>#include <malloc.h>#define max 100typedef char Elemtype;typedef struct list{ char data[max]; int length;} Sqlist;void Initlist(Sqlist *&l);void Createlist(Sqlist *&l,Elemtype a[],int n);void Displist(Sqlist *l);int Listlength(Sqlist *l);bool Listempty(Sqlist *l);void Dispelement(Sqlist *l,int loc);void Disploc(Sqlist *l,Elemtype e);bool Insertelement(Sqlist *&l,int loc,Elemtype e);bool Deleteelement(Sqlist *&l,int loc,Elemtype &e);void Destroylist(Sqlist *&l);
fun.cpp:
#include <stdio.h>#include "list.h"void Initlist(Sqlist *&l){ l=(Sqlist *)malloc(sizeof(Sqlist)); l->length=0;}void Createlist(Sqlist *&l,Elemtype a[],int n){ int i; l=(Sqlist *)malloc(sizeof(Sqlist)); for(i=n-1;i>=0;i--) l->data[i]=a[i]; l->length=n;}void Displist(Sqlist *l){ int i; for(i=0;i<l->length-1;i++) printf("%c ",l->data[i]); printf("%c\n",l->data[l->length-1]);}void Dispelement(Sqlist *l,int loc){ int i; for(i=0;i<l->length;i++) { if(i==loc-1) printf("%c\n",l->data[i]); }}void Disploc(Sqlist *l,Elemtype e){ int i; int flag=0; //判断是否找到,flag=1找到,flag=0未找到 for(i=0;i<l->length;i++) { if(l->data[i]==e) { flag++; printf("%d\n",i+1); } } if(flag==0) printf("未找到该元素\n");}int Listlength(Sqlist *l){ return (l->length);}bool Listempty(Sqlist *l){ if(l->length!=0) return true; else return false;}bool Insertelement(Sqlist *&l,int loc,Elemtype e){ int i; if(loc<1 || loc>l->length+1 || l->length==max) return false; loc--; for(i=l->length;i>loc;i--) l->data[i]=l->data[i-1]; l->data[loc]=e; l->length++; return true;}bool Deleteelement(Sqlist *&l,int loc,Elemtype &e){ int i; if(loc<1 || loc>l->length+1) return false; loc--; e=l->data[loc]; for(i=loc;i<l->length-1;i++) l->data[i]=l->data[i+1]; l->length--; return true;}void Destroylist(Sqlist *&l){ free(l);}
main.cpp:
#include <stdio.h>#include "list.h"int main(){ Sqlist *l; Elemtype a[5]={'a','b','c','d','e'}; //5个元素a,b,c,d,e Elemtype e; Initlist(l); //初始化顺序表L printf("顺序表已被初始化!\n"); if(Listempty(l)) //判断顺序表L是否为空 printf("此顺序表不为空\n"); else printf("此顺序表为空\n"); Createlist(l,a,5); //尾插法依次插入a,b,c,d,e printf("尾插法插入元素后输出的顺序表为:\n"); Displist(l); //输出顺序表L printf("此顺序表长度为:"); printf("%d\n",Listlength(l)); //输出顺序表L长度 if(Listempty(l)) //判断顺序表L是否为空 printf("此顺序表不为空\n"); else printf("此顺序表为空\n"); printf("顺序表L的第3个元素为:"); Dispelement(l,3); printf("元素a的位置为:"); Disploc(l,'a'); Insertelement(l,4,'f'); //在第4个元素位置上插入元素f printf("插入元素后的顺序表为:\n"); Displist(l); //输出顺序表L Deleteelement(l,3,e); //删除L的第3个元素 printf("删除元素后的顺序表为:\n"); Displist(l); //输出顺序表L Destroylist(l); //释放顺序表L printf("顺序表已被销毁!\n"); return 0;}
运行结果:
0 0
- 数据结构第2章上机实验题2.1
- 数据结构第1章上机实验题
- 数据结构第四章上机实验
- 数据结构第五版第一章上机实验题2
- 数据结构课程上机实验题(一)
- 数据结构课程上机实验题(二)
- 数据结构课程上机实验题(三)
- 数据结构上机实验学习体会
- 数据结构(实验一)第2题
- c++第1次上机实验-2
- c++第1次上机实验-2
- c++第1次上机实验-2
- 第1次上机实验-2
- 第1次上机实验-2
- 第1次上机实验—2
- 第1次上机实验-2
- C++第1-2次上机实验
- 第1次上机实验-2
- mysql主从复制错误sql-running no
- lightoj 1134 - Be Efficient
- Numpy和Matplotlib的安装
- 浅谈Linux内存管理机制
- HDU 5381 The sum of gcd(数论+莫队算法)
- 数据结构第2章上机实验题2.1
- [Machine Learning]---ROC(Receiver Operating Characteristic) Curve
- CSS学习笔记
- matlab自带滤波器函数小结(图像处理)
- 深度好文丨读完此文,就知道Hadoop了!
- Microsoft SQL Server 2008 R2完全卸载再安装手册
- android的四大组件及其生命周期
- VS2013连接sql2008的问题-Could not load file or assembly 'Microsoft.SqlServer.Manageme
- 证明的手段 —— 不失一般性的(WLOG)