(1);
}
lnode*q=p->next;
p->next=q->next;
deleteq;
}
voidsortlist(lnode*hl,intk)
{lnode*head=newlnode;head->next=NULL;head->data=hl->next->data;for(lnode*p=hl->next->next;p;p=p->next)
{lnode*q=newlnode;
q->data=p->data;
lnode*cp=head;
lnode*ap=NULL;
while(cp)
{if(k==1)
{if(q->datadata)
break;
else
{ap=cp;cp=cp->next;}
}
else
{if(q->data>cp->data)break;
else
{ap=cp;cp=cp->next;}
}
}
if(ap==NULL)
{q->next=head;head=q;}else
{q->next=cp;ap->next=q;}}
lnode*r=newlnode;r->next=head;
head=r;
looklist(head);
clearlist(head);
}
voidmain()
{
lnode*hl;
initlist(hl);
inti;
elemtypex;
cout<<"inputin5num:
";
for(i=0;i<5;i++)
{cin>>x;
insertrear(hl,x);
}cout<<"input2num:
";cin>>x;insertlist(hl,x,1);cin>>x;insertlist(hl,x,1);looklist(hl);sortlist(hl,1);sortlist(hl,0);
}
栈基本操作
算法四
1、目的:
掌握栈的存储表示方式和栈基本操作的实现方法
2、要求:
栈的基本操作实现方法,栈的应用
3、内容:
栈的实现栈的顺序存储。
#include
#include
#include
#defineERROR0
#defineTRUE1
#defineFALSE0
#defineOK1
#defineEQUAL1
#defineOVERFLOW-1
#defineSTACK_INIT_SIZE100
#defineSTACKINCREMENT10
typedefintStatus;
structSTU{
charname[20];
charstuno[10];
intage;
intscore;
};
typedefstructSTUSElemType;
structSTACK
{
SElemType*base;
SElemType*top;
intstacksize;
};
typedefstructSTACKSqStack;
typedefstructSTACK*pSqstack;
StatusInitStack(SqStack**S);
StatusDestroyStack(SqStack*S);
StatusClearStack(SqStack*S);
StatusStackEmpty(SqStackS);
intStackLength(SqStackS);
StatusGetTop(SqStackS,SElemType*e);
StatusPush(SqStack*S,SElemTypee);
StatusPop(SqStack*S,SElemType*e);
StatusStackTraverse(SqStackS,Status(*visit)());
StatusInitStack(SqStack**S)
{
(*S)=(SqStack*)malloc(sizeof(SqStack));
(*S)->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!
(*S)->base)exit(OVERFLOW);
(*S)->top=(*S)->base;
(*S)->stacksize=STACK_INIT_SIZE;
returnOK;
}
StatusDestroyStack(SqStack*S)
{
free(S->base);free(S);
}
StatusClearStack(SqStack*S)
{
S->top=S->base;
}
StatusStackEmpty(SqStackS)
{
if(S.top==S.base)returnTRUE;
else
returnFALSE;
}
intStackLength(SqStackS)
{
inti;
SElemType*p;
i=0;
p=S.top;
while(p!
=S.base)
{p++;
i++;
}
}
StatusGetTop(SqStackS,SElemType*e){
if(S.top==S.base)returnERROR;*e=*(S.top-1);
returnOK;
}
StatusPush(SqStack*S,SElemTypee)
{
/*
if(S->top-S->base>=S->stacksize)
{
S->base=(SElemType*)realloc(S->base,
(S->stacksize+STACKINCREMENT)*sizeof(SElemType));if(!
S->base)exit(OVERFLOW);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*/
*(S->top++)=e;
returnOK;
}
StatusPop(SqStack*S,SElemType*e)
{
if(S->top==S->base)returnERROR;
*e=*--S->top;
returnOK;
}
StatusStackPrintElem(SElemType*e)
{
printf("%s%s%d%d\n",e->name,e->stuno,e->age,e->score);
}
StatusStackTraverse(SqStackS,Status(*visit)())
{
while(S.top!
=S.base)
visit(--S.top);
}
main()
{
SElemTypee;
SqStack*Sa;
clrscr();
printf("\n\nSqStackDemoisrunning...\n\n");
printf("FirstisPushfunction.\n");
InitStack(&Sa);
strcpy(e.name,"stu1");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
printf("NowStackisEmpty.\n");
StackTraverse(*Sa,StackPrintElem);
Push(Sa,e);
printf("NowStackhasoneelement.\n");
StackTraverse(*Sa,StackPrintElem);
strcpy(e.name,"stu3");
strcpy(e.stuno,"100002");
e.age=80;
e.score=1000;
Push(Sa,e);
printf("