目录
-
第16题
#include <stdio.h>
//静态分配
#define MaxSize 50
typedef int ElemType;//方便后续修改顺序表存储的数据类型
typedef struct{
ElemType data[MaxSize];
int Length;
}SqList;
//插入函数
bool ListInsert(SqList &L,int i,ElemType element){
//判断插入位置是否合法
if(i<1 || i>L.Length+1)
{
return false;
}
//判断顺序表是否已经存满
if(L.Length==MaxSize)
{
return false;
}
//第i个位置对应下标为i-1,所以原来下标是i-1以及之后的元素都要向后移动,j>=i-1
//for()括号内考虑循环结束时的边界条件
for(int j=L.Length;j>=i-1;j--)
{
//考虑循环开始时的边界条件
//第Length个元素对应下标为Length-1,移动到下标为Length的位置上
L.data[j]=L.data[j-1];
}
L.data[i-1]=element;//将新元素插入下标为i-1的位置上
L.Length++;//顺序表长度加1
return true;
}
//删除函数
bool ListDelete(SqList &L,int i,ElemType &del){
//判断删除的位置是否合法
if(i<1 || i>L.Length)
{
return false;
}
del=L.data[i-1];
for(int j=i;j<=L.Length-1;j++)
{
L.data[j-1]=L.data[j];
}
L.Length--;
return true;
}
//打印函数
void PrintList(SqList L){
for(int i=0;i<=L.Length-1;i++)
{
printf("%3d",L.data[i]);
}
printf("\n");
}
int main() {
SqList L;
bool ret;
ElemType element,del;
int i;//要删除元素的位置
L.data[0]=1;
L.data[1]=2;
L.data[2]=3;
L.Length=3;
scanf("%d",&element);
ret=ListInsert(L,2,element);
if(!ret)
{
printf("false\n");
return 0;
}
PrintList(L);
scanf("%d",&i);
ret=ListDelete(L,i,del);
if(!ret)
{
printf("false\n");
return 0;
}
PrintList(L);
return 0;
}
-
第17题
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
void PrintList(LinkList L){
L=L->next;
while(L!=NULL)
{
printf("%d",L->data);//打印当前结点数据
L=L->next;//指向下一个结点
if(L!=NULL)
{
printf(" ");
}
}
printf("\n");
}
LinkList List_HeadInsert(LinkList &L){
LNode *s;
ElemType x;
L=(LNode *)malloc(sizeof(LNode));
L->next=NULL;
scanf("%d",&x);
while(x!=9999){
s=(LNode *)malloc(sizeof(LNode));
s->data=x;
s->next=L->next;
L->next=s;
scanf("%d",&x);
}
return L;
}
LinkList List_TailInsert(LinkList &L){
ElemType x;
L=(LNode *)malloc(sizeof(LNode));
L->next=NULL;
LNode *s,*r=L;
scanf("%d",&x);
while(x!=9999){
s=(LNode *)malloc(sizeof(LNode));
s->data=x;
r->next=s;
r=s;
scanf("%d",&x);
}
r->next=NULL;
return L;
}
int main(){
LinkList L;
List_HeadInsert(L);
PrintList(L);
List_TailInsert(L);
PrintList(L);
return 0;
}
-
第18题
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
void PrintList(LinkList L)
{
L = L->next;
while (L != NULL)
{
printf("%3d", L->data);//打印当前结点数据
L = L->next;//指向下一个结点
}
printf("\n");
}
LinkList List_TailInsert(LinkList &L){
ElemType x;
L=(LNode *)malloc(sizeof(LNode));
L->next=NULL;
LNode *s,*r=L;
scanf("%d",&x);
while(x!=9999){
s=(LNode *)malloc(sizeof(LNode));
s->data=x;
r->next=s;
r=s;
scanf("%d",&x);
}
r->next=NULL;
return L;
}
LNode *GetElem(LinkList L,int i){
LNode *p=L;
int j=0;
if(i<0)
{
return NULL;
}
while(j<i && p!=NULL)
{
p=p->next;
j++;
}
return p;
}
bool ListInsert(LinkList &L,int i,ElemType e){
LNode *p=GetElem(L,i-1);
if(p==NULL)
{
return false;
}
LNode *s=(LNode *)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
bool ListDelete(LinkList &L,int i,ElemType &e){
LNode *p=GetElem(L,i-1);
if(p==NULL || p->next==NULL)
{
return false;
}
LNode *q=p->next;
e=q->data;
p->next=q->next;
free(q);
return true;
}
int main(){
LinkList L;
ElemType e;
List_TailInsert(L);
printf("%d\n",GetElem(L,2)->data);
ListInsert(L,2,99);
PrintList(L);
ListDelete(L,4,e);
PrintList(L);
return 0;
}
-
第19题
#include <stdio.h>
#define Size 50
typedef int ElemType;
typedef struct{
ElemType data[Size];
int top;
}SqStack;
#define MaxSize 5
typedef int ElemType;
typedef struct{
ElemType data[MaxSize];
int front,rear;
}SqQueue;
void InitQueue(SqQueue &Q){
Q.front=Q.rear=0;
}
bool EnQueue(SqQueue &Q,ElemType x){
if((Q.rear+1)%MaxSize==Q.front)
{
return false;
}
Q.data[Q.rear]=x;
Q.rear=(Q.rear+1)%MaxSize;
return true;
}
bool DeQueue(SqQueue &Q,ElemType &x){
if(Q.rear==Q.front)
{
return false;
}
x=Q.data[Q.front];
Q.front=(Q.front+1)%MaxSize;
return true;
}
void InitStack(SqStack &S){
S.top=-1;
}
bool Push(SqStack &S,ElemType x){
if(S.top==MaxSize-1)
{
return false;
}
S.data[++S.top]=x;
return true;
}
bool Pop(SqStack &S,ElemType &x){
if(S.top==-1)
{
return false;
}
x=S.data[S.top--];
return true;
}
int main(){
SqStack S;
InitStack(S);
ElemType x;
for(int i=1;i<=3;i++)
{
scanf("%d",&x);
Push(S,x);
}
for(int i=1;i<=3;i++)
{
Pop(S,x);
printf("%2d",x);
}
printf("\n");
SqQueue Q;
InitQueue(Q);
for(int i=1;i<=5;i++)
{
int ret;
scanf("%d",&x);
ret=EnQueue(Q,x);
if(!ret)
{
printf("false\n");
}
}
for(int i=1;i<=4;i++)
{
DeQueue(Q,x);
printf("%2d",x);
}
printf("\n");
return 0;
}
-
第20题
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef struct tagNode{
BiTNode *p;
struct tagNode *next;
}tagNode,*tag;
void PreOrder(BiTree T){
if(T!=NULL) {
printf("%c", T->data);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
int main(){
ElemType c;
BiTree T=NULL;
BiTNode *tnew;
tagNode *rear=NULL,*pcur=NULL,*s;
while(scanf("%c",&c))
{
if(c=='\n')
{
break;
}
tnew=(BiTNode *)calloc(1,sizeof(BiTNode));
tnew->data=c;
s=(tagNode *)calloc(1,sizeof(tagNode));
s->p=tnew;
if(T==NULL)
{
T=tnew;
rear=pcur=s;
}else{
rear->next=s;
rear=s;
if(pcur->p->lchild==NULL)
{
pcur->p->lchild=tnew;
}else if(pcur->p->rchild==NULL){
pcur->p->rchild=tnew;
pcur=pcur->next;
}
}
}
PreOrder(T);
return 0;
}
-
第21题
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef struct tagNode{
BiTNode *p;
struct tagNode *next;
}tagNode,*tag;
typedef struct LinkNode{
BiTNode *data;
struct LinkNode *next;
}LinkNode;
typedef struct{
LinkNode *front,*rear;
}LinkQueue;
bool IsEmpty(LinkQueue Q){
return Q.front==Q.rear;
}
void InitQueue(LinkQueue &Q){
Q.front=Q.rear=(LinkNode *)malloc(sizeof(LinkNode));
Q.front->next=NULL;
}
void EnQueue(LinkQueue &Q,BiTNode * x){
LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=x;
s->next=NULL;
Q.rear->next=s;
Q.rear=s;
}
bool DeQueue(LinkQueue &Q,BiTNode * &x){
if(Q.rear==Q.front)
{
return false;
}
LinkNode *p=Q.front->next;
x=p->data;
Q.front->next=p->next;
if(Q.rear==p)
{
Q.rear=Q.front;
}
free(p);
return true;
}
void InOrder(BiTree T){
if(T!=NULL) {
InOrder(T->lchild);
printf("%c", T->data);
InOrder(T->rchild);
}
}
void PostOrder(BiTree T){
if(T!=NULL) {
PostOrder(T->lchild);
PostOrder(T->rchild);
printf("%c", T->data);
}
}
void LevelOrder(BiTree T){
LinkQueue Q;
InitQueue(Q);
BiTNode *p;
EnQueue(Q,T);
while(!IsEmpty(Q))
{
DeQueue(Q,p);
putchar(p->data);
if(p->lchild!=NULL)
{
EnQueue(Q,p->lchild);
}
if(p->rchild!=NULL)
{
EnQueue(Q,p->rchild);
}
}
}
int main(){
ElemType c;
BiTree T=NULL;
BiTNode *tnew;
tagNode *rear=NULL,*pcur=NULL,*s;
while(scanf("%c",&c))
{
if(c=='\n')
{
break;
}
tnew=(BiTNode *)calloc(1,sizeof(BiTNode));
tnew->data=c;
s=(tagNode *)calloc(1,sizeof(tagNode));
s->p=tnew;
if(T==NULL)
{
T=tnew;
rear=pcur=s;
}else{
rear->next=s;
rear=s;
if(pcur->p->lchild==NULL)
{
pcur->p->lchild=tnew;
}else if(pcur->p->rchild==NULL){
pcur->p->rchild=tnew;
pcur=pcur->next;
}
}
}
InOrder(T);
printf("\n");
PostOrder(T);
printf("\n");
LevelOrder(T);
printf("\n");
return 0;
}
-
第22题
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct{
ElemType *elem;
int TableLen;
}SSTable;
typedef struct BSTNode{
ElemType data;
struct BSTNode *lchild,*rchild;
}BSTNode,*BiTree;
void InOrder(BiTree T,int str[]){
static int k=0;
if(T!=NULL) {
InOrder(T->lchild,str);
printf("%3d", T->data);
str[k++]=T->data;
InOrder(T->rchild,str);
}
}
bool BST_Insert(BiTree &T,ElemType m){
BSTNode * NewNode=(BSTNode *)calloc(1,sizeof(BSTNode));
NewNode->data=m;
if(T==NULL){
T=NewNode;
return true;
}
BSTNode * p=T,*parent=NULL;
while (p!=NULL){
parent=p;
if(m<p->data){
p=p->lchild;
}
else if(m>p->data){
p=p->rchild;
}
else{
return false;
}
}
if(m>parent->data){
parent->rchild=NewNode;
}else{
parent->lchild=NewNode;
}
return true;
}
void Creat_BST(BiTree &T,ElemType str[],int len){
T=NULL;
for(int i=0;i<len;i++)
BST_Insert(T,str[i]);
}
int Binary_Search(SSTable ST,ElemType key){
int low=0,high=ST.TableLen-1,mid;
while(low<=high)
{
mid=(low+high)/2;
if(key==ST.elem[mid])
{
return mid;
}
else if(ST.elem[mid]>key)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
return -1;
}
int main(){
BiTree T=NULL;
int str[10];
for(int i=0;i<10;i++){
scanf("%d",&str[i]);
}
Creat_BST(T,str,10);
InOrder(T,str);
printf("\n");
SSTable ST;
ST.elem=str;
ST.TableLen=10;
printf("%d",Binary_Search(ST,21));
return 0;
}
-
第24题
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
void swap(ElemType &a,ElemType &b){
ElemType temp=a;
a=b;
b=temp;
}
void print(ElemType A[],int len){
for(int i=0;i<len;i++){
printf("%3d",A[i]);
}
printf("\n");
}
void InsertSort(ElemType A[],int len){
int i,j;
ElemType temp;
for(i=1;i<len;i++){
if(A[i]<A[i-1]){
temp=A[i];
for(j=i-1;j>=0&&A[j]>temp;j--){
A[j+1]=A[j];
}
A[j+1]=temp;
}
}
}
void BubbleSort(ElemType A[],int len){
for(int i=0;i<len-1;i++){
bool flag= false;
for(int j=len-1;j>i;j--){
if(A[j]<A[j-1]){
swap(A[j],A[j-1]);
flag= true;
}
}
if(!flag){
return;
}
}
}
int Partition(ElemType A[],int low,int high){
ElemType pivot=A[low];
while(low<high){
while(low<high&&A[high]>=pivot){
high--;
}
A[low]=A[high];
while(low<high&&A[low]<=pivot){
low++;
}
A[high]=A[low];
}
A[low]=pivot;
return low;
}
void QuickSort(ElemType A[],int low,int high){
if(low<high){
int pivotpos= Partition(A,low,high);
QuickSort(A,low,pivotpos-1);
QuickSort(A,pivotpos+1,high);
}
}
int main(){
ElemType A[10],B[10],C[10];
for(int i=0;i<=9;i++){
scanf("%d",&A[i]);
B[i]=C[i]=A[i];
}
BubbleSort(A,10);
print(A,10);
InsertSort(B,10);
print(B,10);
QuickSort(C,0,9);
print(C,10);
return 0;
}
-
第25题
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
void swap(ElemType &a,ElemType &b){
ElemType temp=a;
a=b;
b=temp;
}
void print(ElemType A[],int len){
for(int i=0;i<len;i++){
printf("%3d",A[i]);
}
printf("\n");
}
void printA(ElemType A[],int len){
for(int i=1;i<len;i++){
printf("%3d",A[i]);
}
printf("\n");
}
void SelectSort(ElemType A[],int len){
for(int i=0;i<len-1;i++){
int min=i;
for(int j=i+1;j<len;j++){
if(A[j]<A[min]){
min=j;
}
}
if(min!=i){
swap(A[i],A[min]);
}
}
}
void HeadAdjust(ElemType A[],int key,int len){
A[0]=A[key];
for(int i=2*key;i<=len;i=2*i){
if(i<len&&A[i]<A[i+1]){
i++;
}
if(A[0]>A[i]){
break;
}else{
A[key]=A[i];
key=i;
}
}
A[key]=A[0];
}
void BuildMaxHeap(ElemType A[],int len){
for(int i=len/2;i>0;i--){
HeadAdjust(A,i,len);
}
}
void HeapSort(ElemType A[],int len){
BuildMaxHeap(A,len);
for(int i=len;i>1;i--){
swap(A[i],A[1]);
HeadAdjust(A,1,i-1);
}
}
int main(){
ElemType A[11],B[10];
A[0]=0;
for(int i=0;i<=9;i++){
scanf("%d",&B[i]);
A[i+1]=B[i];
}
SelectSort(B,10);
print(B,10);
HeapSort(A,11);
printA(A,11);
return 0;
}
-
第26题
#include<stdio.h>
#define N 10
typedef int ElemType;
void print(ElemType A[],int len){
for(int i=0;i<len;i++){
printf("%3d",A[i]);
}
printf("\n");
}
void Merge(ElemType A[],int low,int mid,int high){
static ElemType B[N];
int i=low,j=mid+1,k;
for(k=low;k<=high;k++){
B[k]=A[k];
}
for(k=low;i<=mid&&j<=high;k++){
if(B[i]<=B[j]){
A[k]=B[i++];
}else{
A[k]=B[j++];
}
}
while(i<=mid){
A[k++]=B[i++];
}
while(j<=high){
A[k++]=B[j++];
}
}
void MergeSort(ElemType A[],int low,int high){
if(low<high){
int mid=(low+high)/2;
MergeSort(A,low,mid);
MergeSort(A,mid+1,high);
Merge(A,low,mid,high);
}
}
int main(){
ElemType A[10];
for(int i=0;i<=9;i++){
scanf("%d",&A[i]);
}
MergeSort(A,0,9);
print(A,10);
return 0;
}