// 00.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "stdlib.h"
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#include
#include
typedef char ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
int InitList_Sq(SqList &L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (!L.elem) return (0);
else
{ L.length=0;
L.listsize=LIST_INIT_SIZE;
return (1);
}
}
void ListInsert_Sq(SqList &L,int i,ElemType e)
{
ElemType *p,*q;
q=L.elem+(i-1);p=L.elem+L.length-1;
for(;p>=q;p--) *(p+1)=*p;
*q=e;
L.length++;
}
void DisplayList(SqList L) // 输出函数
{
int i;
printf("\n");
for(i=1;i<=L.length;i++)
printf(" %d -- %c\n",i,*(L.elem+i-1));
printf("\n");
}
int Listlength(SqList &L) //长度函数
{
return(L.length);
}
int LocateElem_Sq(SqList L,ElemType e) //判断位置的函数
{
int i;
for(i=1;i<=L.length;i++)
if(e==*(L.elem+i-1)) return i;
return 0;
}
char GetElem (SqList L,int i, ElemType &e)
{ElemType *p;
p=L.elem+(i-1);
e=*p;
return(e);
}
void ListDeleteList_Sq(SqList &L,int i,ElemType &e)
{
ElemType *q,*p;
q=L.elem+i-1;
p=L.elem+L.length-1;
e=*q;
for(;q<=p;q++)
*q=*(q+1);
L.length--;
}
int DestoryList(SqList& L)
{ free(L.elem);
L.elem=NULL;
L.length=0;
L.listsize=0;
return(1);
}
int ListEmpty(SqList L)
{if(L.length==0) return 0;
return 1;
}
int main(int argc, char* argv[])
{ SqList L;
int flag,i;
ElemType e;
flag=InitList_Sq(L);
if (!flag) printf("no\n");
else printf("分配成功\n");
char data[5]={'a','b','c','d','e'};
for(i=1;i<=5;i++)
ListInsert_Sq(L,i,data[i-1]);
DisplayList(L);
int m;
m=Listlength(L);
printf("表的长度为%d\n",m);
int t;
t=ListEmpty( L);
if(t!=0)
printf("\n该表非空\n");
else printf("\n该表为空\n");
printf("\n请输入要查找的字符 "); //判断位置的函数
char b;
scanf("%s",&b);
i=LocateElem_Sq( L,b);
if(i!=0)
printf("它所在的位置是%d\n",i);
else printf("对不起,你输入的字符不属于本顺序表\n");
int q;
printf("\n请输入你要查询的位置 " ); //根据位置找元素的函数
scanf("%d",&q);
char e1;
printf("该位置的元素为%c\n",GetElem ( L,q,e1));
printf("\n在第四个位置插入元素f");
ListInsert_Sq(L,4,'f'); //在第四个位置插入f
DisplayList(L);
printf("\n删除第三个元素");
ListDeleteList_Sq(L,3,e); //删除第三个元素
DisplayList(L);
DestoryList( L);
printf("释放后:L.elem=%u.length=%d%L.listsize=%d\n",L.elem,L.length,L.listsize);
return 0;
}
不会