数据结构基本操作源代码
[cpp] view plaincopyprint?****************************************************************
三元组
****************************************************************
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef ElemType *Triplet;
#include "h2.h"
Status InitTriplet(Triplet&T,ElemType v1,ElemType v2,ElemType v3)
{
T = (ElemType *)malloc(3*sizeof(ElemType));
if (!T) exit (OVERFLOW);
T[0] = v1; T[1] = v2; T[2] = v3;
return OK;
}
Status DestroyType(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 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;
}
Status Average(Triplet T,ElemType &e)
{
e = (T[0] + T[1] + T[2])/3;
return OK;
}
void main()
{
Triplet p;
ElemType e,v1,v2,v3;
int select,i;
printf("输入三个数并建立一个三元组\n");
scanf("%d%d%d",&v1,&v2,&v3);
if (InitTriplet(p,v1,v2,v3)==OVERFLOW)
printf("分配失败,退出程序!");
else
do
{
printf("1:取三元组第i个元素\n");
printf("2:求最大值\n");
printf("3:求最小值\n");
printf("4:求平均值\n");
printf("5:结束\n");
printf("输入你的选择\n");
scanf("%d",&select);
switch(select)
{ case 1:
printf("\ni=");
scanf("%d",&i);
if (Get(p,i,e)==ERROR) printf("i值不合法\n");
else printf("第%d个元素的值为:%d\n",i,e); break;
case 2:
Max(p,e);
printf("最大值是:%d\n",e);break;
case 3:
Min(p,e);
printf("最大值是:%d\n",e);break;
case 4:
Average(p,e);
printf("平均值是: %d\n",e);break;
case 0:
printf("操作结束!"); break;
default: printf("输入选择出错!\n");
}//end switch
}while(select!=0); //end of while
DestroyType(p);
}// end of main
****************************************************************************************************
顺序表
*****************************************************************************************************
#include <stdio.h>
#include <stdlib.h>
#define listinitsize 100
#define listincrement 10
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType *elem;
ElemType length;
ElemType listsize;
}Sqlist;
//.cpp
#include"h1.h"
Sqlist la;
ElemType *a;
ElemType e,k,pre_e,next_e,choice;
Status makelist(Sqlist &la)
{
ElemType i;
la.elem=(ElemType *)malloc(listinitsize *sizeof(ElemType));
if(!la.elem) exit(OVERFLOW);
la.length = 80;
la.listsize=listinitsize;
for(i=0 ; i<=la.length-1 ;i++)
la.elem[i]=i;
return OK;
}
Status listlengh(Sqlist &la)
{
return la.length ;
}
Statu
补充:软件开发 , C++ ,