?? 新建 文本文檔.txt
字號:
我也來個數據結構綜合
大家請給挑挑錯誤,找找不足吧,謝謝各位
#include<iostream.h>
#include<stdlib.h>
int whatdo1()//總菜單
{
int n;
cout<<"你想做什么?"<<endl;
cout<<"創建鏈表 1"<<endl;
cout<<"創建二叉樹 2"<<endl;
cout<<"圖的遍歷 3"<<endl;
cout<<"數據查找 4"<<endl;
cout<<"數據排序 5"<<endl;
cout<<"退出 0"<<endl;
cin>>n;
return n;
}
int whatdo2()//鏈表輸出插入刪除
{
int n;
cout<<"鏈表輸出、插入、刪除"<<endl;
cout<<"鏈表輸出 1"<<endl;
cout<<"鏈表插入 2"<<endl;
cout<<"鏈表刪除 3"<<endl;
cin>>n;
return n;
}
int whatdo3()//二叉樹遍歷
{
int n;
cout<<"二叉樹排列順序"<<endl;
cout<<"先序 1"<<endl;
cout<<"中序 2"<<endl;
cout<<"后序 3"<<endl;
cout<<"葉子及節點數 4"<<endl;
cin>>n;
return n;
}
int whatdo4()//排序
{
int n;
cout<<"數據排序!"<<endl;
cout<<"快速排序 1"<<endl;
cout<<"冒泡排序 2"<<endl;
cout<<"選擇排序 3"<<endl;
cout<<"插入排序 4"<<endl;
cout<<"堆排序 5"<<endl;
cout<<"數組輸出 6"<<endl;
cin>>n;
return n;
}
int whatdo5()
{
int n;
cout<<"選擇查找方式"<<endl;
cout<<"數組查找 1"<<endl;
cout<<"二叉排序樹查找 2"<<endl;
cin>>n;
return n;
}
int whatdo6()
{
int n;
cout<<"數組查找"<<endl;
cout<<"順序查找 1"<<endl;
cout<<"二分查找 2"<<endl;
cout<<"數組輸出 3"<<endl;
cin>>n;
return n;
}
int whatdo7()
{
int n;
cout<<"深度優先遍歷 1"<<endl;
cout<<"廣度優先遍歷 2"<<endl;
cin>>n;
return n;
}
/*以下為鏈表*/
typedef struct node//定義鏈表
{
int data;
struct node * next;
}listnode;
typedef listnode * linklist;
int getch()//輸入函數
{
int a;
cin>>a;
return a;
}
void del(linklist head) /*刪除一個結點*/
{
linklist p,q;
int a;
if(head->next==NULL)
cout<<"鏈表為空!"<<endl;
else {
cout<<"輸入刪除數據:";
cin>>a;
p=q=head->next;
if(a==0)cout<<"查無此節點!"<<endl;
else{
if(head->next==NULL)cout<<"數據表為空"<<endl;
else if (head->next->data==a)
{
p=head->next;
head->next=p->next;
free(p);
cout<<"刪除完成!"<<endl;
}
else{
while(p->next && p->next->data!=a)
p=p->next;
if(p->next==NULL)cout<<"查無此節點!"<<endl;
else { q=p->next;
p->next=q->next;
free(q);;
cout<<"刪除完成!"<<endl;
}
}
}
}
}
void insertnode(linklist head)//插入
{
linklist p,s;
int i=2,m,n;
cout<<"輸入插入數據和位置:";
cin>>n>>m;
p=head;
while(p->next!=NULL && p->data!=m)
p=p->next;
if(p->data==m)
cout<<"鏈表中有此節點,數據重復,不能插入!!!"<<endl;
else{
p=head;
s=(linklist)malloc(sizeof(node));
s->data=m;
while(i<n && p)
{
p=p->next;
i++;
}
if(!p)
{
cout<<"無此節點!新節點插入鏈表尾!"<<endl;
p=head;
while(p->next!=NULL)
p=p->next;
p->next=s;
cout<<"插入完成!"<<endl;
}
else
{
s->next=p->next;
p->next=s;
cout<<"插入完成!"<<endl;
}
}
}
void shownode(linklist head)//輸出鏈表
{
linklist p;
if(head->next==NULL)
cout<<"鏈表為空!"<<endl;
else
{
p=head->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
}
void crsc(int n,linklist head)//插入刪除函數
{
switch(n)
{
case 1:shownode(head);break;
case 2:insertnode(head);
break;
case 3:del(head);
break;
}
}
linklist createlist( )//創建鏈表
{
int ch;
int n;
char w='y';
cout<<"鏈表元素為整型,輸入0結束!"<<endl;
linklist head=(linklist)malloc(sizeof(listnode));
listnode *s,*r;
r=head;
while((ch=getch())!=0)
{
s=(listnode*)malloc(sizeof(listnode));
s->data=ch;
r->next=s;
r=s;
}
r->next=NULL;
while(w=='y' || w=='Y')
{
n=whatdo2();
crsc(n,head);
cout<<"是否繼續插入或刪除(y/n):";
cin>>w;
}
return head;
}
/*以下為二叉樹*/
typedef struct list//定義二叉樹
{
int data;
struct list * lchild;
struct list * rchild;
}tree;
typedef tree * tr;
int yz(tr p)
{
int m=1,n=0;
if(p->lchild==NULL&& p->rchild==NULL)
return m;
else m++;
if(p->lchild==NULL)
n=yz(p->rchild);
if(p->rchild==NULL)
m=yz(p->lchild);
return m+n;
}
void xian(tr p)//先序
{
if(p)
{
cout<<p->data<<" ";
xian(p->lchild);
xian(p->rchild);
}
}
int num(tr p)//計算節點個數
{
static int m=0;
if(p)
{
m++;
num(p->lchild);
num(p->rchild);
}
return m;
}
void mid(tr p)//中序
{
if(p)
{
mid(p->lchild);
cout<<p->data<<" ";
mid(p->rchild);
}
}
void late(tr p)//后序
{
if(p)
{
late(p->lchild);
late(p->rchild);
cout<<p->data<<" ";
}
}
void shuenxu(int n,tr p)//選擇二叉樹遍歷方式
{
int m,k;
switch(n)
{
case 1:xian(p);break;
case 2:mid(p);break;
case 3:late(p);break;
case 4:m=yz(p);
k=num(p);
cout<<"葉子數為"<<m<<endl;
cout<<"節點數為"<<k;
break;
}
cout<<endl;
}
void a(tr p)//二叉樹遍歷
{
int n;
char w='y';
while(w=='y' || w=='Y')
{
n=whatdo3();
shuenxu(n,p);
cout<<"是否繼續查看其他順序(y/n):";
cin>>w;
}
}
tr create()//創建二叉樹
{
tr p;
char m;
p=(tr)malloc(sizeof(tree));
p->lchild=NULL;
p->rchild=NULL;
cout<<"輸入節點內的數據:";
cin>>p->data;
cout<<"請問"<<p->data<<"節點是否有左子樹(y/n)?";
cin>>m;
if(m=='y' || m=='Y')
p->lchild=create();
cout<<"請問"<<p->data<<"節點是否有右子樹(y/n)?";
cin>>m;
if(m=='y' || m=='Y')
p->rchild=create();
return p;
}
/*以下為圖*/
char geta()
{
char m;
cout<<"輸入一個頂點:";
cin>>m;
return m;
}
void showa(int edges[][100],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<edges[i][j]<<" ";
cout<<endl;
}
}
typedef enum{FALSE,TRUE} Boolean;
Boolean visited[100];
void DFSM(char vexs[],int edges[100][100],int i,int n)
{
int j;
cout<<vexs[i];
visited[i]=TRUE;
for(j=0;j<n;j++)
if(edges[i][j]==1 && !visited[j])
DFSM(vexs,edges,j,n);
}
void DFST(char vexs[],int edges[100][100],int n)
{
int i;
for(i=0;i<n;i++)
visited[i]=FALSE;
for(i=0;i<n;i++)
if(!visited[i])
DFSM(vexs,edges,i,n);
cout<<endl;
}
void createmgragh()
{
int edges[100][100]={0};
char vexs[100],l='y';
int e,n,i,j,k,p;
cout<<"輸入頂點數:";
cin>>n;
cout<<"輸入邊數:";
cin>>e;
for(i=0;i<n;i++)
vexs[i]=geta();
for(i=0;i<n;i++)
for(j=0;j<n;j++)
edges[i][j]=0;
for(k=0;k<e;k++)
{
cout<<"輸入邊號i、j:";
cin>>i>>j;
edges[i-1][j-1]=1;
edges[j-1][i-1]=1;
}
cout<<"領接鋸陣為:"<<endl;
showa(edges,n);
while(l=='y' || l=='Y')
{
p=whatdo7();
switch(p)
{
case 1:DFST(vexs,edges,n);
//case 2:bfst();
}
cout<<"是否繼續其他遍歷(y/n):";
cin>>l;
}
}
/*以下為查找*/
void show(int c[],int high)
{
int i;
for(i=0;i<high;i++)
cout<<c[i]<<" ";
cout<<endl;
}
int sort(int a[],int n,int k)//順序查找
{
int i,m=-1;
for(i=0;i<n;i++)
if(a[i]==k)
m=i;
return m;
}
int cz(int a[],int m,int n,int k)//折半查找
{
int mid,p=-1;
if(m<=n)
{
mid=(m+n)/2;
if(a[mid]==k)
p=mid;
if(a[mid]>k)
p=cz(a,m,mid-1,k);
if(a[mid]<k)
p=cz(a,mid+1,n,k);
}
return p;
}
//定義二叉排序樹
void in(tr *t,int k)
{
tree *f,*p,*q=*t;
p=(tr)malloc(sizeof(tree));
p->data=k;
p->lchild=p->rchild=NULL;
if(*t==NULL)
*t=p;
else
{
while(q)
{
if(q->data==k)
cout<<"樹中有此數據,無須插入!"<<endl;
else
{
f=q;
q=(k<q->data)?q->lchild:q->rchild;
}
}
if(k<f->data)
f->lchild=p;
else f->rchild=p;
}
}
tr chzh(tr t,int k)//二叉排序樹查找
{
if(t==NULL || k==t->data)
return t;
if(k<t->data)
return chzh(t->lchild,k);
else
return chzh(t->rchild,k);
}
void ecs(tr t)//查找函數
{
char w='y';
tr p;
int k;
xian(t);
while(w=='y' || w=='Y')
{
cout<<"輸入查找數據:";
cin>>k;
p=chzh(t,k);
if(p==NULL)
cout<<"無此數據!!!"<<endl;
else cout<<p->data<<endl;
cout<<"是否繼續查找(y/n):";
cin>>w;
}
}
tr createtree()//創建二叉排序樹
{
tr t=NULL;
int k;
cout<<"輸入二叉樹的數據,輸入0結束!"<<endl;
cin>>k;
while(k!=0)
{
in(&t,k);
cin>>k;
}
return t;
}
void sxcz(int k,int a[],int n,int m)//向量查找函數
{
int x,p;
switch(k)
{
case 1:cout<<"輸入查找數據:";
cin>>x;
p=sort(a,m,x);
if(p==-1)
cout<<"無此數據!!!"<<endl;
else cout<<"查找數據在數組的第"<<p+1<<"位上!"<<endl;
break;
case 2:cout<<"輸入查找數據:";
cin>>x;
p=cz(a,n,m,x);
if(p==-1)
cout<<"無此數據!!!"<<endl;
else cout<<"查找數據在數組的第"<<p+1<<"位上!"<<endl;
break;
case 3:show(a,m);break;
}
}
//以下為排序
//void in(int a[],int n)//直接插入排序
void insertsort(int a[],int m)
{
int i,j,x;
for(i=1;i<m;i++)
if(a[i]<a[i-1])
{
x=a[i];
j=i-1;
do
{
a[j+1]=a[j];
j--;
}while(x<a[j]);
a[j+1]=x;
}
show(a,m);
}
void bubblesort(int a[],int n)//冒泡排序
{
int i,j,x;
for(i=0;i<n;i++)
for(j=n-2;j>=i;j--)
if(a[j+1]<a[j])
{
x=a[j+1];
a[j+1]=a[j];
a[j]=x;
}
show(a,n);
}
int partition(int a[],int i,int j)//快速排序
{
int pivot=a[i];
while(i<j)
{
while(i<j && a[j]>=pivot)
j--;
if(i<j)
a[i++]=a[j];
while(i<j && a[i]<=pivot)
i++;
if(i<j)
a[j--]=a[i];
}
a[i]=pivot;
return i;
}
void quicksort(int a[],int m,int n)//快速排序
{
int pivotpos;
if(m<n)
{
pivotpos=partition(a,m,n);
quicksort(a,m,pivotpos-1);
quicksort(a,pivotpos+1,n);
}
}
void selectsort(int a[],int n)//選擇排序
{
int i,j,k,x;
for(i=0;i<n;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(a[j]<a[k])
k=j;
if(k!=i)
{
x=a[i];
a[i]=a[k];
a[k]=x;
}
}
show(a,n);
}
void heapify(int a[],int low,int high)//堆的篩選
{
int large;
int temp=a[low];
for(large=2*low;large<=high;large=large*2)
{
if(large<high && a[large]<a[large+1])
large++;
if(temp>=a[large])
break;
a[low]=a[large];
low=large;
}
a[low]=temp;
}
void buildheap(int a[],int n)//堆的建立
{
int i;
for(i=n/2-1;i>=0;i--)
heapify(a,i,n);
}
void heapsort(int a[],int n)//堆排序
{
int i,x;
buildheap(a,n);
for(i=n-1;i>0;i--)
{
x=a[0];
a[0]=a[i];
a[i]=x;
heapify(a,0,i-1);
}
show(a,n);
}
void czpx(int n,int c[],int low,int high)//排序函數
{
switch(n)
{
case 1:quicksort(c,low,high);
show(c,high);
break;
case 2:bubblesort(c,high);
break;
case 3:selectsort(c,high);break;
case 4:insertsort(c,high);break;
case 5:heapsort(c,high);break;
case 6:cout<<"數組輸出"<<endl;
show(c,high);
break;
}
}
void shuzu(int p)//創建數組
{
int a[100];
char w='y';
int m=0,x,n,low=0,k;
cout<<"輸入數組數據,輸入0結束!"<<endl;
cin>>x;
while(x!=0)
{
a[m]=x;
cin>>x;
m++;
}
k=p;
if(k==2)
{
while(w=='y' || w=='Y')
{
n=whatdo4();
czpx(n,a,low,m);
cout<<"是否繼續其他的排序(y/n):";
cin>>w;
}
}
if(k==1)
{
while(w=='y' || w=='Y')
{
n=whatdo6();
sxcz(n,a,low,m);
cout<<"是否繼續其他的查找(y/n):";
cin>>w;
}
}
}
void cz()
{
int n,m=1;
tr p;
n=whatdo5();
switch(n)
{
case 1:shuzu(m);break;
case 2:p=createtree();
ecs(p);
break;
}
}
/*領接鋸陣*/
/*主函數*/
void main()
{
cout<<"*******************************************************************************"<<endl;
cout<<"* 歡迎使用數據結構綜合應用系統 *"<<endl;
cout<<"* *"<<endl;
cout<<"* WELCOME USE *"<<endl;
cout<<"*******************************************************************************"<<endl;
int n,m=2;
linklist head;
tr p;
n=whatdo1();
while(n!=0)
{
switch(n)
{
case 1:head=createlist();break;
case 2:p=create();a(p);break;
case 3:createmgragh();break;
case 4:cz();break;
case 5:shuzu(m);break;
}
n=whatdo1();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -