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 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 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;
}
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);
}
6.栈(数据转化)
#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<<"栈已空"<<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,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 );
}
7.循环队列
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#include<stack>
#include<set>
#include<string>
#include<cstring>
#include<list>
#include<stdlib.h>
#define MAXSIZE 11
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(MAXSIZE*sizeof(sqQuene));
if(!Q.base)
{
cout<<"memory error!"<<endl;
return 0;
}
Q.front = Q.rear = 0;
return 1;
}
status enqueue(sqQuene &Q , elemtype e)
{
if( (Q.rear + 1)%MAXSIZE == Q.front )
{
cout<<"queue is full !"<<endl;
return 0;
}
Q.base[Q.rear] = e;
Q.rear = (Q.rear+1)%MAXSIZE;
return 1;
}
status dequeue(sqQuene &Q , elemtype &e)
{
if(Q.front == Q.rear)
{
cout<<"There is no any elem!"<<endl;
return 0;
}
e = Q.base[Q.front];
Q.front = (Q.front + 1)%MAXSIZE;
return 1;
}
status qulength(sqQuene Q)
{
return (Q.rear - Q.front + MAXSIZE)%MAXSIZE;
}
int main ()
{
elemtype e;
sqQuene Q;
initqueue(Q);
cout<<"开始入队总共10个元素"<<endl;
for(int i = 0 ; i < MAXSIZE-1 ; i ++ )
{
cin >> e;
enqueue(Q,e);
}
cout<<"出队一个元素"<<endl;
dequeue(Q,e);
cout<<"出队元素为:"<<e<<endl;
cout<<"再入队一个元素"<<endl;
cin >> e;
enqueue(Q,e);
cout<<"队列长度:"<<qulength(Q)<<endl;
cout<<"所有元素出队"<<endl;
for(int i = 0 ; i < MAXSIZE-1 ; i ++ )
{
dequeue(Q,e);
cout<<e<<" ";
}
cout<<endl;
return 0;
}
8.定长顺序串的求子串、串的合并等
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#include<stack>
#include<set>
#include<string>
#include<cstring>
#include<list>
#include<stdlib.h>
#define MAXSIZE 11
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(MAXSIZE*sizeof(sqQuene));
if(!Q.base)
{
cout<<"memory error!"<<endl;
return 0;
}
Q.front = Q.rear = 0;
return 1;
}
status enqueue(sqQuene &Q , elemtype e)
{
if( (Q.rear + 1)%MAXSIZE == Q.front )
{
cout<<"queue is full !"<<endl;
return 0;
}
Q.base[Q.rear] = e;
Q.rear = (Q.rear+1)%MAXSIZE;
return 1;
}
status dequeue(sqQuene &Q , elemtype &e)
{
if(Q.front == Q.rear)
{
cout<<"There is no any elem!"<<endl;
return 0;
}
e = Q.base[Q.front];
Q.front = (Q.front + 1)%MAXSIZE;
return 1;
}
status qulength(sqQuene Q)
{
return (Q.rear - Q.front + MAXSIZE)%MAXSIZE;
}
int main ()
{
elemtype e;
sqQuene Q;
initqueue(Q);
cout<<"开始入队总共10个元素"<<endl;
for(int i = 0 ; i < MAXSIZE-1 ; i ++ )
{
cin >> e;
enqueue(Q,e);
}
cout<<"出队一个元素"<<endl;
dequeue(Q,e);
cout<<"出队元素为:"<<e<<endl;
cout<<"再入队一个元素"<<endl;
cin >> e;
enqueue(Q,e);
cout<<"队列长度:"<<qulength(Q)<<endl;
cout<<"所有元素出队"<<endl;
for(int i = 0 ; i < MAXSIZE-1 ; i ++ )
{
dequeue(Q,e);
cout<<e<<" ";
}
cout<<endl;
return 0;
}
9.串的模式匹配
#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<<"模式串位置为: "<<index(t3,t4,pos)<<endl;
}
else{
cout<<"查询失败"<<endl;
}
return 0;
}
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>
#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 Print(TM G) //打印稀疏矩阵
{
int k=1;
for(int i=1;i<=G.mu;i++)
{
for(int j=1;j<=G.nu;j++)
{
if(G.data[k].i == i && G.data[k].j == j)
{
printf("%4d",G.data[k].e);
k++;
}
else
printf(" 0");
}
cout<<endl;
}
return 1;
}
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;
// Print(M);
cout<<"三元组:"<<endl;
outTM(M);
TransposeSMatrix1(M,T);
cout<<"使用第一种方法转置的矩阵为:"<<endl;
// Print(T);
cout<<"三元组:"<<endl;
outTM(T);
TransposeSMatrix2(M,T2);
cout<<"使用第二种方法转置的矩阵为:"<<endl;
// Print(T2);
outTM(T2);
}
/*
test
6 7 8
1 2 12
1 3 9
3 1 -3
3 6 14
4 3 24
5 2 18
6 1 15
6 4 -7
*/
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>
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);
}
12.二叉树的创建及非递归的前序与二种中序遍历
#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);
}
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;
typedef struct
{
int weight;
int parent,lchird,rchild;
}HTNode,*HuffmanTree;
typedef char** HuffmanCode;
void Select(HuffmanTree t,int n ,int &s1, int &s2)
{
int temp;
temp = 99999;
for(int i = 1 ; i<= n ; i ++ )
{
if(t[i].weight<temp && t[i].parent == 0 )
{
temp=t[i].weight;
s1 = i;
}
}
temp = 99999;
for(int i = 1 ; i <= n ; i++ )
{
if(t[i].weight<temp && t[i].parent == 0 &&i!=s1)
{
temp = t[i].weight;
s2 = i;
}
}
}
void InitHuffmanTree(HuffmanTree &H,int n)
{
if(n<=1) return ;
int m=2*n-1;
H= (HTNode *)malloc( (m+1)*sizeof(HTNode) );//new HTNode[m+1];
for (int i = 1; i <= m; i++)
{
H[i].parent=0;
H[i].lchird=0;
H[i].rchild=0;
}
for (int i = 1; i <= n; i++)
{
cin>>H[i].weight;
}
}
void CreateHuffmanTree(HuffmanTree &H,int n)
{
int m=2*n-1;
int s1,s2;
for (int i = n+1; i <= m; i++)
{
Select(H,i-1,s1,s2);
H[s1].parent=i; H[s2].parent=i;
H[i].lchird=s1; H[i].rchild=s2;
H[i].weight=H[s1].weight+H[s2].weight;
}
}
void CreateHuffmanCode(HuffmanTree H,HuffmanCode &HC,int n)
{
HC=new char*[n+1];
char* cd=new char[n];
cd[n-1]='\0';
for (int i = 1; i <= n; i++)
{
int start=n-1;
int c=i;
int f=H[i].parent;
while (f!=0)
{
--start;
if (H[f].lchird==c)
{
cd[start]='0';
}
else cd[start]='1';
c=f;
f=H[f].parent;
}
HC[i]=new char[n-start];
strcpy(HC[i],&cd[start]);
}
delete cd;
}
void out(HuffmanTree t, int n)
{
int i;
printf(" w p l r\n");
printf("------------------------\n");
for (i = 1; i <= 2 * n - 1; i++)
{
printf("%6d%6d%6d%6d\n", t[i].weight, t[i].parent, t[i].lchird, t[i].rchild);
}
printf("------------------------\n");
}
int main()
{
HuffmanTree H;
HuffmanCode HC;
int n;
cout<<"请输入哈夫曼树的节点数:"<<endl;
cin>>n;
cout<<"输入每个节点的权值:"<<endl;
InitHuffmanTree(H,n);
CreateHuffmanTree(H,n);
CreateHuffmanCode(H,HC,n);
out(H,n);
for (int i = 1; i <= n; i++)
{
printf("H[%d] = %d 哈夫曼编码为:",i,H[i].weight);
cout<<HC[i]<<endl;
}
return 0;
}
/*
test
8
3 5 11 23 29 14 7 8
*/
333