考研复试
准备考研复试中,正在重新学习数据结构,pta上的数据结构与算法题目集,由于复试要求用c语言,所以全文均由c语言实现,持续更新ing
函数题
6-1 单链表逆转
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
ElementType Data;
PtrToNode Next;
};
typedef PtrToNode List;
List Read(); /* 细节在此不表 */
void Print( List L ); /* 细节在此不表 */
List Reverse( List L );
int main()
{
List L1, L2;
L1 = Read();
L2 = Reverse(L1);
Print(L1);
Print(L2);
return 0;
}
/* 你的代码将被嵌在这里 */
接口实现
/* 该函数用于实现单链表的逆转 */
List Reverse( List L ){
PtrToNode prev=NULL; //前驱节点
PtrToNode current=L; //当前节点
PtrToNode next; //下一节点
//不断更新当前节点,直到当前节点为空
while(current!=NULL){
next=current->Next; //先记录当前节点的下一节点,后续使用
current->Next=prev; //节点逆转,当前节点的下一节点为前驱节点
prev=current; //更新前驱节点为当前节点
current=next; //更新当前节点为之前记录的下一节点
}
return prev;
}
过程分析
具体来说,假设原始链表中有节点 A -> B -> C,其中 A 是链表的头节点,C 是链表的尾节点。在反转链表的过程中,首先初始化 prev 为 NULL,current 指向 A,然后进行循环。
在第一次迭代中,current->Next = prev; 就是将 A 的 Next 指针指向 NULL,即 A -> NULL。接着更新 prev 为 A,current 移动到 B。
在第二次迭代中,current->Next = prev; 就是将 B 的 Next 指针指向 A,即 B -> A。再次更新 prev 为 B,current 移动到 C。
在第三次迭代中,current->Next = prev; 就是将 C 的 Next 指针指向 B,即 C -> B。再次更新 prev 为 C,current 移动到 NULL。
此时,整个链表已经反转完成,变为 C -> B -> A。在循环结束后,prev 指向的节点即为新链表的头节点,所以 return prev; 返回的是逆转后链表的头指针。
这个过程会不断地修改节点的 Next 指针方向,最终完成链表的反转。
6-2 顺序表操作集
本题要求实现顺序表的操作集。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5
#define ERROR -1
typedef enum {false, true} bool;
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last; /* 保存线性表中最后一个元素的位置 */
};
List MakeEmpty();
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );
int main()
{
List L;
ElementType X;
Position P;
int N;
L = MakeEmpty();
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
if ( Insert(L, X, 0)==false )
printf(" Insertion Error: %d is not in.\n", X);
}
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
P = Find(L, X);
if ( P == ERROR )
printf("Finding Error: %d is not in.\n", X);
else
printf("%d is at position %d.\n", X, P);
}
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &P);
if ( Delete(L, P)==false )
printf(" Deletion Error.\n");
if ( Insert(L, 0, P)==false )
printf(" Insertion Error: 0 is not in.\n");
}
return 0;
}
/* 你的代码将被嵌在这里 */
接口实现
//创建一个空链表
List MakeEmpty() {
List L = (List)malloc(sizeof(struct LNode));
if (L == NULL) {
printf("Memory allocation failed\n");
}
L->Last = -1;
return L;
}
//返回线性表中X的位置。若找不到则返回ERROR;
Position Find(List L, ElementType X) {
for (int i = 0; i <= L->Last; i++) {
if (L->Data[i] == X) {
return i;
}
}
return ERROR;
}
//将X插入在位置P并返回true。
bool Insert(List L, ElementType X, Position P) {
if (L->Last == MAXSIZE - 1) {
printf("FULL");
return false;
}
if (P < 0 || P > L->Last+1) {
printf("ILLEGAL POSITION");
return false;
}
for (int i = L->Last; i >= P; i--) {
L->Data[i + 1] = L->Data[i];
}
L->Data[P] = X;
L->Last++;
return true;
}
//将位置P的元素删除并返回true。若参数P指向非法位置,则打印“POSITION P EMPTY”(其中P是参数值)并返回false。
bool Delete(List L, Position P) {
if (P < 0 || P > L->Last) {
printf("POSITION %d EMPTY", P);
return false;
}
for (int i = P; i < L->Last; i++) {
L->Data[i] = L->Data[i + 1];
}
L->Last--;
return true;
}
过程分析
提示一些需要注意的地方,在for循环时注意区分什么时候用maxsize和l->last
6-3 求链式表的表长
本题要求实现一个函数,求链式表的表长。
裁判测试程序样例:
#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;
}
/* 你的代码将被嵌在这里 */
接口实现
int Length( List L ){
int count=0;
while(L){
count++;
L=L->Next;
}
return count;
}
6-4 链式表的按序号查找
本题要求实现一个函数,找到并返回链式表的第K个元素。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#define ERROR -1
typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode List;
List Read(); /* 细节在此不表 */
ElementType FindKth( List L, int K );
int main()
{
int N, K;
ElementType X;
List L = Read();
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &K);
X = FindKth(L, K);
if ( X!= ERROR )
printf("%d ", X);
else
printf("NA ");
}
return 0;
}
/* 你的代码将被嵌在这里 */
接口实现
ElementType FindKth( List L, int K ){
int i=1;//注意计数是从1开始的
if(L==NULL||K<0)
return -1;
while(L){
if(i==K){
return L->Data;
}
i++;
L=L->Next;
}
return -1;
}
6-5 链式表操作集
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#define ERROR NULL
typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;
Position Find( List L, ElementType X );
List Insert( List L, ElementType X, Position P );
List Delete( List L, Position P );
int main()
{
List L;
ElementType X;
Position P, tmp;
int N;
L = NULL;
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
L = Insert(L, X, L);
if ( L==ERROR ) printf("Wrong Answer\n");
}
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
P = Find(L, X);
if ( P == ERROR )
printf("Finding Error: %d is not in.\n", X);
else {
L = Delete(L, P);
printf("%d is found and deleted.\n", X);
if ( L==ERROR )
printf("Wrong Answer or Empty List.\n");
}
}
L = Insert(L, X, NULL);
if ( L==ERROR ) printf("Wrong Answer\n");
else
printf("%d is inserted as the last element.\n", X);
P = (Position)malloc(sizeof(struct LNode));
tmp = Insert(L, X, P);
if ( tmp!=ERROR ) printf("Wrong Answer\n");
tmp = Delete(L, P);
if ( tmp!=ERROR ) printf("Wrong Answer\n");
for ( P=L; P; P = P->Next ) printf("%d ", P->Data);
return 0;
}
/* 你的代码将被嵌在这里 */
接口实现
/* 返回线性表中首次出现X的位置。若找不到则返回ERROR;*/
Position Find( List L, ElementType X ){
while(L){
if(L->Data==X){
return L;
}
L=L->Next;
}
return ERROR;
}
/*将X插入在位置P指向的结点之前,返回链表的表头。
如果参数P指向非法位置,则打印“Wrong Position for Insertion”,返回ERROR;*/
List Insert(List L, ElementType X, Position P) {
List F = L;
List node = (List)malloc(sizeof(struct LNode));
node->Data = X;
node->Next = NULL;
if (P == L) {
// 在链表头部插入新节点
node->Next=L;
//此时node为新的头节点
return node;
}
while(L!=NULL){
if(L->Next==P){
node->Next=L->Next;
L->Next=node;
return F;
}
L=L->Next;
}
printf("Wrong Position for Insertion\n");//以上情况都不满足,有问题输出返回
return ERROR;
}
/*将位置P的元素删除并返回链表的表头。若参数P指向非法位置,则打印“Wrong Position for Deletion”并返回ERROR。*/
List Delete( List L, Position P ){
//头删除,当不为NULL时,说明有可删除的元素,才进行删除操作
if(L!=NULL)
{
if(L==P)
{
L=L->Next;
return L;
}
}
List F=L;
while(L){
if(L->Next==P){
L->Next=P->Next;
return F;
}
L=L->Next;
}
printf("Wrong Position for Deletion\n");
return ERROR;
}
6-6 带头结点的链式表操作集
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#define ERROR NULL
typedef enum {false, true} bool;
typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;
List MakeEmpty();
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );
int main()
{
List L;
ElementType X;
Position P;
int N;
bool flag;
L = MakeEmpty();
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
flag = Insert(L, X, L->Next);
if ( flag==false ) printf("Wrong Answer\n");
}
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
P = Find(L, X);
if ( P == ERROR )
printf("Finding Error: %d is not in.\n", X);
else {
flag = Delete(L, P);
printf("%d is found and deleted.\n", X);
if ( flag==false )
printf("Wrong Answer.\n");
}
}
flag = Insert(L, X, NULL);
if ( flag==false ) printf("Wrong Answer\n");
else
printf("%d is inserted as the last element.\n", X);
P = (Position)malloc(sizeof(struct LNode));
flag = Insert(L, X, P);
if ( flag==true ) printf("Wrong Answer\n");
flag = Delete(L, P);
if ( flag==true ) printf("Wrong Answer\n");
for ( P=L->Next; P; P = P->Next ) printf("%d ", P->Data);
return 0;
}
/* 你的代码将被嵌在这里 */
接口实现
/* 创建并返回一个空的线性表*/
List MakeEmpty(){
List L=(List)malloc(sizeof(struct LNode));
L->Next=NULL;
return L;
}
/*返回线性表中X的位置。若找不到则返回ERROR;*/
Position Find( List L, ElementType X ){
Position p=L->Next;
while(p!=NULL&&p->Data!=X){
p=p->Next;
}
return p;
}
/*将X插入在位置P指向的结点之前,返回true。如果参数P指向非法位置,则打印“Wrong Position for Insertion”,返回false;*/
bool Insert( List L, ElementType X, Position P ){
Position node;
node = (Position)malloc(sizeof(struct LNode));
node->Data=X;
node->Next=NULL;
//如果插入的位置是首结点
if(L==P){
node->Next=L;
return true;
}
//插入的位置正常
Position temp=L;
while(temp){
if(temp->Next==P){
node->Next=P;
temp->Next=node;
return true;
}
temp=temp->Next;
}
//插入的位置非法
printf("Wrong Position for Insertion\n");
return false;
}
/*将位置P的元素删除并返回true。若参数P指向非法位置,则打印“Wrong Position for Deletion”并返回false。*/
bool Delete( List L, Position P ){
//删除的位置为首节点
if(L==P){
L=L->Next;
return true;
}
//删除的位置正常
Position temp=L;
while(temp){
if(temp->Next==P){
temp->Next=P->Next;
return true;
}
temp=temp->Next;
}
//删除的位置非法
printf("Wrong Position for Deletion\n");
return false;
}
6-7 在一个数组中实现两个堆栈
本题要求在一个数组中实现两个堆栈。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#define ERROR 1e8
typedef int ElementType;
typedef enum { push, pop, end } Operation;
typedef enum { false, true } bool;
typedef int Position;
struct SNode {
ElementType *Data;
Position Top1, Top2;
int MaxSize;
};
typedef struct SNode *Stack;
Stack CreateStack( int MaxSize );
bool Push( Stack S, ElementType X, int Tag );
ElementType Pop( Stack S, int Tag );
Operation GetOp(); /* details omitted */
void PrintStack( Stack S, int Tag ); /* details omitted */
int main()
{
int N, Tag, X;
Stack S;
int done = 0;
scanf("%d", &N);
S = CreateStack(N);
while ( !done ) {
switch( GetOp() ) {
case push:
scanf("%d %d", &Tag, &X);
if (!Push(S, X, Tag)) printf("Stack %d is Full!\n", Tag);
break;
case pop:
scanf("%d", &Tag);
X = Pop(S, Tag);
if ( X==ERROR ) printf("Stack %d is Empty!\n", Tag);
break;
case end:
PrintStack(S, 1);
PrintStack(S, 2);
done = 1;
break;
}
}
return 0;
}
/* 你的代码将被嵌在这里 */
接口实现
//用于创建堆栈
Stack CreateStack( int MaxSize ){
Stack S=(Stack)malloc(sizeof(struct SNode));
S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
S->Top1=-1;
S->Top2=MaxSize;
S->MaxSize=MaxSize;
return S;
}
//入栈
bool Push( Stack S, ElementType X, int Tag ){
if(S->Top1+1==S->Top2){
printf("Stack Full\n");
return false;
}else if(Tag==1){
S->Data[++S->Top1]=X;
return true;
}else if(Tag==2){
S->Data[--S->Top2]=X;
return true;
}
}
//出栈
ElementType Pop( Stack S, int Tag ){
ElementType res;
if(Tag==1){
if(S->Top1==-1){
printf("Stack %d Empty\n",Tag);
return ERROR;
}else{
res=S->Data[S->Top1];
S->Top1--;
return res;
}
}else if(Tag==2){
if(S->Top2==S->MaxSize){
printf("Stack %d Empty\n",Tag);
return ERROR;
}else{
res=S->Data[S->Top2];
S->Top2++;
return res;
}
}
}
6-8 求二叉树高度
本题要求给定二叉树的高度。
裁判测试程序样例:
#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;
}
/* 你的代码将被嵌在这里 */
接口实现
int GetHeight(BinTree BT) {
if (BT == NULL) {
return 0; // 空树的高度为0
}
// 递归计算左右子树的高度,然后取最大值并加1
if(GetHeight(BT->Left)>GetHeight(BT->Right)){
return GetHeight(BT->Left)+1;
}
return GetHeight(BT->Right)+1;
}
6-9 二叉树的遍历
本题要求给定二叉树的4种遍历。
裁判测试程序样例:
#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 InorderTraversal( BinTree BT );
void PreorderTraversal( BinTree BT );
void PostorderTraversal( BinTree BT );
void LevelorderTraversal( BinTree BT );
int main()
{
BinTree BT = CreatBinTree();
printf("Inorder:"); InorderTraversal(BT); printf("\n");
printf("Preorder:"); PreorderTraversal(BT); printf("\n");
printf("Postorder:"); PostorderTraversal(BT); printf("\n");
printf("Levelorder:"); LevelorderTraversal(BT); printf("\n");
return 0;
}
/* 你的代码将被嵌在这里 */
接口实现
/* 前序遍历 */
void InorderTraversal( BinTree BT ){
if (BT) {
InorderTraversal(BT->Left);
printf(" %c", BT->Data);
InorderTraversal(BT->Right);
}
}
/* 中序遍历 */
void PreorderTraversal( BinTree BT ){
if (BT) {
printf(" %c", BT->Data);
PreorderTraversal(BT->Left);
PreorderTraversal(BT->Right);
}
}
/* 后序遍历 */
void PostorderTraversal( BinTree BT ){
if (BT) {
PostorderTraversal(BT->Left);
PostorderTraversal(BT->Right);
printf(" %c", BT->Data);
}
}
/* 层序遍历 */
void LevelorderTraversal( BinTree BT ){
if(BT){
BinTree queue[1000];
int front=-1,rear=-1;
queue[++rear]=BT;
while(front!=rear){
BinTree temp = queue[++front];
printf(" %c",temp->Data);
if(temp->Left){
queue[++rear]=temp->Left;
}
if(temp->Right){
queue[++rear]=temp->Right;
}
}
}
}
6-10 二分查找
本题要求实现二分查找算法。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
#define NotFound 0
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last; /* 保存线性表中最后一个元素的位置 */
};
List ReadInput(); /* 裁判实现,细节不表。元素从下标1开始存储 */
Position BinarySearch( List L, ElementType X );
int main()
{
List L;
ElementType X;
Position P;
L = ReadInput();
scanf("%d", &X);
P = BinarySearch( L, X );
printf("%d\n", P);
return 0;
}
/* 你的代码将被嵌在这里 */
接口实现
/* 函数BinarySearch要查找X在Data中的位置,即数组下标(注意:元素从下标1开始存储)。
找到则返回下标,否则返回一个特殊的失败标记NotFound。 */
Position BinarySearch( List L, ElementType X ){
if(L){
Position low=1,high=L->Last,mid;
while(low<=high){
mid=(low+high)/2;
if(L->Data[mid]==X)
return mid;
if(L->Data[mid]>X){
high=mid-1;
}
if(L->Data[mid]<X){
low=mid+1;
}
}
return NotFound;
}
}
6-11 先序输出叶结点
本题要求按照先序遍历的顺序输出给定二叉树的叶结点。
裁判测试程序样例:
#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;
}
/* 你的代码将被嵌在这里 */
接口实现
/* 本题要求按照先序遍历的顺序输出给定二叉树的叶结点。 */
void PreorderPrintLeaves( BinTree BT ){
if(BT){
if(BT->Left==NULL&&BT->Right==NULL)
printf(" %c",BT->Data);
PreorderPrintLeaves(BT->Left);
PreorderPrintLeaves(BT->Right);
}
}
6-12 二叉搜索树的操作集
本题要求实现给定二叉搜索树的5种常用操作。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
void PreorderTraversal( BinTree BT ); /* 先序遍历,由裁判实现,细节不表 */
void InorderTraversal( BinTree BT ); /* 中序遍历,由裁判实现,细节不表 */
BinTree Insert( BinTree BST, ElementType X );
BinTree Delete( BinTree BST, ElementType X );
Position Find( BinTree BST, ElementType X );
Position FindMin( BinTree BST );
Position FindMax( BinTree BST );
int main()
{
BinTree BST, MinP, MaxP, Tmp;
ElementType X;
int N, i;
BST = NULL;
scanf("%d", &N);
for ( i=0; i<N; i++ ) {
scanf("%d", &X);
BST = Insert(BST, X);
}
printf("Preorder:"); PreorderTraversal(BST); printf("\n");
MinP = FindMin(BST);
MaxP = FindMax(BST);
scanf("%d", &N);
for( i=0; i<N; i++ ) {
scanf("%d", &X);
Tmp = Find(BST, X);
if (Tmp == NULL) printf("%d is not found\n", X);
else {
printf("%d is found\n", Tmp->Data);
if (Tmp==MinP) printf("%d is the smallest key\n", Tmp->Data);
if (Tmp==MaxP) printf("%d is the largest key\n", Tmp->Data);
}
}
scanf("%d", &N);
for( i=0; i<N; i++ ) {
scanf("%d", &X);
BST = Delete(BST, X);
}
printf("Inorder:"); InorderTraversal(BST); printf("\n");
return 0;
}
/* 你的代码将被嵌在这里 */
接口实现
/* 函数Insert将X插入二叉搜索树BST并返回结果树的根结点指针; */
BinTree Insert( BinTree BST, ElementType X ){
//如果BST是空树,插入的节点为头节点
if(BST==NULL){
BinTree node=(BinTree)malloc(sizeof(struct TNode));
node->Data=X;
node->Left=NULL;
node->Right=NULL;
return node;
}
else if(BST->Data>X){
BST->Left=Insert(BST->Left,X);
}
else if(BST->Data<X){
BST->Right=Insert(BST->Right,X);
}
return BST;
}
/*函数Delete将X从二叉搜索树BST中删除,并返回结果树的根结点指针;如果X不在树中,则打印一行Not Found并返回原树的根结点指针;*/
BinTree Delete( BinTree BST, ElementType X ){
//如果BST是空树
if(BST==NULL){
printf("Not Found\n");
return NULL;
}else{
//BST的值等于X,分成四种情况
if(BST->Data==X){
//左右子树都为空
if(BST->Left==NULL&&BST->Right==NULL){
return NULL;
}
//左子树为空,右子树不为空
else if(BST->Left==NULL&&BST->Right!=NULL){
BST=BST->Right;
}
//右子树为空,左子树不为空
else if(BST->Right==NULL&&BST->Left!=NULL){
BST=BST->Left;
}
//左右子树都不为空
else if(BST->Right!=NULL&&BST->Left!=NULL){
BST->Data=FindMin(BST->Right)->Data;
BST->Right=Delete(BST->Right,BST->Data);
}
}
//BST的值大于X,递归遍历左子树
else if(BST->Data>X){
BST->Left=Delete(BST->Left,X);
}
//BST的值小于X,递归遍历右子树
else if(BST->Data<X){
BST->Right=Delete(BST->Right,X);
}
}
return BST;
}
/*函数Find在二叉搜索树BST中找到X,返回该结点的指针;如果找不到则返回空指针;*/
Position Find( BinTree BST, ElementType X ){
if(BST){
if(BST->Data==X){
return BST;
}
if(BST->Data>X){
return Find(BST->Left,X);
}
if(BST->Data<X){
return Find(BST->Right,X);
}
}
return NULL;
}
/*函数FindMin返回二叉搜索树BST中最小元结点的指针;*/
Position FindMin( BinTree BST ){
if(BST==NULL) return NULL;
while(BST->Left){
BST=BST->Left;
}
return BST;
}
/*函数FindMax返回二叉搜索树BST中最大元结点的指针。*/
Position FindMax( BinTree BST ){
if(BST==NULL) return NULL;
while(BST->Right){
BST=BST->Right;
}
return BST;
}