顺序栈
1 2 3 4 5 6 7 8 9 10 11 12 13
| Status Push (SqStack *s , SElemType e)
{
if (S->top == MAXSIZE -1)
return error;
S->top ++;
s->data[S->top] = e;
}
|
出栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| Status Pop (SqStack *s , SElemType *e)
{
if(S->top == -1)
return error;
*e=s->data[S->top];
s->top --;
return ok;
}
|
链栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| Status Push(LinkStack *S , SElemType e)
{
LinkStackptr s = (LinkStackptr)malloc (sizeof(StackNode));
s->data =e;
s->next = S->top;
S-top =s;
S->count++
return ok;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| Status Pop(LinkStack *S, SElemType *e)
{
LInkStackptr p;
if(StackEmpty (*S))
return error;
*e = S->top->data;
p= S->top;
S->top = S->top->next;
free(p);
S->count -- ;
return ok;
}
|
树的基本操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| BtNode Creatree1(){
BtNode = NULL
ElemType data;
cin>>data;
if(data !="")
{
s = BtNode *s =(Btnode )malloc(sizeof(BtNode));
s->data = data;
s->left = CreatTree1();
s->right=CreatTree1();
}
return s;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| void DestroyBTree(BtNode *p)
{
if (p!=NULL)
{
DestroyBTress(p->left);
DestroyBTree(p->right);
FreeNode(p);
}
}
|
1 2 3 4 5 6 7 8 9
| int GetDepth(BtNode *ptr)
{
if (NULL == ptr) return 0;
else max(GetDepth(ptr -> lefrchild), GetDepth(ptr->rightchild))+1;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| {
if(ptr == NULL || ptr ->data ==x) return ptr;
else
{
BtNode * p = findValue(Ptr->left child,x)
if(NULL == p)
p= findVlaue(ptr -> rightchild .x);
return p;
}
}
|
排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| void SelectSort(SqList *L)
{
int i,j,min;
for (i = 1; i <L->length ; i++)
{
min = i ;
for( j=i+1; j<=L->length ; j ++)
{ if (L->r[min]>L-r[j])
min = j;
}
if(i!=min)
wap(L,i,min);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| void InsertSort(SqList *L)
{
int i,j;
for(i= 2; i <=L->length ; i++)
{
if (L->r[i]<L->r[i-1])
{
L->r[0] = L->r[i];
for(j=i ; L->r[j]>L->r[0] ; j--)
L->r[j+1] = L-r[j];
L->r[i+1] = L->r[0]
}
}
}
|
快速排序,和归并排序看一下 了解一下过程 ,15年以后的真题已经基本不再考排序算法的默写了