顺序表的基本操作#include<iostream>using namespace std;typedef int datatype;#define maxsize 1024#define NULL -1typedef struct {datatype *data;int last;}sequenlist;void SETNULL(sequenlist &L) {L.data=new datatype[maxsize];for(int i=0;i<maxsize;i++)L.data[i]=NULL;}void INITIALIZE(sequenlist &L) {cout<<"请输入结点个数:"<<endl;cin>>st;cout<<"请输入"<<st<<"个初始值:"<<endl;for(int i=0;i<st;i++)cin>>L.data[i];}int LENGTH(sequenlist &L) {int i=0;while(L.data[i]!=NULL)i++;return i;}datatype GET(sequenlist &L,int i) {if(i<1||i>st) {cout<<"error1"<<endl;return NULL;}elsereturn L.data[i-1];}int LOCATE(sequenlist &L,datatype x) {int j=0;while(L.data[j]!=x)j++;if(j==st) {cout<<"所查找值不存在!"<<endl;return NULL;}elsereturn (j+1);}int INSERT(sequenlist &L,datatype x,int i) { int j;if(st>=maxsize-1) {cout<<"overflow";return NULL;}elseif(i<1||(i>st)) {cout<<"error2"<<endl;return NULL;}else{for(j=(st);j>=i-1;j--)L.data[j+1]=L.data[j];L.data[i-1]=x;st++;}return 1;}int DELETE(sequenlist &L,int i) {int j;if((i<1)||(i>st+1)) {cout<<"error3"<<endl;return NULL;}else {for(j=i;j<=st;j++)L.data[j-1]=L.data[j];st--;}return 1;}int main() {sequenlist L;datatype x=5;int i=4;SETNULL(L);INITIALIZE(L);int a=LENGTH(L);cout<<"线性表中的结点个数为:"<<a<<endl;datatype b=GET(L,i);cout<<"线性表中第"<<i<<"个结点为:"<<b<<endl;int c=LOCATE(L,x);cout<<"值为"<<x<<"的结点位置为:"<<c<<endl;int d=INSERT(L,x,i);cout<<"插入新结点后的线性表:"<<endl;for(int j=0;j<st;j++)cout<<L.data[j]<<" ";cout<<endl;int e=DELETE(L,i);cout<<"删除一个结点后的线性表:"<<endl;for(int j=0;j<st;j++)cout<<L.data[j]<<" ";cout<<endl;return 0;}单链表的基本操作#include<iostream>using namespace std;typedef char datatype;typedef struct node {datatype data;struct node *next;}linklist;linklist *CREATLISTF() {cout<<"请输入字符并以ENTER键结束"<<endl;char ch;linklist *head,*s;head=(linklist*)malloc(sizeof(linklist));head=NULL;ch=getchar();while(ch!='\n') {s=(linklist*)malloc(sizeof(linklist));s->data=ch;s->next=head;head=s;ch=getchar();}return head;}/*头插法建表*/linklist *CREATLISTR() {cout<<"请输入字符并以ENTER键结束"<<endl;char ch;linklist *head,*s,*r;head=NULL;r=NULL;ch=getchar();while(ch!='\n') {s=(linklist*)malloc(sizeof(linklist));s->data=ch;if(head==NULL)head=s;elser->next=s;r=s;ch=getchar();}if(r!=NULL)r->next=NULL;return head;}/*尾插法建表*/linklist *GET(linklist *head,int i) {int j;linklist *p;p=head;j=1;while((p->next!=NULL)&&(j<i)) {p=p->next;j++;}if(i==j)return p;elsereturn NULL;}/*按序号查找*/linklist *LOCATE(linklist *head,datatype key) { linklist *p;p=head;while(p!=NULL)if(p->data!=key)p=p->next;elsebreak;return p;}/*按值查找*/void INSERTAFTER(linklist *p,datatype x) { linklist *s;s=(linklist*)malloc(sizeof(linklist));s->data=x;s->next=p->next;p->next=s;}/*后插操作*/void INSERTBEFORE(linklist *p,datatype x) { linklist *s;s=(linklist*)malloc(sizeof(linklist));s->data=p->data;s->next=p->next;p->data=x;p->next=s;}/*前插操作*/void DELETEAFTER(linklist *p) {linklist *r;r=p->next;p->next=r->next;free(r);}/*删除运算*/int LENGTH(linklist *head) {int i;for(i=1;head->next!=NULL;i++)head=head->next;return i;}/*长度运算*/void SETNULL(linklist *head) {linklist *p,*q;p=head;while(p->next!=NULL) {q=p;p=p->next;free(q);}head->next=NULL;}/*置空表*/void PRINTLIST(linklist *head) {linklist *r;r=head;while((r->next)!=NULL) {putchar(r->data);r=r->next;}putchar(r->data);cout<<endl;}/*输出链表*/void main() {linklist *head1,*head21,*p,*q,*r1;int i=4;datatype key='c',x='h';head1=CREATLISTF();PRINTLIST(head1);head21=CREATLISTR();PRINTLIST(head21);r1=head1->next->next->next->next;p=GET(head1,i);cout<<"head1中第"<<i<<"个结点为:"<<p->data<<endl;q=LOCATE(head1,key);cout<<"值为"<<key<<"的结点为:"<<q->data<<endl;INSERTAFTER(r1,x);PRINTLIST(head1);INSERTBEFORE(r1,x);PRINTLIST(head1);DELETEAFTER(r1);PRINTLIST(head1);int j=LENGTH(head1);cout<<"单链表长度为:"<<j<<endl;SETNULL(head1);j=LENGTH(head1);cout<<j<<endl;PRINTLIST(head1);}顺序队列的基本操作#include<iostream>using namespace std;typedef int datatype;#define maxsize 64#define NULL -1typedef struct {datatype *data;int front,rear;}sequeue;void SETNULL(sequeue &sq) {sq.data=new datatype[maxsize];sq.front=-1;sq.rear=-1;}/*置空队*/int EMPTY(sequeue &sq) {if(sq.rear==sq.front)return 1;elsereturn 0;}/*判队空*/datatype FRONT(sequeue &sq) {if(EMPTY(sq)) {cout<<"queue is empty"<<endl;return NULL;}elsereturn sq.data[(sq.front+1)%maxsize]; }/*取队头元素*/int ENQUEUE(sequeue &sq,datatype x) { if(sq.front==(sq.rear+1)%maxsize) {cout<<"queue is full"<<endl;return NULL;}else {sq.rear=(sq.rear+1)%maxsize;sq.data[sq.rear]=x;return 1;}}/*入队*/datatype DEQUEUE(sequeue &sq) {if(EMPTY(sq)) {cout<<"queue is empty"<<endl;return NULL;}else {sq.front=(sq.front+1)%maxsize;return sq.data[sq.front];}}/*出队*/void PRIQUEUE(sequeue &sq) {int i;for(i=sq.front+1;i<=(sq.rear)%maxsize;i++) cout<<sq.data[i%maxsize]<<" ";cout<<endl;}/*输出队列*/int LENQUEUE(sequeue &sq) {return(sq.rear-sq.front);}/*队列长度*/int main() {sequeue sq;int lenth,i;datatype j,x;SETNULL(sq);cout<<"请输入队列元素个数:"<<endl;cin>>lenth;cout<<"请输入队列初始元素:"<<endl;for(i=0;i<lenth;i++) {cin>>x;j=ENQUEUE(sq,x);}PRIQUEUE(sq);j=DEQUEUE(sq);PRIQUEUE(sq);cout<<"判断队列是否为空:"<<EMPTY(sq)<<endl;cout<<"队头元素为:"<<FRONT(sq)<<endl;cout<<"队列长度为:"<<LENQUEUE(sq)<<endl;SETNULL(sq);cout<<"队列置空后为:"<<endl;PRIQUEUE(sq);return 0;}链队列的基本操作#include<iostream>using namespace std;typedef char datatype;typedef struct node {datatype data;struct node *next;}linklist;typedef struct {linklist *front,*rear;}linkqueue;void SETNULL(linkqueue* q) {q->front=(linklist*)malloc(sizeof(linklist));q->front->next=NULL;q->rear=q->front;}/*置空队*/int EMPTY(linkqueue* q) {if(q->front==q->rear)return 1;elsereturn 0;}/*判队空*/datatype FRONT(linkqueue* q) {if(EMPTY(q)) {cout<<"queue is empty"<<endl;return NULL;}elsereturn q->front->next->data;}/*取队头元素*/void ENQUEUE(linkqueue* q,datatype x) {q->rear->next=(linklist*)malloc(sizeof(linklist));q->rear=q->rear->next;q->rear->data=x;q->rear->next=NULL;}/*入队*/datatype DEQUEUE(linkqueue* q) {linklist* s;if(EMPTY(q)) {cout<<"queue is empty"<<endl;return NULL;}else {s=q->front;q->front=q->front->next;free(s);return q->front->data;}}/*出队*/void PRIQUEUE(linkqueue* q) {linklist *r;r=q->front;while(r->next!=NULL) {putchar(r->next->data);r=r->next;}cout<<endl;}/*输出链表*/int main() {linkqueue* q;q=(linkqueue*)malloc(sizeof(linkqueue));datatype x;SETNULL(q);cout<<"请输入初始元素:"<<endl;x=getchar();while(x!='\n') {ENQUEUE(q,x);x=getchar();}PRIQUEUE(q);cout<<"被删除的队头元素为:"<<DEQUEUE(q)<<endl;PRIQUEUE(q);cout<<"判断队列是否为空:"<<EMPTY(q)<<endl;cout<<"队头结点数据为:"<<FRONT(q)<<endl;SETNULL(q);cout<<"队列置空后为:"<<endl;PRIQUEUE(q);return 0;}顺序栈的基本操作#include<iostream>using namespace std;typedef int datatype;#define maxsize 64#define FALSE 0#define TRUE 1#define NULL -1typedef struct {datatype *data;int top;}seqstack;void SETNULL(seqstack& s) {s.data=new datatype[maxsize];s.top=NULL;}/*置空栈*/int EMPTY(seqstack& s) {if(s.top>=0)return FALSE;elsereturn TRUE;}/*判栈空*/void PUSH(seqstack& s,datatype x) {if(s.top==maxsize-1)cout<<"overflow"<<endl;else {s.top++;s.data[s.top]=x;}}/*进栈*/datatype POP(seqstack& s) {if(EMPTY(s)) {cout<<"underflow"<<endl;return NULL;}else {s.top--;return(s.data[s.top+1]);}}/*出栈*/datatype TOP(seqstack& s) {if(EMPTY(s)) {cout<<"stack is empty"<<endl;return NULL;}elsereturn(s.data[s.top]);}/*取栈顶*/void PRINT(seqstack& s) {int i=0;for(i=s.top;i>=0;i--)cout<<s.data[i]<<" ";cout<<endl;}/*输出栈*/int STACKFULL(seqstack& s) {if(s.top==maxsize)return TRUE;elsereturn FALSE;}/*判栈满*/int main() {seqstack s;int i,LENGTH;datatype x,y,z;SETNULL(s);cout<<"请输入初始值个数:"<<endl;cin>>LENGTH;cout<<"请输入初始值:"<<endl;for(i=0;i<LENGTH;i++) {cin>>x;PUSH(s,x);}PRINT(s);y=POP(s);cout<<"删除的栈顶元素为:"<<y<<endl;PRINT(s);z=TOP(s);cout<<"栈顶元素为:"<<z<<endl;cout<<"判断栈是否满:"<<STACKFULL(s)<<endl;return 0;}链栈的基本操作#include<iostream>using namespace std;typedef char datatype;#define TRUE 1#define FALSE 0typedef struct node {datatype data;struct node* next;}linkstack;void SETNULLSTACK(linkstack* top) {linkstack *p,*q;p=top;while(p->next!=NULL) {q=p;p=p->next;free(q);}top->next=NULL;}/*置空表*/int EMPTYLSTACK(linkstack* top) {if(top->next==NULL)return TRUE;elsereturn FALSE;}/*判表空*/linkstack* PUSHLSTACK(linkstack* top,datatype x) { linkstack* p;p=(linkstack*)malloc(sizeof(linkstack));p->data=x;p->next=top;return p;}/*输入函数*/linkstack* POPLSTACK(linkstack* top,linkstack* datap) { linkstack* p;if(top->next==NULL) {cout<<"underflow"<<endl;return NULL;}else {datap=top;p=top;top=top->next;free(p);return top;}}/*删除函数*/void PRINTLSTACK(linkstack* top) {linkstack* p;p=top;while(p->next!=NULL) {cout<<p->data<<" ";p=p->next;}cout<<endl;}/*输出函数*/datatype TOPLSTACK(linkstack* top) {return top->data;}/*取栈顶*/int main() {linkstack* top,*d;datatype x;d=(linkstack*)malloc(sizeof(linkstack));top=(linkstack*)malloc(sizeof(linkstack));top->next=NULL;cout<<"请输入初始值并以ENTER键结束:"<<endl;x=getchar();while(x!='\n') {top=PUSHLSTACK(top,x);x=getchar();}PRINTLSTACK(top);top=POPLSTACK(top,d);cout<<"删除栈顶元素后为:"<<endl;PRINTLSTACK(top);cout<<"判断栈是否为空:"<<EMPTYLSTACK(top)<<endl;cout<<"栈顶元素为:"<<TOPLSTACK(top)<<endl;SETNULLSTACK(top);cout<<"置空后为:"<<endl;PRINTLSTACK(top);return 0;}顺序串的基本操作#include<stdio.h>#define maxsize 32typedef struct {char data[maxsize];int length;}seqstring;void StrAssign(seqstring &str,char cstr[]) {int i;for(i=0;cstr[i]!='\0';i++)str.data[i]=cstr[i];str.length=i;}/*赋值*/int StrLen(seqstring &s) {return s.length;}/*求串长*/void StrCat(seqstring &s,seqstring &t) {int i,j=StrLen(s);for(i=0;i<StrLen(t);i++)s.data[i+j]=t.data[i];s.length=s.length+t.length;}/*联接*/int StrCmp(seqstring &s,seqstring &t) { int i,j;if(s.length>t.length)i=t.length;elsei=s.length;for(j=0;j<i;j++) {if(s.data[j]>t.data[j]) {return 1;break;}if(s.data[j]<t.data[j]) {return -1;break;}}if(j==i||s.length==t.length)return 0;else {if(j==i||s.length>t.length)return 1;else if(j==i||s.length<t.length)return -1;}}/*比较串的大小*/seqstring SubStr(seqstring &s,int i,int j) { seqstring str;int k;str.length=0;if(i<=0||i>s.length||j<0||i+j-1>s.length) { printf("参数不正确\n");return str;}for(k=i-1;k<i+j-1;k++)str.data[k-i+1]=s.data[k];str.length=j;return str;}/*求子串*/seqstring InsStr(seqstring &s1,int i,seqstring &s2) { int j;seqstring str;str.length=0;if(i<=0||i>s1.length+1) {printf("参数不正确\n");return s1;}for(j=0;j<i-1;j++)str.data[j]=s1.data[j];for(j=0;j<s2.length;j++)str.data[i+j-1]=s2.data[j];for(j=i-1;j<s1.length;j++)str.data[s2.length+j]=s1.data[j];str.length=s1.length+s2.length;return str;}/*插入*/seqstring DelStr(seqstring s,int i,int j) {int k;seqstring str;str.length=0;if(i<=0||i>s.length||i+j>s.length+1) {printf("参数不正确\n");return str;}for(k=0;k<i-1;k++)str.data[k]=s.data[k];for(k=i+j-1;k<s.length;k++)str.data[k-j]=s.data[k];str.length=s.length-j;return str;}/*删除*/seqstring RepStr(seqstring &s,int i,int j,seqstring &t) { int k;seqstring str;str.length=0;if(i<=0||i>s.length||i+j-1>s.length) {printf("参数不正确\n");return str;}for(k=0;k<i-1;k++)str.data[k]=s.data[k];for(k=0;k<t.length;k++)str.data[i+k-1]=t.data[k];for(k=i+j-1;k<s.length;k++)str.data[t.length+k-j]=s.data[k];str.length=s.length-j+t.length;return str;}/*置换*/int IndStr(seqstring &s1,seqstring &s2) {int i,j,d=0;for(i=0;i<StrLen(s1)-StrLen(s2)+1;i++) {for(j=0;j<StrLen(s2);j++)if(s1.data[i+j]!=s2.data[j])break;if(j==StrLen(s2)) {d=i+1;break;}}return d;}/*子串定位*/void DispStr(seqstring &s) {int i;if(s.length>0) {for(i=0;i<s.length;i++)printf("%c",s.data[i]);printf("\n");}}void main() {seqstring s,s1,s2,s3,s4;printf("建立串s和串s1\n");StrAssign(s,"ABCDEFGHIJKLMN");StrAssign(s1,"xyz");printf("输出串s:");DispStr(s);printf("串s的长度:%d\n",StrLen(s));printf("在串的第八个字符位置插入串s1而产生串s2\n");s2=InsStr(s,8,s1);DispStr(s2);printf("删除串s第三个字符开始的三个字符而产生串s2\n");s2=DelStr(s,3,3);DispStr(s2);printf("将串s第三个字符开始的六个字符替换成串s1而产生串s2\n");s2=RepStr(s,3,6,s1);DispStr(s2);printf("提取串s的第三个字符开始的十个字符而产生串s3\n");s3=SubStr(s,3,10);DispStr(s3);printf("将串s1和串s2联接起来而产生新的串s1\n");StrCat(s1,s2);DispStr(s1);printf("将串s1和串s2比较之后得结果:%d\n",StrCmp(s1,s2));StrAssign(s4,"xyz");printf("子串s4在主串s2中的位置为:%d\n",IndStr(s2,s4));}十字链表的基本操作#include<iostream>using namespace std;#define smax 16typedef int datatype;typedef struct lnode {int i,j;struct lnode *rptr,*cptr;union {struct lnode *next;datatype v;}uval;}link;link *CREATLINKMAT() {link *p,*q,*l,*cp[smax];int i,j,m,n,t,s;datatype v;cout<<"请输入所创建矩阵的行数,列数以及非零元素个数:"<<endl;cin>>m;cin>>n;cin>>t;if(m>n)s=m;elses=n;l=(link*)malloc(sizeof(link));/*建立十字链表头结点*/l->i=m;l->j=n;cp[0]=l;for(m=1;m<=s;m++) { /*建立头结点循环链表*/ p=(link*)malloc(sizeof(link));p->i=0;p->j=0;p->rptr=p;p->cptr=p;cp[m]=p;cp[m-1]->uval.next=p;}cp[s]->uval.next=l;for(n=1;n<=t;n++) {cout<<"请输入一个非零元素的三元组:"<<endl;cin>>i;cin>>j;cin>>v;p=(link*)malloc(sizeof(link));p->i=i;p->j=j;p->uval.v=v;q=cp[i];/*以下是将*p结点插入第i行链表中*/while((q->rptr!=cp[i])&&(q->rptr->j<j))q=q->rptr;p->rptr=q->rptr;q->rptr=p;q=cp[j];/*以下是将*p结点插入第j列链表中*/while((q->cptr!=cp[j])&&(q->cptr->i<i))q=q->cptr;p->cptr=q->cptr;q->cptr=p;}return l;}/*创建十字链表*/void INSERT(link *head,int x,int y,datatype t) {if(x<1||x>head->i||y<1||y>head->j)cout<<"error1"<<endl;else {link *p,*q,*r,*s;int i=x,j=y;p=head->uval.next;while(--i)p=p->uval.next;q=p->rptr;s=p;while(q->j<y&&q->j!=0) {s=q;q=q->rptr;}if(q->j==y)q->uval.v=t;else if(q->j>y||q->j==0) {r=(link*)malloc(sizeof(link));r->i=x;r->j=y;r->uval.v=t;s->rptr=r;r->rptr=q;}p=head->uval.next;while(--j)p=p->uval.next;q=p->cptr;s=p;while(q->i<x&&q->i!=0) {s=q;q=q->cptr;}if(q->i>x||q->i==0) {s->cptr=r;r->cptr=q;}}}/*插入结点*/void DELETE1(link *head,int x,int y) { if(x<1||x>head->i||y<1||y>head->j)cout<<"error2"<<endl;else {link *p,*q,*r;int i=x,j=y;p=head->uval.next;while(--i)p=p->uval.next;q=p->rptr;r=p;while(q->j<y&&q->j!=0) {r=q;q=q->rptr;}if(q->j==y)r->rptr=q->rptr;p=head->uval.next;while(--j)p=p->uval.next;q=p->cptr;r=p;while(q->i<x&&q->i!=0) {r=q;q=q->cptr;}if(q->j==x) {r->cptr=q->cptr;free(q);}}}/*按行列删除结点*/void DELETE2(link *head,datatype t) { link *p,*q,*r;p=head->uval.next;while(p!=head) {q=p->rptr;r=p;while(q!=p) {if(q->uval.v==t)r->rptr=q->rptr;r=q;q=q->rptr;}p=p->uval.next;}p=head->uval.next;while(p!=head) {q=p->cptr;r=p;while(q!=p) {if(q->uval.v==t) {r->cptr=q->cptr;free(q);}elser=q;q=q->cptr;}p=p->uval.next;}}/*按值删除结点*/void PRINT(link *head) {cout<<"输出矩阵元素:"<<endl;int j=1,i=head->i;link *p,*q;p=head->uval.next;while(i--) {q=p->rptr;while(j!=head->j+1) {if(q->j==j) {cout<<q->uval.v<<" ";q=q->rptr;}elsecout<<"0 ";j++;}j=1;cout<<endl;p=p->uval.next;}}/*输出十字链表*/int main() {link *head;int i,j;datatype v;head=CREATLINKMAT();PRINT(head);cout<<"请输入插入结点的行数,列数和值:"<<endl;cin>>i;cin>>j;cin>>v;INSERT(head,i,j,v);PRINT(head);cout<<"请输入删除结点的行数,列数:"<<endl;cin>>i;cin>>j;DELETE1(head,i,j);PRINT(head);cout<<"请输入删除结点的值:"<<endl;cin>>v;DELETE2(head,v);PRINT(head);return 0;}二叉树的基本操作#include<iostream>using namespace std;typedef char datatype;#define maxsize 64typedef struct node {int ltag,rtag;datatype data;struct node *lchild,*rchild;}bithptr;bithptr *pre;bithptr *Q[maxsize];bithptr *CREATREE() {char ch;int front,rear;bithptr *root,*s;root=NULL;front=1;rear=0;ch=getchar();while(ch!='#') {s=NULL;if(ch!='@') {s=(bithptr*)malloc(sizeof(bithptr));s->data=ch;s->lchild=NULL;s->rchild=NULL;}rear++;Q[rear]=s;if(rear==1)root=s;else {if(s&&Q[front])if(rear%2==0)Q[front]->lchild=s;elseQ[front]->rchild=s;if(rear%2==1)front++;}ch=getchar();}return root;}/*建立二叉树*/void INTHREAD(bithptr *p) {if(p!=NULL) {INTHREAD(p->lchild);if(p->lchild==NULL)p->ltag=1;if(p->rchild==NULL)p->rtag=1;if(pre!=NULL) {if(pre->rtag==1)pre->rchild=p;if(p->ltag==1)p->lchild=pre;}pre=p;INTHREAD(p->rchild);}}/*二叉树线索化*/bithptr *INORDERNEXT(bithptr *p) { bithptr *q;if(p->rtag==1)return(p->rchild);else {q=p->rchild;while(q->ltag!=1)q=q->lchild;return q;}}/*中序后继*/void TRA VERSEINTHREAD(bithptr *p) { if(p!=NULL) {while(p->ltag!=1)p=p->lchild;do {cout<<" "<<p->data;p=INORDERNEXT(p);}while(p!=NULL);}}/*线索中序遍历*/void INSERTRIGHT(bithptr *p,datatype x) { bithptr *s,*q;q=(bithptr*)malloc(sizeof(bithptr));s=INORDERNEXT(p);q->data=x;q->ltag=1;q->lchild=p;q->rtag=p->rtag;q->rchild=p->rchild;p->rtag=0;p->rchild=q;if((s!=NULL)&&(s->ltag==1))s->lchild=q;}/*插入右子树*/void INORDER(bithptr *t) {if(t) {INORDER(t->lchild);cout<<" "<<t->data;INORDER(t->rchild);}}/*中序遍历*/int HEIGHT(bithptr *t) {int m,n;if(t==NULL)return 0;else {m=HEIGHT(t->lchild);n=HEIGHT(t->rchild);return (m>n)?m+1:n+1;}}/*二叉树的高度*/int COUNT(bithptr *t) {if(t==NULL)return 0;elsereturn 1+COUNT(t->lchild)+COUNT(t->rchild);}/*计算结点数*/int LEAF_COUNT(bithptr *t) {if(!t)return 0;else if(!t->lchild&&!t->rchild)return 1;elsereturn LEAF_COUNT(t->lchild)+LEAF_COUNT(t->rchild); }/*计算叶子数*/bithptr *a;void GET(bithptr *t,datatype value) {if(t) {if(t->data==value)a=t;GET(t->lchild,value);GET(t->rchild,value);}}bithptr *FIND(bithptr *ptr,datatype value,int *pos) {bithptr *previous;previous=ptr;*pos=0;GET(ptr,value);while(ptr!=NULL) {if(ptr->data==value)return previous;elseprevious=ptr;if(ptr->lchild==a) {ptr=ptr->lchild;*pos=-1;}else if(ptr->rchild==a) {ptr=ptr->rchild;*pos=1;}}return NULL;}/*二叉树查找*/bithptr *DELETE(bithptr *root,int value) { bithptr *previous;bithptr *ptr;bithptr *next;int pos;previous=FIND(root,value,&pos);cout<<pos<<endl;if(previous==NULL)return root;switch(pos){case -1:ptr=previous->lchild;break;case 1:ptr=previous->rchild;break;case 0:ptr=previous;break;}if (ptr->lchild==NULL ) {if(previous!=ptr)previous->rchild=ptr->rchild;elseroot=root->rchild;free(ptr);return root;}if(ptr->rchild==NULL) {if(previous!=ptr)previous->lchild=ptr->lchild;elseroot=root->lchild;free(ptr);return root;}previous=ptr;next=ptr->lchild;while(next->rchild!=NULL) {previous=next;next=next->rchild;}ptr->data=next->data;if(previous->lchild==next)previous->lchild=next->lchild;elseprevious->rchild=next->rchild;free(next);return root;}/*删除结点*/int main() {bithptr *root;datatype x;cout<<"请输入二叉树的值并以‘#’键结束"<<endl;root=CREATREE();INORDER(root);cout<<endl;cout<<"二叉树的高度为:"<<HEIGHT(root)<<endl;cout<<"二叉树的结点数为:"<<COUNT(root)<<endl;cout<<"二叉树的叶子数为:"<<LEAF_COUNT(root)<<endl;cout<<"请输入删除结点的值"<<endl;cin>>x;DELETE(root,x);INORDER(root);cout<<endl;INTHREAD(root);cout<<"请输入插入结点的值:"<<endl;cin>>x;INSERTRIGHT(root,x);TRA VERSEINTHREAD(root);return 0;}图的基本操作#include<iostream>using namespace std;#define n 6#define e 6#define TRUE 1typedef char vextype;typedef float adjtype;typedef struct {vextype vexs[n];adjtype arcs[n][n];}graph;typedef int datatype;#define maxsize 64typedef struct {datatype *data;int front,rear;}sequeue;int visited1[n];int visited2[n];graph g;void SETNULL(sequeue &sq) {sq.data=new datatype[maxsize];sq.front=-1;sq.rear=-1;}/*置空队*/int EMPTY(sequeue &sq) {if(sq.rear==sq.front)return 1;elsereturn 0;}/*判队空*/int ENQUEUE(sequeue &sq,datatype x) { if(sq.front==(sq.rear+1)%maxsize) { cout<<"queue is full"<<endl;return NULL;}else {sq.rear=(sq.rear+1)%maxsize;sq.data[sq.rear]=x;return 1;}}/*入队*/datatype DEQUEUE(sequeue &sq) { if(EMPTY(sq)) {cout<<"queue is empty"<<endl;return NULL;}else {sq.front=(sq.front+1)%maxsize;return sq.data[sq.front];}}/*出队*/void CREATGRAPH() {int i,j,k;adjtype w;cout<<"请输入"<<n<<"个顶点信息:"<<endl;for(i=0;i<n;i++)g.vexs[i]=getchar();for(i=0;i<n;i++)for(j=0;j<n;j++)g.arcs[i][j]=0;cout<<"请输入"<<e<<"条边的两个顶点位置以及权:"<<endl;for(k=0;k<e;k++) {cin>>i;cin>>j;cin>>w;g.arcs[i][j]=w;g.arcs[j][i]=w;}}/*建立无向连通图*/void DFS(int i) {int j;cout<<"node1:"<<g.vexs[i]<<endl;visited1[i]=TRUE;for(j=0;j<n;j++)if((g.arcs[i][j]!=0)&&(!visited1[j]))DFS(j);}/*深度优先遍历*/void BFS(int k) {int i,j;sequeue Q;SETNULL(Q);cout<<"node2:"<<g.vexs[k]<<endl;visited2[k]=TRUE;ENQUEUE(Q,k);while(!EMPTY(Q)) {i=DEQUEUE(Q);for(j=0;j<n;j++)if((g.arcs[i][j]!=0)&&(!visited2[j])) {cout<<"node2:"<<g.vexs[j]<<endl;visited2[j]=TRUE;ENQUEUE(Q,j);}}}/*广度优先遍历*/void INSERT(int i,int j,adjtype w) {g.arcs[i][j]=w;}/*插入边*/void DELETE(int i,int j) {g.arcs[i][j]=0;}/*删除边*/int main() {int i,j;adjtype w;CREATGRAPH();DFS(1);for(i=0;i<n;i++)visited1[i]=0;BFS(2);for(i=0;i<n;i++)visited2[i]=0;cout<<"请输入插入边的两个顶点位置以及权:"<<endl;cin>>i;cin>>j;cin>>w;INSERT(i,j,w);DFS(1);for(i=0;i<n;i++)visited1[i]=0;cout<<"请输入删除边的两个顶点位置"<<endl;cin>>i;cin>>j;DELETE(i,j);DFS(1);for(i=0;i<n;i++)visited1[i]=0;。