? ?
一.?实现三元组.
二.?实现顺序表.
? ?
1.实现三元组代码
#include<iostream.h>?
#include?<stdlib.h>?
typedef?int?ElemType;?
typedef?ElemType?*Triplet;?
typedef?int?Status;?
#define?OK?1?
#define?ERROR?0??
#define?OVERFLOW?-2
Status?InitTriplet?(Triplet?&t,ElemType?v1,ElemType?v2,ElemType?v3)?{
t=(ElemType?*)malloc(3*sizeof(ElemType));?
if(!t)?return?OVERFLOW;?
t[0]=v1;t[1]=v2;t[2]=v3;?
return?OK;?
}?
Status?DestroyTriplet?(Triplet?&t)?{
free(t);?t=NULL;?
return?OK;??
}?
Status?get(Triplet?t,int?i,ElemType?&e)?
{if?(i<1||i>3)?return?ERROR;?
e=t[i-1];?
return?OK;?}?
Status?put(Triplet?&t,int?i,ElemType?e)??
{if?(i<1||i>3)?return?ERROR;?
t[i-1]=e;?
return?OK;?}?
Status?IsAscend(Triplet?t)?
{return?(t[0]<t[1])&&(t[1]<t[2]);?}?
Status?Isdescending(Triplet?t){
return(t[0]>=t[1])&&(t[1]<=t[2]);}
Status?IsDescending(Triplet?t){
return(t[0]<=t[1])&&(t[1]>=t[2]);}
Status?Max(Triplet?t,ElemType?&e)?
{e=(t[0]>=t[1])?((t[0]>=t[2])?t[0]:t[2]):((t[1]>=t[2])?t[1]:t[2]);?
return?OK;??}
Status?Min(Triplet?t,ElemType?&e)
{e=(t[0]<=t[1])?((t[0]<=t[2])?t[0]:t[2]):((t[1]<=t[2])?t[1]:t[2]);
return?OK;}?
void?main()?
{?Triplet?T;?
ElemType?e,v1,v2,v3;?
int?i;?
char?select;?
cout<<"输入三个数,建立一个三元组"<<endl;?
cin>>v1>>v2>>v3;
if?(InitTriplet(T,v1,v2,v3)==OVERFLOW)?
cout<<"分配失败,退出程序!"<<endl;?
else?
do{cout<<"请输入1.查找?2.递增?3.最大值?4.置换?5.递减?6.最小值"<<endl;
cin>>select;
switch?(select)?
{case?'1':?cout<<"请输入i"<<endl;?cin>>i;if?(get(T,i,e)==ERROR)?cout<<"cuowu"<<endl;
else?cout<<"第i个元素的值为:"<<e<<endl;?break;?
case?'2':?if?(IsAscend(T)==1)?cout<<"三元组递增"<<endl;?
else?cout<<"三元组非递增n";?break;?
case?'3':?Max(T,e);?cout<<"最大值是:"<<e<<endl;break;?
case?'4':?cout<<"请输入i,e"<<endl;?cin>>i>>e;if?(put(T,i,e)==ERROR)?cout<<"i值不合法n";?
else?cout<<"置换第i个素后的3个元素分别为:"<<T[0]<<T[1]<<T[2]<<endl;break;?
case?'5':??if?(IsDescending(T)==1)?cout<<"三元组递减"<<endl;?
else?cout<<"三元组非递减n";?break;?
c++编程 数据结构实验报告代码_数据结构实验报告
case?'6':?Min(T,e);?cout<<"最小值是:"<<e<<endl;break;}
}while(select!='0');
}
2.实现顺序表
#include<stdio.h>
#include<malloc.h>
#include<iostream.h>
#define?List_init_size?100
#define?Listincrement?20
typedef?int?elemtype;
typedef?int?Status;
#define?OK??1
typedef?struct
{
elemtype?*elem;
int?length;
int?listsize;
}Sqlist;
Status?InitList_Sq(Sqlist?&L)
{
L.elem=(elemtype?*)malloc(List_init_size*sizeof(elemtype));
L.length=0;
L.listsize=List_init_size;
return?OK;
}
Status?ListCreate_Sq(Sqlist?&L,int?length)?{
int?x;
cout<<"请输入顺序表的长度:";
cin>>length;
cout<<"请输入顺序表:n";
for(int?i=1;i<=length;++i){
cin>>x;
L.elem[i-1]=x;
++L.length?;
}
return?OK;
}
void?print(Sqlist?&L){
int?i;
for(i=0;i<L.length;i++)
????cout<<L.elem[i]<<"?";
cout<<endl;
}
Status?ListInsert_Sq(Sqlist?&L,int?i,elemtype?e)
{
elemtype?*newbase,*q,*p;
if(i<0||i>L.length+1)
????return(0);
if(L.length>=L.listsize){
????newbase=(elemtype?*)realloc(L.elem,(L.listsize+Listincrement)*sizeof(elemtype));
L.elem=newbase;
L.listsize=L.listsize+Listincrement;}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)
????*(p+1)=*p;
*q=e;
++L.length;
return?OK;
}
Status?ListDelete_Sq(Sqlist?&L,int?i,int?e)
{
elemtype?*p,*q;
if(i<0||i>L.length)
????return?OK;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)?*(p-1)=*p;
????*q=*(q+1);
--L.length;
return?e;
}
int??LocateElem_Sq(Sqlist?L,elemtype?e){
int?i=1,*p;
p=L.elem;
while(i<=L.length)
{?if(L.elem[i-1]!=e)
???i++;
else
return?i;}
return?0;
}
void?main()
{
Sqlist?L;
int?length;
elemtype?e,i;
InitList_Sq(L);
ListCreate_Sq(L,length);
print(L);
cout<<"请输入要插入的元素及位置:";
cin>>e>>i;
ListInsert_Sq(L,i,e);
print(L);
cout<<"请输入要删除元素的位置:";
cin>>i;
ListDelete_Sq(L,i,e);
print(L);
cout<<"输入要找的元素";
cin>>e;
LocateElem_Sq(L,e);
}
? ?