主题:严蔚敏版程序运行实例.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个回复)
沙发
bottles [专家分:670] 发布于 2006-10-15 17:14:00
//linkqueue_main.cpp
//此程序用c++编写
#include <malloc.h>
#include <iostream>
#include <stdlib.h>
#include "linkqueue.cpp"
using namespace std;
int main4()
{
linkqueue q;
q.Menu();
cout << "\n返回上级菜单!!\n";
system ("pause");
return 0;
}
//qnode.h
#ifndef qnode_header
#define qnode_header
#include "linkqueue.h"
class linkqueue;
class qnode{//with head
int data;
class qnode *next;
public:
friend class linkqueue;
};
#endif
//linkqueue.h
#ifndef LinkQueue_header
#define LinkQueue_header
#include "qnode.h"
class linkqueue{
// queueptr front;
//qnode* queueptr rear;
qnode* front;
qnode* rear;
public:
linkqueue();
int DestroyQueue();
int EnQueue(int e);
int DeQueue();
void Print();
void Menu();
int Empty();
};
#endif
//linkqueue.cpp
#include "linkqueue.h"
//#include "qnode.h"
//#define NULL 0
#include <malloc.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
linkqueue::linkqueue()
{
front = rear = new qnode;
if(!front){
cout << "存储分配失败!!\n";
return -1;
}
q.front -> next = NULL;
return 1;
}
int linkqueue::DestroyQueue()//to destroy the queue
{
while(front){
rear = front -> next;
free (front);
front = rear;
}
cout << "销毁完毕!!\n";
system("pause");
return 1;
}
int linkqueue::EnQueue(int e)//insert element e as a new one
{
qnode* p = new qnode;
if(!p){
cout << "存储空间分配失败!!\n";
return -1;
}
p -> data = e;
p -> next =NULL;
rear -> next = p;
rear = p;
return 1;
}
int linkqueue::DeQueue()//if stack isn't blank, delete the first number
{
int e;
qnode* p = new qnode;
if(front == rear){
cout << "栈空,请返回上级菜单输入相应值!!\n";
system("pause");
return -1;
}
p = front -> next;
e = p -> data;
front -> next = p -> next;
if(rear == p)
rear = front;
free(p);
return e;
}
void linkqueue::Print()
{
qnode* p = new qnode;
p = front ->next;
cout << "此链队列为:\n";
while (p){
cout << p -> data << ' ';
p = p -> next;
}
cout << endl;
system("pause");
}
void linkqueue::Menu()
{
int e;
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请输入选择:";
cin >> d;
if (d=='1'){
cout << "已初始化";
}
else if (d =='2'){
DestroyQueue();
}
else if(d=='3'){
cout << "请输入插入的元素:" ;
cin >> e;
EnQueue(e);
}
else if (d=='4'){
DeQueue();
}
else if (d=='5')
Print();
else if (d=='6')//Í˳ö
break;
}//while
}
int linkqueue::Empty()
{
if(front == rear)
return 1;
else
return 0;
}
板凳
bottles [专家分:670] 发布于 2006-10-15 17:16:00
//sxb1.cpp
//此程序用c编写
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *elem;
int length;
int listsize;
} sqlist;
int initlist_sq(sqlist* l)
{
l->elem = (int *)malloc(LIST_INIT_SIZE * sizeof(int));
if(! l->elem)
return -2;
l->length = 0;
l->listsize = LIST_INIT_SIZE;
return 1;
}
void input_sq(sqlist* l)
{
int i;
system("cls");
printf("请为数组的10个数值负值!");
for (i = 0; i < LIST_INIT_SIZE; i++){
scanf("%d", &l->elem[i]);
l->length++;
}
}
int listinsert_sq(sqlist* l, int i, int e)
{
int * newbase;
int *p, *q;
int j;
system("cls");
if (i < 1 || i > l->length+1)
return 0;
if (l->length >= l->listsize){
newbase = (int *)realloc(l->elem, (l->length + LISTINCREMENT) * sizeof(int));
if(!newbase) return -2;
l->elem = newbase;
for (j = 0; j < l->listsize; j++)
printf ("%d ", l->elem[j]);
l->listsize += LISTINCREMENT;
}
q = &(l->elem[i-1]);
for (p = &(l->elem[l->length - 1]); p >= q; --p)
*(p+1) = *p;
*q = e;
++l->length;
return 1;
}
int listdelete_sq(sqlist *l, int i, int e)
{
int *q, *p;
system("cls");
if ((i < 1) || (i > l->length))
return -2;
p = &(l->elem[i-1]);
e = *p;
q = l->elem + l->length - 1;
for (++p; p <=q; ++p)
*(p - 1) = *p;
--l->length;
return 1;
}
int compare(int *p, int e)
{
return *p == e? 0 : 1;
}
int locateelem_sq(sqlist* l, int e)
{
int i = 1;
int *p = l->elem;
while (i <= l->length && compare(p, e)) {
*p++;
++i;
}
if (i <= l->length)
return i;
else
return 0;
}
void order_sq(sqlist* l)
{
int i;
int temp;
int j;
for(i = 0; i <l->length-1; i++)
for(j = i+1; j< l->length; j++)
if(l->elem[i]>l->elem[j]){
temp = l->elem[i];
l->elem[i]=l->elem[j];
l->elem[j] = temp;
}
}
3 楼
bottles [专家分:670] 发布于 2006-10-15 17:17:00
int mergelist_sq (sqlist* l, sqlist* m, sqlist* c)
{
int *pa = l->elem, *pb = m->elem, *pc;
int *pa_last, *pb_last;
c->listsize = c->length = l->length + m->length;
pc = (int *)malloc(c->listsize * sizeof(int));
c->elem = pc ;//= l->elem;
if (!l->elem)
return -2;
pa_last = l->elem + l->length - 1;
pb_last = m->elem + m->length - 1;
while (pa <= pa_last && pb <= pb_last){
if (*pa <= *pb)
*pc++ = *pa++;
else *pc++ = *pb++;
}
while (pa <= pa_last)
*pc++ = *pa++;
while (pb <= pb_last)
*pc++ = *pb++;
return 0;
}
int main1()
{
sqlist l1 ,m1, c1;
sqlist *l = &l1, *m = &m1, *c = &c1;
int i, e, j;
//int temp;
char d;
initlist_sq(l);
initlist_sq(m);//initial
while(1){
system("cls");
printf("\t\t**********************\n\t\t欢迎来到数组的初始化,\n\t\t查找,删除,添加,合并界面\n\t\t**********************\n");
printf("1、初始化数据\n2、插入数字\n3、删除数字\n4、查找数字\n5、排列两表\n6、合并排列好的两表\n7、显示两数组\n8、结束程序\n\n请输入选择:");
d = getchar();
if (d=='1'){
input_sq(l);//input the numbers
input_sq(m);
for (i = 0; i < LIST_INIT_SIZE; i++)
printf ("%d\t", l->elem[i]);
printf ("\n");
for (i = 0; i < LIST_INIT_SIZE; i++)
printf ("%d\t", m->elem[i]);
printf ("\n");
getchar();
getchar();
}
else if (d=='2'){
printf("please put the position and the number.(add)\n");
scanf ("%d%d", &i, &e);
listinsert_sq(l, i, e);//insert a number
printf("\n");
for (i = 0; i < l->length; i++)
printf ("%d\t", l->elem[i]);
getchar();
getchar();
}
else if(d=='3'){
printf("\nplease put the position.(delete)\n");
scanf ("%d", &i);
listdelete_sq(l, i, e);//delete a number
for (i = 0; i < l->length; i++)
printf ("%d\t", l->elem[i]);
getchar();
getchar();
}
else if (d=='4'){
printf("\nplease put the number(find).\n");
scanf ("%d", &e);
i = locateelem_sq(l, e);//look for a number
for (j = 0; j < LIST_INIT_SIZE; j++)
printf ("%d\t", l->elem[j]);
printf("\nthe finding number's position is: %d \n", i);
getchar();
getchar();
}
else if (d == '5'){//排列两数组
order_sq(l);
order_sq(m);
}
else if (d == '6'){
mergelist_sq(l, m, c);//to add two lists.
printf ("the synthetic array must be as followed:\n");
for (i = 0; i < c->length; i++)
printf ("%d\t", c->elem[i]);
getchar();
getchar();
}
else if (d == '7'){
system("cls");
for (i = 0; i < l->length; i++)
printf ("%d\t", l->elem[i]);
printf ("\n");
for (i = 0; i < m->length ; i++)
printf ("%d\t", m->elem[i]);
printf ("\n");
getchar();
getchar();
}
else if (d=='8')//退出
break;
}
printf("\n返回上级菜单!!\n");
getchar();
getchar();
return 0;
}
4 楼
bottles [专家分:670] 发布于 2006-10-15 17:18:00
//sqstack_main
//此程序用c++编写
#include "sqstack.cpp"
#include "sqstack.h"
# include <iostream>
using namespace std;
void conversion()
{
sqstack s1;
int n;
system("cls");
cout << "恭喜你已经通过了初始阶段,现在进入实战阶段:\n欢迎来到十进制转入八进制界面!!\n请输入任意一个非负十进制整数:";
cin >> n;
cout << endl;
while(n){
s1.push( n % 8);
n = n / 8;
}
cout << "此八进制数为:";
while(!s1.stackempty()){
cout << s1.pop();
}
cout << endl;
system ("pause");
}
int main2()
{
sqstack s;
s.Menu();
conversion();
cout << "\n返回上级菜单!!\n";
system ("pause");
return 0;
}
//sqstack.h
# ifndef sqstack_header
# define sqstack_header
class sqstack{
int* base;
int* top;
int stacksize;
public:
sqstack();
int gettop();
int push(int e);
int pop();
int stackempty();
int Menu();
};
# endif
//sqstack.cpp
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#include <iostream>
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include "sqstack.h"
using namespace std;
sqstack::sqstack()
{
base = (int *)malloc(STACK_INIT_SIZE * sizeof(int));
if(!base){
cout << "初始化失败!!\n";
system("pause");
}
top = base;
stacksize = STACK_INIT_SIZE;
}
int sqstack::gettop()//get the top element
{
if(top == base){
cout <<"此栈为空栈!!\n";
return -1;
}
cout << "栈顶元素为:" << *(top - 1);
return 1;
}
int sqstack::push(int e)//insert element e as a new one
{
if (top - base >= stacksize){
base = (int*)realloc(base,(stacksize + STACKINCREMENT) * sizeof(int));
if(!base){
cout << "存储分配失败!";
return -1;
}
top = base + stacksize;
stacksize += STACKINCREMENT;
}
*top++ = e;
return 1;
}
int sqstack::pop()//if stack isn't blank, delete the top number
{
int e;
//system("cls");
if(top == base){
cout << "栈空,请返回上级菜单输入相应值!!\n";
system("pause");
return -1;
}
e = *--top;
return e;
}
int sqstack::stackempty()
{
return (base == top)? 1: 0;
}
int sqstack::Menu()
{
int e;
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、进入实战界面\n\n请输入选择:";
d = getchar();
if (d=='1'){
cout << "栈已初始化";
system("pause");
}
else if (d =='2'){//insert a number
cout <<"请输入要插入的元素。\n";
cin >> e;
push(e);
}
else if(d=='3'){//delete a number
cout << "删除的栈顶元素为:" << pop();
system("pause");
}
else if (d=='4'){
gettop();
system("pause");
}
else if (d == '5')//退出
break;
}//while
return 0;
}
5 楼
bottles [专家分:670] 发布于 2006-10-15 17:20:00
#include "lb3.cpp"
#include "linkqueue_main.cpp"
#include "sxb1.cpp"
#include "xsecs5.cpp"
#include "sqstack_main.cpp"
#include "Graph_main.cpp"
#include "bitree_main.cpp"
#include <iostream>
using namespace std;
int main(){
while (1){
system("cls");
cout<<"\n*************************欢迎进入数据结构实例运行程序***************************\n";
cout<<" ◢■■◣ ◢■■◣ ◢■■◣ ◢■■◣"<<endl;
cout<<" ■ ■ ■ ■ ■ ■ "<<endl;
cout<<" ◢■■◤ ■ ■ ■ ■ ■■■◣ "<<endl;
cout<<" ■ ■ ■ ■ ■ ■ ■"<<endl;
cout<<" ◥■■◤ ◥■■◤ ◥■■◤ ◥■■◤"<<endl;
cout<<" ★☆★ ★☆★ ★☆★ ★☆★"<<endl;
cout<<" ★ 欢 ★★ 迎 ★★ 光 ★★ 临 ★"<<endl;
cout<<" ★☆★ ★☆★ ★☆★ ★☆★"<<endl;
cout<<" ╭╧╮╭╧╮╭╧╮╭╧╮╭╧╮ ╭╧╮"<<endl;
cout<<" ║制│║作│║人│║:│║ │ ║ │"<<endl;
//cout<<" ╘∞╛╘∞╛╘∞╛╘∞╛╘∞╛ ╘∞╛"<<endl<<endl;
cout<<"\n*******************************俺是华丽丽的分割线*******************************\n";
cout<<"请输入密码:\n";
int i = 111;
int j;
cin >> j;
if (i == j){
while(1){
system("cls");
cout << "\t\t\t############################\n\t\t\t#\t沦丧的主界面\t #\n\t\t\t############################\n";
cout << "\n\t\t1、链队列\n\t\t2、链表\n\t\t3、顺序表\n\t\t4、顺序栈\n\t\t5、线索二叉树\n\t\t6、图的邻接矩阵表示法\n\t\t7、图的邻接表表示法\n\t\t8、二叉排序树\n\n\t\t按其他任意键结束程序\n\n请输入感兴趣的选择:";
char d;
cin >> d;
if (d=='1')
main4();
//break;
else if (d =='2')
main3();
else if(d=='3')
main1();
else if (d=='4')
main2();
else if (d=='5'){
main5();
}
else if (d=='6'){
main6();
}
else if (d=='7'){
// main7();
break;
}
else if (d=='8'){
main8();
}
else{
goto end;
}
}//while
}//if
else {
cout <<"密码错误,请重新输入!!\n";
system("pause");
}
}
end: cout << "\n欢迎您下次光临!!\n";
system ("pause");
return 0;
}
6 楼
sjp2003 [专家分:1090] 发布于 2006-10-15 17:20:00
顶.
7 楼
zy1121 [专家分:7950] 发布于 2006-10-15 18:12:00
顶一下!
8 楼
xieyong456 [专家分:2620] 发布于 2006-10-15 18:28:00
不错不错~
[em4]学习就要如此脚踏实地~
9 楼
bottles [专家分:670] 发布于 2006-10-22 19:22:00
//xsecs5.h
//此程序用c++完成
#include <iostream>
//#include <malloc.h>
using namespace std;
int i = 0;
typedef enum PointerTag{link, thread};
typedef struct bithrnode{
int RTag;
int LTag;
struct bithrnode *lchild;
struct bithrnode *rchild;
char data;
}bithrnode, *bithrtree;
void CreateBiTree(bithrtree &T)//建立新的二叉树//right
{
char t;
if(i == 0){
cout << "请输入建立二叉树的字母与!,!表示输入的下一个节点为空。\n";
i = 1;
}
cin >> t;
if(t == '!')
T = NULL;
else{
if (!(T = new bithrnode)){
cout << "程序出错!!" << endl;
return;
}
T -> data = t;
CreateBiTree(T -> lchild);
CreateBiTree(T -> rchild);
}
return;
}
int visit(char e){
cout << e << ' ';
return 1;
}
/*int PreOrderTraverse(BiTree* &T){//visit the bitree
if(T){
if(visit(T->data))
if(PreOrderTraverse(T->lchild))
if(PreOrderTraverse(T->rchild))
return 1;
return 0;//i don't know this..
}
else return 1;
}*/
void inthreading(bithrtree& p, bithrtree& pre){//right
if(p){
p->RTag = p ->LTag = link;
inthreading(p->lchild ,pre);
if(!(p ->lchild)){
p->LTag = thread;
p->lchild = pre;
}
if(!pre ->rchild ){
pre ->RTag = thread;
pre ->rchild = p;
}
pre = p;
inthreading(p->rchild, pre);
}
}
int InOrderThreading(bithrtree& thrt, bithrtree& T){//threading bitree//right
if(!(thrt = new bithrnode)){
cout << "建立失败\n";
return -1;
}
thrt ->LTag = link;
thrt ->RTag = thread;
thrt ->rchild = thrt;
if (!T)
thrt ->lchild = thrt;
else {
thrt->lchild = T;
bithrtree pre;
pre = new bithrnode;
pre = thrt;
inthreading(T,pre);
pre -> rchild = thrt;
pre->RTag = thread;
thrt ->rchild = pre;
}
return 0;
}
int inordertraverse_thr(bithrtree& T)//中序遍历二叉线索树的非递归算法,对每个数据元素调用visit
{
//system("cls");
bithrtree p;
p = T ->lchild;//T point to the head
while (p != T){
while (p -> LTag == link)
p = p -> lchild;
if (!visit(p -> data)){
cout << "访问失败!!\n";
system("pause");
return -1;
}
while (p -> RTag == thread && p -> rchild != T){
p = p -> rchild;
visit (p -> data);
}
p = p -> rchild;
}
system("pause");
return 0;
}
int main5()
{
bithrtree t, thrt;
char d;
while(1){
system("cls");
cout << "\t\t**********************\n\t\t欢迎来到二叉树的初始化,\n\t\t线索化,中序遍历界面\n\t\t**********************\n";
cout << "1、建树\n2、中序线索化二叉树\n3、中序遍历二叉树的非递归算法\n4、返回上级菜单\n\n请输入选择:";
d = getchar();
if (d=='1'){
CreateBiTree(t);//input the numbers
}
else if (d =='2'){//insert a number
InOrderThreading(thrt, t);
}
else if(d=='3'){
inordertraverse_thr(thrt);
}
else if (d=='4')//退出
break;
}
cout << "\n返回上级菜单!!\n";
system ("pause");
return 0;
}
10 楼
liuyi31152 [专家分:0] 发布于 2006-10-22 20:11:00
真的很佩服你们
我是一个新手
我希望有一天能像你们一样
我来回复