#include <malloc.h>
#define MaxSize 50
typedef char ElemType
typedef struct
{
ElemType elem[MaxSize]
int length
}SqList
void InitList(SqList *L) /* 初始化顺序表 */
{
L=(SqList *)malloc(sizeof(SqList))
L->length=0
}
void CreateListR(SqList *L,ElemType a[],int n) /*创建顺序表 */
{
int i
for(i=0i<ni++)
L=(SqList *)malloc(sizeof(SqList))
L->elem[i]=a[i]
L->length++
}
void DispList(SqList *L) /* 输出顺序表 */
{
int i
if(ListEmpty(L)) return
for(i=0i<L->lengthi++)
printf("%c",L->elem[i])
printf("\n")
}
int ListLength(SqList *L) /* 求顺序表的长度 */
{
return(L->length)
}
int ListEmpty(SqList *L) /* 求顺序表是否为空 */
{
return(L->length==0)
}
int GetElem(SqList *L,int i,ElemType e) /*求顺序表中某个数据的元素值 */
{
if(i<1||i>L->length)
return 0
else
e=L->elem[i-1]
return 1
}
int LocateElem(SqList *L,ElemType e)/*按值查找元素*/
{
int i=0
while(i<L->length&&L->elem[i]!=e) i++
if(i>=L->length)
return 0
else
return i+1
}
int ListInsert(SqList *L,int i,ElemType e) /*按i位置插入元素*/
{
int j
if(i<1||i>L->length+1)
return 0
i--
for (j=L->lengthj>ij--)
L->elem[j]=L->elem[j-1]
L->elem[i]=e
L->length++
return 1
}
int ListDelete(SqList *L,int i,ElemType e) /*删除某个位置的元素*/
{
int j
if (i<1||i>L->length)
return 0
i--
e=L->elem[i] /*e中的放elem[i]有何用..以后没用到*/
for(j=ij<L->length-1j++)
L->elem[j]=L->elem[j+1]
L->length--
return 1
}
void DestroyList(SqList *L) /*销毁链表*/
{
free(L)
}
void main()
{
SqList L
ElemType a[]={'a','b','c','d'}
int c
int e
while(1)
{
printf("Please Choose the step you want ?\n\n")
scanf("%d",&c)
if(c==0) break
switch(c)
{
case 1: InitList(&L)break
case 2: CreateListR(&L,a,4)break
case 3: DispList(&L)break
case 4: printf("long %d", ListLength(&L))break
case 5: printf("%d",ListEmpty(&L))break
case 6: GetElem(&L,3,e)break
case 7: LocateElem(&L,'a')break
case 8: ListInsert(&L,4,'f')break
case 9: DispList(&L)break
case 10: ListDelete(&L,3,e)break
case 11: DispList(&L)break
case 12: DestroyList(&L)break
default: printf("error data")break
}
}
}
typedef struct{
int *elem
int length
int listsize
} Sqlist
status Create_sq(Sqlist *L,int n)
{
int i
L->elem=(int*)malloc(100*sizeof(int))
if(!L->elem) return 0
for(i=0i<ni++)
scanf("%d",&(L->elem[i]))
L->length=n
L->listsize=100
return 1
}
status Listinsert_sq(Sqlist *L,int i,int e)
{
int *q,*p,*newbase
if(i<1||i>L->length+1) return 0
if(L->length>=L->listsize)
{
newbase=(int*)realloc(L->elem,(L->listsize+10)*sizeof(int))
if(!newbase) exit(-2)
L->elem=newbase
L->listsize+=10
}
q=&(L->elem[i-1])
for(p=&(L->elem[L->length-1])p>=q--p)
*(p+1)=*p
*q=e
++L->length
return 1
}
int main()
{
Sqlist L1
int n,a
int i,e
printf("\n please input the number of data:\n")
scanf("%d",&n)
if(Create_sq(&L1,n)==1)
{
scanf("%d%d",&i,&e)
a=Listinsert_sq(&L1,i,e)
if(a==1)
printf("insert success\n")
else printf("insert false\n")
printf("the list elements are:\n")
for(i=1i<=L1.lengthi++)
{
printf("%d\t",L1.elem[i-1])
}
}
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)