一些数据结构的测试程序 绝对值得学习
以下是一个树的创建及遍历程序~ 不是二叉树!二叉树的创建在网上很多
程序代码:#include <cstdlib>
#include <iostream>
using namespace std;
struct node
{
char data;
node** child;
int m;
};
node* CreateNode(node *root)
{
char val;
int num;
cout<<"结点值:";
cin>>val;
if(val=='*')
return NULL;
else{
root=new node;
root->data=val;
cout<<root->data<<"结点的子结点个数:";
cin>>num;
root->m=num;
for(int i=0;i<root->m;i++)
root->child[i]=CreateNode(root);
}
return root;
}
void TreePreOrder(node *tree)
{
if(tree!=NULL)
{
cout<<tree->data;
for(int i=0;i<tree->m;i++)
TreePreOrder(tree->child[i]);
}
}
int main(int argc, char *argv[])
{
node *root=NULL;
char op;
while(1)
{
system("CLS");
cout<<"选项:\n//第一次输入时为根结点的值,char类型,输入*结点为空,字符串导致程序崩溃."<<endl;
cout<<"1.动态创建树 2.先根遍历 0.退出"<<endl;
cin>>op;
switch(op)
{
case '1':
{
root=CreateNode(root);
break;
}
case '2':
{
if(root==NULL)
cout<<"空树";
else
{
cout<<"先根遍历结果如下:"<<endl;
TreePreOrder(root);
}
cout<<endl;
break;
}
case '0':exit(0);
default:break;
}
system("PAUSE");
}
return EXIT_SUCCESS;
}
以下是线性表,栈,队列和字符串的演示程序//顺序和链式实现 所有程序在dev-c++测试成功!
[local]1[/local]
以后有时间的话再发图结构和一些应用实现 比如多项式计算,迷宫,多种排序...






