/**
* <!--
* File : binarytree.h
* Author : fancy
* Email : fancydeepin@yeah.net
* Date : 2013-02-03
* --!>
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <Stack>
#include <Queue>
using namespace std
#define Element char
#define format "%c"
typedef struct Node {
Element data
struct Node *lchild
struct Node *rchild
} *Tree
int index = 0 //全局索引变量
//二叉树构造器,按先序遍历顺序构造二叉树
//无左子树或右子树用'#'表示
void treeNodeConstructor(Tree &root, Element data[]){
Element e = data[index++]
if(e == '#'){
root = NULL
}else{
root = (Node *)malloc(sizeof(Node))
root->data = e
treeNodeConstructor(root->lchild, data) //递归构建左子树
treeNodeConstructor(root->rchild, data) //递归构建右子树
}
}
//深度优先遍历
void depthFirstSearch(Tree root){
stack<Node *>nodeStack //使用C++的STL标准模板库
nodeStack.push(root)
Node *node
while(!nodeStack.empty()){
node = nodeStack.top()
printf(format, node->data) //遍历根结点
nodeStack.pop()
if(node->rchild){
nodeStack.push(node->rchild) //先将右子树压栈
}
if(node->lchild){
nodeStack.push(node->lchild) //再将左子树压栈
}
}
}
//广度优先遍历
void breadthFirstSearch(Tree root){
queue<Node *>nodeQueue //使用C++的STL标准模板库
nodeQueue.push(root)
Node *node
while(!nodeQueue.empty()){
node = nodeQueue.front()
nodeQueue.pop()
printf(format, node->data)
if(node->lchild){
nodeQueue.push(node->lchild) //先将左子树入队
}
if(node->rchild){
nodeQueue.push(node->rchild) //再将右子树入队
}
}
}
/**
* <!--
* File : BinaryTreeSearch.h
* Author : fancy
* Email : fancydeepin@yeah.net
* Date : 2013-02-03
* --!>
*/
#include "binarytree.h"
int main() {
//上图所示的二叉树先序遍历序列,其中用'#'表示结点无左子树或无右子树
Element data[15] = {'A', 'B', 'D', '#', '#', 'E', '#', '#', 'C', 'F','#', '#', 'G', '#', '#'}
Tree tree
treeNodeConstructor(tree, data)
printf("深度优先遍历二叉树结果: ")
depthFirstSearch(tree)
printf("\n\n广度优先遍历二叉树结果: ")
breadthFirstSearch(tree)
return 0
}
1 思路: 主要是链表的插入和删除 *** 作
2 代码
#include<stdio.h>#include<stdlib.h>
typedef struct node
{
int data
struct node *next
}node_type
void push(node_type* &stack, int elem){
node_type*node = (node_type*)malloc(sizeof(node_type))
node->data = elem
node->next = stack
stack = node
}
int pop(node_type* &stack){
int elem = stack->data
node_type*node = stack
stack = stack->next
free(node)
return elem
}
bool IsEmpty(node_type* stack){
return stack == NULL
}
void display(node_type*stack){
while (stack){
printf("%d ", stack->data)
stack = stack->next
}
puts("")
}
void destroy(node_type*stack){
while (!IsEmpty(stack)){
pop(stack)
}
}
int main(){
puts("(1) 建立空链栈")
node_type*stack = NULL
puts("\n(2) 调用进栈函数,将从键盘输入的数据元素逐个进栈,输入0结束;")
int num
scanf("%d", &num)
while (num != 0){
push(stack, num)
scanf("%d", &num)
}
puts("\n(3) 显示进栈后的数据元素")
display(stack)
puts("\n(4) 调用两次出栈函数,显示出栈后的数据元素")
if (!IsEmpty(stack))
printf("%d\n", pop(stack))
if (!IsEmpty(stack))
printf("%d\n", pop(stack))
destroy(stack)
getchar()
getchar()
return 0
}
3 运行效果
你的问题很简单,就是你没有区分清楚函数形参和实参之间传值和传址的区别。
#include <stdio.h>
#include <malloc.h>
typedef struct node { int data
struct node *next
} stacknode
/*
stacknode *initnode()
{
stacknode *top//这声明的是个局部变量,你这里这样用是没有意义的,因为局部变量在函数结束后就释放了
top = NULL
return top
}
//修改为:*/
void initnode(stacknode **top)
{
*top = NULL
}
int emptynode(stacknode * top) //判空
{
if (top == NULL)
return 1
else
return 0
//return top == NULL//可写为一行代码
}
/*从本质上来说,函数调用都是值传递的。这里,crnode(top)的实参top把top的值赋给了crnode函数里形参top的值,即相当于top(形参) = top(实参)注意,这里两个top是不一样的变量,两者的内存地址也不同,形参top是crnode函数里分配的内存,而实参top则是main函数里分配的内存。所以你crnode函数里去改变top的值当然就不会相对应的改变main函数里top的值*/
/*void crnode(stacknode * top) //进栈
{
stacknode *pint i
for (i = 0i <5i++) {
p = (stacknode *) malloc(sizeof(stacknode))
printf("input data:\n")
scanf("%d", &p->data)
p->next = top
top = p//这样是无法改变主函数里top的值的,只是改变了该函数里top指针的值
}
}
//修改为:*/
void crnode(stacknode ** top) //进栈,
{
stacknode *pint i
for (i = 0i <5i++) {
p = (stacknode *) malloc(sizeof(stacknode))
printf("input data:\n")
scanf("%d", &p->data)
p->next = *top
*top = p
}
}
int numnode(stacknode * top) //计栈中结点数
{
stacknode *p, *s
int i = 0//i应该从0开始,这样当top为NULL时i才会为0
s = top
while (s != NULL) {
p = s->next
s = p
i++
}
return i}
void outnode(stacknode * top) //出栈
{
stacknode *p
int x
while (top != NULL) {
x = top->data
printf("%d\t", top->data)
p = top->next
top = p
//top = top->next//上面两个代码写为一行就行了。。。
}
}
void main()
{
int i
stacknode *top
initnode(&top)//top = initnode()
i = emptynode(top)
if (i == 1)
printf("栈空")
crnode(&top)//crnode(top)
i = emptynode(top)
if (i == 1)
printf("栈空")
printf("%d\n", numnode(top))
outnode(top)
i = emptynode(top)
if (i == 1)
printf("栈空")
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)