{
1.线性表的转置(线性表)
2.链表的转置
3.单链表的建立
4.链表的插入一个元素仍然有序(非递减)
5.删除偶数元素
6.合并一个有序链表(非递增)
7.拆分成两个链表(一个偶数一个奇数)
8.多项式合并
}
1.线性表的转置(线性表)
# include <iostream>
# include <bits/stdc++.h>
using namespace std;
# define maxsize 100
typedef int ElemType;
typedef struct{
ElemType elem[maxsize];
int sizeth;
}linelist;
void initlist (linelist &head)
{if(!head.elem)
exit(0);
head.sizeth=0;
}
void T(linelist &head,ElemType m)
{int n;
int i;
for(i=0;i<m/2;i++)
{
n=head.elem[i];
head.elem[i]=head.elem[m-i-1];
head.elem[m-i-1]=n;
}
for(i=0;i<m;i++)
{printf("%d ",head.elem[i]);
}
}
int main()
{ int n,m,i,j;
scanf("%d",&n);
linelist(head);
initlist(head);
for(i=0;i<n;i++)
{
scanf("%d",&m);
head.elem[i]=m;
}
for(i=0;i<n;i++)
{
printf("%d ",head.elem[i]);
}
printf("\n");
T(head,n);
return 0;
}
2.链表的转置
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
# define maxsize 100
typedef int ElemType;
typedef struct node
{ int elem;
struct node *next;
}linelist;
linelist *creat()
{ int m;
linelist *tp;
linelist *head;
head=new linelist;
head->next=NULL;
tp=head;
while(~scanf("%d",&m))
{ if(m==0) break;
linelist *p;
p=new linelist;
p->elem=m;
p->next=tp->next;
tp->next=p;
}
return head;
}
int main()
{ int i,j;
linelist *head;
head=creat();
head=head->next;
while(head!=NULL)
{
printf("%d ",head->elem);
head=head->next;
}
return 0;
}
3.单链表的建立
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
# define maxsize 100
typedef int ElemType;
typedef struct node
{
int elem;
struct node *next;
}linelist;
linelist *creat()
{ int m;
linelist *tp;
linelist *head;
head=new linelist;
head->next=NULL;
tp=head;
while(~scanf("%d",&m))
{ if(m==0) break;
linelist *p;
p=new linelist;
p->elem=m;
p->next=tp->next;
tp->next=p;
tp=p;
}
return head;
}
int main()
{ int i,j;
linelist *head;
head=creat();
head=head->next;
while(head!=NULL)
{
printf("%d ",head->elem);
head=head->next;
}
/* for(i=n-1;i>=0;i--)
{
printf("%d ",head.elem[i]);
}*/
return 0;
}
4.链表的插入一个元素仍然有序(非递减)
#include <iostream>
#include <bits/c++io.h>
using namespace std;
typedef struct List{
int data;
struct List *next;
}linelist;
void init(linelist *Head,int num){
linelist *tp;
linelist*now;
tp=Head;
now=Head->next;
while(now!=NULL){
if(num<=now->data){
linelist *p;
p=new linelist;
p->data=num;
p->next=now;
tp->next=p;
break;
}
now=now->next;
tp=tp->next;
}
if(!now){
linelist *node;
node=new linelist;
node->data=num;
node->next=NULL;
tp->next=node;
}
}
int main()
{
linelist *Head;
Head=new linelist;
Head->next=NULL;
int m;
while(~scanf("%d",&m)&&m!=0){
init(Head,m);
}
linelist *p;
p=Head->next;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
printf("\n");
return 0;
}
5.删除偶数元素
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
# define maxsize 100
typedef int ElemType;
typedef struct node
{
int elem;
struct node *next;
}linelist;
linelist *creat()
{ int m;
linelist *tp;
linelist *head;
head=new linelist;
head->next=NULL;
tp=head;
while(~scanf("%d",&m))
{ if(m==0) break;
linelist *p;
p=new linelist;
p->elem=m;
p->next=tp->next;
tp->next=p;
tp=p;
}
linelist *tp1;
tp1=head;
linelist *now;
now=head->next;
while(now)
{
if(now->elem%2==0)
{
tp1->next=now->next;
free(now);
now=tp1->next;
}
else{
now=now->next;
tp1=tp1->next;
}
}
linelist *l;
l=head->next;
while(l!=NULL)
{
printf("%d ",l->elem);
l=l->next;
}
return head;
}
int main()
{ int i,j;
linelist *head;
head=creat();
/* for(i=n-1;i>=0;i--)
{
printf("%d ",head.elem[i]);
}*/
return 0;
}
6.合并一个有序链表(非递增)
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef int ElemType;
typedef struct node
{
int elem;
struct node *next;
}linelist;
linelist *creat()
{ int m;
linelist *tp;
linelist *head;
head=new linelist;
head->next=NULL;
tp=head;
while(~scanf("%d",&m))
{ if(m==0) break;
linelist *p,*pre,*p2;
pre=head;
p=head->next;
while(p!=NULL&&p->elem>m)
{
pre=p;
p=p->next;
}
p2=new linelist;
p2->elem=m;
p2->next=p;
pre->next=p2;
}
return head;
}
void print(linelist *head)
{
linelist *p;
p=head->next;
while(p!=NULL)
{
printf("%d ",p->elem);p=p->next;
}
}
void mergelist(linelist *head,linelist *head1,linelist *Head)
{
linelist *p1,*p2,*Head1;
p1=head->next;
p2=head1->next;
Head=new linelist;
Head1=Head;
while(p1&&p2)
{
if(p1->elem>p2->elem)
{
Head1->next=p1;
Head1=p1;
p1=p1->next;
}
else
{
Head1->next=p2;
Head1=p2;
p2=p2->next;
}
}
if(p2!=NULL)
{
Head1->next=p2;
}
if(p1!=NULL)
{
Head1->next=p1;
}
print(Head);
}
int main()
{
linelist *p;
linelist *head,*head1;
head=new linelist;
head1=new linelist;
linelist *Head;
head=creat();
head1=creat();
mergelist(head,head1,Head);
return 0;
}
7.拆分成两个链表(一个偶数一个奇数)
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
typedef struct student
{
int data;
struct student *next;
}stu;
void creat(stu *a,stu *b)
{int m;
struct student *p1,*p2,*p3,*p4;
p1=a;p3=b;
while(~scanf("%d",&m)&&m!=0)
{if(m%2!=0)
{p2=(stu*)malloc(sizeof(stu));
p2->data=m;
p1->next=p2;p1=p2;
}
else if(m%2==0)
{p4=(stu*)malloc(sizeof(stu));
p4->data=m;
p3->next=p4;p3=p4;
}
}
p1->next=NULL;
p3->next=NULL;
}
void pr(stu *a)
{stu *b;
b=a->next;
while(b!=NULL)
{printf("%d ",b->data);
b=b->next;
}
}
int main()
{int i,n;
stu *d,*b;
d=(stu*)malloc(sizeof(stu));
d->next=NULL;
b=(stu*)malloc(sizeof(stu));
b->next=NULL;
creat(d,b);
pr(d);
printf("\n");
pr(b);
return 0;
}
8.多项式合并
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct l
{
int coef;
int expn;
struct l *next;
}list1;
list1 *creat()
{
list1*head,*r;
head=new list1;
r=head;
int n,i,x,y;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d,%d",&x,&y);
list1 *p;
p=new list1;
p->coef=x;p->expn=y;
r->next=p;r=p;
}
r->next=NULL;
return head;
}
list1 *hebing(list1 *L1,list1 *L2)
{
int sum;
list1*p1,*p2,*q,*p;
p1=L1->next;
p2=L2->next;
q=L1;
while(p1!=NULL&&p2!=NULL)
{
if(p1->expn==p2->expn)
{
sum=p1->coef+p2->coef;
if(sum==0)
{
p=p1;p1=p1->next;
free(p);
p=p2;p2=p2->next;
free(p);
}
else
{
p1->coef=sum;
q->next=p1;q=q->next;
p1=p1->next;
p=p2;p2=p2->next;
free(p);
}
}
else if(p1->expn<p2->expn)
{
q->next=p1;q=q->next;
p1=p1->next;
}
else if(p1->expn>p2->expn)
{
q->next=p2;q=q->next;
p2=p2->next;
}
}
if(p1!=NULL) q->next=p1;
else q->next=p2;
return L1;
}
void print(list1 *L)
{
list1 *p;
p=L->next;
while(p!=NULL)
{
printf("%d*x^%d ",p->coef,p->expn);
p=p->next;
}
}
int main()
{
list1 *L1,*L2,*L;
L1=creat();
L2=creat();
L=hebing(L1,L2);
print(L);
return 0;
}
{
4(请输入第一个一元多项式的项数)
2,2 3,3 5,5 7,7(请输入第一个一元多项式的系数和指数)
5(请输入第一个一元多项式的项数)
-2,2 2,3 4,4 5,7 9,9(请输入第一个一元多项式的系数和指数)
}
//串串串串串串串串串串
#include <iostream>
#include <bits/stdc++.h>
#define MAXLENGTH 100
typedef char ElemType;
using namespace std;
1.串的定长储存
typedef struct chunk
{
ElemType ch[MAXLENGTH+1];
int length;
}sqchunk;
2.串的堆式储存;
typedef struct chunk{
char *ch;
int length;
}Chunk,;
3.串的链式(块状)储存
typedef struct chunk{
char ch[MAXLENGTH];
struct chunk *next;
}Chunk;
typedef struct {
Chunk *head,*tail;
int length;
}
4.BF算法
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct
{
char ch[100];
int length;
}sstring;
void creat(sstring &S)
{
int i;
i=0;
char ss;
S.length=0;
while(scanf("%c",&ss)&&ss!='#')
{
i++;
S.ch[i]=ss;
S.length++;
}
}
int BF(sstring s,sstring t,int pos)
{
int i,j;
i=pos;j=1;
while(i<=s.length&&j<=t.length)
{
if(s.ch[i]==t.ch[j])
{ i++;j++; }
else
{
i=i-j+2;j=1;
}
}
if(j>t.length) return (i-j+1);
else return 0;
}
int main()
{
sstring s,t;
int site;
creat(s);//aaabcds#bcd#
creat(t);//
site=BF(s,t,1);//2
if(site==0)
printf("no");
else
printf("%d",site);
return 0;
}
5.KMP算法
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct
{
char ch[100];
int length;
}sstring;
void creat(sstring &S)
{
int i;i=0;
char cc;
S.length=0;
while(scanf("%c",&cc)&&cc!='#')
{
i++;
S.ch[i]=cc;
S.length++;
}
}
void getnext(sstring t,int next[])
{
next[1]=0;int j,k;j=1;k=0;
while(j<t.length)
{
if((k==0)||t.ch[j]==t.ch[k])
{
j++;k++;
next[j]=k;
}
else k=next[k];
}
}
int KMP(sstring s,sstring t,int pos,int next[])
{
int i,j;
i=pos;
j=1;
while(i<=s.length&&j<=t.length)
{
if(j==0||s.ch[i]==t.ch[j])
{ i++;j++; }
else
{
j=next[j];
}
}
if(j>t.length) return i-t.length;
else return 0;
}
int main()
{
sstring s,t;
int site;
int next[20]={0};
creat(s);//aaabcds#bcd#
creat(t);//
getnext(t,next);
site=KMP(s,t,1,next);//2
if(site==0)
printf("no");
else
printf("%d",site);
return 0;
}
6. Bestnext
void getnextval(sstring t,int nextval[])
{
nextval[1]=0;int i=1;int j=0;
while(i<t.length)
{
if((j==0)||t.ch[j]==t.ch[i])
{
j++;i++;
if(t[j]!=t[j])
nextval[i]=j;
else nextval[i]=nextval[j];
}
else j=nextval[j];
}
}
int KMP(sstring s,sstring t,int pos,int nextval[])
{
int i,j;
i=pos;
j=1;
while(i<=s.length&&j<=t.length)
{
if(j==0||s.ch[i]==t.ch[j])
{ i++;j++; }
else
{
j=nextval[j];
}
}
if(j>t.length) return i-t.length;
else return 0;
}
//树
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
//;二叉树的顺序储存结构
#define MAX_TREE 10000;
typedef Telemtype SiTree[MAX_PATH];
SiTree bT;
//二叉树的链式储存结构
typedef struct SiTNode{
Telemtype data;
struct SiTNode *lchild,*rchild;
}SiTNode,*SiTree;
//三叉树的链式结构
typedef struct SiTNode{
Telemtype data;
struct SiTNode *parent,*lchild,*rchild;
}SiTNode,*SiTree;
//遍历
//前中后序递归遍历
void preTree(SiTNode *t)
{
if(t=NULL)
exit(0);
else
{
cout<<t->data;
preTree(t->lchild);
preTree(t->rchild);
}
}
void minTree(SiTNode *t)
{
if(t=NULL)
exit(0);
else
{
preTree(t->lchild);
cout<<t->data;
preTree(t->rchild);
}
}
void lastTree(SiTNode *t)
{
if(t=NULL)
exit(0);
else
{
preTree(t->lchild);
preTree(t->rchild);
cout<<t->data;
}
}
//非递归算法遍历
//前中序遍历
void preTree(SiTNode *t)
{
SiTree s[MAX_PATH],p;
int top=0;
while(p||top>0)
{
while(p)
{
top++;
s[top]=p;
cout<<p->data;
p=p->lchild;
}
if(top>0)
{
p=s[top];
top--;
p=p->rchild;
}
}
}
//后序非递归遍历
void lastTree(SiTree *t)
{
struct {
BiTNode *pp;
int tag;
}ss[MAX_PATH];
int top;
top=0;
SiTNode *P;
p=t;
while(p||top>0)
{
while(p)
{
top++;
ss[top].pp=p;
ss[top].tag=0;
p=p->lchild;
}
if(top>0)
{
if(ss[top].tag==0)
{
ss[top].tag=1;
p=ss[top].pp;
p=p->rchild;
}
else{
p=ss[top].pp
cout<<p->data;
top--;
p=NULL;
}
}
}
}
//层次遍历
void laveltree(SiTNode &t)
{ BiTree s[MAX_PATH],p;
int front rear;
p=t;
if(t!=NULL)
{
rear++;
s[top]=p;
while(front!=rear)
{
front++;
p=s[front];
cout<<p->data;
if(p->lchild)
{
rear++;
s[rear]=p->lchild;
}
if(p->rchild)
{
rear++;
s[rear]=p->rchild;
}
}
}
}
//统计二叉树中叶子结点的个数
int addTnode(Bitree *t)
{
if(t=NULL) return 0;
else
if(t->rchild==NULL&&t->lchild==NULL)
n++;
addTnode(t->rchild);
addTnode(t->lchild);
return n;
}
// 求二叉树的深度
int findtree(BiTree *t)
{ int l,r;
if(t==NULL) return 0;
else
{
l=findtree(t->lchhild);
r=findtree(t->rchild);
if(l<r) return r+1;
else return l+1;
}
}
//复制二叉树
void copytree(BiTree t1,BiTree t2)
{ if(t1==NULL)
t2=NULL;
else {
t2=new BiTNode;
t2->data=t1->data;
t2->lchild=NULL;
t2->rchild=NULL;
copytree(t1->rchild,t2->rchild);
copytree(t1->lchild,t2->lchild);
}
}
//建立一个二叉树
void creatree(BiTree &t);
{
char ch;
count<<ch<<" ";
t->data=ch;
if(ch!='@')
{
t=new BiTNode;
creatree(t->lchild);
creatree(t->rchild);
}
else
t=NULL;
}
二叉树的建立(先序遍历序列:ABD@F@@@@CE@@@)
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct TNode
{
char data;
struct TNode *lchild,*rchild;
}BT,*BiTree;
void creatTree(BiTree &T)
{ char ch;
cin>>ch;
if(ch!='@')
{
T=new BT;
T->data=ch;
creatTree(T->lchild);
creatTree(T->rchild);
}
else T=NULL;
}
int n=0;
void find_leave(BiTree T)
{
if(T!=NULL)
{
if(T->lchild==NULL&&T->rchild==NULL)
{
n+=1;
}
find_leave(T->lchild);
find_leave(T->rchild);
}
}
int main()
{
BiTree T;
creatTree(T);
find_leave(T);
printf("%d",n);
return 0;
}
期中前的所有算法
最新推荐文章于 2024-04-26 17:30:00 发布