p=p->next;
++j;
}
if(!
(p->next)||j>i-1)returnERROR;
q=p->next;
p->next=q->next;
*e=q->data;
free(q);
returnOK;
}//ListDelete_L
voidCreateList_L(LinkList*L,intn){
LinkListp;
inti;
*L=(LinkList)malloc(sizeof(LNode));
(*L)->next=NULL;
for(i=n;i>0;--i){
p=(LinkList)malloc(sizeof(LNode));
p->data=i;
p->next=(*L)->next;(*L)->next=p;
}
}//CreateList_L
voidmain()
{
LinkListL;
ElemTypee;
CreateList_L(&L,5);
print_L(L);
GetElem_L(L,3,&e);
ListInsert_L(L,5,79);
print_L(L);
ListDelete_L(L,5,&e);
print_L(L);
}
2-SqStack
#include
#include
typedefintStatus;
#defineOK1
#defineERROR0
#defineTRUE1
#defineFALSE0
#defineINFEASIBLE-1
#defineOVERFLOW-2
#defineSTACK_INIT_SIZE100
#defineSTACKINCREMENT10
typedefintSElemType;
typedefstruct{
SElemType*base;
SElemType*top;
intstacksize;
}SqStack;
StatusInitStack(SqStack*S){
S->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!
S->base)exit(OVERFLOW);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
returnOK;
}//InitStack
StatusDestroyStack(SqStack*S){
if(!
S->base)returnERROR;
free(S->base);
returnOK;
}//DestroyStack
StatusClearStack(SqStack*S){
if(!
S->base)returnERROR;
if(S->top!
=S->base){
S->top=S->base;
}
returnOK;
}//ClearStack
StatusGetTop(SqStackS,SElemType*e){
if(S.top==S.base)returnERROR;
*e=*(S.top-1);
returnOK;
}//GetTop
StatusStackEmpty(SqStackS){
if(S.top==S.base)
returnTRUE;
else
returnFALSE;
}
intStackLength(SqStackS){
returnS.top-S.base;
}
StatusPush(SqStack*S,SElemTypee){
if(S->top-S->base>=S->stacksize){
S->base=(SElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!
S->base)exit(OVERFLOW);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*(S->top++)=e;
returnOK;
}//Push
StatusPop(SqStack*S,SElemType*e){
if(S->top==S->base)returnERROR;
*e=*(--S->top);
returnOK;
}//Pop
voidconvert(intnum,intdig){
SqStackS;
charc;
intd;
InitStack(&S);
while(num!
=0){
d=num%dig;
Push(&S,d);
num/=dig;
}
while(!
StackEmpty(S)){
Pop(&S,&d);
if(d<10)
printf("%d",d);
else{
c='A'+(d-10);
printf("%c",c);
}
}
printf("\n");
}
voidmain(){
convert(1388,16);
}
3-BiTree
#include
#include
#include
typedefstructbitnode{
chardata;
structbitnode*lchild;
structbitnode*rchild;
}BiTNode,*Bitree;
intk=0;
voidOrder_leafnum(BitreeT){
if(T==NULL)return;
if(T->lchild==NULL&&T->rchild==NULL){
printf("%3c",T->data);
k++;}
Order_leafnum(T->lchild);
Order_leafnum(T->rchild);
}
voidpreOrder(BitreeT)
{
if(T){
printf("%c",T->data);
preOrder(T->lchild);
preOrder(T->rchild);
}
}
voidinOrder(BitreeT)
{
if(T){
inOrder(T->lchild);
printf("%c",T->data);
inOrder(T->rchild);
}
}
voidproOrder(BitreeT)
{
if(T){
proOrder(T->lchild);
proOrder(T->rchild);
printf("%c",T->data);
}
}
voidCreate(Bitree*T)
{
charch;
ch=getchar();
if(ch=='@')*T=NULL;
else
{
*T=(Bitree)malloc(sizeof(BiTNode));
(*T)->data=ch;
Create(&(*T)->lchild);
Create(&(*T)->rchild);
}
}
voidExchange_BiTree(Bitreebt){
Bitreep;
if(bt){
p=bt->lchild;
bt->lchild=bt->rchild;
bt->rchild=p;
Exchange_BiTree(bt->lchild);
Exchange_BiTree(bt->rchild);
}
}
voidmain(){
BitreeT;
Create(&T);
Order_leafnum(T);
printf("\nleafnum=%d",k);
preOrder(T);
inOrder(T);
proOrder(T);
Exchange_BiTree(T);
proOrder(T);
}
4-ALGraph
#include
#include
#definemax50
typedefintvextype;
typedefstructArcNode{
intadjvex;
structArcNode*nextarc;
}ArcNode,*ArcPtr;
typedefstruct{
vextypedata;
ArcNode*firstarc;
}VNode,AdjList[max];
typedefstruct{
VNodevertices[max];
intvexnum;
intarcnum;
}ALGraph;
voidCreat_AG(ALGraph*AG)
{//输入顶点和边的信息,建立图的邻接表
ArcPtrp;inti,j,k;
printf("\ninputvexnum:
");
scanf("%d",&AG->vexnum);
printf("\ninputarcnum:
");
scanf("%d",&AG->arcnum);
for(i=1;i<=AG->vexnum;++i){//初始化
printf("\ninput%dthvex(char):
",i);
scanf("%d",&AG->vertices[i].data);
getchar();
AG->vertices[i].firstarc=NULL;
}
for(k=1;k<=AG->arcnum;k++){//输入边信息,建立邻接表,无向无权图
printf("\ninput%dtharci(int)j(int):
",k);
scanf("%d%d",&i,&j);
/*在第i个链表上插入序号为j的表结点*/
p=(ArcPtr)malloc(sizeof(ArcNode));
p->adjvex=j;
p->nextarc=AG->vertices[i].firstarc;
AG->vertices[i].firstarc=p;
/*在第j个链表上插入序号为i的表结点*/
p=(ArcPtr)malloc(sizeof(ArcNode));
p->adjvex=i;
p->nextarc=AG->vertices[j].firstarc;
AG->vertices[j].firstarc=p;
}
}
intvisited[50];
voidBFS(ALGraphG,inti){
intQ[50];
intrear,front;
rear=0;
front=0;
intj;
ArcPtrp;
Q[rear++]=1;
while(front!
=rear){
i=Q[front++];
printf("%3c",G.vertices[i].data);
for(p=G.vertices[i].firstarc;p;p=p->nextarc){
j=p->adjvex;
if(visited[j]==0){
Q[rear++]=j;}
visited[j]=1;
}
}
voidTraverse(ALGraphG)
{
inti;
for(i=1;i<=G.vexnum;i++)visited[i]=0;//访问标志数组初始化
for(i=1;i<=G.vexnum;i++)
if(visited[i]==0)BFS(G,i);//对未访问的顶点调用BFS
}
//intvisited[50];
//voidDFS(ALGraphG,inti){
//intj;
//ArcPtrp;
//visited[i]=1;
//printf("%3d",G.vertices[i].data);
//for(p=G.vertices[i].firstarc;
//p;p=p->nextarc)
//{
//j=p->adjvex;
//if(!
visited[j])DFS(G,j);
//}
//Indegree[k]--j;
//if(indegree[k]==0)s[top++]=k;
//}
//voidTraverse(ALGraphG)
//{
//inti;
//for(i=1;i<=G.vexnum;i++)
//visited[i]=0;
//for(i=1;i<=G.vexnum;i++)
//if(!
visited[i])DFS(G,i);
//}
intTopologicalSort(ALGraphG){//拓扑排序
ints[50];
inttop;
inti,j,k;
intcount;
ArcPtrp;
intindegree[50];
for(i=1;i<=G.vexnum;i++)
indegree[i]=0;
for(i=1;i<=G.vexnum;i++)
for(p=G.vertices[i].firstarc;p!
=NULL;p=p->nextarc){
j=p->adjvex;
indegree[j]++;}
top=0;
for(i=1;i<=G.vexnum;i++)
if(indegree[i]==0){
s[top++]=i;}
count=0;
while(top!
=0){
i=s[--top];
printf("%3c",G.vertices[i].data);
count++;}
for(p=G.vertices[i].firstarc;p;p=p->nextarc)
{
k=p->adjvex;
indegree[k]--;
if(indegree[k]==0)s[top++]=k;}
if(count==G.vexnum)
return1;
else
return0;
}
intvisited[50];
voidDFS(ALGraphG,inti)
{intj;
ArcPtrp;
visited[i]=1;
printf("%3c",G.vertices[i].vexdata);
for(p=G.vertices[1].firstarc;p;p=p->nextarc)
{
j=p->adjvex;
if(!
visited[j])DFS(G,j)}
}
voidBFS(ALGraphG,intv)
{
intQ[30],i,j;
intrear=0,front=0;
ArcPtrp;
printf("%c\t",G.vertices[v].vexdata);
visited[v]=1;
rear++;Q[rear]=v;
while(rear!
=front){
front++;i=Q[front];
for(p=G.vertices[1].firstarc;p;p=p->nextarc)
{
j=p->adjvex;
if(!
visited[i])
{
printf("%ct",G.vertices[j].vexdata)}
visited[j]=1;rear++;Q[rear]=j}}}
voidmain()
{
ALGraphG;
Creat_AG(&G);
//DFS(G,1);
//Traverse(G);
BFS(G,1);
Traverse(G);
TopologicalSort(G);
}
5-Search_seq
#include
#include
#definemax20
typedefintElemType;
typedefstruct{
ElemTypeelem[max];
intlength;
}SSTable;
intSearch_seq0(SSTableST,ElemTypekey)
{
inti;
i=ST.length;
while(ST.elem[i]!
=key&&i>0)i--;
returni;
}
intSearch_seq1(SSTableST,ElemTypekey)
{
inti;
ST.elem[0]=key;
i=ST.length;
while(ST.elem[i]!
=key)i--;
returni;
}
intSearch_seq2(SSTableST,ElemTypekey)
{
inti;
ST.elem[ST.length+1]=key;
i=1;
while(ST.elem[i]!
=key)i++;
returni;
}
intSearch_Bin_d(SSTableST,ElemTypekey,intlow,inthigh){
intmid;
low=1;
high=ST.length;
if(low<=high)
{
mid=(low+high)/2;
if(key==ST.elem[mid])returnmid;
elseif(keyreturn(Search_Bin_d(ST,key,low,mid-1));
else
return(Search_Bin_d(ST,key,mid+1,high));
}
elsereturn0;
}
intSearch_Bin(SSTableST,ElemTypekey){
intlow;
inthigh;
intmid;
low=1;
high=ST.length;
while(low<=high)
{
mid=(low+high)/2;
if(key==ST.elem[mid])returnmid;
elseif(keyelselow=mid+1;
}
return0;
}
voidCreateList__Sq(SSTable*ST,intn){
inti;
printf("\n请输入%d个整数!
",n);
for(i=1;ielem[i]);}
ST->length=n;
}
voidmain(){
SSTableST,STy;
CreateList__Sq(&ST,7);
inti=Search_seq2(ST,65);
printf("%d",i);
CreateList__Sq(&STy,7);
intj=Search_Bin(STy,76);
printf("%d",j);
j=Search_Bin_d(STy,76,1,7);
printf("%d",j);
}
6-Insert
#include
#include
#definemaxsize20
typedefintKeyType;
typedefstruct{
KeyTypekey;
}RedType;
typedefstruct{
RedTyper[maxsize+1];
intlength;
}Sqlist;
inti;
voidCreateList__Sq(Sqlist*