// BTree.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
typedef int Status;
#define OK 1
#define FALSE 0
/* 二叉链表的结点结构 */
typedef int BElemType;
typedef struct BiTNode
{
BElemType data; /* 结点数据 */
struct BiTNode *lchild, *rchild; /* 左右孩子指针 */
} BiTNode, *BiTree;
/* 即将在主函数中调用的函数 */
void CreateBiTree(BiTree *T);
void InOrderTraverse(BiTree T);
/* 二叉排序树的查找操作(递归)
* T为待查找的二叉排序树
* key为要查找的关键字
* f为指向双亲结点的指针,初始时为NULL
* 当查找成功时,p指向查找到的结点,当查找失败时,
* p指向最后一次访问到的结点
*/
Status SearchBST(BiTree T, BElemType key, BiTree f, BiTree *p)
{
if (T == NULL) {
*p = f;
return FALSE;
}
else if (key == T->data) {
*p = T;
return OK;
}
else if (key < T->data)
return SearchBST(T->lchild, key, T, p);
else
return SearchBST(T->rchild, key, T, p);
}
/* 二叉排序树的插入操作 */
Status InsertBST(BiTree *T, BElemType key)
{
BiTree p, s;
if (!SearchBST(*T, key, NULL, &p)) { /* 当二叉排序树中没有key时进行插入操作 */
s = (BiTree)malloc(sizeof(BiTNode));
s->data = key;
s->lchild = s->rchild = NULL;
if (!*T) /* 当为空树时 */
*T = s; /* 插入s为根结点 */
else if (key < p->data)
p->lchild = s;
else
p->rchild =s;
return OK;
}
else
return FALSE;
}
/* 二叉排序树的建立 */
#define MAXSIZE 100
void CreateBiTree(BiTree *T)
{
int i, num;
BElemType data[MAXSIZE];
*T = NULL; /* 初始化为空树 */
printf("输入结点个数: ");
scanf("%d", &num);
for (i = 0; i < num; i++)
scanf("%d", &data[i]);
for (i = 0; i < num; i++) /* 利用二叉排序树的插入操作建立二叉排序树 */
InsertBST(T, data[i]);
}
/* 二叉树的中序遍历 */
void InOrderTraverse(BiTree T)
{
if (T == NULL) {
// printf("二叉树为空 !\n");
// exit(1);
return;//别直接退出程序,不方便调试啊!
}
InOrderTraverse(T->lchild);
printf("%d ", T->data);//%d后面加个空格或者换行社么的,免得打印出来的是一串数字
InOrderTraverse(T->rchild);
}
int _tmain(int argc, _TCHAR* argv[]){
BiTree T;
/* 建立二叉排序树 */
CreateBiTree(&T);
/* 中序遍历查看建立的二叉排序树 */
InOrderTraverse(T);
int i = 0;
system("pause");
return 0;
}
#include<stdio.h>
#include<iostream>
using namespace std;
template<class Type> class Stack//:public Stack<Type>
{
private:
int size;
int top;
Type *listArray;
public:
Stack(int sz=0) //Constructor
{
size=sz;
top=0;
listArray=new Type[sz];
if(listArray==0)
cout<<"内存空间分配失败!"<<endl;
}
void changeStackLength(int sz)
{
size=sz;
top=0;
listArray=new Type[sz];
if(listArray==0)
cout<<"内存空间分配失败!"<<endl;
}
~Stack() //Destructor
{
delete[]listArray;
}
void push(const Type& item)
{
if(top==size)
cout<<"堆栈已满!"<<endl;
else
{
listArray[top++]=item;
}
}
Type pop()
{
if(top==0)
{
cout<<"堆栈已空!"<<endl;
return 0;
}
else
{
return listArray[--top];
}
}
Type topValue() const
{
if(top==0)
{
cout<<"堆栈为空,无栈顶元素!"<<endl;
return 0;
}
else
{
return listArray[top-1];
}
}
int length() const
{
return top;
}
};
int main()
{
Stack<int> st(10);
int num,n;
char option;
while(1)
{
cout<<"\n堆栈操作练习:"<<endl;
cout<<"1:创建一个堆栈"<<endl;
cout<<"2:往堆栈中压入一个元素"<<endl;
cout<<"3:从堆栈中弹出一个元素"<<endl;
cout<<"4:求栈顶元素的值"<<endl;
cout<<"5:获取堆栈的长度"<<endl;
cout<<"0:退出"<<endl;
cout<<"请输入你的选择:"<<endl;
cin>>option;
switch(option)
{
case'0':
break;
case'1':
cout<<"请输入要创建的堆栈的长度"<<endl;
cin>>num;
st.changeStackLength(num);
break;
case'2':
cout<<"请输入要压入元素的值"<<endl;
cin>>num;
st.push(num);
break;
case'3':
cout<<"弹出的元素为:"<<st.pop()<<endl;;
break;
case'4':
cout<<"栈顶元素为:"<<st.topValue()<<endl;;
break;
case'5':
cout<<"堆栈的长度为:"<<st.length()<<endl;;
break;
default:
cout<<"输入错误,请重新输入!"<<endl;
}
if(option=='0')
break;
}
system("pause");
return 0;
}