DS||静态链表

#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;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值