线性表
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ERROR 0
#define OK 1
#define OVERFLOW -2
#include <iostream>
#include <malloc.h>
#include <stdlib.h>
using namespace std;
struct sqlist
{
int *elem;
int length;
int listsize;
};
int InitList_Sq(sqlist &L)
{
L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L.elem)
exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
int listinsert(sqlist &L,int i,int e)
{
int *newbase,*p,*q;
if(i<1||i>L.length+1)return ERROR;
if(L.length>=L.listsize)
{
newbase=(int *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(int));
if(!newbase)return OVERFLOW;
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;
}
int locate(sqlist L,int e)
{
int i,*p;
i=1;p=L.elem;
while(i<=L.length && e!=*p)
{
++i;
++p;
}
if(i<=L.length)return i;
else return 0;
}
void unionmerge(sqlist &la,sqlist lb)
{
int i,e;
for(i=0;i<lb.length;i++)
{
e=*(lb.elem+i);
if(!locate(la,e))
listinsert(la,la.length+1,e);
}
}
int ListDelete_Sq(sqlist &L,int i,int &e)
{
int *p,*q;
if(i<1||(i>L.length))
return ERROR;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)*(p-1)=*p;
--L.length;
return OK;
}
void main()
{
int i,e;
sqlist la;
int List1 [] ={1,2,3};
InitList_Sq(la);
for(i=0;i<3;i++)
listinsert(la,i+1,List1[i]);
cout<<"第一个集合la中的"<<la.length<<"个元素依次是";
for(i=0;i<la.length;i++)
cout<<*(la.elem+i)<<" ";
cout<<endl;
cout<<"请输入要删除第几个元素";
cin>>i;
if(ListDelete_Sq(la,i,e))
cout<<"被删除的元素是:"<<e<<endl;
cout<<"删除后集合la中的"<<la.length<<"个元素依次是";
for(i=0;i<la.length;i++)
cout<<*(la.elem+i)<<" ";
cout<<endl;
system("pause");
cout<<endl;
}

浙公网安备 33010602011771号