PTA Zhejiang University Edition "Data Structure (2nd Edition)" question set reference answers (function questions)

PTA Zhejiang University Edition "Data Structure (2nd Edition)" question set reference answers (function questions)

This answer is accompanied by a detailed tutorial column, which provides a more detailed analysis of the answers to each question. Welcome to subscribe to the column.

Column link:PTA Zhejiang University Edition "Data Structure (2nd Edition)" Question Collection Detailed Tutorial_Shaoxia PSY's Blog-CSDN Blog

Exercise 1.8 Binary Search

Position BinarySearch( List L, ElementType X ){
    
    
    int left,mid,right;
    left = 1;
    right = L->Last;
	while( left<=right ){
    
    
		mid = (left+right)/2;
		if( X==L->Data[mid] ) return mid;
		if( X>L->Data[mid] ) left = mid+1;
		else right = mid-1;
	}
	if( left>right ) return NotFound;
}

Exercise 1.9 Insertion into ordered array

bool Insert( List L, ElementType X ){
    
    
    bool insert;
    if (L->Last+1 >= MAXSIZE)insert = false;
    else{
    
    
        for (int i = 0; i <= L->Last; i++){
    
    
            if (X == L->Data[i]){
    
    
                insert = false;
                break;
            }
            if (X > L->Data[i]){
    
    
                for (int j = L->Last+1; j > i; j--)
                    L->Data[j] = L->Data[j-1];
                L->Data[i] = X;
                L->Last++;
                insert = true;
                break;
            }
            if (i == L->Last && X < L->Data[i]){
    
    
                L->Data[L->Last+1] = X;
                L->Last++;
                insert = true;
                break;
            }
        }
    }
        return insert;
}

Exercise 2.4 Inserting an increasing sequence of integers into a linked list

List Insert( List L, ElementType X ){
    
    
    List emptyL = (List)malloc(sizeof(struct Node));
    List NewL = L;
    emptyL->Data = X;
    if (NewL->Next == NULL){
    
    //原链表为空
        emptyL->Next = NULL;
        NewL->Next = emptyL;
    }else if(NewL->Next->Data >= X){
    
    //X比链表第一个值更大,X加在链表最前面
            emptyL->Next = NewL->Next;
            NewL->Next = emptyL;
    }else{
    
    
        while (NewL->Next != NULL && NewL->Next->Data < X)
            NewL = NewL->Next;
        if (NewL->Next == NULL){
    
    
            emptyL->Next = NULL;
            NewL->Next = emptyL;
        }else{
    
    
            emptyL->Next = NewL->Next;
            NewL->Next = emptyL;
        }
    }
    return L;
}

Exercise 2.5 Merging two ordered linked list sequences

List Merge(List L1, List L2){
    
    
    List Merge,tail;
    Merge=(List)malloc(sizeof(struct Node));
    Merge->Next=NULL;
    tail=Merge;
    while(L1->Next&&L2->Next){
    
    
        if(L1->Next->Data<L2->Next->Data){
    
    
            tail->Next=L1->Next;
            L1->Next=L1->Next->Next;
            tail=tail->Next;
            tail->Next=NULL;
        }else{
    
    
            tail->Next=L2->Next;
            L2->Next=L2->Next->Next;
            tail=tail->Next;
            tail->Next=NULL;
        }
    }
    if(L1->Next){
    
    
        tail->Next=L1->Next;
        L1->Next=NULL;
    }
    if(L2->Next){
    
    
        tail->Next=L2->Next;
        L2->Next=NULL;
    }
    return Merge;
}

Exercise 2.6 Recursively find partial sums of simple alternating power series

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

Exercise 2.7 Pinball distance

double dist( double h, double p ){
    
    
    double sum = 0;
    if( h >= TOL )sum = h + h * p + dist(h * p , p) ;
    if( h * p < TOL)sum = h ;
    return sum ;
}

Exercise 3.3 Interval deletion of linear list elements

List Delete( List L, ElementType minD, ElementType maxD ){
    
    
    Position i;
    int len = 0 ;
    for(i = 0 ; i <= L->Last ; i++ ){
    
    
        if(L->Data[i]>minD && L->Data[i]<maxD )continue;
        else L->Data[len++] = L->Data[i];
    }
    L->Last = len -1 ;
    return L;
}

Exercise 3.5 Find the mth element from the bottom of the linked list

ElementType Find( List L, int m ){
    
    
    List Head = L;
    int sum = 0;
    while(L->Next) {
    
    
        L = L->Next;
        sum++;
    }
    if(m > sum) return ERROR;
    int t;
    L = Head -> Next;
    for(int i = 0; i < (sum - m); i++)L = L->Next;
    t = L->Data;
    return t;
}

Exercise 3.12 Alternative Circular Queue

bool IsFull( Queue Q ){
    
    
	return (Q->Count == Q->MaxSize);
}

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

bool IsEmpty( Queue Q ){
    
    
	return (Q->Count==0);
}

ElementType DeleteQ( Queue Q ){
    
    
	if ( IsEmpty(Q) ) {
    
     
		printf("Queue Empty\n");
		return ERROR;
	}
	else  {
    
    
		Q->Front =(Q->Front+1)%Q->MaxSize;
		Q->Count--;
		return  Q->Data[Q->Front];
	}
}

Exercise 3.13 Deque

bool IsFull( Deque D ){
    
    
	return ((D->Front-D->Rear+D->MaxSize)%D->MaxSize == 1);
}
bool IsEmpty( Deque D ){
    
    
	return (D->Front == D->Rear);
}

bool Push( ElementType X, Deque D ){
    
    
     if (IsFull(D)) return false;
     D->Front = (D->Front-1+D->MaxSize)%D->MaxSize;
     D->Data[D->Front] = X;
     return true;
}

ElementType Pop( Deque D ){
    
    
     ElementType X;
     if (IsEmpty(D)) return ERROR;
     X = D->Data[D->Front];
     D->Front = (D->Front+1)%D->MaxSize;
     return X;
}

bool Inject( ElementType X, Deque D ){
    
    
     if (IsFull(D)) return false;
     D->Data[D->Rear] = X;
     D->Rear = (D->Rear+1)%D->MaxSize;
     return true;
}

ElementType Eject( Deque D ){
    
    
     if (IsEmpty(D)) return ERROR;
     D->Rear = (D->Rear-1+D->MaxSize)%D->MaxSize;
     return D->Data[D->Rear];
}

Exercise 3.14 Alternative Stacks

bool IsFull( Stack S ){
    
    
	return (S->Top == S->MaxSize);
}

bool Push( Stack S, ElementType X ){
    
    
	if ( IsFull(S) ) {
    
    
		printf("Stack Full\n");
		return false;
	}
	else {
    
    
		S->Data[S->Top++] = X;
        return true;
    }
}

bool IsEmpty( Stack S ){
    
    
	return (S->Top == 0);
}

ElementType Pop( Stack S ){
    
    
	if ( IsEmpty(S) ) {
    
    
		printf("Stack Empty\n");
		return ERROR; 
	}
	else 
		return ( S->Data[--(S->Top)] );
}

Exercise 4.3 Is it a binary search tree?

#include <stdio.h>

#define MAXN 1001
#define MINH -10001

int H[MAXN], size;

void Create (){
    
    
	size = 0;
	H[0] = MINH;
}

void Insert ( int X ){
    
     
	int i;
	for (i=++size; H[i/2] > X; i/=2)
		H[i] = H[i/2];
	H[i] = X;
}

int main(){
    
    
	int n, m, x, i, j;

	scanf("%d %d", &n, &m);
	Create();
	for (i=0; i<n; i++) {
    
    
		scanf("%d", &x);
		Insert(x);
	}
	for (i=0; i<m; i++) {
    
    
		scanf("%d", &j);
		printf("%d", H[j]);
		while (j>1) {
    
    
			j /= 2;
			printf(" %d", H[j]);
		}
		printf("\n");
	}
	return 0;
}

Exercise 5.10 Finding Functions for Linear Detection Methods

Position Find( HashTable H, ElementType Key ){
    
    
	Position CurrentPos, NewPos;
	NewPos = CurrentPos = Hash( Key, H->TableSize );
	while( H->Cells[NewPos].Info!=Empty && H->Cells[NewPos].Data!=Key ) {
    
    
           NewPos ++;
	       if ( NewPos >= H->TableSize )
				NewPos -= H->TableSize;
	       if (NewPos == CurrentPos) return ERROR;
	}
	return NewPos;
}

Exercise 5.11 Delete operation function of detached link method

bool Delete( HashTable H, ElementType Key ){
    
    
     Position P, t;
     Index Pos;
     
     Pos = Hash( Key, H->TableSize );
     P = H->Heads+Pos;
     while( P->Next && strcmp(P->Next->Data, Key) )
        P = P->Next;
     if (!P->Next) return false;
     else {
    
    
          t = P->Next;
          P->Next = t->Next;
          free(t);
          printf("%s is deleted from list Heads[%d]\n", Key, Pos);
          return true;
     }
}

Exercise 6.1 Depth-first traversal of adjacency matrix storage graph

#include<stdio.h>
#include<stdlib.h>

#define  MaxVertexNum  1000
#define ERROR -1
typedef enum {
    
    false, true} bool;
typedef int Vertex;
typedef Vertex ElementType;

typedef int Position;
struct QNode {
    
    
	ElementType *Data;
	Position Front, Rear;
	int MaxSize;
};
typedef struct QNode *Queue;

Queue CreateQueue( int MaxSize ){
    
    
	Queue Q = (Queue)malloc(sizeof(struct QNode));
	Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
	Q->Front = Q->Rear = -1;
	Q->MaxSize = MaxSize;
	return Q;
}

bool IsFull( Queue Q ){
    
    
	return ((Q->Rear+1)%Q->MaxSize == Q->Front);
}

bool AddQ( Queue Q, ElementType X ){
    
    
	if ( IsFull(Q) ) {
    
    
		printf("队列满");
		return false;
	}
	else {
    
    
		Q->Rear = (Q->Rear+1)%Q->MaxSize;
		Q->Data[Q->Rear] = X;
		return true;
	}
}

bool IsEmpty( Queue Q ){
    
    
	return (Q->Front == Q->Rear);
}

ElementType DeleteQ( Queue Q ){
    
    
	if ( IsEmpty(Q) ) {
    
     
		printf("队列空");
		return ERROR;
	}
	else  {
    
    
		Q->Front =(Q->Front+1)%Q->MaxSize;
		return  Q->Data[Q->Front];
	}
} 

typedef struct ENode *PtrToENode;
struct ENode{
    
    
    Vertex V1, V2;
};
typedef PtrToENode Edge;


typedef struct AdjVNode *PtrToAdjVNode; 
struct AdjVNode{
    
    
	Vertex AdjV;		
	PtrToAdjVNode Next;	
};

typedef struct Vnode{
    
    
	PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];

typedef struct GNode *PtrToGNode;
struct GNode{
    
      
	int Nv;
	int Ne;
	AdjList G;
};
typedef PtrToGNode LGraph;

#define SIX 6
int Visited[MaxVertexNum];

LGraph CreateGraph( int VertexNum ){
    
     
    Vertex V;
	LGraph Graph;
    
    Graph = (LGraph)malloc( sizeof(struct GNode) ); 
	Graph->Nv = VertexNum;
    Graph->Ne = 0;
   	for (V=0; V<Graph->Nv; V++)
		Graph->G[V].FirstEdge = NULL;
			
	return Graph; 
}
       
void InsertEdge( LGraph Graph, Edge E ){
    
    
    PtrToAdjVNode NewNode;
    
    NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
	NewNode->AdjV = E->V2;
	NewNode->Next = Graph->G[E->V1].FirstEdge;
	Graph->G[E->V1].FirstEdge = NewNode;
		
    NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
	NewNode->AdjV = E->V1;
	NewNode->Next = Graph->G[E->V2].FirstEdge;
	Graph->G[E->V2].FirstEdge = NewNode;
}

LGraph BuildGraph(){
    
    
	LGraph Graph;
	Edge E;
	int Nv, i;
	
	scanf("%d", &Nv);
	Graph = CreateGraph(Nv); 
	
	scanf("%d", &(Graph->Ne));
	if ( Graph->Ne != 0 ) {
    
    
	    E = (Edge)malloc( sizeof(struct ENode) );
	    for (i=0; i<Graph->Ne; i++) {
    
    
		    scanf("%d %d", &E->V1, &E->V2); 
			E->V1--; E->V2--;
            InsertEdge( Graph, E );
		}
	} 

	return Graph;
} 

void InitializeVisited( int Nv ){
    
    
	Vertex V;
	for ( V=0; V<Nv; V++ )
		Visited[V] = false;
}

int SDS_BFS( LGraph Graph, Vertex S ){
    
    
	Queue Q;
	Vertex V, Last, Tail;
	PtrToAdjVNode W;
	int Count, Level;

	Q = CreateQueue( MaxVertexNum );
	Visited[S] = true; 
	Count = 1;
	Level = 0;
	Last = S;
    AddQ (Q, S);
    
	while ( !IsEmpty(Q) ) {
    
    
		V = DeleteQ(Q);
		for( W=Graph->G[V].FirstEdge; W; W=W->Next ) {
    
    
			if ( !Visited[W->AdjV] ) {
    
    
				Visited[W->AdjV] = true;
				Count++;
				Tail = W->AdjV;
                AddQ (Q, W->AdjV);
			}
		}
		if ( V==Last ) {
    
    
			Level++;
			Last = Tail;
		}
		if ( Level==SIX ) break;
	}
	return Count;
}

void Six_Degrees_of_Separation( LGraph Graph ) {
    
    
	Vertex V;
	int count;

	for( V=0; V<Graph->Nv; V++ ) {
    
    
		InitializeVisited( Graph->Nv );
		count = SDS_BFS( Graph, V );
		printf("%d: %.2f%%\n", V+1, 100.0*(double)count/(double)Graph->Nv);
	}
}

int main(){
    
    
	LGraph G = BuildGraph();
	Six_Degrees_of_Separation(G);
	return 0;
}

Exercise 6.2 Breadth-first traversal of adjacency list storage graph

typedef Vertex ElementType;
typedef int Position;
typedef struct QNode *PtrToQNode;
struct QNode {
    
    
	ElementType *Data;     /* 存储元素的数组 */
	Position Front, Rear;  /* 队列的头、尾指针 */
	int MaxSize;           /* 队列最大容量 */
};
typedef PtrToQNode Queue; 

Queue CreateQueue( int MaxSize ){
    
    
	Queue Q = (Queue)malloc(sizeof(struct QNode));
	Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
	Q->Front = Q->Rear = 0;
	Q->MaxSize = MaxSize;
	return Q;
}

bool IsFull( Queue Q ){
    
    
	return ((Q->Rear+1)%Q->MaxSize == Q->Front);
}

bool AddQ( Queue Q, ElementType X ){
    
    
	if ( IsFull(Q) ) {
    
    
		printf("队列满");
		return false;
	}
	else {
    
    
		Q->Rear = (Q->Rear+1)%Q->MaxSize;
		Q->Data[Q->Rear] = X;
		return true;
	}
}

bool IsEmpty( Queue Q ){
    
    
	return (Q->Front == Q->Rear);
}

#define ERROR -1
ElementType DeleteQ( Queue Q ){
    
    
	if ( IsEmpty(Q) ) {
    
     
		printf("队列空");
		return ERROR;
	}
	else  {
    
    
		Q->Front =(Q->Front+1)%Q->MaxSize;
		return  Q->Data[Q->Front];
	}
}

void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ){
    
    
		Queue Q; 	
		Vertex V;
		PtrToAdjVNode W;
	
		Q = CreateQueue( MaxVertexNum );
		Visit( S );
		Visited[S] = true;
	    AddQ(Q, S);
	    
		while ( !IsEmpty(Q) ) {
    
    
			V = DeleteQ(Q);
			for( W=Graph->G[V].FirstEdge; W; W=W->Next )
				if ( !Visited[W->AdjV] ) {
    
    
					Visit( W->AdjV );
					Visited[W->AdjV] = true;
	                AddQ(Q, W->AdjV);
				}
		} 
}

Guess you like

Origin blog.csdn.net/weixin_40171190/article/details/134147258