2023 Data Structure Midterm Test-Fall 2023-Computer + Future Network Major

In order to prevent people from looking at the answers unconsciously, light fonts are specially used.

Multiple choice questions

2-1
The time complexity of the following program segment is ( )

for(i=0; i<n-1; i++for(j=0; j<n-1-i; j++
        t=a[j], a[j]=a[j+1], a[j+1]=t;

A.O(1)
B.O(n)
C.O(n2)
D.O(n3 )、

Answer: C.
The cyclical order is (n-1) +(n-2) + ··· +1, O(n2)

2-2
Among the following algorithms that implement a given operation on a sequentially stored ordered list (length n), the average time complexity is O(1):
A. Algorithm to find elements containing specified value
B. Algorithm to insert elements containing specified value
C. Delete i-th element Algorithm for (1≤i≤n) elements
D. Algorithm for obtaining the i-th (1≤i≤n) element

Answer: D
Sequential table acquisition can get the i-th element by accessing the subscript, and the time complexity is O(1)

2-3
Connecting linear tables La and Lb head to tail requires a time complexity of O(1) and takes up as little auxiliary space as possible. Which structure should be used?
A. Single linked list
B. Single circular linked list
C. Single circular linked list with tail pointer< a i=5> D. Double circular linked list with head nodeAnswer: CLinear tables La and Lb are connected head to tail. You must first find the tail sum of La Lb's head. For a single circular linked list with a tail pointer, the next of the tail node is the head node; and for a double circular linked list with a head node, although the tail node can also be found through the prev of the head node, the question requires that the auxiliary space should be as small as possible. Choose C


2-4
It is known that the head pointer h points to a non-empty single circular linked list with the head node. The node structure is data | next, where next is the pointer to the immediate successor node, p is the tail pointer, and q is the temporary pointer. Now to delete the first element of the linked list, the correct sequence of statements is:

A.h->next=h->next->next; q=h->next; free(q);
B.q=h->next; h->next=h->next->next; free(q);
C.q=h->next; h->next=q->next; if (p!=q) p=h; free(q);
D.q=h->next; h->next=q->next; if (p==q) p=h; free(q);

Answer: D

2-5
Assume that 5 integers are pushed onto the stack in the order of 1, 2, 3, 4, 5, and the order of popping them out is 3, 5, 4, 2, 1, then in order to obtain such an output, the stack size must be at least:
A.2
B.3
C .4
D.5

Answer: C

2-6
There are six elements pushed onto the stack in the order of 6, 5, 4, 3, 2, 1. Which one is not a legal pop sequence?
A.2 3 4 1 5 6
B.3 4 6 5 2 1
C.5 4 3 6 1 2
D.4 5 3 1 2 6
Answer: B

2-7
If the stack is stored in sequential storage, the two stacks share space V[m]: top[i] represents the i-th (i=1 or 2) The top of the stack; the bottom of stack 1 is at V[0], and the bottom of stack 2 is at V[m-1]. The condition for the stack to be full is:
A.|top[2] -top[1]|==0
B.top[1]+top[2]==m

C.top[1] == top[2]
D.top[1]+1==top[2]
Answer : D
The condition for the stack to be full is that the top pointers of the two stacks meet. Because in the sequential storage structure, when the top pointers of two stacks are adjacent, it means that the space of the two stacks is full

2-8
循环队列的队满条件为 ( )
A.(sq.rear+1) % maxsize ==(sq.front+1) % maxsize
B.(sq.front+1) % maxsize ==sq.rear
C.(sq.rear+1) % maxsize ==sq.front
D.sq.rear ==sq.front

Answer: C

2-9
Expression a*(b+c)-d after expression is:
A.a b c + * d -< /span> D.- + * a b c d C. a b c * + d -
B. a b c d * + -

Answer: A
can be restored according to the suffix expression of the option. A is a(b+c)-d; B is a -(b+cd); C is a+bc-d; D is pure nonsense, at first glance it is really a prefix *

2-10
Each element of array A[1…5,1…6] occupies 5 units, and is stored in row-major order at the starting address of 1000 In a continuous memory unit, the address of element A[5,5] is:
A.1120
B.1125
C.1140
D.1145
Answer: C
As shown in the picture, calculate by yourself
Insert image description here

2-11
The maximum number of nodes on level 5 (the root level number is 1) in the binary tree is:
A. 8
B.15
C.16
D.32
Answer: C< /span>n-1
The maximum number of nodes at level n in a binary tree is 2

2-12
During the first two forks, the number of double branch points is 15, the number of half branch points is 30, and the number of second branch points is ().
A.15
B.16
C.17
D.47 a>
Answer: B
对于n个结端的二弉树,n = n0+ n1+n2; n0 = n2 + 1, instant solution possible

2-13
Using a binary linked list as the storage structure of a binary tree, in a binary linked list with n nodes (n>0), the number of empty link fields For__
A.n+1
B.n
C.n−1
D. Unable to determine
Answer: A
has n nodes, which means there are 2n pointer fields, and because there are n The binary tree of nodes has n-1 edges, then the null pointer field has 2n-(n-1)=n+1

2-14
If the result of post-order traversal of a binary tree is FDEBGCA and the result of in-order traversal is FDBEACG, what is the result of pre-order traversal of the binary tree?
A.ABCDEFG
B.ABDFEGC
C.ABDFECG
D.ABDEFCG a>
Answer: C

2-15
Assume that each node of a d-ary tree has d pointers pointing to subtrees. How many empty link domains does a d-ary tree with n nodes have?
A.nd
B.n(d−1)
C.n(d−1)+1
D. None of the above
Answer: C
Exactly the same as 2-13

2-16
It is known that the tree shape of a binary tree is as shown in the figure below, and its sequence sequence is { e, a, c, b, d, g, f } . The nodes at the same level as node a in the tree are:
Insert image description here

A.c
B.d
C.f
D.g

Answer: B
Insert image description here

2-17
Among the following clue binary trees (the clues are represented by dotted lines), those that meet the definition of post-order clue trees are:

A. Insert image description here

B. Insert image description here

C. Insert image description here

D. Insert image description here
Answer: B
Because it is a post-order clue tree, the post-order traversal of the tree in the option is dbca, so the predecessor of d is NULL, exclude CD; follow b, exclude A

2-18
The depth of a complete binary tree with 65 nodes is (the depth of the root is 1):
A.8n ⌋ +12The depth of a complete binary tree with n nodes = ⌊ logAnswer: B D.5 C.6
B.7



2-19
Suppose a piece of text contains the characters {a, b, c, d, e}, and the corresponding frequency of occurrence is {3, 2, 5, 1, 1 }. Then after Huffman encoding, the number of bytes occupied by the text is:
A.40
B.36
C .25
D.12
Answer: C
Insert image description here

2-20
A Huffman tree is composed of four leaf nodes with weights of 9, 2, 5, and 7 respectively. The weighted path length of the tree For:
A.23
B.37
C.44
D.46
Answer: C
Same as above

Function questions

6-1 Find the length of the linked list

This question requires the implementation of a function to find the length of a linked list.

Function interface definition:

cint Length( List L );

The List structure is defined as follows:

typedef struct LNode *PtrToLNode;
struct LNode {
    
    
    ElementType Data;
    PtrToLNode Next;
};
typedef PtrToLNode List;

L is a given singly linked list, and the function Length should return the length of the linked list.

Sample Referee Test Procedure:

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

typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
    
    
    ElementType Data;
    PtrToLNode Next;
};
typedef PtrToLNode List;

List Read(); /* 细节在此不表 */

int Length( List L );

int main()
{
    
    
    List L = Read();
    printf("%d\n", Length(L));
    return 0;
}

/* 你的代码将被嵌在这里 */

Example:
1 3 4 5 2 -1
Example:
5 a>

untie:

int Length( List L )
{
    
    
    int ret = 0;
    while(L)
    {
    
    
        ret++;
        L = L->Next;
    }
    return ret;
}

6-2 Create a linked list from data in reverse order

This question requires the implementation of a function to create a linked list in the reverse order of input data.

Function interface definition:

cstruct ListNode *createlist();

The function createlist uses scanf to obtain a series of positive integers from the input. When −1 is read, the input ends. Create a linked list in reverse order of the input data and return the head pointer of the linked list. The linked list node structure is defined as follows:

struct ListNode {
    
    
    int data;
    struct ListNode *next;
};

Sample Referee Test Procedure:

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

struct ListNode {
    
    
    int data;
    struct ListNode *next;
};

struct ListNode *createlist();

int main()
{
    
    
    struct ListNode *p, *head = NULL;

    head = createlist();
    for ( p = head; p != NULL; p = p->next )
        printf("%d ", p->data);
    printf("\n");

    return 0;
}

/* 你的代码将被嵌在这里 */

Example of import:
1 2 3 4 5 6 7 -1
Example of export:
7 6 5 4 3 2 1
Solution:

struct ListNode* createlist()
{
    
    
    struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* cur = head;
    cur->next = NULL;
    int tmp = 0;
    scanf("%d",&tmp);
    while(tmp != -1)
    {
    
    
        cur->data = tmp;
        scanf("%d",&tmp);
        struct ListNode* tmp = (struct ListNode*)malloc(sizeof(struct ListNode));
        tmp->next = cur;
        cur = tmp;
        
    }
    return cur->next;
    
}

6-3 Delete even nodes in singly linked list

This question requires the implementation of two functions, which store the read data into a singly linked list and delete the even-valued nodes in the linked list. The linked list nodes are defined as follows:

struct ListNode {
    
    
    int data;
    struct ListNode *next;
};

Function interface definition:

struct ListNode *createlist();
struct ListNode *deleteeven( struct ListNode *head );

The function createlist reads a series of positive integers from the standard input and creates a singly linked list in the order in which they are read. When −1 is read, it indicates the end of input, and the function should return a pointer to the head node of the singly linked list.

The function deleteeven deletes the even-valued node in the singly linked list head and returns the head pointer of the resulting linked list.

Sample Referee Test Procedure:

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

struct ListNode {
    
    
    int data;
    struct ListNode *next;
};

struct ListNode *createlist();
struct ListNode *deleteeven( struct ListNode *head );
void printlist( struct ListNode *head )
{
    
    
     struct ListNode *p = head;
     while (p) {
    
    
           printf("%d ", p->data);
           p = p->next;
     }
     printf("\n");
}

int main()
{
    
    
    struct ListNode *head;

    head = createlist();
    head = deleteeven(head);
    printlist(head);

    return 0;
}

/* 你的代码将被嵌在这里 */

Example of import:
1 2 2 3 4 5 6 7 -1
Example of import:
1 3 5 7
Solution:

struct ListNode *createlist()
{
    
    
    struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* cur = head;
    cur->data = -1;
    cur->next = NULL;
    int tmp = 0;
    while ( tmp != -1)
    {
    
    
        scanf("%d", &tmp);
        if(tmp != -1)
        {
    
    
        struct ListNode* ts = (struct ListNode*)malloc(sizeof(struct ListNode));
        cur->next = ts;
        ts->data = tmp;
        ts->next = NULL;
        cur = ts;
        }
    }
    return head->next;
}

struct ListNode *deleteeven( struct ListNode *head )
{
    
    
    if(head->next == NULL)
    {
    
    
        if(head->data%2 == 0)
            return NULL;
        else
            return head;
    }
    struct ListNode *ret = head;
    while(ret->data%2 == 0)
    {
    
    
        struct ListNode* del = ret;
        ret = ret->next;
        free(del);
        if(ret->next == NULL)
        {
    
    
            if(ret->data%2 == 0)
                return NULL;
            else
                return ret;
        }
    }
        
    struct ListNode * cur = ret;
    while(cur->next)
    {
    
    
        if(cur->next->data%2 == 0)
        {
    
    
            struct ListNode* del = cur->next;
            cur->next = cur->next->next;
            free(del);
        }
        else
            cur = cur->next;
    }
    return ret;
}

6-4 Find the height of a binary tree

This question requires the height of a given binary tree.

Function interface definition:

int GetHeight( BinTree BT );

The BinTree structure is defined as follows:

typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    
    
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

The function is required to return the height value of the given binary tree BT.

Sample Referee Test Procedure:

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

typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    
    
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

BinTree CreatBinTree(); /* 实现细节忽略 */
int GetHeight( BinTree BT );

int main()
{
    
    
    BinTree BT = CreatBinTree();
    printf("%d\n", GetHeight(BT));
    return 0;
}
/* 你的代码将被嵌在这里 */

Example output (for the tree given in the figure):
Insert image description here
4
Solution:

int max(int a,int b)
{
    
    
    return a>b?a:b;
}
int GetHeight( BinTree BT )
{
    
    
    if(BT == NULL)
        return 0;
    return max(GetHeight(BT->Left),GetHeight(BT->Right))+1;
}

6-5 Pre-order output leaf nodes

This question requires outputting the leaf nodes of a given binary tree in the order of preorder traversal.

Function interface definition:

void PreorderPrintLeaves( BinTree BT );

The BinTree structure is defined as follows:

typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    
    
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

The function PreorderPrintLeaves should output the leaf nodes of the given binary tree BT in the order of preorder traversal, in the format of a space followed by a character.

Sample Referee Test Procedure:

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

typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    
    
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

BinTree CreatBinTree(); /* 实现细节忽略 */
void PreorderPrintLeaves( BinTree BT );

int main()
{
    
    
    BinTree BT = CreatBinTree();
    printf("Leaf nodes are:");
    PreorderPrintLeaves(BT);
    printf("\n");

    return 0;
}
/* 你的代码将被嵌在这里 */

Sample output (for the tree given in the figure):

Insert image description here
Leaf nodes are: D E H I
untie:

void PreorderPrintLeaves( BinTree BT )
{
    
    
    if(BT == NULL)
        return;
    if(BT->Left == NULL && BT->Right == NULL)
    {
    
    
        printf(" %c",BT->Data);
        return;
    }
        
    PreorderPrintLeaves(BT->Left);
    PreorderPrintLeaves(BT->Right);
}

Guess you like

Origin blog.csdn.net/m0_74195626/article/details/134363392