主题:严蔚敏版程序运行实例.12/15新加图的数组表示法输入,深度,广度遍历,二叉排序树
bottles
[专家分:670] 发布于 2006-12-15 15:15:00
以下程序全部通过编译,在vc6.0上运行
链表,线性表,顺序表,顺序栈,线索二叉树,12/15新加图的数组表示法输入,深度,广度遍历,二叉排序树
将以前的某些程序进行类封装。
//lb3.cpp
//此程序用c++编写
#include <malloc.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct lnode{
int data;
struct lnode *next;
}lnode, *linklist;
void getelem_l(linklist l, int i)//return no.i element
{
linklist p = (linklist)malloc (sizeof (lnode));
int j = 1;
system("cls");
p = l -> next;
while(p && j< i){
p = p -> next;
++j;
}
if(!p || j > i)
cout << "您输入的位置不存在!!\n";
else
cout << p -> data << endl;
}
void creatlist_l(linklist& l,int n)//creat list with reverse order
{
int i;
linklist p;//why cannot initialize the space here??
system("cls");
l = (linklist)malloc (sizeof (lnode));
l -> next = NULL;
cout <<"请为数组的" << n << "个数值负值!\n";
for (i = n; i > 0; --i){
p = (linklist) malloc (sizeof (lnode));//why must here??
cin >> p -> data;
p -> next = l -> next;//with head
l -> next = p;
}
}
int listinsert_l(linklist& l, int i, int e)
{
linklist p = (linklist)malloc (sizeof (lnode));
int j = 0;
p = l;
while (p && j < i-1){
p = p -> next;
++j;
}
if (!p || j > i-1){//1、i is larger than the length 2、i is nagetive number
cout << "您输入的位置不存在!!\n";
system("pause");
return -1;
}
linklist s = (linklist) malloc (sizeof(lnode));
s -> data =e;
s -> next = p -> next;
p -> next = s;
return 1;
}
int listdelete_l(linklist &l, int i)
{
linklist p = (linklist)malloc (sizeof (lnode));
int j = 0;
system("cls");
p = l;
while (p -> next && j < i-1){
p = p -> next;
++j;
}
if(!(p -> next) || j > i-1){
cout << "删除位置不合理!!请返回上级菜单重新输入!\n";
system("pause");
return -1;
}
linklist q = (linklist)malloc (sizeof (lnode));
q = p -> next;
p -> next = q -> next;
cout << "被删除的数字为:" << q -> data << endl;
free (q);
system("pause");
return 1;
}
int order_l(linklist& l)//from small to big
{
int temp;
linklist p = (linklist)malloc (sizeof (lnode));
linklist m = (linklist)malloc (sizeof (lnode));
for(p = l -> next ; p->next; p = p -> next)
for(m = p -> next; m; m = m -> next)
if(p->data > m->data){
temp = p -> data;
p->data = m->data;
m->data = temp;
}
return 0;
}
void mergelist_l (linklist la, linklist lb, linklist lc)
{
linklist pa = (linklist)malloc (sizeof (lnode));
linklist pb = (linklist)malloc (sizeof (lnode));
linklist pc = (linklist)malloc (sizeof (lnode));
pa = la -> next;
pb = lb -> next;
lc = pc = la;
while (pa && pb){
if (pa -> data <= pb -> data){
pc -> next = pa;
pc = pa;
pa = pa -> next;
}
else{
pc -> next = pb;
pc = pb;
pb = pb -> next;
}
}
pc -> next = pa ? pa : pb;
//free(lb);
linklist p = (linklist)malloc (sizeof (lnode));
cout <<"合成的列表按以下形式排列:\n";
p = la -> next;
while (p){
cout << p -> data << ' ';
p = p -> next;
}
cout << endl;
}
int main3()
{
linklist l1 ,m1, c1;
int i, e, n;
char d;
while(1){
system("cls");
cout << "\t\t**********************\n\t\t欢迎来到链表的初始化,\n\t\t查找,删除,添加,合并界面\n\t\t**********************\n";
cout << "1、初始化数据\n2、插入数字\n3、删除数字\n4、输出第i个数字\n5、排列两表\n6、合并排列好的两表,如不事先排列(选择5)请勿合并,否则后果自负。\n7、显示两数组\n8、结束程序\n\n请输入选择:";
d = getchar();
if (d=='1'){
cout << "请键入共建立的数据个数:";
cin >> n;
creatlist_l(l1, n);//input the numbers
creatlist_l(m1, n);//input the numbers
}
else if (d =='2'){//insert a number
cout <<"请输入数组一要插入的位置和元素,输入完毕后选择7查看效果。\n";
cin >> i >> e;
listinsert_l(l1, i, e);
}
else if(d=='3'){
cout << "\n请输入数组一要删除的位置,输入完毕后选择7查看效果。\n";
cin >> i;
listdelete_l(l1, i);//delete a number
}
else if (d=='4'){
cout << "\n请输入在数组一中你想要输出的数的位置.\n";
cin >> e;
getelem_l(l1, e);//look for a number
system("pause");
}
else if (d == '5'){//排列两数组
order_l(l1);
order_l(m1);
}
else if (d == '6'){
mergelist_l(l1, m1, c1);//to add two lists.
system("pause");
}
else if (d == '7'){
system("cls");
linklist p = (linklist)malloc (sizeof (lnode));
p = l1 -> next;
while (p){
cout << p -> data << ' ';
p = p -> next;
}
cout << endl;
p = m1 -> next;
while (p){
cout << p -> data << ' ';
p = p -> next;
}
cout << endl;
system ("pause");
}
else if (d=='8')//退出
break;
}
cout << "\n返回上级菜单!!\n";
system ("pause");
return 0;
}
回复列表 (共20个回复)
11 楼
liuyi31152 [专家分:0] 发布于 2006-10-22 20:51:00
写得很好
另外希望你们多注释一下
照顾以下我们新手
谢谢大家了
12 楼
liuyi31152 [专家分:0] 发布于 2006-10-22 20:53:00
辛苦了!
另外希望你们多注释一下
照顾我们新手
谢谢啦
[em1][em1][em1][em1]
13 楼
bottles [专家分:670] 发布于 2006-10-30 14:20:00
[quote]辛苦了!
另外希望你们多注释一下
照顾我们新手
谢谢啦
[em1][em1][em1][em1][/quote]
下次我注意一下
16 楼
zhang0405301 [专家分:0] 发布于 2006-12-02 13:57:00
顶!!真是好东西!!谢谢!!
17 楼
Shadowfax [专家分:890] 发布于 2006-12-03 10:33:00
顶一下
18 楼
bottles [专家分:670] 发布于 2006-12-15 15:05:00
//此程序用c++完成
//图的数组表示法输入,深度,广度遍历
#define INFINITY INT_MAX
#define MAX_VERTEX_NUM 20
#define FALSE 0
#define TRUE 1
#define NULL 0
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "linkqueue.h"
using namespace std;
bool visited[MAX_VERTEX_NUM];
typedef struct Arccell{
int adj;//是顶点关系类型,对无权图,用1或0表示;对带权图,则为权值类型.
}Arccell, Adj[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct{
int vexs[MAX_VERTEX_NUM];//图的顶点线形表表示
Adj arcs;//图的邻接矩阵存储表示法
int vexnum, arcnum;//图的顶点数和弧数
}Mgraph;
int LocateVex(Mgraph G, int v)//确认v是否是G中的一个元素
{
for (int i = 0; i < G.vexnum; i++){
if(v == G.vexs[i])
return i;
else
continue;
}
cout << "没有这个数值,返回重新输入!!\n";
system("pause");
return -1;
}
int creatudn(Mgraph &G)//创造邻接矩阵
{
int j, i, k;
cout << "请输入图的顶点数和弧数:";
cin >> G.vexnum >> G.arcnum;
cout << "请输入顶点的数值:";
for(i = 0; i< G.vexnum; ++i)
cin >> G.vexs[i];
for(i = 0; i < G.vexnum; ++i)
for(j = 0; j < G.vexnum; ++j)
G.arcs[i][j].adj = NULL;//初始化
for(k = 0; k < G.arcnum; ++k){
int v1, v2, w;
cout << "请输入有连接的两个顶点的在之前输入表中的值,并输入边的权值:";//无权图用1或0表示,带权图输入权值类型:";
cin >> v1 >> v2 >> w;
if((i = LocateVex(G, v1)) == -1)
continue;
if((j = LocateVex(G, v2)) == -1)
continue;
G.arcs[i][j].adj = w;
G.arcs[j][i] = G.arcs[i][j];
}
return 0;
}
void VisitFunc(int v, Mgraph G)//访问第v+1个元素
{
cout << G.vexs[v] << ' ';
}
int FirstAdjVex(Mgraph& G, int v)//图G存在,v是G的某个顶点,返回v的第一个邻接顶点,若无,返回空。
{
for (int j = 0; j < G.vexnum; j++)
if (G.arcs[v][j].adj && !visited[j])//key//为了这条折腾了我n久阿
return j;
return NULL;
}
int NextAdjVex(Mgraph& G, int v, int w)//图G存在,v是G的某个顶点,w是v的邻接顶点,返回v的(相对于w的)下一个邻接顶点,若是最有一个,返回空。
{
for (int j = 0; j < G.vexnum; j++)
if (G.arcs[w][j].adj && (j != v) && !visited[j])//visited是为了防止出现无限循环
return j;
return NULL;
}
void DFS(Mgraph& G, int v)
{
visited[v] = TRUE;
VisitFunc(v, G);
for(int w = FirstAdjVex(G, v); w != NULL; w = NextAdjVex(G, v, w))
if(!visited[w])
DFS(G,w);
}
void DFSTraverse(Mgraph& G)
{
for(int v = 0; v < G.vexnum; ++v)
visited[v] = FALSE;
for(int v = 0; v < G.vexnum; ++v)//用于遍历第一个临界点的另几条分支
if(!visited[v])
DFS(G,v);
}
void print(Mgraph& G)//打印图的邻接矩阵
{
cout << "图的邻接矩阵表示为" << endl;
for(int i = 0; i < G.vexnum; i++){
for(int j=0; j < G.vexnum; j++)
cout << G.arcs[i][j].adj << '\t';
cout << endl;
}
}
void DFS2Traverse(Mgraph G)//深度遍历的非递归算法,汗,为什么书上说是广度遍历阿。。。
{
linkqueue Q;
for(int v = 0; v < G.vexnum; ++v)
visited[v] = FALSE;
for(int v = 0; v < G.vexnum; ++v)//防止其有几条分支
if(!visited[v]){
visited[v] = TRUE;
VisitFunc(v, G);
Q.EnQueue(v);
while(!Q.Empty()){
int u;
u = Q.DeQueue();
for(int w = FirstAdjVex(G,u); w != NULL ; w = NextAdjVex(G, u, w))
if(!visited[w]){
visited[w] = TRUE;
VisitFunc(w, G);
Q.EnQueue(w);
}//if
}//while
}//if
}//bfstraverse//good habit!!
19 楼
bottles [专家分:670] 发布于 2006-12-15 15:05:00
void BFSTraverse(Mgraph G)//广度遍历,书上果然错了。。。
{
linkqueue Q;
for(int v = 0; v < G.vexnum; ++v)
visited[v] = FALSE;
for(int v = 0; v < G.vexnum; ++v)//防止其有几条分支
if(!visited[v]){
visited[v] = TRUE;
VisitFunc(v, G);
Q.EnQueue(v);
while(!Q.Empty()){
int u;
u = Q.DeQueue();
for(int w = FirstAdjVex(G,u); w != NULL ; w = NextAdjVex(G, w, u))
if(!visited[w]){
visited[w] = TRUE;
VisitFunc(w, G);
Q.EnQueue(w);
}//if
}//while
}//if
}//bfstraverse
int main6()
{
Mgraph G;
char d;
while(1){
system("cls");
cout << "\t\t**********************\n\t\t欢迎来到无向图的初始化,\n\t\t深度,广度遍历界面\n\t\t**********************\n";
cout << "\n1、初始化图(用邻接矩阵表示)\n2、深度遍历图递归算法\n3、深度遍历的非递归算法(用队列)\n4、广度遍历\n5、打印无向图\n6、返回上级菜单\n\n请输入选择:";
d = getchar();
if (d=='1'){
creatudn(G);
}
else if (d =='2'){
DFSTraverse(G);
system ("pause");
}
else if(d=='3'){
DFS2Traverse(G);
system("pause");
}
else if (d=='4'){
BFSTraverse(G);
system("pause");
}
else if (d == '5'){
print(G);
system("pause");
}
else if (d=='6')//退出
break;
}//while
cout << "\n返回上级菜单!!\n";
system ("pause");
return 0;
}
20 楼
bottles [专家分:670] 发布于 2006-12-15 15:11:00
//bitree_main
#define TRUE 1
#define FALSE 0
#include <iostream>
using namespace std;
typedef struct BiTNode{
int data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
int SearchBST(BiTree &T, int key, BiTree f, BiTree &p)
{
if(!T){
p = f;
return FALSE;
}
else if(key == T->data){
p = T;
return TRUE;
}
else if(key <= T-> data)
return SearchBST(T ->lchild, key, T, p);
else
return SearchBST(T -> rchild, key, T, p);
}//SearchBST
int InsertBST(BiTree &T, int key)
{
BiTree p = new BiTNode;
if(!SearchBST(T, key, NULL, p)){
BiTree s = new BiTNode;
s ->data = key;
s->lchild = s -> rchild = NULL;
if(!p)
T = s;
else if (key <= p ->data)
p -> lchild =s;
else p -> rchild =s;
return TRUE;
}
else return FALSE;
}//InsertBST
int visit(int data)
{
if(data){
cout << data << ' ';
return TRUE;
}
return FALSE;
}
void print(BiTree T)
{
if(T){
print(T -> lchild);
visit(T ->data);
print(T -> rchild);
}
}
int main8()
{
BiTree T = new BiTNode;
T = NULL;
int key, i;
i = 1;
while (1){
cout << "请输入第" << i << "个数字,输入0为结束:";
i++;
cin >> key;
if(key == 0) break;
InsertBST(T, key);
}
print(T);
system("pause");
return 0;
}
我来回复