帮忙写一个VC++的程序,急用。五十行以上。跪求。

2024-12-28 16:49:27
推荐回答(2个)
回答1:

  /******************合并线性表程序**********************/
#include

int merge(int *a,int n1,int *b,int n2,int *s)
{
int i=0,j=0,k=0;
while(i {
if(a[i]>b[j]){
if(k&&s[k-1]==b[j])/*有重复元素*/
j++;
else
s[k++]=b[j++];
}
else{
if(k&&s[k-1]==a[i])/*有重复元素*/
i++;
else
s[k++]=a[i++];
}
}
while(i if(k&&s[k-1]==a[i])
i++;
else
s[k++]=a[i++];
}
while(j if(k&&s[k-1]==b[j])/*有重复元素*/
j++;
else
s[k++]=b[j++];
}
return k;
}
int main()
{
int a[100],b[100],s[200],n1,n2,i,n3;
printf("input number count of first list");
scanf("%d",&n1);
printf("input %d numbers:",n1);
for(i=0;i scanf("%d",&a[i]);
printf("input number count of second list");
scanf("%d",&n2);
printf("input %d numbers:",n1);
for(i=0;i scanf("%d",&b[i]);
n3=merge(a,n1,b,n2,s); /*a和b数组的元素合并到s数组里去(并消除重复元素),并返回合并后数组的元素个数*/
printf("merged list:");
for(i=0;i printf("%d ",s[i]);
printf("\n");
return 0;
}

/*
附测试数据:
5
3 50 50 100 100
3
8 50 80
输出为:3 8 50 80 100

3
2 3 4
3
3 4 5
输出为:2 3 4 5

4
1 3 5 7
4
1 3 5 7
输出为:1 3 5 7

*/
/**二叉树程序*/
#include
#include
#include
struct treenode {/* 二叉树结构 */
struct treenode *left;
struct treenode *right;
char value;
};

struct nodelist {/* 链表结构用于非递归前序遍历该二叉树 */
struct treenode *node;
struct nodelist *next;
};

char *LRD1 = "DEBFCA"; /* 测试数据1,后序 */
char *LDR1 = "DBEAFC"; /* 测试数据1,中序 */

char *LRD2 = "HIDJEBKFLMGCA"; /* 测试数据2,后序 */
char *LDR2 = "HDIBEJAKFCLGM"; /* 测试数据2,中序 */

char *LRD3 = "DEBFGCA"; /* 测试数据3,后序 */
char *LDR3 = "DBEAFCG"; /* 测试数据3,中序 */

void dlrtree(struct treenode *root) /* 非递归前序遍历该二叉树 */
{
struct treenode *node = root;
struct nodelist *last = NULL, *tmp = NULL, *ptr;

/*
* 非递归前序遍历方法的思想,构造一个队列(链表)
* 不停的把当前节点的左右子树都放到队列尾部
* 遍历指针从链表头开始逐个移动到链表尾部,就是前序遍历的结果
*/
ptr = (struct nodelist *)malloc(sizeof(struct nodelist));
if (ptr == NULL)
return;
ptr->node = root;
ptr->next = NULL;
last = ptr;
printf("dlrtree:");
while(ptr != NULL)
{
node = ptr->node;
printf("%c", node->value);

if (node->left != NULL)/* 有左子树,则加入到链表尾部 */
{
tmp = (struct nodelist *)malloc(sizeof(struct nodelist));
if (tmp == NULL)
return;
last->next = tmp;
last = tmp;
tmp->next = NULL;
last->node = node->left;
}

if (node->right != NULL)/* 有右子树,则加入到链表尾部 */
{
tmp = (struct nodelist *)malloc(sizeof(struct nodelist));
if (tmp == NULL)
return;
last->next = tmp;
last = tmp;
tmp->next = NULL;
last->node = node->right;
}
tmp = ptr;
ptr = ptr->next; /* ptr指向当前遍历到的节点,处理完当前节点以后,向

后移动,并释放前一个节点内存 */
free(tmp);
}
printf("\n");
}

void replacechar(char *str)/* 用于显示二叉树使用 */
{
int i;
for (i = 0; i < strlen(str); i++)
{
if (str[i] == '+') str[i] = '|';
if (str[i] == '\\') str[i] = ' ';
if (str[i] == '-') str[i] = ' ';
}
}

void displaytree(struct treenode *root, char *deepstr) /* 图形显示一个二叉树 */
{
int l;
char *ptr;
printf("%s%c\n", deepstr, root->value);
l = strlen(deepstr) + 4;
ptr = (char *)malloc(l);
strcpy(ptr, deepstr);
replacechar(ptr);
if (root->left != NULL && root->right != NULL)
{
printf("%s |\n", ptr);
strcat(ptr, " +-");
displaytree(root->left, ptr);
strcpy(ptr, deepstr);
replacechar(ptr);
printf("%s |\n", ptr);
strcat(ptr, " \\-");
displaytree(root->right, ptr);
}
else if (root->left != NULL)
{
printf("%s |\n", ptr);
strcat(ptr, " \\-");
displaytree(root->left, ptr);
}
else if (root->right != NULL)
{
printf("%s |\n", ptr);
strcat(ptr, " \\-");
displaytree(root->right, ptr);
}

return ;

}
/* 根据后序,中序,生成一个二叉树 */
/* lrdlst是后续字串 */
/* ldrlst是中序字串 */
struct treenode *createtree(struct treenode *root, char *lrdlst, char *ldrlst)
{
char *llrd, *lldr;
char *rlrd, *rldr;
int len;
int i, k;
len = strlen(lrdlst);
/* 基本思想,取得当前这个串对应的根节点,然后分别生成左、右子树的后续和中序

字串并递归调用 */

/* 申请 左右子树的字串空间 */
llrd = (char *)malloc(len + 1);
lldr = (char *)malloc(len + 1);
rlrd = (char *)malloc(len + 1);
rldr = (char *)malloc(len + 1);

/* 异常处理 */
if (llrd == NULL || lldr == NULL || rlrd == NULL || rldr == NULL)
{
if (llrd != NULL)
free(llrd);
if (lldr != NULL)
free(lldr);
if (rlrd != NULL)
free(rlrd);
if (rldr != NULL)
free(rldr);
return NULL;
}

/* 申请根节点 */
if (root == NULL && strlen(ldrlst) > 0)
{
root = (struct treenode *)malloc(sizeof(struct treenode));
if (root == NULL)
{
free(llrd);
free(lldr);
free(rlrd);
free(rldr);
return NULL;
}
}/* 这是一种错误情况 要直接退出 */
else if (root == NULL && strlen(ldrlst) == 0)
{
return NULL;
}
/* 找到根节点 */
root->value = lrdlst[len - 1];

strcpy(lldr, ldrlst);
/* 判断根节点是否有左子树 */
*strchr(lldr, root->value) = 0;
if (strlen(lldr) == 0)
root->left = NULL;
else
{
/* 申请左子树的空间并递归 */
root->left = (struct treenode *)malloc(sizeof(struct treenode));
k = 0;
for (i = 0; i < len; i++)
{
if (strchr(lldr, lrdlst[i]) != NULL)
{
llrd[k++] = lrdlst[i];
}
}
llrd[k] = 0;
createtree(root->left, llrd, lldr);
}
strcpy(rldr, ldrlst);
strcpy(rldr, strchr(rldr, root->value) + 1);
/* 判断右子树情况 */
if (strlen(rldr) == 0)
root->right = NULL;
else
{
/* 同上 递归 */
root->right = (struct treenode *)malloc(sizeof(struct treenode));
k = 0;
for (i = 0; i < len; i++)
{
if (strchr(rldr, lrdlst[i]) != NULL)
{
rlrd[k++] = lrdlst[i];
}
}
rlrd[k] = 0;
createtree(root->right, rlrd, rldr);
}
/* 释放临时空间 */
free(llrd);
free(lldr);
free(rlrd);
free(rldr);
return root;
}

int main()
{
struct treenode * r;
/* 把 LRD1和LDR1 分别换成全部换成2或者3就可以更换测试数据 */
r = createtree(NULL, LRD2, LDR2);/* 创建一棵树 */
printf("Tree View\n");/* 显示树 */
displaytree(r, "");
dlrtree(r);/* 前序遍历 */
return 0;;
}
/*

char *LRD1 = "DEBFCA"; 测试数据1,后序
char *LDR1 = "DBEAFC"; 测试数据1,中序

预期结果:ABCDEF
实际结果:ABCDEF

char *LRD2 = "HIDJEBKFLMGCA"; 测试数据2,后序
char *LDR2 = "HDIBEJAKFCLGM"; 测试数据2,中序

预期结果:ABCDEFGHIJKLM
实际结果:ABCDEFGHIJKLM

char *LRD3 = "DEBFGCA"; 测试数据3,后序
char *LDR3 = "DBEAFCG"; 测试数据3,中序

预期结果:ABCDEFG
实际结果:ABCDEFG

函数功能

dlrtree 非递归前序遍历二叉树
createtre 根据后序,中序,生成一个二叉树
*/

回答2:

可以帮你写,但太长了,这点分数不想写