#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#define MAXSIZE 11
#define OK 1
#define ERROR 0
typedef char ElemType[20];
void ElemInput(ElemType *ep)
{
scanf("%s",ep);
}
void ElemOutput(ElemType e)
{
printf("%-8s",e);
}
int ElemCmp(ElemType a, ElemType b)
{
return strcmp(a,b);
}
void ElemCpy(ElemType *a, ElemType b)
{
return strcpy(*a, b);
}
typedef int Status;
typedef struct SLNode
{
ElemType data;
int cur;//next
}SLNode;
typedef struct
{
SLNode *space;
int freehead;
int datahead;
}StaticList;
Status MallocSL(StaticList *Sp, int *newnode)
{
*newnode = Sp->space[Sp->freehead].cur;
if(*newnode)
{
Sp->space[Sp->freehead].cur=Sp->space[*newnode].cur;
return OK;
}
return ERROR;
}
Status FreeSL(StaticList *Sp, int deletenode)
{
Sp->space[deletenode].cur = Sp->space[Sp->freehead].cur;
Sp->space[Sp->freehead].cur = deletenode;
return OK;
}
Status InitSL(StaticList *Sp)
{
Sp->space = (SLNode *)malloc(sizeof(SLNode)*MAXSIZE);
int i;
if(!Sp->space)
return ERROR;
for(i=0;i<MAXSIZE;i++)
{
Sp->space[i].cur = (i+1)%MAXSIZE;
ElemCpy(&Sp->space[i].data, "");
}
Sp->freehead = 0;
if(MallocSL(Sp, &Sp->datahead)==ERROR)
{
return ERROR;
}
Sp->space[Sp->datahead].cur=0;
return OK;
}
Status DestorySL(StaticList *Sp)
{
if(Sp->space)
return ERROR;
free(Sp->space);
Sp->space = NULL;
}
Status InsertSL(StaticList *Sp, int i, ElemType e)
{
int j=0,p=Sp->datahead, newnode;
for(j=0;j<i-1;j++)
{
p = Sp->space[p].cur;
if(!p)
return ERROR;
}
if(!MallocSL(Sp, &newnode))
return ERROR;
ElemCpy(&Sp->space[newnode].data , e);
Sp->space[newnode].cur = Sp->space[p].cur;
Sp->space[p].cur = newnode;
return OK;
}
Status DeleteSL(StaticList *Sp, int i, ElemType *ep)
{
int j=0;
int p = Sp->datahead,deletenode;
for(j=0;j<i-1;j++)
{
p = Sp->space[p].cur;
if(!p || !Sp->space[p].cur)
{
return ERROR;
}
deletenode = Sp->space[p].cur;
Sp->space[p].cur=Sp->space[deletenode].cur;
ElemCpy(ep, Sp->space[deletenode].data);
FreeSL(Sp,deletenode);
return OK;
}
}
Status Search(StaticList S, ElemType e, int *locp)
{
int p = S.space[S.datahead].cur,cnt=1;
while(p)
{
if(ElemCmp(S.space[p].data,e)==0)
{
*locp = cnt;
return OK;
}
cnt++;
p=S.space[p].cur;
}
*locp = 0;
return ERROR;
}
Status Show(StaticList S)
{
int i;
for(i=0;i<MAXSIZE;i++)
{
ElemOutput(S.space[i].data);
printf("%2d\n",S.space[i].cur);
}
printf("*******************\n");
return 0;
}
int main()
{
int i;
ElemType e;
StaticList S;
InitSL(&S);
char op[20];
while(scanf("%s",op)!=EOF)
{
switch (op[2])
{
case 's':
//printf("请输入要插入的位置及元素值:")
scanf("%d%s",&i,&e);
InsertSL(&S,i,e);
break;
case 'l':
scanf("%d",&i);
DeleteSL(&S, i, &e);
break;
case 'o':
Show(S);
break;
default:
scanf("%s",&e);
Search(S,e,&i);
printf("%2d\n",i);
break;
}
}
}
DS||静态链表
最新推荐文章于 2022-06-17 23:17:21 发布

1756

被折叠的 条评论
为什么被折叠?



