代码我放在下面了
恳请各位大佬帮忙看看(能上优快云的除我都是大佬哈哈哈)
连续写三个题目都是RUNTIME ERROR!!!
找指针越界找的心力交瘁也没找出来!!!
呜呜呜
这是第一题的题目
计算二叉树的高度和结点数
Description:
二叉树是非常重要的树形数据结构,根据该树的先序、中序或后序遍历序列可以建立一棵二叉树。例如输入先序遍历序列A B # D # # C E # # F # #可以建立图1019-1所示的二叉树,这里用#代表空树或空子树(另一种说法:若无孩子结点,则用#代替),如图1019-2。
图1019-1
图1019-2请实现基于遍历的二叉树运算:求高度、计算结点数目 Input: 二叉树的先序遍历序列,用#代表空树或空子树。
Output: 共五行
前三行依次输出先序、中序和后序遍历序列,
第四行输出二叉树的高度,
第五行依次输出二叉树总结点数目、叶子结点数目、度为1的结点数目。
Sample Input:
A B # D # # C E # # F # #
Sample Output:
PreOrder:A B D C E F
InOrder: B D A E C F
PostOrder: D B E F C A
3
6 3 1
下面是我写的代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ERROR 0
#define OK 1
static i = 0; //遍历数组所需变量
typedef struct BinaryTreeNode
{
char Data;
struct BinaryTreeNode *lChild, *rChild;
}BinaryTreeNode;
typedef struct BinaryTree
{
BinaryTreeNode *root;
}BT;
void Input(char *a);
BinaryTreeNode* PreCreateBinaryTree(BinaryTreeNode *t, char *a);
void PreOrderTransverse(BinaryTreeNode *t);
void InOrderTransverse(BinaryTreeNode *t);
void PostOrderTransverse(BinaryTreeNode *t);
void getLeavesCount(BinaryTreeNode* T, int *count);
int Height(BinaryTreeNode *t) //强
{
int height;
int leftHeight = 0;
int rightHeight = 0;
if (t->lChild != NULL)
leftHeight = Height(t->lChild);//得到左子树高度
if (t->rChild != NULL)
rightHeight = Height(t->rChild);//得到右子树高度
height = (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;//返回高度
return height;
}
void OneChildCount(BinaryTreeNode* T, int *count);
int main()
{
BT t;
int count = 0, index;
char a[50];
Input(a);
t.root = NULL;
t.root = PreCreateBinaryTree(&t.root, a);
printf("PreOrder:");
PreOrderTransverse(t.root);
printf("\n");
printf("InOrder:");
InOrderTransverse(t.root);
printf("\nPostOrder:");
PostOrderTransverse(t.root);
printf("\n");
printf("%d\n", Height(t.root));
for (index = 0; index < strlen(a); index++)
{
if (*(a + index) == '\0') //字符串结束就退出循环
break;
if (*(a + index) != '#')
count++;
}
printf("%d ", count);
count = 0;
getLeavesCount(t.root, &count);
printf("%d ", count);
count = 0;
OneChildCount(t.root, &count);
printf("%d", count);
getchar(); getchar();
return 0;
}
void Input(char *a)
{
char ch;
int x = 0;
while ((ch = getchar()) != '\n')
{
if (ch == ' ')
continue;
*(a + x) = ch;
x++;
}
*(a + x) = '\0'; //作为字符串结尾标志
}
BinaryTreeNode* PreCreateBinaryTree(BinaryTreeNode *t, char *a)
{
char ch;
t = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
ch = *(a + i);
i++;
if (ch == '#')
t = NULL;
else
{
t->Data = ch;
t->lChild = PreCreateBinaryTree(t->lChild, a);
t->rChild = PreCreateBinaryTree(t->rChild, a);
}
return t;
}
void PreOrderTransverse(BinaryTreeNode *t)
{
if (t != NULL)
{
printf(" %c", t->Data);
PreOrderTransverse(t->lChild);
PreOrderTransverse(t->rChild);
}
}
void InOrderTransverse(BinaryTreeNode *t)
{
if (t != NULL)
{
InOrderTransverse(t->lChild);
printf(" %c", t->Data);
InOrderTransverse(t->rChild);
}
}
void PostOrderTransverse(BinaryTreeNode *t)
{
if (t != NULL)
{
PostOrderTransverse(t->lChild);
PostOrderTransverse(t->rChild);
printf(" %c", t->Data);
}
}
void getLeavesCount(BinaryTreeNode* T, int *count)
{
if (T != NULL && T->lChild == NULL && T->rChild == NULL)
*count = *count + 1;
if (T)
{
getLeavesCount(T->lChild, count);
getLeavesCount(T->rChild, count);
}
}
void OneChildCount(BinaryTreeNode* T, int *count)
{
if (T != NULL && ((T->lChild == NULL && T->rChild != NULL) || (T->lChild != NULL && T->rChild == NULL)))
*count = *count + 1;
if (T)
{
OneChildCount(T->lChild, count);
OneChildCount(T->rChild, count);
}
}
第二题题目:
多项式加法 1000ms 65536K
Description:
线性表是一种最简单、最基本,也是最常用的数据结构,其用途十分广泛,例如,用带表头结点的单链表求解一元整系数多项式加法和乘法运算。现给两个一元整系数多项式,请求解两者之和。
Input:
两组数据,每一组代表一个一元整系数多项式,有多行组成,其中每一行给出多项式每一项的系数和指数,这些行按指数递减次序排序,每一组结束行为0 -1
Output: 三组数据,前两组为一元整系数多项式,最后一组为两个多项式的和。
一元整系数多项式输出形式如下:
(1)多项式项4x输出为4X
(2)多项式项4x2输出为4X^2
(3)第一项系数为正数时,加号不要输出
(4)除常系数项外,项系数为1不显式输出,-1输出为-
例如,4x3- x2+x-1正确输出形式为4X3-X2+X-1,错误输出形式为 +4X3-1X2+1X-1
Sample Input:
3 14
-8 8
6 2
2 0
0 -1
2 10
4 8
-6 2
0 -1
Sample Output:
3X^14 - 8X^8 + 6X^2 + 2
2X^10 + 4X^8 - 6X^2
3X^14 + 2X^10 - 4X^8 + 2
第二题代码:
#include<stdio.h>
typedef struct pNode {
int coef; //系数
int exp; //指数
struct pNode* link; //连接下一项的指针
}PNode;
typedef struct polynominal {
PNode *head;
}Polynominal; //带表头结点的单链表
void Init(Polynominal* p)
{
p->head = (PNode*)malloc(sizeof(PNode));
p->head->coef = 0;
p->head->exp = -1;
p->head->link = p->head; //单循环链表
}
void Create(Polynominal* p)
{
PNode *pn, *pre, *q;
Init(p);
while (1)
{
pn = (PNode*)malloc(sizeof(PNode)); //循环每次开始时初始化pn
scanf_s("%d%d", &pn->coef, &pn->exp);
if (pn->exp == -1)
break;
if (pn->coef == 0)
continue;
pre = p->head;
q = pre->link;
while (q && q->exp > pn->exp) //多项式按指数从大到小排列
{
pre = q;
q = q->link;
}
pn->link = q;
pre->link = pn; //插入pn到单链表中
}
}
void Copy(PNode* a, PNode* b)
{
a->coef = b->coef;
a->exp = b->exp;
a->link = b->link;
}
Polynominal* Add(Polynominal* p1, Polynominal* p2)
{
Polynominal* p; //作为输出
PNode *pn1, *pn2,*pn,*temp;
p = (Polynominal*)malloc(sizeof(Polynominal));
Init(p);
temp = (PNode*)malloc(sizeof(PNode));
pn = p->head;
pn1 = p1->head->link;
pn2 = p2->head->link;
while (!(pn1->exp == -1 && pn2->exp == -1))
{
temp = (PNode*)malloc(sizeof(PNode));
if (pn1->exp > pn2->exp)
{
Copy(temp, pn1);
temp->link = pn->link;
pn->link = temp;
pn1 = pn1->link;
pn = pn->link;
}
else if (pn1->exp < pn2->exp)
{
Copy(temp, pn2);
temp->link = pn->link;
pn->link = temp;
pn2 = pn2->link;
pn = pn->link;
}
else
{
temp->coef = pn1->coef + pn2->coef;
temp->exp = pn1->exp;
temp->link = pn->link;
pn->link = temp;
pn1 = pn1->link;
pn2 = pn2->link;
pn = pn->link;
}
}
return p;
}
void Output(Polynominal* p)
{
PNode *pn;
pn = p->head->link; //第一个数据结点
if (pn->exp != 1 && pn->exp != 0 && pn->coef != 1 && pn->coef != 0)
printf("%dX^%d", pn->coef, pn->exp);
else if (pn->exp != 1 && pn->exp != 0 && pn->coef == 1)
printf("X^%d", pn->exp);
else if (pn->exp == 0 && pn->coef != 0)
printf("%d", pn->coef);
else if (pn->exp == 1 && pn->coef != 1 && pn->coef != 0)
printf("%dX", pn->coef);
else if (pn->exp == 1 && pn->coef == 1)
printf("X");
else if (pn->exp < 0)
return;
pn = pn->link;
while (pn->exp >= 0)
{
if (pn->coef > 0 && pn->coef != 1 && pn->exp != 1 && pn->exp != 0)
printf("+%dX^%d", pn->coef, pn->exp);
else if (pn->coef > 0 && pn->coef != 1 && pn->exp == 1)
printf("+%dX", pn->coef);
else if (pn->coef > 0 && pn->exp == 0)
printf("+%d", pn->coef);
else if (pn->coef < 0 && pn->coef != -1 && pn->exp != 1 && pn->exp != 0)
printf("%dX^%d", pn->coef, pn->exp);
else if (pn->coef < 0 && pn->coef != -1 && pn->exp == 1)
printf("%dX", pn->coef);
else if (pn->coef < 0 && pn->exp == 0)
printf("%d", pn->coef);
else if (pn->coef == 1 && pn->exp != 1 && pn->exp != 0)
printf("+X^%d", pn->exp);
else if (pn->coef == -1 && pn->exp != 1 && pn->exp != 0)
printf("X^%d", pn->exp);
else if (pn->coef == 1 && pn->exp == 1)
printf("+X");
else if (pn->coef == -1 && pn->exp == 1)
printf("-X");
pn = pn->link;
}
}
void main()
{
Polynominal *p1, *p2,*p;
p1 = (Polynominal*)malloc(sizeof(Polynominal));
p2 = (Polynominal*)malloc(sizeof(Polynominal));
Create(p1);
Create(p2);
p = Add(p1, p2);
Output(p1);
printf("\n");
Output(p2);
printf("\n");
Output(p);
getchar(); getchar();
}
第三题和第二题差不多,这里我就不多写了。。。
真心力交瘁
谢谢看到这里的大大大大大佬!!!
拜托帮忙解答一个问题也行!!!
谢谢!