1.顺序表创建和就地逆置
Status ListCreate_Sq(SqList &L) {
L.elem = (ElemType*)malloc(sizeof(ElemType)*LIST_INIT_SIZE);
if(!L.elem)
exit(OVERFLOW);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
int num;
scanf("%d",&num);
if(num <= 0)
return ERROR;
while(num--){
int number;
scanf("%d",&number);
L.elem[L.length++] = number;
}
return OK;
};
void ListReverse_Sq(SqList &L) {
int tp;
for (int i = 0; i < L.length / 2; i++) {
tp = L.elem[i];
L.elem[i] = L.elem[L.length - i - 1];
L.elem[L.length - i - 1] = tp;
}
}
2.二叉树求深度和叶子数
int GetDepthOfBiTree ( BiTree T){
if(T == NULL)
return 0;//空树返回0
else{
int m = 0;
int n = 0;
m = GetDepthOfBiTree(T->lchild);
n = GetDepthOfBiTree(T->rchild);
if(m > n)
return m+1;//加根节点
else
return n+1;
}
}
int LeafCount(BiTree T){
if(T == NULL)
return 0;
if(T->lchild == NULL && T->rchild == NULL)
return 1;//如果是叶子结点返回1
else
return LeafCount(T->lchild)+LeafCount(T->rchild);
}
3.单链表元素定位
int ListLocate_L(LinkList L, ElemType x){
int cnt=0;
if(L==NULL)return 0;
else{
while(L!=NULL){
if(L->data==x)return cnt;
cnt++;
L=L->next;
}
}
}
4.计算二叉树的深度
int GetDepthOfBiTree ( BiTree T){
if(T==NULL)return 0;
else{
int m=GetDepthOfBiTree(T->lchild);
int n=GetDepthOfBiTree(T->rchild);
if(m>n)return m+1;
else return n+1;
}
}
5. 单链表中数据逆置
void reverse(LinkList H){
if (H->next == NULL) return;
LinkList now, p;
now = H->next;
while (now->next)
{
p = now->next;
now->next = p->next;
p->next = H->next;
H->next = p;//每一次都将当前的p插前面去。
}
}
6.求单链表表长
int Length_LinkList(LinkList L){
if(L==NULL)return 0;
int cnt=-1;
while(L!=NULL){
L=L->next;
cnt++;
}
return cnt;
}
7.有序数组的插入
bool Insert( List L, ElementType X ){
int i=0,tag;
while(L->Data[i]>X){
i++;
}
tag=i;
if((L->Data[tag]==X)||(L->Last>=MAXSIZE-1)) return false;
for(i=L->Last;i>=tag;i--){
L->Data[i+1]=L->Data[i];
}
L->Data[tag]=X;
L->Last++;
return true;
}
8.有向图出度为0的顶点个数
int GetCount(MGraph G){
int sum=0;
for(int i=0;i<G.vexnum;i++){
int od=0;
for(int j=0;j<G.vexnum;j++){
if(G.arcs[i][j]==1)od++;
}
if(od==0)sum++;
}
return sum;
}