1.array
#include
#include
#include
#include
#include
using namespace std;
#define MAX 100
struct Array
{
int *head;//头结点
int len;//长度
int num;//有效位数
};
void Creatarray(struct Array &A,int length)
{
A.head=(int *)malloc(sizeof(int)*length);
if(A.head==NULL)
{
cout<<"wrong!";
exit(-1);
}
A.num=0;
A.len=length;
}
int seek(struct Array &A,int x)
{
for(int i=0;i>n;
for(int i=0;i>a;
Initarray(A,a);
}
Showarray(A);
cout<<"\n";
cout<<"how many number in B:";
int m;
cin>>m;
for(int j=0;j>b;
Initarray(B,b);
}
Showarray(B);
cout<<"\n";
cout<<"compare A and B:"<
2.sqlist
#include
#include
#include
#include
using namespace std;
struct Lnode
{
char data;
struct Lnode *next;
};
struct Linklist
{
Lnode *head;
int num;//元素个数
};
void Creatlist(struct Linklist &L)
{
L.head=NULL;
L.num=0;
}
void Insertlist(struct Linklist &L,char x)
{
Lnode *node=new Lnode;
node->data=x;
node->next=L.head;
L.head=node;
L.num++;
}
void Seeklist(Linklist L,char x)
{
if(L.num==0)
cout<<"it is empty";
else
{
Lnode *p=L.head;
for(int i=0;idata==x)
cout<next;
}
}
}
void Delelist(Linklist &L,int pos)
{
if(pos==0)
L.head=L.head->next;
else
{
Lnode *p,*q;
p=L.head;
for(int i=0;inext;
q=p->next;
p->next=q->next;
L.num--;
}
}
void Showlist(struct Linklist L)
{
Lnode *p;
p=L.head;
for(int i=0;i"<data<next;
}
}
void Isempty(struct Linklist L)
{
if(L.num==0)
cout<<"it is empty";
else
cout<<"it is not empty";
}
int main()
{
Linklist L;
Creatlist(L);
cout<<"**************************************"< Insert"< Delelist"< seek"< showlist"< isempty"<>str;
int s=strlen(str);
for(int i=0;i
3.stack
#include
#include
#include
#include
using namespace std;
struct Stack
{
int *elem;
int top;
int stacksize;
};
void Initstack(struct Stack &S,int maxsize)
{
S.elem=new int[maxsize];
S.top=-1;
S.stacksize=maxsize;
}
void Push(struct Stack &S,int e)
{
S.elem[++S.top]=e;
}
void Pop(struct Stack &S,int &e)
{
if(S.top==-1)
cout<<"it is empty";
else
{
e=S.elem[S.top--];
}
}
void Showstack(struct Stack S)
{
if(S.top==-1)
cout<<"it is empty";
else
{
while(S.top!=-1)
{
cout<next=NULL;
Q.num=0;
}
void Enqueue(struct Queue &Q,int x)
{
Queuenode *p=new Queuenode;
p->data=x;
Q.rear->next=p;
Q.rear=p;
}
void Dequeue(struct Queue &Q,int &e)
{
if(Q.front==Q.rear)
cout<<"it is empty";
else{
Queuenode *p;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p)
Q.rear=Q.front;
delete(p);
}
}
void Showqueue(struct Queue Q)
{
if(Q.front==Q.rear)
cout<<"it is empty";
else
{
Queuenode *p=Q.front->next;
while(p!=Q.rear)
{
cout<data<<" ";
p=p->next;
}
cout<data;
}
}
int main()
{
Stack S;
Initstack(S,10);
cout<<"****** stack******"<>n;
for(int i=0;i>d;
Push(S,d);
}
cout<<"show the stack:"<>r;
Push(S,r);
Showstack(S);
cout<<"\n";
cout<<"******queue******"<>k;
for(int i=0;i>a;
Enqueue(Q,a);
}
cout<<"show the queue:"<>l;
Enqueue(Q,l);
Showqueue(Q);
return 0;
}
4.tree
#include
#include
#include
#include
using namespace std;
typedef struct Treenode
{
int data;
struct Treenode*lchild;
struct Treenode*rchild;
}*Tree,Treenode;
void Creattree(Tree &T)
{
T=NULL;
}
bool Searchtree(Tree T,int x,Tree &p,Tree &q)
{
p=T;
while(p)
{
if(x==p->data)
{
return true;
}
else if(xdata)
{
q=p;
p=p->lchild;
}
else
{
q=p;p=p->rchild;
}
}
return false;
}
bool Insert(Tree &T,int x)
{
Tree m,n;
if(Searchtree(T,x,m,n))
return false;
else
{
Treenode *s=new Treenode;
s->data=x;
s->lchild=NULL;
s->rchild=NULL;
if(!T)T=s;
else if(xdata)n->lchild=s;
else n->rchild=s;
return true;
}
}
void Deletree(Tree &T,int x)
{
Tree m,n;
if(Searchtree(T,x,m,n))
{
if(m->datadata)
{
if(m->rchild==NULL)
{
n->lchild=m->lchild;
delete(m);
}
else
{
n->lchild=m->rchild;
delete(m);
}
}
else
{
if(m->rchild==NULL)
{
n->rchild=m->lchild;
delete(m);
}
else
{
n->rchild=m->rchild;
delete(m);
}
}
}
else
cout<<"can't find";
}
void Showtree1(Tree T)//中序遍历
{
if(T){
Showtree1(T->lchild);
cout<data<<" ";
Showtree1(T->rchild);
}
}
void Showtree2(Tree T)//先序
{
if(T)
{
cout<data<<" ";
Showtree2(T->lchild);
Showtree2(T->rchild);
}
}
void Showtree3(Tree T)//后序
{
if(T)
{
Showtree3(T->lchild);
Showtree3(T->rchild);
cout<data<<" ";
}
}
int main()
{
Tree T;
Creattree(T);
cout<<"how many numbers?";
int n;
cin>>n;
for(int i=0;i>a;
Insert(T,a);
}
cout<<"中序遍历:"<>q;
Insert(T,q);
cout<<"中序遍历:"<>z;
Deletree(T,z);
cout<<"中序遍历:"<
5.graph
#include
#include
#include
#include
using namespace std;
#define MAX 20
typedef struct ArcCell
{
int adj;
}Adj[MAX][MAX],ArcCell;
struct Graph
{
char vex[MAX];//顶点数组
bool visited[MAX];//是否访问标志
Adj arcs;//邻接矩阵
int vexnum,arcnum;//顶点和边的数量
};
int Locate(char a[],char x)
{
for(int i=0;;i++)
if(x==a[i])
return i;
return -1;
}
void CreatGraph(Graph &g)
{
cout<<"how many vex:";
cin>>g.vexnum;
cout<<"how many arc:";
cin>>g.arcnum;
for(int i=0;i>g.vex[i];
g.visited[i]=false;
}
for(int i=0;i>v1>>v2;
int m,n;
m=Locate(g.vex,v1);
n=Locate(g.vex,v2);
int d;
cout<<"input the info";
cin>>d;
g.arcs[m][n].adj=g.arcs[n][m].adj=d;
}
}
void Initgraph(Graph &g)
{
g.arcnum=g.vexnum=0;
}
void Showgraph(Graph g)
{
for(int i=0;i"<";
for(int i=0;inext=NULL;
Q.num=0;
}
void Enqueue(struct Queue &Q,int x)
{
Queuenode *p=new Queuenode;
p->data=x;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
Q.num++;
}
bool Dequeue(struct Queue &Q,int &e)
{
if(Q.front==Q.rear)
return false;
else
{
Queuenode *p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p)
Q.rear=Q.front;
delete(p);
Q.num--;
return true;
}
}
void BFS(Graph &g)
{
Queue Q;
Initqueue(Q,g.vexnum);
for(int i=0;i"<";
Enqueue(Q,i);
while(!Q.num){
int e;
Dequeue(Q,e);
for(int j=0;j"<";
Enqueue(Q,j);
}
}
}
}
int main()
{
Graph g;
Initgraph(g);
CreatGraph(g);
Showgraph(g);
cout<<"DFS:"<