东北大学842考研

1998-c-一

void merge(LNode *A,LNode * B) { LNode *r = A;//用于指向新链表的链尾,A为新链表的头指针 LNode *p = A->next; LNode *q = B->next; free(B); while(p->next!=NULL && q->next!=NULL) { //按照从小到大顺序排列 if(p->data <q->data) { r->next=p; r=r->next; p=p->next; }else{ r->next=q; r=r->next; q=q->next; } } if(p->next!=NULL) r->next=p; if(q->next!=NULL) r->next=q; }

1997-d-三

#include <stdio.h> //数组p中有n个数 void move (int p[],int n) { int i=0; int j=n-1; int t=p[0]; while(i<j) { while(i<j&&p[j]%2==0) j--;//从后往前找到奇数位置 if(i<j) { p[i]=p[j];//将找到的奇数移到前面 i++;//开始位置后移 } while(i<j&&p[i]%2==1) i++;//从前往后找到偶数位置 if(i<j) { p[j]=p[i]; j--;//结束位置前移 } } p[i]=t; } int main() { int p[] = {2,6,1,5,14,3,18,9,10,11}; for(int i=0;i<10;i++) printf("%d\n",p[i]); printf("-----------\n"); move(p,10); for(int i=0;i<10;i++) printf("%d\n",p[i]); }

1996-c-8

//假设有那个元素已经存在于数组a中,用尾插法建立链表 //需要改变的变量用引用型 void creatListR(LNode *&c,int a[],int n) { LNode *s,*r; int i; c=(LNode *)malloc(sizeof(LNode)); c->next=NULL; r=c; for(i=1;i<=n;++i) { s=(LNode *)malloc(sizeof(LNode)); s->data=a[i]; r->next=s; r=r->next; } r->next=NULL; }

1996-c-六

#include <stdio.h> //汉诺塔移动,递归算法 //将n个盘子从 x 借助 y 移动到 z void move (int n,char x,char y,char z){ if (n==1) printf("%c-->%c\n",x,z); else { move (n-1,x,z,y); printf("%c-->%c\n",x,z); move(n-1,y,x,z); } } int main(){ int n; printf("请输入汉诺塔的层数:"); scanf("%d",&n); printf("移动步骤如下:\n"); move(n,'X','Y','Z'); return 0; }

1997-c-三

#include <stdio.h>#include <stdlib.h>//不能使用数组,但是可以用malloc生成一串连续的存储单元 typedef struct StuInfo { int grades; int stu_id; }StuInfo; int main() { int N;//输入的学生人数 StuInfo *buffer = NULL;//指向连续的存储空间 int i = 0; int sum = 0; int averge = 0; scanf("%d", &N); printf("%d\n", N); buffer =(StuInfo *)malloc(sizeof(StuInfo) * N); //输入学生信息,并统计学生成绩的总和 for (i = 0 ; i < N; i++) { printf("Enter student information inluding grades and stu_id.\n"); scanf("%d%d", &((buffer + i)->grades), &((buffer + i)->stu_id)); sum += (buffer + i)->grades; } averge = sum / N;//计算学生的平均值 //求大于平均分的学生的成绩,并输入成绩和学号 for (i = 0; i < N; i++) { if ((buffer + i)->grades >= averge) { printf("%d %d\n", (buffer + i)->grades, (buffer + i)->stu_id); } } }

1997-c-四

#include <stdio.h>#include <stdlib.h>/* **数组中有N个元素,要移动K个位置,那么数组的大小最小是N+K **宏定义N与K */ #define N 3 //存储的数据 #define K 2 //移动的位置 #define MAXSIZE (N + K) void EnterIntegers(int *p); void Move_k_Position(int *p); void ShowArray(int *p); int main() { int A[MAXSIZE]; //输入N个整数 EnterIntegers(A); //移动K个位置 Move_k_Position(A); //显示数组元素 ShowArray(A); } //输入N个整数 void EnterIntegers(int *p) { int i = 0; printf("Enter N integers.\n"); for (i = 0; i < N; i++) { scanf("%d", p + i); printf("%d\n", *(p + i)); } } //移动K个位置 void Move_k_Position(int *p) { int i = 0; p = p + N -1; //从头部开始,每个元素都往后移动K个位置 for (i = N - 1 ; i >= 0; i--) { *(p + K) = *p; *p = -1;//移动后将数据域清零 p--; } //输出数组中的数据域中的内容 } //显示数组元素 //空出来的位置以-1标记 void ShowArray(int *p) { int i = 0; for (i = 0; i < N + K; i++) { printf("%d\n", p[i]); } }

1996-d-五

/** 交换二叉树每个结点左右孩子节点(非递归算法) 算法的基本思想: 1.判断根节点地址是否为空,为空返回 2.将结点放入顺序队列中,每次出队一个结点 3.交换该节点的左右孩子结点,并将孩子结点插入队尾 **/ void change(BTNode *p){ if(p==null) retrun;//判断树是否为空 BTNode *queue[maxSize];//定义一个队列用于存储结点 BTNode *out,*temp;//out为出队指针、temp用于交换左右子树 int front=0;//队列头尾 int rear=0; queue[front]=p;//初始化队列 queue[rear]=p; //循环交换左右子树 while(rear<front) { out=queue[front++]; //出队 //交换左右子树 temp=out->lchild; out->lchild=out->rchild; ouot->rchild=temp; //将交换完成结点插入队列 if(out->lchild!=NULL) queue[++rear]=out->lchild; if(out->rchild!=NULL) queue[++rear]=out->rchild; } } /** 递归算法 **/ void change(BTNode *p){ if(p==NULL)return; BTNode *temp; temp=p->lchild; p->lchild=p->rchild; p->rchild=temp; change(p->lchild); change(p->rchild); }

1996-c-二

#include <stdio.h>#include <stdlib.h> #define MAXSIZE 100 int main() { int reverse_string(char *string);//函数原型声明 char str[MAXSIZE];//数组最大下标 scanf("%s", str); printf("%s\n", str); reverse_string(str); } int reverse_string(char *string) { char *tail = NULL; char *head = NULL; head = string; tail = string; while(*(++tail));//调整tail指针指向字符串尾部 tail--;//在while判断时多移动一个位置 printf("*head:%c\n", *head); printf("*tail:%c\n", *tail); while ((tail >= head) && (*tail== *head)) { //当前位置对称,指针往后移动 //不对称,或者尾指针小于头指针,就结束比较 tail--; head++; } //尾指针大于等于头指针,说明不是回文 //因为没有比较完字符串中的所有元素就中断了循环,存在*tail != *head的情况 if (tail >= head) { printf("Not reverse string.

1996-c-四

//基本思想生成一条新串,不是在原有串上进行复杂操作 int delsubStr(char* str_in,char* str_sub, char* str_out) { int start = 0; /* 记录开始比较下标 */ int count = 0; /* 记录子串个数 */ int j = 0; /* 记录子串的下标 */ int k = 0; /* 记录结果字符串的下标 */ int i; /*循环变量*/ for (i = 0; str_in[i] != '\0'; i++) { start = i; /* 临时保存比较下标 */ j = 0; /* 每次开始比较的时候,子串都从0开始,如果第一个字符相等, 那么就接着比较下一个字符,否则进入下一轮循环 */ while ((str_in[i] != '\0')&&(str_sub[j] != '\0')&&(str_in[i] == str_sub[j])) { i++; /* 源串后移一个位置 */ j++; /* 字串后移一个位置 */ } if (str_sub[j] !