首页 > Sentiment > 实验四 线性表的顺序表示和实现

实验四 线性表的顺序表示和实现

2008年3月20日 timmy 发表评论 阅读评论 673 views

test4.cpp

#include
#include
#include
typedef int ElemType;
#define MAXSize 10;
#include “SeqList.h”
void main(void)
{
SeqList myList;
int i=1, x, sum=0, n;

InitList (myList);
scanf(“%d”, &x);
while ( x!= -1 )
{
if (InsertList (myList,x,i)==0) {
printf(“错误!\n”);
return ;
}
i++;
scanf(“%d”, &x);
}
n = LengthList (myList);
for (i=1; i<=n; i++)
{
x=GetList(myList, i);
sum =sum+ x;
}
printf(“%d\n “, sum);
if(DeleteElem(myList,10,100)==true){
printf(“After Deleting the digits between 10-100:”);
n = LengthList (myList);
for (i=1; i cout< }
cout< }
ClearList(myList);
}

seqlist.h

typedef struct List{
ElemType *list;
int size;
int MaxSize;
}SeqList;

void InitList(SeqList &L)
{ //初始化线性表
L.MaxSize=10;
L.list=new ElemType[L.MaxSize];
if(L.list==NULL){
cout<<"exit running"< exit(1);
}
L.size=0;
}

void ClearList(SeqList &L)
{ //清除线性表
if(L.list!=NULL){
delete []L.list;
L.list=NULL;
}
L.MaxSize=0;
L.size=0;
}

int LengthList(SeqList L)
{ //求线性表长度
return L.size;
}

bool InsertList(SeqList &L, ElemType item, int pos)
{ //按给定条件pos向线性表插入一个元素
if(pos<-1||pos>L.size+1){
cout<<"pos is unavailible"< return false;
}
int i;
if(pos==0){
for(i=0;i if(item break;
pos=i+1;
}
else if(pos==-1)
pos=L.size+1;
if(L.size==L.MaxSize){
int k=sizeof(ElemType);
L.list=(ElemType*) realloc(L.list,2*L.MaxSize*k);
if(L.list==NULL){
cout<<"exit running"< exit(1);
}
L.MaxSize=2*L.MaxSize;
}
for(i=L.size-1;i>=pos-1;i–)
L.list[i+1]=L.list[i];
L.list[pos-1]=item;
L.size++;
return true;
}

ElemType GetList(SeqList L, int pos)
{ //在线性表L中求序号为pos的元素,该元素作为函数值返回
if(pos<1||pos>L.size)
{
cerr<<"pos is out range"< exit(1);
}
return L.list[pos-1];
}

bool DeleteList(SeqList &L, ElemType& item, int pos)
{
if(L.size==0){
cout<<"the list is null"< return false;
}
if(pos<-1||pos>L.size){
cout<<"pos is unavailible"< return false;
}
int i;
if(pos==0){
for(i=0;i if(item==L.list[i])
break;
if(i==L.size)
return false;
pos=i+1;
}
else if(pos==-1) pos=L.size;
item=L.list[pos-1];
for(i=pos;i L.list[i-1]=L.list[i];
L.list–;
if(float(L.size)/L.MaxSize<0.4&&L.MaxSize>10){
int k=sizeof(ElemType);
L.list=(ElemType*) realloc(L.list,L.MaxSize*k/2);
L.MaxSize=L.MaxSize/2;
}
return true;
}

bool DeleteElem(SeqList &L, int min, int max)
{
if(L.size==0){
cout<<"the list is null"< return false;
}
int i,j;
for(i=0;i if(L.list[i]>=min&&L.list[i]<=max){
for(j=i;j L.list[j]=L.list[j+1];
L.size–;
}
if(float(L.size)/L.MaxSize<0.4&&L.MaxSize>10){
int k=sizeof(ElemType);
L.list=(ElemType*) realloc(L.list,L.MaxSize*k/2);
L.MaxSize=L.MaxSize/2;
}
return true;
}


[sfile]http://imtimmy.com/wp-content/boblog/attachment/200803/1205986140_1851b97e.rar[/sfile]

若日志经rss订阅或导入到外站,可能有些视频和图片无法显示,请点击原文链接查看。
本文链接地址: http://imtimmy.com/%E5%AE%9E%E9%AA%8C%E5%9B%9B%E7%BA%BF%E6%80%A7%E8%A1%A8%E7%9A%84%E9%A1%BA%E5%BA%8F%E8%A1%A8%E7%A4%BA%E5%92%8C%E5%AE%9E%E7%8E%B0/

转载请注明: 转载自Timmy's Blog

如果你觉得本博内容不错,欢迎 [订阅 Timmy's Blog],以便第一时间了解本博更新内容;


不妨再看看这些相关的日志:

  1. 实验5__线性表的链式表示和实现
  2. 实验4 栈的应用-算术表达式计算
  3. 实验八 队列的基本操作以及舞伴配对问题
  4. 实验七 栈的基本操作
  5. 实验二  抽象数据类型的表示和实现

分类: Sentiment 标签: ,
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :cool: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O