C语言节课作业200行,急求啊,写一个200行的C语言程序,什么都可以,带解释,哪位大神帮帮忙...

匿名用户

1级

2013-06-30 回答

#include #define OK 1

#define ERROR 0

#define TRUE 1

#define FALSE 0#define MAXSIZE 100#define LElemType int

#define Status int

#define BOOL inttypedef struct

{

LElemType data;

int cur;

}Component,SLinkList[MAXSIZE];int Malloc(SLinkList space)

{

//若备用链表非空,则返回分配的结点下标(备用链表的第一个结点),否则返回0

int i=space[0].cur;

if (i)

space[0].cur=space[i].cur;

return i;

}Status Free(SLinkList space, int k)

{

//将下标为空的空闲结点回收到备用链表(成为备用链表的第一个结点)

space[k].cur=space[0].cur;

space[0].cur=k;

return OK;

}Status InitList(SLinkList L)

{

//构造一个空的链表L,表头为L的最后一个单元L[MAXSIZE-1],其余单元链成

//一个备用链表,表头为L的第一个单元L[0],“0”表示空指针

int i;

L[MAXSIZE-1].cur=0;

for (i=0;i

L[i].cur=i+1;

L[MAXSIZE-2].cur=0;

return OK;

}Status ClearList(SLinkList L)

{

//初始条件:线性表L已存在。操作结果:将L重置为空表

int i,j,k;

i=L[MAXSIZE-1].cur;

L[MAXSIZE-1].cur=0;

k=L[0].cur;

L[0].cur=i;

while (i)

{

j=i;

i=L[i].cur;

}

L[j].cur=k;

return OK;

}BOOL ListEmpty(SLinkList L)

{

//若L是空表,返回TRUE;否则返回FALSE

if (!L[MAXSIZE-1].cur)

return TRUE;

else

return FALSE;

}int ListLength(SLinkList L)

{

//返回L中数据元素个数

int i,len;

i=L[MAXSIZE-1].cur;

len=0;

while (i)

{

i=L[i].cur;

len++;

}

return len;

}Status GetElem(SLinkList L,int i,LElemType *e)

{

//用e返回L中第i个元素的值

int j,k=MAXSIZE-1;

if (i<1||i>ListLength(L))

return ERROR;

for (j=1;j<=i;j++)

k=L[k].cur;

*e=L[k].data;

return OK;

}int LocateElem(SLinkList L,LElemType e)

{

//在静态单链线性表L中查找第1个值为e的元素。若找到,则返回它在L中的

//位序,否则返回0。(与其它LocateElem()的定义不同)

int i=L[MAXSIZE-1].cur;

while (i&&L[i].data!=e)

i=L[i].cur;

return i;

}Status PriorElem(SLinkList L,LElemType cur_e,LElemType *pre_e)

{

//初始条件:线性表L已存在

//操作结果:若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱,

//          否则操作失败,pre_e无定义

int i,j;

i=L[MAXSIZE-1].cur;

do{

j=i;

i=L[i].cur;

}while (i&&L[i].data!=cur_e);

if (i)

{

*pre_e=L[j].data;

return OK;

}

return ERROR;

}Status NextElem(SLinkList L,LElemType cur_e,LElemType *next_e)

{

//初始条件:线性表L已存在

//操作结果:若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,

//          否则操作失败,next_e无定义

int i,j;

i=LocateElem(L,cur_e);

if (i)

{

j=L[i].cur;

if (j)

{

*next_e=L[j].data;

return OK;

}

}

return ERROR;

}Status ListInsert(SLinkList L,int i,LElemType e)

{

//在L中第i个元素之前插入新的数据元素e

int j,k,l;

k=MAXSIZE-1;

if (i<1||i>ListLength(L)+1)

return ERROR;

j=Malloc(L);

if (j)

{

L[j].data=e;

for(l=1;l<=i-1;l++)

k=L[k].cur;

L[j].cur=L[k].cur;

L[k].cur=j;

return OK;

}

return ERROR;

}Status ListDelete(SLinkList L,int i,LElemType *e)

{

//删除在L中第i个数据元素e,并返回其值

int j,k;

if (i<1||i>ListLength(L))

return ERROR;

k=MAXSIZE-1;

for (j=1;j<=i-1;j++)

k=L[k].cur;

j=L[k].cur;

L[k].cur=L[j].cur;

*e=L[j].data;

Free(L,j);

return OK;

}Status ListTraverse(SLinkList L,void (* visit)(LElemType e))

{

int i=L[MAXSIZE-1].cur;

while (i)

{

visit(L[i].data);

i=L[i].cur;

}

return OK;

}void Visit(LElemType e)

{

printf("%d\n",e);

}int main()

{

int i,j,k;

LElemType e,e0;

SLinkList L;

InitList(L);

for(j=1;j<=5;j++)

i=ListInsert(L,1,j);

ListTraverse(L,Visit);

//判断链表是否为空

i=ListEmpty(L);

printf("%d\n",i);

//打印链表长度

printf("%d\n",ListLength(L));

//清空静态链表

ClearList(L);

ListTraverse(L,Visit);

for(j=1;j<=10;j++)

ListInsert(L,j,j);

//插入新节点后

ListTraverse(L,Visit);

//获取链表中的第5个元素

GetElem(L,5,&e);

printf("%d\n",e);

for(j=0;j<=1;j++)

{

k=LocateElem(L,j);

if(k)

printf("%d %d\n",j,k);//j在链表中的序号k

else

printf("Can't find %d\n",j);//链表中不存在j

}

for(j=1;j<=2;j++) //测试头两个数据

{

GetElem(L,j,&e0); //把第j个数据赋给e0

i=PriorElem(L,e0,&e); //求e0的前驱

if(!i)

printf("No elem before %d\n",e0);

else

printf("Elem before %d is %d\n",e0,e);//数据e0的前驱

}

for(j=ListLength(L)-1;j<=ListLength(L);j++) //最后两个数据

{

GetElem(L,j,&e0); //把第j个数据赋给e0

i=NextElem(L,e0,&e); //求e0的后继

if(!i)

printf("No elem after %d\n",e0);

else

printf("The elem after % is %d\n",e0,e);//数据e0的后继

}

k=ListLength(L); //k为表长

for(j=k+1;j>=k;j--)

{

i=ListDelete(L,j,&e); //删除第j个数据

if(i)

printf("Delete Succussfully\n");

else

printf("Can't find the elem\n",j);

}

ListTraverse(L,Visit);

return 0;

}给你找了个静态链表的代码,能编译运行

追问:

大神,灰常感谢啊,麻烦能不能说一下这个程序的目的和思路啊,要交报告的

追答:

自行百度静态链表吧,这个太烦了,说不清楚

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值