#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
void InitList_Sq(SqList &L)
{
L.elem=(ElemType*)malloc(sizeof(ElemType)*LIST_INIT_SIZE);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return;
}
int Display_Sq(SqList &L)
{
printf("表中的数据元素为:\n");
int i;
for(i=0;i<L.length;i++)
{
printf("%d\t",L.elem[i]);
}
printf("\n");
}
void Destroy_Sq(SqList &L)
{
free(L.elem);
L.elem=NULL;
}
int main()
{
SqList(L);
InitList_Sq(L);
int n;
printf("请输入表的元素个数: ");
scanf("%d",&n);
int i;
for(i=0;i<n;i++)
{
printf("请输入元素:");
scanf("%d",&L.elem[i]);
L.length++;
}
Display_Sq(L);
Destroy_Sq(L);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
void InitList_Sq(SqList &L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
L.length=0;
L.listsize=LIST_INIT_SIZE;
return;
}
int GetElem_Sq(SqList L,int i,ElemType &e)
{
if(i<0||i>L.length-1)
{
return 0;
}
e=L.elem[i-1];
return 1;
}
int Display_Sq(SqList &L)
{
printf("表中的数据元素为:\n");
int i;
for(i=0;i<L.length;i++)
{
printf("%d\t",L.elem[i]);
}
printf("\n");
}
void Destroy_Sq(SqList &L)
{
free(L.elem);
L.elem=NULL;
return;
}
int main()
{
SqList(L);
InitList_Sq(L);
int n;
printf("表中元素的个数: ");
scanf("%d",&n);
int i;
for(i=0;i<n;i++)
{
printf("输入元素: ");
scanf("%d",&L.elem[i]);
L.length++;
}
Display_Sq(L);
int j,e;
printf("请输入第几个元素:");
scanf("%d",&j);
int result;
result=GetElem_Sq(L,j,e);
printf("%d\n",result);
printf("这个元素的位置:%d\n",e);
Destroy(L);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
void InitList_Sq(SqList &L)
{
L.elem=(ElemType*)malloc(sizeof(ElemType)*LIST_INIT_SIZE);
L.length=0;
return;
}
int display(SqList &L)
{
printf("表中的数据元素为:\n");
int i;
for(i=0;i<L.length;i++)
{
printf("%d\t",L.elem[i]);
}
printf("\n");
}
int LocateElem_Sq(SqList L,ElemType e,Status (*compare)(ElemType,ElemType))
{
int i=1;
int *p;
p=L.elem;
while(i<=L.length&&!(*compare)(*p++,e))
{
++i;
}
if(i<=L.length)
{
return i;
}
else
{
return 0;
}
}
void Destroy_Sq(SqList &L)
{
free(L.elem);
L.elem=NULL;
return;
}
int main()
{
SqList L;
InitList_Sq(L);
int n;
printf("请输入表的个数:");
scanf("%d",&n);
int i;
for(i=0;i<n;i++)
{
printf("请输入元素:");
scanf("%d",&L.elem[i]);
L.length++;
}
display(L);
int e;
printf("请输入要查找的元素e:");
scanf("%d",&e);
int result;
result=(L,e);
printf("输出下标:%d\n",result);
Destroy(L);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
#define OVERFLOW -2
#define LISTINCREMENT 10
typedef int ElemType;
typedef int Status;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
void InitList_Sq(SqList &L)
{
L.elem=(ElemType*)malloc(sizeof(ElemType)*LIST_INIT_SIZE);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return;
}
int Display_Sq(SqList &L)
{
printf("表中的数据元素为:\n");
int i;
for(i=0;i<L.length;i++)
{
printf("%d\t",L.elem[i]);
}
printf("\n");
}
int ListInsert_Sq(SqList &L,int i,ElemType e)
{
int *newbase;
int *q;
int *p;
if(i<1||(i>L.length+1))
{
return 0;
}
if(L.length>=L.listsize)
{
newbase=(ElemType*)realloc(L.elem,(LIST_INIT_SIZE+LISTINCREMENT)*sizeof(ElemType));
if(!newbase)
{
exit(OVERFLOW);
}
L.elem=newbase;
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 1;
}
void Destroy_Sq(SqList &L)
{
free(L.elem);
L.elem=NULL;
return;
}
int main()
{
SqList L;
InitList_Sq(L);
int n;
printf("请输入表的个数:");
scanf("%d",&n);
int i;
for(i=0;i<n;i++)
{
printf("请输入元素:");
scanf("%d",&L.elem[i]);
L.length++;
}
Display_Sq(L);
int e,j;
printf("输入要插入的元素:");
scanf("%d",&e);
printf("输入要插入的位置下标:");
scanf("%d",&j);
int result;
result=ListInsert_Sq(L,j,e);
printf("%d\n",result);
Display_Sq(L);
Destroy_Sq(L);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
void InitList_Sq(SqList &L)
{
L.elem=(ElemType*)malloc(sizeof(ElemType)*LIST_INIT_SIZE);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return;
}
int Display_Sq(SqList &L)
{
printf("输出表中的数据元素:\n");
int i=0;
int *p,*q;
for(i=0;i<L.length;i++)
{
printf("%d\t",L.elem[i]);
}
printf("\n");
}
int ListDelete_Sq(SqList &L,int i,ElemType &e)
{
int *p,*q;
if(i<1||(i>L.length))
{
return 0;
}
p=&L.elem[i-1];
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)
{
*p=*(p+1);
}
--L.length;
return 1;
}
void Destroy_Sq(SqList &L)
{
free(L.elem);
L.elem=NULL;
return;
}
int main()
{
SqList(L);
InitList_Sq(L);
int n;
printf("请输入数据元素:");
scanf("%d",&n);
int i;
for(i=0;i<n;i++)
{
printf("请输入数据元素:");
scanf("%d",&L.elem[i]);
++L.length;
}
Display_Sq(L);
int j,e;
printf("请输入删除元素的下标:");
scanf("%d",&j);
int result;
result=ListDelete_Sq(L,j,e);
printf("%d\n",result);
Display_Sq(L);
Destroy_Sq(L);
return 0;
}