目录
1.数组的创建与合并
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<string>
#include<cstring>
#include<list>
using namespace std;
/*
burn it all down ;
cause if I gotta !;
瞎写的脚本
铁打的手
*/
int n , m ;
void input( int arry[] ,int len )
{
for(int i = 0 ; i < len ; i ++ )
{
cin >> arry[i] ;
}
}
void output( int arry[] , int len )
{
for(int i = 0 ; i < len ; i ++ )
{
cout<< arry[i] << " " ;
}
}
void mix(int arrya[],int arryb[], int arryc[] )
{
for(int i = 0 ; i < n ; i ++ )
{
arryc[i] = arrya[i];
}
for(int i = n , j = 0 ; i < m + n ; i ++ , j ++ )
{
arryc[i] = arryb[j];
}
}
const int N = 1e5 + 10;
int main ()
{
int a[N],b[N];
int c[N<<1];
int k;
cout<<"输入数组a的大小 n = " ;
cin >> n;
cout<<"输入数组b的大小 m = " ;
cin >> m;
/*
cout<<"输入数组b的大小 m = " ;
cin >> m;
*/
cout<<"输入a数组"<<n<<"个元素"<<endl;
input(a, n);
cout<<"输入b数组"<<m<<"个元素"<<endl;
input(b, m);
cout << "a数组内容:"<<endl;
output(a,n);
cout<<endl;
cout<< "b数组内容:" <<endl;
output(b,m);
cout<<endl;
//cout<<"请输入要输出多少个a数组元素"<<endl;
cout<<"数组a+b合并后的内容:"<<endl;
mix(a,b,c);
output(c,m+n);
}
2.线性表的插入删除
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<string>
#include<cstring>
#include<list>
#include<stdlib.h>
using namespace std;
typedef int status;
typedef int elemtype;
const int LIST_INIT_SIZE = 100;
typedef struct{
elemtype *elem;
int length;
int listsize;
}sqlist;
status initsqlist(sqlist *L)//初始化
{
L->elem=(elemtype *)malloc(LIST_INIT_SIZE*sizeof(elemtype));
if(L->elem == NULL )
return 0;
L->length = 0;
L->listsize = LIST_INIT_SIZE;
}
status listinsert(sqlist *L ,int i , elemtype e)//插入
{
if(i < 0 || i>L->length + 1 )
{
cout<<"ERROR"<<endl;
return 0;
}
if(L->length >= L->listsize )
{
elemtype *newbase = (elemtype *)realloc(L->elem , (L->listsize + 10)*sizeof(elemtype) );
if(newbase == NULL )
{
cout<<"ERROR"<<endl;
return 0;
}
L->elem;
L->listsize += 10;
}
elemtype *q = &(L->elem[i-1]);
for(elemtype *p = &(L->elem[L->length -1 ]); p >= q ; -- p)
{
*(p+1)=*p;
}
*q=e;
L->length+=1;
return 1;
}
status listdelete(sqlist *L , int i , elemtype *e)//删除
{
if(i < 0 || i > L -> length + 1)
{
cout<<"ERROR"<<endl;
return 0;
}
*e = L->elem[i-1];
elemtype *p = &(L->elem[i-1]);
elemtype *q = &(L->elem[ L->length -1 ] );
for(++p ; p<= q ; p++ )
{
*(p-1)=*p;
}
L->length-=1;
}
status outlist(sqlist *L)// 输出函数
{
for(int i = 0 ; i <L->length ; i ++ )
{
cout<<L->elem[i]<<" ";
}
cout<<endl;
return 1;
}
int main ()
{
sqlist L;
initsqlist(&L);
int n,m;
cout<<"输入顺序表的元素个数:";
cin >> n;
for(int i = 0 ; i< n ; i ++ )
{
printf("请输入第%d个元素:",i+1);
elemtype elem;
cin >> elem;
listinsert(&L,i+1,elem);
}
cout<<"顺序表内元素:"<<endl;
outlist(&L);
cout<<"请输入需要插入的元素和位置:"<<endl;
elemtype tmp;
cin >> tmp >> n;
listinsert(&L, n , tmp);
cout<<"顺序表内元素:"<<endl;
outlist(&L);
cout<<"输入需要删除的元素位置:";
cin >> m ;
elemtype e;
if(listdelete(&L, m , &e))
{
cout<<"删除成功!"<<endl;
printf("删除的元素是%d:\n",e);
}
}
3.线性表的排序与合并
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<string>
#include<cstring>
#include<list>
#include<stdlib.h>
#include<windows.h>
using namespace std;
typedef int status;
typedef int elemtype;
const int LIST_INIT_SIZE = 100;
typedef struct{
elemtype *elem;
int length;
int listsize;
}sqlist;
status initsqlist(sqlist *L)
{
L->elem=(elemtype *)malloc(LIST_INIT_SIZE*sizeof(elemtype));
if(L->elem == NULL )
return 0;
L->length = 0;
L->listsize = LIST_INIT_SIZE;
}
status listinsert(sqlist &L ,int i ,elemtype e)
{
if(i < 0 || i>L.length + 1 )
{
cout<<"ERROR"<<endl;
return 0;
}
if(L.length >= L.listsize )
{
elemtype *newbase = (elemtype *)realloc(L.elem , (L.listsize + 10)*sizeof(elemtype) );
if(newbase == NULL )
{
cout<<"ERROR"<<endl;
return 0;
}
L.elem = newbase ;
L.listsize += 10;
}
elemtype *q = &(L.elem[i-1]);
for(elemtype *p = &(L.elem[L.length -1 ]); p >= q ; -- p)
{
*(p+1)=*p;
}
*q=e;
L.length+=1;
return 1;
}
status listdelete(sqlist *L , int i , elemtype *e)
{
if(i < 0 || i > L -> length + 1)
{
cout<<"ERROR"<<endl;
return 0;
}
*e = L->elem[i-1];
elemtype *p = &(L->elem[i-1]);
elemtype *q = &(L->elem[ L->length -1 ] );
for(++p ; p<= q ; p++ )
{
*(p-1)=*p;
}
L->length-=1;
}
status outlist(sqlist *L)
{
for(int i = 0 ; i < L->length*2 ; i ++ )
cout<<"-";
cout<<endl;
for(int i = 0 ; i <L->length ; i ++ )
{
cout<<L->elem[i]<<" ";
}
cout<<endl;
for(int i = 0 ; i < L->length*2 ; i ++ )
cout<<"-";
cout<<endl;
return 1;
}
status greater_sqsort(sqlist &L)
{
int i,qw,q;
for(i=1;i<L.length;i++)
{
q = i-1;
qw = L.elem[i];
while(q >= 0&& qw < L.elem[q])
{
L.elem[q+1] = L.elem[q];
q--;
}
L.elem[q+1] = qw;
}
return 1;
}
status less_sqsort(sqlist &L)
{
int i,qw,q;
for(i=1;i< L.length;i++)
{
q = i-1;
qw = *(L.elem+i);
while(q >= 0&& qw >= *(L.elem+q) )
{
*(L.elem+q+1)= *(L.elem+q);
q--;
}
*(L.elem+q+1) = qw;
}
return 1;
}
void pd(sqlist &L)
{
int d;
cout<<"1.从小到大"<<endl;
cout<<"2.从大到小"<<endl;
cin>> d;
if(d == 1)
greater_sqsort(L);
else if(d == 2)
less_sqsort(L);
else
{
cout<<"error"<<endl;
pd(L);
}
}
void sqmerge(sqlist LA,sqlist LB , sqlist &LC)
{
int i = 0 , j = 0 , k = 0 ;
while(i < LA.length && j < LB.length )
{
if( *(LA.elem+i) <= *(LB.elem+j) )
{
// elemtype elem = *(LA->elem+i);
listinsert(LC,k+1,*(LA.elem+i));
i++;
k++;
}
else
{
listinsert(LC,k+1,*(LB.elem+j));
j++;
k++;
}
}
while(i<LA.length)
{
listinsert(LC,k+1,*(LA.elem+i));
i++;
k++;
}
while(j<LB.length)
{
listinsert(LC,k+1,*(LB.elem+j));
j++;
k++;
}
}
int main ()
{
sqlist L;
initsqlist(&L);
int n,m;
cout<<"输入顺序表的元素个数:";
cin >> n;
for(int i = 0 ; i< n ; i ++ )
{
printf("请输入第%d个元素:",i+1);
elemtype elem;
cin >> elem;
listinsert(L,i+1,elem);
}
cout<<"顺序表内元素:"<<endl;
outlist(&L);
cout<<"输入排序方式:"<<endl;
pd(L);
cout<<"排序后结果为:"<<endl;
outlist(&L);
sqlist Q;
initsqlist(&Q);
cout<<"输入顺序表2的元素个数:";
cin >> n;
for(int i = 0 ; i< n ; i ++ )
{
printf("请输入第%d个元素:",i+1);
elemtype elem;
cin >> elem;
listinsert(Q,i+1,elem);
}
//listinsert(&Q,2,*(L.elem));
cout<<"顺序表2内元素:"<<endl;
outlist(&Q);
cout<<"输入排序方式:"<<endl;
pd(Q);
cout<<"排序后结果为:"<<endl;
outlist(&Q);
sqlist LC;
initsqlist(&LC);
sqmerge(L,Q,LC);
cout<<"表1和表2合并后结果为:" <<endl;
outlist(&LC);
}
4.链表的插入与删除
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<string>
#include<cstring>
#include<list>
#include<stdlib.h>
using namespace std;
typedef struct node{
int data;
struct node *next;
}lnode,*linklist;
int initlist(linklist &L)
{
L=(lnode *)malloc(sizeof(lnode));
if(L == NULL)
{
return 0;
}
L->next = NULL;
return 1;
}
int getlength(linklist L)
{
int length = 0;
lnode *p = L->next;
while( p )
{
length ++ ;
p = p->next;
}
return length;
}
void tailinsert(linklist &L,int n)
{
int d;
lnode *end;
end = L;
while(n--)
{
cin >> d;
lnode *p = (linklist)malloc( sizeof(lnode) );
p->data = d;
end->next = p;
end = p;
}
end->next = NULL;
}
void headinsert(linklist &L,int n)
{
int d;
while(n--)
{
cin >> d;
lnode *p = (linklist)malloc( sizeof(lnode) );
p->data = d;
p->next = L->next;
L->next = p;
}
}
void print(linklist &L)
{
lnode *p;
p = L;
p=p->next;
while(p->next!=NULL)
{
cout<< p->data << " ";
p=p->next;
}
cout<<p->data<<endl;
}
void listinsert(linklist L,int n , int e)
{
lnode *pre;
pre=L;
int count = 0;
if(n<=0)
{
printf("插入位置小于0\n");
return ;
}
while(pre!=NULL && count < n -1 )
{
pre = pre->next;
count ++ ;
}
if(pre == NULL )
{
printf("插入位置过大\n");
return ;
}
lnode *s = (linklist)malloc( sizeof(lnode) );
s->data = e;
s->next = pre->next;
pre->next = s;
}
void listdel(linklist &L,int n)
{
lnode *pre,*s;
pre=L;
int count = 0;
if(n<=0)
{
printf("删除位置小于0\n");
return ;
}
while(pre!=NULL && count < n - 1)
{
pre = pre->next;
count ++ ;
}
if(pre == NULL )
{
printf("删除位置过大\n");
return ;
}
s=pre->next;
pre->next = s->next;
free(s);
}
void getelem(linklist &L , int n)
{
node *pre ;
pre = L;
int count;
while(pre!=NULL && count < n )
{
pre=pre->next;
count ++;
}
printf("%d.位置的值为:",n);
cout<<pre->data<<endl;
}
int main ()
{
linklist L;
initlist(L);
printf("输入链表L节点数:");
int n;
cin >> n ;
cout<<"插入方式选择:"<<endl;
cout<<"1.头插法 2.尾插法"<<endl;
int ok = 0;
cin >> ok;
while(ok != 1&&ok != 2)
{
cout<<"重新输入"<<endl;
cin >> ok;
}
cout<<"输入链表内元素"<<endl;
if(ok==1)
{
headinsert(L,n);
}
else
{
tailinsert(L,n);
}
cout<<"链表内元素:"<<endl;
print(L);
cout<<"输入插入元素位置"<<endl;
int m;
cin >> m ;
int data;
cout<<"输入元素的值"<<endl;
cin >> data;
listinsert(L,m,data);
cout<<"链表内元素:"<<endl;
print(L);
cout<<"输入删除元素位置:"<<endl;
cin >> m;
listdel(L,m);
cout<<"链表内元素:"<<endl;
print(L);
cout<<"输入需要查询的元素位置"<<endl;
cin >> m;
getelem(L,m);
}
5.链表的二种创建与输出
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<string>
#include<cstring>
#include<list>
#include<stdlib.h>
using namespace std;
typedef struct Lnode
{
char data;
struct Lnode *next;
}Lnode,*Linklist;
//逆序创建
Linklist Createlist(Linklist &L)
{
Linklist p;
int n;
L = (Linklist)malloc(sizeof(Lnode));
L->next = NULL;
printf("请输入将要载入链表的元素个数:");
scanf("%d", &n);
getchar();
printf("请逆序输入%d个元素的值:\n", n);
for (int i = n; i > 0;i--)
{
p = (Linklist)malloc(sizeof(Lnode));
scanf("%s", &p->data);
p->next = L->next;
L->next = p;
}
printf("逆序建立带表头结点的单链表完成!\n\n");
return L;
}
//顺序创建
/*void Createlist(Linklist &L)
{
Linklist p,q;
cout << "此为顺序创建链表" << endl;
L = (Lnode *)malloc(sizeof(Lnode));
L -> next = NULL;
q = L;
p = (Linklist)malloc(sizeof(Lnode));
cout << "输入字符,以“#”结尾:";
cin >> p -> data;
while(p -> data != '#')
{
p -> next = q -> next;
q -> next = p;
q = p;
p = (Linklist)malloc(sizeof(Lnode));
cin >> p -> data;
}
printf("顺序建立带表头结点的单链表完成!\n\n");
}*/
void Printlist(Linklist L)
{
cout << "\n表中的数据为:\n";
Linklist p;
p = L -> next;
while(p)
{
cout << p -> data << ' ';
p = p -> next;
}
cout << endl;
}
int ListNodeCount(Linklist head)
{
Linklist tmp = head;
int nodeCnt = 0;
cout << "链表的节点数为:";
while (tmp != NULL)
{
nodeCnt++;
tmp = tmp -> next;
}
cout << nodeCnt - 1;
}
int main ()
{
Linklist L;
int i,d;
char e;
Createlist(L);
Printlist(L);
ListNodeCount(L);
}
6.链表的排序与合并
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<string>
#include<cstring>
#include<list>
#include<stdlib.h>
using namespace std;
typedef struct node{
int data;
struct node *next;
}lnode,*linklist;
int initlist(linklist &L)
{
L=(lnode *)malloc(sizeof(lnode));
if(L == NULL)
{
return 0;
}
L->next = NULL;
return 1;
}
int getlength(linklist L)
{
int length = 0;
lnode *p = L->next;
while( p )
{
length ++ ;
p = p->next;
}
return length;
}
void tailinsert(linklist &L,int n)
{
int d;
lnode *end;
end = L;
while(n--)
{
cin >> d;
lnode *p = (linklist)malloc( sizeof(lnode) );
p->data = d;
end->next = p;
end = p;
}
end->next = NULL;
}
void headinsert(linklist &L,int n)
{
int d;
while(n--)
{
cin >> d;
lnode *p = (linklist)malloc( sizeof(lnode) );
p->data = d;
p->next = L->next;
L->next = p;
}
}
void print(linklist &L)
{
lnode *p;
p = L;
p=p->next;
while(p!=NULL)
{
cout<< p->data << " ";
p=p->next;
}
// cout<<p->data<<endl;
}
void greatersort(linklist &L , int count )
{
int i , tmp = 0 ;
lnode *pre;
for(int i = 0 ; i < count ; i++ )
{
for(pre = L->next ; pre->next != NULL ; pre=pre->next)
{
if(pre->data < pre->next->data)
{
tmp = pre->data;
pre->data = pre->next->data;
pre->next->data=tmp;
}
}
}
}
void lesssort(linklist &L , int count )
{
int i , tmp = 0 ;
lnode *pre;
for(int i = 0 ; i < count ; i++ )
{
for(pre = L->next ; pre->next != NULL ; pre=pre->next)
{
if(pre->data > pre->next->data)
{
tmp = pre->data;
pre->data = pre->next->data;
pre->next->data=tmp;
}
}
}
}
void listmix(linklist &L,linklist &L2,linklist &ptr)
{
ptr = L;
linklist tmp = ptr;
while(tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = L2->next;
}
int main ()
{
linklist L,L2,L3;
int n;
initlist(L);
initlist(L2);
initlist(L3);
cout<<"输入链表L的元素个数:";
cin >> n ;
cout<<"输入链表L的元素"<<endl;
tailinsert(L,n);
cout<<"输入链表L2的元素个数:";
cin >> n;
cout<<"输入链表L2的元素"<<endl;
tailinsert(L2,n);
cout<<"排序L:"<<endl;
while(1)
{
cout<<"输入1降序排序:"<<endl;
cout<<"输入2升序排序:"<<endl;
int k;
cin >> k;
if(k == 1)
{
greatersort(L,getlength(L));
break;
}
else if (k == 2)
{
lesssort(L,getlength(L));
break;
}
}
cout<<"排序L2:"<<endl;
while(1)
{
cout<<"输入1降序排序:"<<endl;
cout<<"输入2升序排序:"<<endl;
int k;
cin >> k;
if(k == 1)
{
greatersort(L2,getlength(L2));
break;
}
else if (k == 2)
{
lesssort(L2,getlength(L2));
break;
}
}
cout<<"L内元素:";
print(L);
cout<<endl;
cout<<"L2内元素";
print(L2);
cout<<endl;
listmix(L,L2,L3);
cout<<"排序合并后的L1,L2:"<<endl;
while(1)
{
cout<<"输入1降序排序:"<<endl;
cout<<"输入2升序排序:"<<endl;
int k;
cin >> k;
if(k == 1)
{
greatersort(L3,getlength(L3));
break;
}
else if (k == 2)
{
lesssort(L3,getlength(L3));
break;
}
}
cout<<"合并后的L1,L2内元素:"<<endl;
print(L3);
}
7.栈与数据转化
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<string>
#include<cstring>
#include<list>
#include<stdlib.h>
using namespace std;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int status;
typedef int selemtype;
typedef struct
{
selemtype *base;
selemtype *top;
int stacksize;
}sqstack;
status initstack(sqstack &S)
{
S.base = (selemtype *)malloc(STACK_INIT_SIZE*sizeof(selemtype));
if(!S.base)
{
cout << "初始化失败";
exit(0);
}
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return 1;
}
status push(sqstack &S,selemtype e)
{
if(S.top - S.base >= S.stacksize)
{
selemtype *newbase = (selemtype *)realloc(S.base,(S.stacksize + STACKINCREMENT)*sizeof(selemtype));
if(!newbase)
{
cout << "内存不够,栈溢出" << endl;
return 0;
}
S.base = newbase;
S.top = S.base + S.stacksize;
S.stacksize +=STACK_INIT_SIZE;
}
*(S.top++) = e;
return 1;
}
status pop(sqstack &S,selemtype &e)
{
if(S.top == S.base)
{
cout << "栈空" ;
return 0;
}
e = *(--S.top);
return 1;
}
status showstack(sqstack S)
{
while (S.base != S.top)
{
cout << *(--S.top) << " ";
}
cout << endl;
return 1;
}
status emptystack(sqstack &S)
{
if(S.top != S.base)
{
return 0;
}
else return 1;
}
status stacklength(sqstack &S)
{
return (S.top - S.base);
}
status clearstack(sqstack &S)
{
S.top = S.base ;
return 1;
}
status DestroyStack(sqstack &S)
{
free(S.base);
S.base = S.top = NULL;
S.stacksize = 0;
return 1;
}
status TopStack(sqstack &S,int &e)
{
if(S.top == S.base)
return 0;
e=*(S.top - 1);
return 1;
}
status JinZhi(sqstack &S,int e,int jinzhi)
{
while (e)
{
push(S,e%jinzhi);
e/=jinzhi;
}
return 1;
}
status ShowjzStack(sqstack &S)
{
while(!emptystack(S))
{
int ans=0;
pop(S,ans);
if(ans < 10)
{
cout << ans;
}
else
{
char c = ans - 10 + 'A';
cout << c;
}
}
return 1;
}
int main()
{
sqstack S;
initstack(S);
int order = 0 ,init = 0,des = 0 ,len = 0,e = 0;
cout<<"*----------------栈--------------*"<<endl;
cout<<"*---------1.初始化栈-------------*"<<endl;
cout<<"*---------2.销毁栈---------------*"<<endl;
cout<<"*---------3.清空栈---------------*"<<endl;
cout<<"*---------4.栈判空---------------*"<<endl;
cout<<"*---------5.求栈长度-------------*"<<endl;
cout<<"*---------6.获取栈顶元素---------*"<<endl;
cout<<"*---------7.插入一个元素---------*"<<endl;
cout<<"*---------8.删除一个元素---------*"<<endl;
cout<<"*---------9.输出所有元素---------*"<<endl;
cout<<"*---------10.进制转换------------*"<<endl;
do{
cout<<"请输入指令"<<endl;
cin >> order;
if(order <= 0 || order > 10 )
{
cout<<"error"<<endl;
}
else if(!init && order>1 && order!=10)
cout<<"请先初始化栈"<<endl;
else if(des && order>1 && order!=10)
cout<<"栈已销毁,请先初始化"<<endl;
else
{
switch (order) {
case 1: initstack(S);
init=1;
des = 0;
cout<<"栈初始化已完成"<<endl;
break;
case 2:
DestroyStack(S);
cout<<"成功销毁栈"<<endl;
des = 1;
break;
case 3:
clearstack(S);
cout<<"栈已清空"<<endl;
break;
case 4:
if(!emptystack(S))
cout<<"栈非空"<<endl;
else
cout<<"栈为空"<<endl;
break;
case 5:
len = stacklength(S);
cout<<"栈长度为:"<<len<<endl;
break;
case 6:
if(TopStack(S,e))
cout<<"栈顶元素为:"<<e<<endl;
else
cout<<"空栈,无栈顶元素"<<endl;
break;
case 7:
cout<<"请输入插入元素:";
cin>>e;
push(S,e);
cout<<"入栈成功"<<endl;
break;
case 8:
if(pop(S,e))
cout<<"栈顶元素"<<e<<"出栈成功"<<endl;
else
cout<<"栈为空"<<endl;
break;
case 9:
cout<<"栈为:"<<endl;
showstack(S);
break;
case 10:
cout<<"输入一个十进制数"<<endl;
cin>>e;
cout<<"输入要转换的进制"<<endl;
int jinzhi;
cin>>jinzhi;
sqstack J;
initstack(J);
cout<<"10进制数"<<e<<"转换为"<<jinzhi<<"进制数后为:";
if(e<0)
{
cout<<'-';
e=-e;
}
if(!e)
cout<<0;
JinZhi(J,e,jinzhi);
ShowjzStack(J);
cout<<endl;
break;
default:
cout<<"程序已退出"<<endl;
break;
}
}
}while(order > 0 );
}
8.队列的表示与实现
#include <iostream>
#include <stack>
#include <string>
#include <list>
#include <stdlib.h>
using namespace std;
typedef int status;
typedef int elemtype;
typedef struct {
elemtype *base;
int front;
int rear;
}sqQuene;
status initqueue(sqQuene &Q)
{
Q.base = (elemtype *)malloc(11*sizeof(Q));
if(!Q.base)
{
cout << "内存错误" << endl;
return 0;
}
Q.front = Q.rear = 0;
return 1;
}
status enqueue(sqQuene &Q,elemtype e)
{
if((Q.rear + 1)%11 == Q.front)
{
cout << "队列满" << endl;
return 0;
}
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1)%11;
return 1;
}
status dequeue(sqQuene &Q,elemtype &e)
{
if(Q.front == Q.rear)
{
cout << "这里没有数据" << endl;
return 0;
}
e = Q.base[Q.front];
Q.front = (Q.front + 1 )%11;
return 1;
}
status qulength(sqQuene Q)
{
return (Q.rear - Q.front + 11)%11;
}
int main()
{
int z;
elemtype e;
sqQuene Q;
initqueue(Q);
cout << "请输入进入队列的10个元素" << endl;
for (int i = 0 ; i < 10 ; i ++)
{
cin >> e;
enqueue(Q,e);
}
cout << "选择出队元素个数" << endl;
cin >> z;
for (int i = 0 ; i < z ; i++)
{
dequeue(Q,e);
cout << "出队元素为:" << e << endl;
}
cout << "再入队相同个数元素" << endl;
for (int i = 0 ; i < z ; i++)
{
cin >> e;
enqueue(Q,e);
}
cout << "队列长度:" << qulength(Q) <<endl;
cout << "队列中元素" << endl;
for (int i = 0 ;i < 10 ; i++)
{
dequeue(Q,e);
cout << e << " ";
}
cout << endl;
return 0;
}
9.定长串的基本操作
#include <iostream>
#include <cstring>
using namespace std;
#define MAXSTRLEN 200
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef unsigned char sstring[MAXSTRLEN + 1];
int strassig(sstring &T,char chars[])
{
int i;
if(strlen(chars)>MAXSTRLEN)
return ERROR;
else
{
T[0] = strlen(chars);
for(i = 0 ; i < strlen(chars); i++)
T[i + 1] = chars[i];
}
return OK;
}
int strcopy(sstring &T,sstring s)
{
if(s[0] > MAXSTRLEN)
return ERROR;
else
{
for(int i = 0 ; i <= s[0] ; i++)
T[i] = s[i];
}
return OK;
}
int strempty(sstring s)
{
if(s[0] == 0)
return OK;
else
return ERROR;
}
int strcompare(sstring s,sstring T)
{
int i ;
for(i = 1 ; i <= s[0] && i <= T[0] ; i ++)
if(s[i] != T[i])
return (s[i] - T[i]);
return (s[i] - T[i]);
}
int strlength(sstring s)
{
return s[0];
}
void clearstring(sstring &s)
{
s[0] = 0;
s[1] = '\0';
}
void concat (sstring &T,sstring s1,sstring s2)
{
int i , j , uncut;
if(s1[0] + s2[0] <= MAXSTRLEN)
{
for(i = 1 ; i < s1[0] ; i++)
T[i] = s1[i];
for(j = 1 ; j <= s2[0] ; j++)
T[i + j] = s2[j];
T[0] = s1[0] + s2[0] ;
}
else if(s1[0] < MAXSTRLEN)
{
for(i = 1 ; i < s1[0] ; i++)
T[i] = s1[i];
for(j = 1 ; j < MAXSTRLEN - s1[0] ; j++)
T[i + j] = s2[j];
T[0] = s1[0] + j;
}
else if(s1[0] == MAXSTRLEN )
{
for(i = 1 ; i < s1[0];i++)
T[i] = s1[i];
T[0] = s1[0];
uncut = FALSE;
}
}
int substring(sstring &sub,sstring s,int pos,int len)
{
if(pos < 1 || pos > s[0] || len < 0 ||len > s[0] - pos + 1)
return ERROR;
for (int i = 0 ; i <= len; i ++)
sub[i] = s[pos - 1 + i];
sub[0] = len;
return OK;
}
void shuchu(sstring s)
{
for(int i = 1 ; i <= s[0] ; i++)
cout << s[i];
cout << endl;
}
int main()
{
int i,m,n;
sstring T,s,sub1,sub2;
char a[256];
char chars1[] = "abcdefg";
char chars2[8] = "hijklmn";
strassig(T,chars1);
cout << "将chars1的值赋予T" << endl;
shuchu(T);
cout << "字符串的长度为" << strlength(T) << endl;
cout << "将chars2的值赋予s" << endl;
strassig(s,chars2);
shuchu(s);
cout << "字符串的连接";
concat(T,T,s);
shuchu(T);
cout << "请输入m,n,并求字符串T中第m个字符以后长度为n的字符子串"<<endl;
cin >> m;
cin >> n;
substring(sub1,T,m,n);
shuchu(sub1);
cout << "请输入m,n,并求字符串s中第m个字符以后长度为n的字符子串"<<endl;
cin >> m;
cin >> n;
substring(sub2,s,m,n);
shuchu(sub2);
concat(sub1,sub1,sub2);
cout << "两个子串的连接";
shuchu(sub1);
}
10.串的模式匹配
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<string>
#include<cstring>
#include<list>
#include<stdlib.h>
using namespace std;
typedef int status;
typedef int selemtype;
#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define MAXLEN 255
typedef int status;
typedef char sstring[MAXLEN+1];
void stringassign(sstring &t, char s[])
{
t[0] = strlen(s);
if(strlen(s) > MAXLEN)
{
cout<<"the string overflow!!!"<<endl;
return ;
}
for(int i = 0 ; i < t[0] ; i ++)
{
t[i+1]=s[i];
}
}
void output(sstring s)
{
for(int i = 1 ; i <= s[0] ; i ++ )
{
printf("%c",s[i]);
}
}
void concat(sstring &T, sstring s1, sstring s2)
{
int i, j, uncut;
if (s1[0] + s2[0] <= MAXLEN)
{
for (i = 1; i<= s1[0]; i++)
T[i] = s1[i];
for (j = 0; j< s2[0]; j++)
T[i + j] = s2[j+1];
T[0] = s1[0] + s2[0];
}
else if (s1[0]< MAXLEN)
{
for (i = 1; i<s1[0]; i++)
T[i] = s1[i];
for (j = 1; j< MAXLEN - s1[0]; j++)
T[i + j] = s2[j];
T[0] = s1[0] + j;
}
else if (s1[0] == MAXLEN)
{
for (i = 1; i<s1[0]; i++)
T[i] = s1[i];
T[0] = s1[0];
uncut = FALSE;
}
}
int substring(sstring &sub, sstring s, int pos, int len)
{
if (pos<1 || pos>s[0] || len<0 || len>s[0] - pos + 1)
{
cout<<"该子串不存在"<<endl;
return 0;
}
for (int i = 1; i <= len; i++)
sub[i] = s[pos - 1 + i];
sub[0] = len;
return OK;
}
int stringcmp(sstring T,sstring T2)
{
int i = 1;
int t = 0;
while( i<=T[0]&&i<=T2[0] )
{
if(T[i] == T2[i])
i++;
else
{
t = T[i] - T2[i];
if ( t < 0 )
{
return -1;
}
else
{
return 1;
}
}
}
if( i > T[0]&&i>T2[0])
return 0;
else
{
if(i>T[0])
{
return -1;
}
else{
return 1;
}
}
}
int index(sstring S,sstring T,int pos)
{
if(pos<1)
{
return 0;
}
int n = S[0],m=T[0];
int i = pos;
while(i <= n - m + 1)
{
sstring sub;
substring(sub,S,i,m);
if(stringcmp(sub,T)!=0)
{
i++;
}
else return i;
}
return 0;
}
/*int index2(sstring S, sstring T,int pos)
{
for(int i = 1 ; i<=S[0]-T[0]+1 ; i ++ )
{
int j = 1;
while(j<=T[0])
{
if(S[i]==T[i])
{
j++;
}
else{
break;
}
}
if(j>=T[0])
{
return i;
}
}
return 0;
}
*/
int main ()
{
char s1[255555],s2[255];
sstring t1,t2,t;
cout<<"请输入字符串s1"<<endl;
gets(s1);
cout<<"请输入字符串s2"<<endl;
gets(s2);
stringassign(t1,s1);
stringassign(t2,s2);
if(!stringcmp(t1,t2))
{
cout<<"s1等于s2"<<endl;
}
else if (stringcmp(t1,t2)==1)
{
cout<<"s1大于s2"<<endl;
}
else
{
cout<<"s1小于s2"<<endl;
}
cout<<"输入主串:"<<endl;
char s3[25555],s4[2555];
gets(s3);
sstring t3,t4;
stringassign(t3,s3);
cout<<"输入模式串:"<<endl;
gets(s4);
stringassign(t4,s4);
int pos= 0;
cout<<"输入在多少位后查找模式串:"<<endl;
cin >> pos;
cout<<"模式1:"<<endl;
if(index(t3,t4,pos))
{
cout<<"模式串位置为: "<<index(t3,t4,pos)<<endl;
}
else{
cout<<"查询失败"<<endl;
}
/* cout<<"模式2:"<<endl;
if(index2(t3,t4,pos))
{
cout<<"模式串位置为: "<<index2(t3,t4,pos)<<endl;
}
else{
cout<<"查询失败"<<endl;
}*/
return 0;
}
11.矩阵的转置
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<string>
#include<cstring>
#include<list>
#include<stdlib.h>
#define ok 1
#define MAX 12500
using namespace std;
typedef int status;
typedef int elemtype;
int num[MAX];
int cpot[MAX];
typedef struct
{
int i , j ;
elemtype e;
}triple;
typedef struct
{
triple data[MAX];
int mu,nu,tu;
}TM;
void Create(TM &M)
{
printf("请输入总行,总列,非零元素的个数\n");
cin >> M.mu>>M.nu>>M.tu;
cout<<"行号,列号,值"<<endl;
for(int t = 1 ; t <= M.tu ; t ++ )
{
cin >> M.data[t].i>>M.data[t].j>>M.data[t].e;
}
}
status TransposeSMatrix1(TM M,TM &T)
{
int col,p,q,t;
T.mu = M.nu;
T.nu = M.mu;
T.tu = M.tu;
if(T.tu)
{
for(col=1;col<=M.nu;++col)
num[col] = 0;
for(t=1;t<=M.tu;++t)
++num[M.data[t].j];
cpot[1] = 1;
for(col=2;col<=M.nu;++col)
cpot[col] = cpot[col-1] + num[col-1];
for(p=1;p<=M.tu;++p)
{
col = M.data[p].j;
q = cpot[col];
T.data[q].i = M.data[p].j;
T.data[q].j = M.data[p].i;
T.data[q].e = M.data[p].e;
++cpot[col];
}
}
return 1;
}
status TransposeSMatrix2(TM &G,TM &T)
{
int k=1;
T.mu = G.nu;
T.nu = G.mu;
T.tu = G.tu;
int col = 0 , p = 0;
for(int col=1;col<=G.nu;col++)
{
for(int p=1;p<=G.tu;p++)
{
if(G.data[p].j == col )
{
T.data[k].i = G.data[p].j;
T.data[k].j = G.data[p].i;
T.data[k].e = G.data[p].e;
k++;
}
}
}
return 1;
}
void outTM(TM M)
{
int i;
printf("矩阵的总行、总列、总非0元素的个数:\n");
printf("%4d%4d%4d\n", M.mu, M.nu, M.tu);
printf("---------------\n");
printf(" i j v\n");
printf("---------------\n");
for (i = 1; i <= M.tu; i++)
{
printf("%4d%4d%4d\n", M.data[i].i, M.data[i].j, M.data[i].e);
}
printf("---------------\n");
}
int main ()
{
TM M,T,T2;
Create(M);
cout<<"矩阵为:"<<endl;
cout<<"三元组:"<<endl;
outTM(M);
TransposeSMatrix1(M,T);
cout<<"使用第一种方法转置的矩阵为:"<<endl;
cout<<"三元组:"<<endl;
outTM(T);
TransposeSMatrix2(M,T2);
cout<<"使用第二种方法转置的矩阵为:"<<endl;
outTM(T2);
}
12.二叉树的基本操作
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<string>
#include<cstring>
#include<list>
#include<stdlib.h>
using namespace std;
typedef int status;
typedef int selemtype;
#define ok 1
#define error 0
typedef struct bitnode
{
char data;
struct bitnode *lchild,*rchild;
}Bitnode,*Bitree;
status creatbitree(Bitree &t)
{
char ch;
ch=getchar();
if(ch == ' ')
{
t=NULL;
}
else{
t=(Bitree)malloc(sizeof(Bitnode));
t->data=ch;
creatbitree(t->lchild);
creatbitree(t->rchild);
}
return 1;
}
void preorder(Bitree root)//递归先序遍历
{
if(root)
{
printf("%c",root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}
void Inorder(Bitree root)
{
if(root)
{
Inorder(root->lchild);
printf("%c",root->data);
Inorder(root->rchild);
}
}
void postorder(Bitree root)
{
if(root)
{
postorder(root->lchild);
postorder(root->rchild);
printf("%c",root->data);
}
}
int main ()
{
Bitree T;
creatbitree(T);
cout<<endl;
cout<<"先序遍历结果为:";
preorder(T);
cout<<endl;
cout<<"中序遍历结果为:";
Inorder(T);
cout<<endl;
cout<<"后序遍历结果为:";
postorder(T);
}
13.二叉树的高度,叶子结点,总结点数
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<string>
#include<cstring>
#include<list>
#include<stdlib.h>
using namespace std;
typedef int status;
typedef int selemtype;
#define ok 1
#define error 0
//设置二叉树
typedef struct bitnode
{
char data;
struct bitnode *lchild,*rchild;
}Bitnode,*Bitree;
//创建二叉树
status creatbitree(Bitree &t)
{
char ch;
ch=getchar();
if(ch == ' ')
{
t=NULL;
}
else{
t=(Bitree)malloc(sizeof(Bitnode));
t->data=ch;
creatbitree(t->lchild);
creatbitree(t->rchild);
}
return 1;
}
//递归先序遍历
void preorder(Bitree root)
{
if(root)
{
printf("%c",root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}
//递归中序遍历
void Inorder(Bitree root)
{
if(root)
{
Inorder(root->lchild);
printf("%c",root->data);
Inorder(root->rchild);
}
}
//递归后序遍历
void postorder(Bitree root)
{
if(root)
{
postorder(root->lchild);
postorder(root->rchild);
printf("%c",root->data);
}
}
//求树高度
int getheight(Bitree root)
{
if (root==NULL)
{
return 0;
}
//求左子树高度
int lheight=getheight(root->lchild);
//求右子树高度
int rheight=getheight(root->rchild);
//当前节点高度
int height=lheight>rheight ? lheight+1 : rheight+1;;
return height;
}
//求叶子节点
void calculateleafnum(Bitree root,int* leafnum)
{
if (root==NULL)
{
return;
}
if (root->rchild==NULL && root->lchild==NULL)
{
(*leafnum)++;
}
//左子树节点数目
calculateleafnum(root->lchild,leafnum);
//右子树节点数目
calculateleafnum(root->rchild,leafnum);
}
void allleafnum(Bitree root,int* leafnum)
{
if (root == NULL)
{
return ;
}
(*leafnum)++;
allleafnum(root->lchild,leafnum);
allleafnum(root->rchild,leafnum);
}
int main ()
{
Bitree T;
creatbitree(T);
cout<<endl;
cout<<"先序遍历结果为:";
preorder(T);
cout<<endl;
cout<<"中序遍历结果为:";
Inorder(T);
cout<<endl;
cout<<"后序遍历结果为:";
postorder(T);
cout << endl;
//计算二叉树高度
int height=getheight(T);
cout<<"二叉树的高度为:"<<height<<endl;
//计算二叉树叶子数
int num=0;
calculateleafnum(T,&num);
cout<<"二叉树的节点为:"<<num<<endl;
int all = 0 ;
allleafnum(T,&all);
cout << "二叉树的总结点数为:" << all << endl;
}
14.二叉树非递归遍历
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<string>
#include<cstring>
#include<list>
#include<stdlib.h>
using namespace std;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int status;
typedef struct bitnode
{
char data;
struct bitnode *lchild,*rchild;
}Bitnode,*Bitree;
typedef Bitree selemtype;
status creatbitree(Bitree &t)
{
char ch;
ch=getchar();
if(ch == ' ')
{
t=NULL;
}
else{
t=(Bitree)malloc(sizeof(Bitnode));
t->data=ch;
creatbitree(t->lchild);
creatbitree(t->rchild);
}
return 1;
}
void preorder(Bitree root)//递归先序遍历
{
if(root)
{
printf("%c",root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}
void inorder(Bitree root)
{
if(root)
{
inorder(root->lchild);
printf("%c",root->data);
inorder(root->rchild);
}
}
void postorder(Bitree root)
{
if(root)
{
postorder(root->lchild);
postorder(root->rchild);
printf("%c",root->data);
}
}
typedef struct
{
selemtype *base;
selemtype *top;
int stacksize ;
}sqstack;
status initstack(sqstack &S)
{
S.base = (selemtype *)malloc(STACK_INIT_SIZE*sizeof(selemtype));
if(!S.base)
{
cout<<"初始化失败";
exit(0);
}
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return 1;
}
status push(sqstack &S , selemtype e)
{
if(S.top-S.base>= S.stacksize)
{
selemtype *newbase=(selemtype *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(selemtype));
if(!newbase)
{
cout<<"内存不足栈空间增加失败"<<endl;
return 0;
}
S.base = newbase;
S.top=S.base + S.stacksize;
S.stacksize+= STACK_INIT_SIZE;
}
*(S.top++) = e;
return 1;
}
status pop(sqstack &S, selemtype &e)
{
if(S.top==S.base)
{
cout<<"栈已空"<<endl;
return 0;
}
e =*(--S.top);
return 1;
}
status showstack(sqstack S)
{
while(S.base != S.top )
{
cout<<*(--S.top)<<" ";
}
cout <<endl;
return 1;
}
status emptystack(sqstack &S)
{
if(S.top != S.base)
{
return 0;
}
else return 1;
}
status stacklength(sqstack &S)
{
return (S.top - S.base);
}
status clearstack (sqstack &S)
{
S.top = S.base;
return 1;
}
status DestroyStack(sqstack &S)
{
free(S.base);
S.base=S.top=NULL;
S.stacksize=0;
return 1;
}
status TopStack(sqstack &S,selemtype &e)
{
if(S.top==S.base)
return 0;
e=*(S.top-1);
return 1;
}
void Preorder(Bitree T)
{
sqstack S;
initstack(S);
Bitnode *p;
p = T;
while(p || !emptystack(S))
{
if(p)
{
printf("%c",p->data);
push(S,p);
p= p->lchild;
}
else
{
pop(S,p);
p=p->rchild;
}
}
}
void Inorder(Bitree T)
{
sqstack S;
initstack(S);
Bitnode *p;
p = T;
while(p || !emptystack(S))
{
if(p)
{
push(S,p);
p = p->lchild;
}
else
{
pop(S,p);
printf("%c",p->data);
p = p->rchild;
}
}
}
void Inorder2(Bitree T)
{
sqstack S;
initstack(S);
Bitnode *p;
p = T;
push(S,p);
while(!emptystack(S))
{
while(TopStack(S,p)&&p)
{
push(S,p->lchild);
}
pop(S,p);
if(!emptystack(S))
{
pop(S,p);
printf("%c",p->data);
push(S,p->rchild);
}
}
}
int main()
{
Bitree T;
creatbitree(T);
cout<<endl<<"非递归先序遍历:"<<endl;
Preorder(T);
cout<<endl<<"递归先序遍历:"<<endl;
preorder(T);
cout<<endl<<"非递归中序,空不进栈:"<<endl;
Inorder(T);
cout<<endl<<"非递归中序,空进栈"<<endl;
Inorder2(T);
cout<<endl<<"递归中序"<<endl;
inorder(T);
}
15.哈夫曼树与哈夫曼编码
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#define ERROR 0;
#define OK 1;
typedef int Status ;
//哈夫曼树的存储和哈夫曼编码的存储
typedef struct{
/*unsigned */int weight;
/*unsigned */int parent,lchild,rchild;
}HTNode,*HuffmanTree; //动态分配数组存储哈夫曼树
typedef char **HuffmanCode; //动态分配数组存储哈夫曼编码表
Status Select(HuffmanTree HT,int n,int &s1,int &s2) {
//在哈夫曼树HT[1..n] 搜索最大权值和最小权值并用s1,s2 返回它们的下标
int temp=9999;
int i;
s1=0;s2=0;
for(i=1;i<=n;i++)
if((HT[i].weight<temp)&&(HT[i].parent==0))
{
s1=i;temp=HT[i].weight;
}
temp=9999;
for(i=1;i<=n;i++)
if((HT[i].weight<temp)&&(HT[i].parent==0)&&(i!=s1))
{
s2=i;temp=HT[i].weight;
}
if((s1==0)&&(s2==0)) return ERROR;
return OK;
}//select
//求huffman编码的算法:
void HuffmanCoding(HuffmanTree &HT,HuffmanCode &HC,int w[],int n)
{
//w存放N个字符的权值(均?>0),构造hufmantree HT,并求N个字符有huffman编码HC
HuffmanTree p;
char *cd;
int s1,s2,i,c,m,start,f;
if(n<1) return ;
m=2*n-1; //哈夫曼树的结点数
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode)); //0号单元未用
p=HT;p++;
for(i=1;i<=n;++i)
{
p->weight=w[i-1];
p->parent=0;
p->lchild=0;
p->rchild=0;
p++;
}//将各结点赋初值
for(;i<=m;++i,++p)
{
p->weight=0;
p->parent=0;
p->lchild=0;
p->rchild=0;
}//后续结点赋初值
for(i=n+1;i<=m;++i)
{ Select(HT,i-1,s1,s2);
//在HT[1..i-1]选择parent为0且weight最小的两个结点,其序号为S1,S2
//每次创建的树放在i的位置其中(i>n)
HT[s1].parent=i;HT[s2].parent=i;
HT[i].lchild=s1;HT[i].rchild=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}
printf("\n生成的哈夫曼树为:\n");
printf(" NO [weight parent lchild rchild]\n");
printf(" --------------------------------\n");
for(i=1;i<=m;i++)
printf("%6d [%6d,%6d,%6d, %6d ]\n",i,HT[i].weight,HT[i].parent,HT[i].lchild,HT[i].rchild);
//从叶子到根逆向求每个字符的Huffman 编码
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
cd=(char *)malloc(n*sizeof(char));
cd[n-1]='\0';
for(i=1;i<=n;++i)
{ start=n-1;
for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)
if(HT[f].lchild==c) cd[--start]='0';
else cd[--start]='1';
HC[i]=(char*)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);
}
free(cd);
}//end huffmancoding
int main()
{
HuffmanTree HT;
int n,i,w[20];
HuffmanCode HC;
printf("请输入叶子结点的个数 n=\n");
scanf("%d",&n);
printf("请输入%d个子结点的权值:\n",n);
for(i=0;i<n;i++)
scanf("%d",&w[i]);
HuffmanCoding(HT,HC,w,n);
getchar();
printf("\n每个叶子结点的哈夫曼编码为:\n");
for(i=1;i<=n;i++)
printf("叶子%d的哈夫曼编码中:%s\n",w[i-1],HC[i]);
}