7-1 栈操作的合法性#
#include <stdio.h>
int main() {
int n,m,c,i,length;
scanf("%d %d ",&n,&m);
for(i=0;i<n;i++){
char a;
int c=0,length=0;
while((a=getchar())!='\n'){
if(a=='S'){
length++;
if(length>m){
c=1;
}
}
if(a=='X'){
length--;
if(length<0){
c=1;
}
}
}
if(c==0&&length==0){
printf("YES\n");
}else{
printf("NO\n");
}
}
return 0;
}
7-3 合并有序数组#
#include <stdio.h>
#include <stdlib.h>
int main(){
int m,n,i;
int *arr1,*arr2;
scanf("%d",&m);
arr1=(int*)malloc(m*sizeof(int));
if(arr1==NULL){
return 1;
}
for(i=0;i<m;i++){
scanf("%d",&arr1[i]);
}
arr2=(int*)malloc(n*sizeof(int));
scanf("%d",&n);
if(arr2==NULL){
free(arr1);
return 1;
}
for(i=0;i<n;i++){
scanf("%d",&arr2[i]);
}
int j=0;
i=0;
while(i<m || j<n){
if(i>0 || j>0){
printf(" ");
}
if(i<m && (arr1[i]<=arr2[j] || j>=n)){
printf("%d",arr1[i]);
i++;
}else{
printf("%d",arr2[j]);
j++;
}
}
printf("\n");
free(arr1);
free(arr2);
return 0;
}
7-4 顺序表的查找#
#include <stdio.h>
#include <stdlib.h>
int main() {
int n,i;
int *arr1;
scanf("%d", &n);
arr1 = (int*)malloc(n * sizeof(int));
for(i=0;i<n;i++){
scanf("%d", &arr1[i]);
}
int target;
while(scanf("%d", &target) == 1 && target != -1){
int found = 0;
for(i=0;i<n;i++){
if(arr1[i] == target){
printf("%d ",i+1);
found = 1;
break;
}
}
if(found==0){
printf("%d ",0);
}
}
free(arr1);
return 0;
}
6-4 统计二叉树叶子结点个数#
int LeafCount ( BiTree T)
{
if(T == NULL){
return 0;
}
if(T->lchild == NULL && T->rchild == NULL){
return 1;
}
else{
return LeafCount(T->lchild) + LeafCount(T->rchild);
}
}
6-1 二叉排序树的查找操作#
BSTree SearchBST(BSTree T, ElemType e)
{
if(T == NULL || T->data == e){
return T;
}
if(T->data > e){
return SearchBST(T->lchild, e);
} else {
return SearchBST(T->rchild, e);
}
}
6-2 求二叉树的高度#
int GetHeight( BinTree BT )
{
if(BT==NULL){
return 0;
}else{
int lt,rt,result;
lt=GetHeight(BT->Left);
rt=GetHeight(BT->Right);
if(lt>rt){
result=lt;
}else{
result=rt;
}
return result+1;
}
}
6-3 统计二叉树结点个数#
int NodeCount ( BiTree T)
{
int count=0;
if(T!=NULL){
count++;
count+=NodeCount (T->Left);
count+=NodeCount (T->Right);
}
return count;
}
6-5 先序输出叶结点#
void PreorderPrintLeaves( BinTree BT )
{
if(BT==NULL){
return;
}else{
if(BT->Left==NULL && BT->Right==NULL){
printf("%c",BT->Data);
}
PreorderPrintLeaves(BT->Left);
PreorderPrintLeaves(BT->Right);
}
}
R6-2 另类循环队列#
bool AddQ(Queue Q,ElementType X){
if(Q->Count==Q->MaxSize){
printf("Queue Full\n");
return false;
}
Q->Data[(Q->Count+Q->Front)%Q->MaxSize]=X;
Q->Count++;
return true;
}
ElementType DeleteQ(Queue Q){
if(Q->Count==0){
printf("Queue Empty\n");
return ERROR;
}
int a=Q->Data[Q->Front];
Q->Count--;
Q->Front=(Q->Front+1)%Q->MaxSize;
return a;
}
R6-9 在一个数组中实现两个堆栈#
Stack CreateStack(int MaxSize)
{
Stack S;
S = (Stack)malloc(sizeof(struct SNode));
S->Data=(ElementType*)malloc(MaxSize*sizeof(ElementType));
S->MaxSize=MaxSize;
S->Top1=-1;
S->Top2=MaxSize;
return S;
}
bool Push(Stack S,ElementType X,int Tag)
{
if(S->Top2-S->Top1==1){
printf("Stack Full\n");
return false;
}else{
if(Tag==1){
S->Top1++;
S->Data[S->Top1]=X;
return true;
}
if(Tag==2){
S->Top2--;
S->Data[S->Top2]=X;
return true;
}
}
}
ElementType Pop(Stack S,int Tag)
{
if(Tag==1){
if(S->Top1==-1){
printf("Stack %d Empty\n",Tag);
return ERROR;
}
int a=S->Data[S->Top1];
S->Top1--;
return a;
}
if(Tag==2){
if(S->Top2==S->MaxSize){
printf("Stack %d Empty\n",Tag);
return ERROR;
}
int a=S->Data[S->Top2];
S->Top2++;
return a;
}
}
R6-8 二叉树的三种遍历(先序、中序和后序)#
void Preorder(BiTree T)
{
if(T==NULL){
return;
}else{
printf(" %c",T->data);
Preorder(T->lchild);
Preorder(T->rchild);
}
}
void Inorder(BiTree T)
{
if(T==NULL){
return;
}else{
Inorder(T->lchild);
printf(" %c",T->data);
Inorder(T->rchild);
}
}
void Postorder(BiTree T)
{
if(T==NULL){
return;
}else{
Postorder(T->lchild);
Postorder(T->rchild);
printf(" %c",T->data);
}
}
R6-7 输出二叉树的所有叶子#
void leaf(Bptr p)
{
if(p==NULL){
return;
}else{
if(p->Lson==NULL&&p->Rson==NULL){
printf("%d ",p->data);
}
leaf(p->Lson);
leaf(p->Rson);
}
}
R6-10 递增的整数序列链表的插入#
List Insert(List L,ElementType X)
{
List a;
a=L;
while(1){
if(a->Next!=NULL&&a->Next->Data>=X){
List newNode;
newNode=(List)malloc(sizeof(struct Node));
newNode->Next=a->Next;
newNode->Data=X;
a->Next=newNode;
return L;
}else{
if(a->Next==NULL){
List newNode;
newNode=(List)malloc(sizeof(struct Node));
newNode->Next=NULL;
newNode->Data=X;
a->Next=newNode;
return L;
}
}
a=a->Next;
}
}
R6-12 统计二叉树度为1的结点个数#
int NodeCount(BiTree T)
{
if(T==NULL){
return 0;
}
int count = 0;
if((T->lchild!=NULL&&T->rchild==NULL)||(T->lchild==NULL&&T->rchild!=NULL)){
count=1;
}
return count+NodeCount(T->lchild)+NodeCount(T->rchild);
}