中国大学MOOC PAT 浙大版《数据结构(第2版)》题目集(函数题1.8-6.2)

习题1.8 二分查找

Position BinarySearch( List L, ElementType X )
{
    Position l = 1, r = L->Last, mid;
    while (r - l >= 0)
    {
        mid = (l + r) / 2;
        if (L->Data[mid] > X)         // 中间位置的数大于要查找的数,那么在中间数的左区间找
            r = mid - 1;
        else if (L->Data[mid] == X)    //中间位置的数等于要查找的数
            return mid;
        else                                // 中间位置的数小于要查找的数,那么在中间数的右区间找
            l = mid + 1; 
    }
    return NotFound;
}

习题1.9 有序数组的插入

bool Insert( List L, ElementType X ){
    if(L->Last==MAXSIZE-1) //表中没有空位
         return false;

    for (int i=0; i<=L->Last; i++) { 
        if(L->Data[i]==X)  //x已经在Data[]中了
             return false;
           
        else if (L->Data[i]Last; j>=i; j--) //找到i的位置
                L->Data[j+1]=L->Data[j]; 
            L->Data[i]=X; 
            L->Last=L->Last+1;
            return true;
        }
        
        else if(i==L->Last&&L->Data[i]>X){
            L->Data[L->Last+1]=X;
            L->Last=L->Last+1;
            return true;
           break;
        }
    }
}

习题2.4 递增的整数序列链表的插入

List Insert(List L, ElementType X)
{
	List head = L;
	List t = head;
	while(t -> Next != NULL &&  t -> Next -> Data <= X) //遍历t
		t = t -> Next;
	
	List q;
	q = (struct Node *)malloc(sizeof(struct Node *));
	q -> Data = X;
	q -> Next = t -> Next;
	t -> Next = q;
		
	return head;
}

习题2.5 两个有序链表序列的合并

List Merge( List L1, List L2 )
{
   
    List p1,p2,p,L;
    p1=L1->Next;//分别遍历两个链表
    p2=L2->Next;
    L=(List)malloc(sizeof(List));//新的头结点
    L->Data=L1->Data+L2->Data;
    p=L;
 
    while(p1!=NULL&&p2!=NULL)
    {
        if((p1->Data)<(p2->Data))
        {
            p->Next=p1;
            p=p->Next;//p是移动的 而头插法头指针L是不动的 这是一个节点接一个节点的哈哈
            p1=p1->Next;
        }
        else
        {
            p->Next=p2;
            p=p->Next;
            p2=p2->Next;
        }
    }
    if(p1==NULL)
    {
        p->Next=p2;
    }
    else if(p2==NULL)
    {
        p->Next=p1;
    }
    L1->Next=NULL;
    L2->Next=NULL;
    return L;
 
}

习题2.6 递归求简单交错幂级数的部分和

double fn(double x,int n)
{
    double ans;
    if(n==0||n==1)
        ans=x;
    else
        ans=x-x*fn(x,n-1);
    return ans;
}

习题2.7 弹球距离

double dist(double h, double p)
{
    double high = h;
    double time = p;
    double mix = time * h;
    while (mix>TOL)
    {
        high =high +2*mix;
        mix=p*mix;
    }
    return high;
}

习题3.3 线性表元素的区间删除

List Delete(List L, ElementType minD, ElementType maxD) {
	Position i;
	Position* a = (Position *)malloc((L->Last + 1) * sizeof(Position));
	Position front = 0, rear = 0;
	Position max = L->Last;
	for (i = 0; i <= max; i++) {
		if (L->Data[i] > minD&&L->Data[i] < maxD) {
			a[rear++] = i;
			L->Last--;
		}
		else {
			a[rear++] = i;
			L->Data[a[front++]] = L->Data[i];
		}
	}
	return L;
}

习题3.5 求链表的倒数第m个元素

ElementType Find(List L, int m) {
	List p = L;
	List q = L;
	int len = 0;
	while (p->Next != NULL) {
		if (len != m) {
			p = p->Next;
			len++;
		}
		else {
			p = p->Next;
			q = q->Next;
		}
	}
	if (len == 0)
		return ERROR;
	return q->Next->Data;
}

习题3.12 另类循环队列

bool AddQ( Queue Q, ElementType X )
{
if(Q->Count >= Q->MaxSize)
{
printf("Queue Full\n");
return false;
}
else
{
Q->Count++;
Q->Data[(Q->Front + Q->Count) % Q->MaxSize] = X;
return true;
}
}
ElementType DeleteQ( Queue Q )
{
if(Q->Count == 0)
{
printf("Queue Empty\n");
return ERROR;
}
else
{
Q->Front = (Q->Front+1) % Q->MaxSize;
Q->Count--;
return Q->Data[Q->Front];
}
}

习题3.13 双端队列

bool Push(ElementType X, Deque D) {
	if ((D->Rear + 1) % D->MaxSize == D->Front) return false;
	else {
		D->Front = (D->Front - 1 + D->MaxSize) % D->MaxSize;
		D->Data[D->Front] = X;
		return true;
	}
}
ElementType Pop(Deque D) {
	if (D->Rear == D->Front) return ERROR;
	else {
		D->Front = (D->Front + 1) % D->MaxSize;
		return D->Data[(D->Front - 1 + D->MaxSize) % D->MaxSize];
	}
}
bool Inject(ElementType X, Deque D) {
	if ((D->Rear + 1) % D->MaxSize == D->Front) return false;
	else {
		D->Data[D->Rear] = X;
		D->Rear = (D->Rear + 1) % D->MaxSize;
		return true;
	}
}
ElementType Eject(Deque D) {
	if (D->Rear == D->Front) return ERROR;
	else {
		D->Rear = (D->Rear - 1+D->MaxSize) % D->MaxSize;
		return D->Data[D->Rear];	
	}
}

习题3.14 另类堆栈

bool Push( Stack S, ElementType X )
{
    if(S->Top == S->MaxSize)
    {
        printf("Stack Full\n");
        return false;
    }
    else
    {
        S->Data[S->Top] = X;
        S->Top ++;
    }
}
ElementType Pop( Stack S )
{
    if(S->Top == 0)
    {
        printf("Stack Empty\n");
        return ERROR;
    }
    else
    {
        ElementType a = S->Data[S->Top - 1];
        S->Top --;
        return a;
    }
}

习题4.3 是否二叉搜索树

bool preJudge(BinTree T, int *minT, int *maxT)
{
    int lmin,lmax,rmin,rmax;
    bool ans1 = false, ans2 = false;
    if(T==NULL) return true;//递归基
    if((T->Left&&preJudge(T->Left,&lmin,&lmax)&&T->Data>lmax)||!T->Left)
        ans1 = true;//左子树为空,左子树为BST;左子树不为空则判断左子树是否为BST,再判断根结点是否大于左子树的最大值
    if((T->Right&&preJudge(T->Right,&rmin,&rmax)&&T->DataRight)
        ans2 = true;//同上
    if(ans1&&ans2)
    {
        if(T->Left == NULL) *minT = T->Data;//左子树为空,那么T的最小值为T->Data;
        else *minT = lmin;
        if(T->Right == NULL) *maxT = T->Data;//同上
        else *maxT = rmax;
        return true;
    }
    else return false;
}
bool IsBST(BinTree T)//统一接口
{
    int minT,maxT;
    return preJudge(T,&minT,&maxT);
}

习题5.10 线性探测法的查找函数

Position Find( HashTable H, ElementType Key )
{
    Position x = Hash(Key, H->TableSize);
    int count = 1;
    while (count != H->TableSize) {
        if (H->Cells[x].Info == Empty || H->Cells[x].Info == Deleted || H->Cells[x].Data == Key) {
            return x;
        }
        x = (x + 1) % H->TableSize;
        count++;
    }
    return ERROR;
}

习题5.11 分离链接法的删除操作函数

bool Delete( HashTable H, ElementType key )
{
    int t;
    t=*(key+0)-'a';
    t=t%H->TableSize;
    PtrToLNode p,q;
    p=(H->Heads+t);
    q=p;
    p=p->Next;
    int m=0;
    
    while(p)
    {
        //q=p;
        if(strcmp(p->Data,key)==0)
        {
            q->Next=p->Next;
            free(p);
            printf("%s is deleted from list Heads[%d]\n",key,t);
            
             return true;
        }
        q=p;
       // free(p);
        p=p->Next;
        
            
        
    }
    return false;

}

练习6.1 邻接矩阵存储图的深度优先遍历

#include
void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) )
{
    Visited[V]=true;
    Visit(V);
    for(int i=0;iNv;i++)
    {
        if(Graph->G[V][i]

练习6.2 邻接表存储图的广度优先遍历

void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ){
    int p[10001];
    Visit(S);
    int cnt=0,x=0;
    Visited[S]=true;
    p[cnt++]=S;
    while(x!=cnt){
        PtrToAdjVNode t=Graph->G[p[x++]].FirstEdge;
        while(t){
            int tt=t->AdjV;
            if(!Visited[tt]){
                p[cnt++]=tt;
                Visit(tt);
                Visited[tt]=true;
            }
            t=t->Next;
        }
    }
}
  • 我只是大自然的搬运工

你可能感兴趣的:(数据结构,数据结构,算法)