데이터 구조 기본 조작 소스 코드
//
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef ElemType *Triplet;
#include "h2.h"
Status InitTriplet(Triplet&T,ElemType v1,ElemType v2,ElemType v3)
{
T = (ElemType *)malloc(3*sizeof(ElemType));
if (!T) exit (OVERFLOW);
T[0] = v1; T[1] = v2; T[2] = v3;
return OK;
}
Status DestroyType(Triplet &T)
{
free(T); T = NULL;
return OK;
}
Status Get(Triplet T,int i,ElemType &e)
{
if(i<1 || i>3) return ERROR;
e = T[i-1];
return OK;
}
Status Max(Triplet T,ElemType &e)
{
e=(T[0]>=T[1])?((T[0]>=T[2])?T[0]:T[2]):((T[1]>=T[2])?T[1]:T[2]);
return OK;
}
Status Min(Triplet T,ElemType &e)
{
e=(T[0]<=T[1])?((T[0]<=T[2])?T[0]:T[2]):((T[1]<=T[2])?T[1]:T[2]);
return OK;
}
Status Average(Triplet T,ElemType &e)
{
e = (T[0] + T[1] + T[2])/3;
return OK;
}
void main()
{
Triplet p;
ElemType e,v1,v2,v3;
int select,i;
printf("
");
scanf("%d%d%d",&v1,&v2,&v3);
if (InitTriplet(p,v1,v2,v3)==OVERFLOW)
printf(" , !");
else
do
{
printf("1: i
");
printf("2:
");
printf("3:
");
printf("4:
");
printf("5:
");
printf("
");
scanf("%d",&select);
switch(select)
{ case 1:
printf("
i=");
scanf("%d",&i);
if (Get(p,i,e)==ERROR) printf("i
");
else printf(" %d :%d
",i,e); break;
case 2:
Max(p,e);
printf(" :%d
",e);break;
case 3:
Min(p,e);
printf(" :%d
",e);break;
case 4:
Average(p,e);
printf(" : %d
",e);break;
case 0:
printf(" !"); break;
default: printf(" !
");
}//end switch
}while(select!=0); //end of while
DestroyType(p);
}// end of main
****************************************************************************************************
*****************************************************************************************************
#include <stdio.h>
#include <stdlib.h>
#define listinitsize 100
#define listincrement 10
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType *elem;
ElemType length;
ElemType listsize;
}Sqlist;
//.cpp
#include"h1.h"
Sqlist la;
ElemType *a;
ElemType e,k,pre_e,next_e,choice;
Status makelist(Sqlist &la)
{
ElemType i;
la.elem=(ElemType *)malloc(listinitsize *sizeof(ElemType));
if(!la.elem) exit(OVERFLOW);
la.length = 80;
la.listsize=listinitsize;
for(i=0 ; i<=la.length-1 ;i++)
la.elem[i]=i;
return OK;
}
Status listlengh(Sqlist &la)
{
return la.length ;
}
Status getelem(Sqlist la,ElemType k)
{
return la.elem[k];
}
Status priorelem(Sqlist &la,ElemType e ,ElemType &pre_e )
{
ElemType i;
for (i=0; i<=la.length-1;i++)
if(la.elem[i]==e)
pre_e=la.elem[i-1];
return OK;
}
Status nextelem(Sqlist &la,ElemType e ,ElemType &next_e )
{
ElemType i;
for (i=0; i<=la.length-1;i++)
if(la.elem[i]==e)
next_e=la.elem[i+1];
return OK;
}
Status listdelete(Sqlist &la,ElemType k,ElemType &e)
{
ElemType j;
if((k<1)||(k>la.length)) return ERROR;
e=la.elem[k-1];
for(j=k+1;j<=la.length-1;j++)
la.elem[j-1]=la.elem[j];
--la.length;
return OK;
}
Status listinsert(Sqlist &la,ElemType k,ElemType e)
{
ElemType j;
if((k<1)||(k>la.length+1)) return ERROR;
/*if(l.length>=l.listsize)
{*/
for(j=la.length;j>=k;j--)
la.elem[j]=la.elem[j-1];
la.elem[k]=e;
++la.length;
return OK;
}
Status main()
{
makelist(la);
do{
printf("1:
");
printf("2: k
");
printf("3:e
");
printf("4:e
");
printf("5: k
");
printf("6: k
");
printf("0: !
");
printf("Enter your choice :
");
scanf("%d",&choice);
switch(choice)
{
case 1:
listlengh(la);
printf("the length is %d
",listlengh(la));
break;
case 2:
printf("Enter a number k :");
scanf("%d",&k);
getelem(la, k);
printf("k is %d
",getelem(la,k));
break;
case 3:
printf("Enter a number e:
");
scanf("%d",&e);
priorelem(la, e , pre_e );
if(priorelem(la, e , pre_e ))
printf("The priorelem is %d
",pre_e);
break;
case 4:
printf("Enter a number e:
");
scanf("%d",&e);
nextelem(la, e , next_e );
if(nextelem(la, e , next_e ))
printf("The nextelem is %d
",next_e);
break;
case 5:
printf("Enter a number k :");
scanf("%d",&k);
listdelete(la,k,e);
printf("the delete number is:%d
",e);
break;
case 6:
printf("Enter a number k :");
scanf("%d",&k);
printf("the insert number is:");
scanf("%d",&e);
listinsert(la,k,e);
break;
case 0:
printf(" !");
break;
}
}while(choice!=0); //end of while
return OK; }
************************************************************************************************************
************************************************************************************************************
#include <stdio.h>
#include <stdlib.h>
#define OK 0
#define ERROR -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct LNode{
ElemType data; //
struct LNode *next; //
}LNode,*Linklist;Status GetElem_L(Linklist L,int i,ElemType &e) //L , i , e
{
Linklist p=L; // ,p ,j
int j=0;
while(p &&j<i) // , p i p
{
p=p->next;
++j;
}
if(!p ||j>i) return ERROR; // i
e=p->data; // i
return OK;
}
Status ListInsert_L(Linklist &L,int i,ElemType e) // L i e
{
Linklist p=L; // ,p ,j
int j=0;
Linklist s;
while(p && j<i-1)
{
p=p->next; // i-1
++j;
}
if(!p ||j>i-1) return ERROR; //i 1 1
s=(Linklist)malloc(sizeof(LNode)); // , linklist
s->data = e; // L
s->next=p->next;
p->next=s;
return OK;
}
Status ListDelete_L(Linklist &L,int i,ElemType &e) // L , i , e
{ Linklist q;
Linklist p=L; // ,p ,j
int j=0;
while(p->next && j<i-1)
{
p=p->next;
++j;
}
if(!(p->next || j>i-1)) return ERROR; //
q=p->next; //
p->next=q->next;
e=q->data;
free(q);
return OK;
}
Status CerateList_L(Linklist &L,int n) // n , L
{
Linklist p;
L=(Linklist)malloc(sizeof(LNode));
L->next=NULL; //
for(int i=n;i>0;--i)
{
p=(Linklist)malloc(sizeof(LNode)); //
scanf("%d",&p->data); //
p->next=L->next; //
L->next=p;
}
return OK;
}
Status Listlength(Linklist L,int &j) //
{
j=0;
Linklist p=L;
while(p->next)
{
p=p->next;
j++;
}
return OK;
}
Status PriorEle(Linklist L,int cur_e,ElemType &pre_e) // cur_e , pre_e
{
Linklist p=L;
while(p->next &&p->next->data !=cur_e)
p=p->next;
if(!p->next ||p==L) return ERROR; //
pre_e=p->data;
return OK;
}
Status search(Linklist L,int k) // k
{
Linklist p,q;
int count =0;
p=q=L->next;
while(p != NULL) //p ,p k , p,q
{
if(count <k)
{count ++;}
else
{q=q->next;}
p=p->next;
}
if(count<k) return ERROR; // k
else
return OK;
}
void Traverse (Linklist L) //
{
Linklist p;
p=L->next;
printf(" :
");
while(p)
{
printf("%d ",p->data);
p=p->next;
}
}
void main()
{
Linklist L,q;
int select,i,e,j,k,cur_e,pre_e,n;
printf(" :");
scanf("%d",&n);
CerateList_L(L,n);
Traverse (L);
do
{
printf("
1:
");
printf("2: i
");
printf("3: i
");
printf("4: i
");
printf("5: cur_e
");
printf("6: k
");
printf("0: !
");
printf(" !
");
scanf("%d",&select);
switch(select)
{
case 1:
Listlength(L,j);
printf(" :%d
",j);
break;
case 2:
printf("i=");
scanf("%d",&i);
if(GetElem_L(L,i,e)==ERROR)
printf(" i
");
else
printf("
%d :%d
",i,e);
Traverse (L);
break;
case 3:
printf("i=");
scanf("%d",&i);
printf("e=");
scanf("%d",&e);
if(ListInsert_L(L,i,e)==ERROR )
printf(" i
");
Traverse (L);
break;
case 4:
printf("i=");
scanf("%d",&i);
if(ListDelete_L(L,i,e)==ERROR)
printf(" i
");
else
printf("
%d %d
",i,e);
Traverse (L);
break;
case 5:
printf("cur_e=");
scanf("%d",&cur_e);
if(PriorEle(L,cur_e,pre_e)==ERROR)
printf("
%d
",cur_e);
else
printf("
%d %d
",cur_e,pre_e);
Traverse (L);
break;
case 6:
printf("k=");
scanf("%d",&k);
if(search(L,k)==ERROR)
printf(" k
");
else
printf(" %d :%d
",k,q->data);
Traverse (L);
break;
case 0:
printf("
");
break;
default:
printf(" !
");
} //end of switch
}while(select != 0); //end of while
}
*************************************************************************************************
***************************************************************************************************
Status InitQueue(SqQueue &Q){
Q.base = (QElemType *) malloc(MAXSIZE * sizeof(QElemType));
if (!Q.base)exit (OVERFLOW);
Q.front = Q.rear = 0;
return OK;
}
Status EnQueue(SqQueue &Q,QElemType e){
if((Q.rear+1)%MAXSIZE == Q.front) return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXSIZE;
return OK;
}
int QueueLength(SqQueue Q){
return ((Q.rear-Q.front+MAXSIZE)%MAXSIZE - 1);
}
Status DeQueue(SqQueue &Q,QElemType &e){
if(Q.front == Q.rear) return ERROR;
e = Q.base[Q.front];
Q.front = (Q.front+1)%MAXSIZE;
return e;
}
/*Status TraversQueue(SqQueue &Q,QElemType e){
e=Q.base[Q.front];
while (Q.base[Q.front])
{
printf("%d ",e);
}
return OK;
}*/
int main()
{
int select,e,i;
SqQueue Q;
printf("
1.
");
printf("2.
");
printf("3.
");
printf("4.
");
printf("0. 。
");
printf(" :");
scanf("%d",&select);
while(select!=0)
{
switch(select)
{
case 1:
InitQueue(Q);
while(e!=-1&&i<=MAXSIZE){
int i=0;
scanf("%d",&e);
EnQueue(Q,e);
i++;
}
break;
case 2:
printf("%d",QueueLength(Q));
break;
case 3:
scanf("%d",&e);
printf(" %d",e);
EnQueue(Q,e);
break;
case 4:
DeQueue(Q,e);
printf(" %d",e);
break;
case 0:
printf("
exit!
");
break;
}
printf("
1.
");
printf("2.
");
printf("3.
");
printf("4.
");
printf("0. 。
");
printf(" :");
scanf("%d",&select);
}
return OK;
}
************************************************************************************************
*************************************************************************************************
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define TURE 1
#define FALSE 0
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
//#define NULL 0;
typedef int Status;
typedef int SElemType;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//.cpp
#include"h1.h"
Status InitStack(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;
return OK;
}
Status StackEmpty(SqStack S)
{
if(S.top==S.base)
return OK;
else return ERROR;
}
Status Push(SqStack &S,SElemType e)
{
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;
return 0;
}
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}
Status Compare()
{
SqStack S;
char ch;
SElemType e;
InitStack(S);
int flag=TURE;
while (((ch= getchar())!='#') && flag )
{
switch (ch) {
case '(' :
case '[' :
case '{' :
Push(S,ch);break;
case ')' :
if ( Pop(S,e)==ERROR || e!='(' )
flag=FALSE;
break;
case ']' :
if ( Pop(S,e)==ERROR || e!='[')
flag=FALSE;
break;
case '}' :
if ( Pop(S,e)==ERROR || e!='{')
flag=FALSE;
break;
}//switch
}
if (flag && ch=='#' && StackEmpty(S))
return TURE;
else return FALSE;
}
Status conversion()
{
SqStack S;
SElemType N,e;
InitStack(S);
printf(" :");
scanf("%d",&N);
while(N)
{
Push(S,N%16);
N=N/16;
}
while(!StackEmpty(S))
{
Pop(S,e);
if(e<10)
{
printf("%d",e);
}
else
{
switch(e)
{
case 10: printf("A"); break;
case 11: printf("B"); break;
case 12: printf("C"); break;
case 13: printf("D"); break;
case 14: printf("E"); break;
case 15: printf("F"); break;
}
}
}
return OK;
}
void main()
{
int select;
printf("1. 。
");
printf("2. 。
");
printf("0. 。
");
printf(" :");
scanf("%d",&select);
while(select!=0)
{
switch(select)
{
case 1:
conversion();
break;
case 2:
printf("
, # :");
if(Compare()==TURE)
printf("
");
else
printf("
");
break;
case 0:
printf(" , 。");
break;
}
printf("
1. 。
");
printf("2. 。
");
printf("0. 。
");
printf(" :");
scanf("%d",&select);
}
}
*************************************************************************************************
**************************************************************************************************
#include "stdio.h"
#define OK 1
#define ERROR 0
#define MAXSIZE 120
typedef int ElemType;
typedef int Status;
typedef struct {
int i,j;
ElemType e;
}Triple;
typedef struct{
Triple data[MAXSIZE + 1];
int mu,nu,tu;
}TSMatrix;
#include "1.h"
void InitMatrix(int A[P+1][N+1])
{ int i,j;
printf(" 4 5
");
for(i=1;i<=P;i++)
for(j=1;j<=N;j++)
scanf("%d",&A[i][j]);
}
TSMatrix MatrixToTriple(int A[P+1][N+1],TSMatrix M)
{ int row,col,k=0;
for(row=1;row<=P;row++)
for(col=1;col<=N;col++)
if(A[row][col]!=0)
{ k++;
M.data[k].i=row;
M.data[k].j=col;
M.data[k].e=A[row][col];
}
M.mu=P; M.nu=N; M.tu=k;
return M;
}
TSMatrix FastTransposeSMatrix(TSMatrix M,TSMatrix T)
{ int col,t,p,q;
int num[N+1],cpot[N+1];
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 T;
}
void PrintMatrix(TSMatrix T)
{ int B[N+1][P+1]={0};
int i,j,k=1;
for(i=1;i<N+1;i++)
for(j=1;j<P+1;j++)
if(i==T.data[k].i && j==T.data[k].j)
{ B[i][j]=T.data[k].e;++k;}
printf("output TransposeMatrix
");
for(i=1;i<=N;i++)
{for(j=1;j<=P;j++)
printf("%4d",B[i][j]);
printf("
");
}
}
int main()
{ int A[P+1][N+1]={0};
TSMatrix M={{0},0,0,0},T={{0},0,0,0};
InitMatrix(A);
M=MatrixToTriple(A,M);
T=FastTransposeSMatrix(M,T);
PrintMatrix(T);
return OK;
}
***********************************************************************************************
***********************************************************************************************
#include "stdio.h"
#include "malloc.h"
#include "iostream.h"
typedef int Status;
typedef int TElemType;
#define OK 1
#define ERROR 0
#define OVERFLOW -1
//
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode, *BiTree;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
//#define NULL 0;
typedef int Status;
typedef int SElemType;
typedef struct TriTNode { //
TElemType data;
struct TriTNode *lchild, *rchild;
//
struct TriTNode *parent; //
} TriTNode, *TriTree;#include "stack.h"
#include "stack.cpp"
#include "queue.cpp"
Status CreateBiTree(BiTree &T) {
char ch;
scanf("%c",&ch);
if (ch=='# ') T = NULL;
else {
if (!(T = (BiTNode *)malloc(sizeof(BiTNode))))
printf("overflow!
");
T->data = ch; //
CreateBiTree(T->lchild); //
CreateBiTree(T->rchild); //
}
return OK;
} // CreateBiTree
void Preorder (BiTree T)
{ //
if (T) {
visit(T->data); //
Preorder(T->lchild); //
Preorder(T->rchild);//
}
}
Status inorder(BiTree T,SqStack &S,SElemType p))
{// ,s
InitStack(S);Push(S,T);
while (!StackEmpty(S)){
while (GetTop(S,p)&& p) Push(S,p->lchild);
Pop(S,p);
if (!StackEmpty(S)){
Pop(S,p);
visit(p->data);
Push(S,p->rchild);
}//if
}//while
return OK;
}//inorder
Status PostOrderTraverse(BiTree T,Status ( * Visit)(TElemType e)){
}
int Depth (BiTree T ){ //
if ( !T ) depthval = 0;
else {
depthLeft = Depth( T->lchild );
depthRight= Depth( T->rchild );
depthval = 1 + (depthLeft > depthRight ?
depthLeft : depthRight);
}
return depthval;
}
Status LevelOrder(BiTree T){
// T, Q
InitQueue(Q);
If (T!=NULL){ //
EnQueue(Q,T); //
While (!QueueEmpty(Q)){
DeQueue(Q,b); //
visit(b->data); //
if (b->lchild!=NULL) EnQueue(Q,b->lchild);// ,
if (b->rchild!=NULL) EnQueue(Q,b->rchild); // ,
}//while
}//if
}LevelOrder
main(){
BiTree T;
printf(" :
");
CreateBiTree(T);
return 0;
}
*************************************************************************************************************************
***************************************************************************************************************************
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 20
#define MAX 100
#define ERROR 0
#define TRUE 1
#define OK 1
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define MAXQSIZE 20
typedef int Status;
typedef char TElemType;
typedef struct
{
int *base;
int front;
int rear;
}SqQueue;
SqQueue Q;
int e=0;
Status InitQueue (SqQueue &Q ) {
Q.base = (int *)malloc(MAXQSIZE *sizeof (int));
if(!Q.base) exit (OVERFLOW);
Q.front = Q.rear = 0;
return OK;
}
Status QueueLength (SqQueue &Q )
{
return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
}
Status EnQueue (SqQueue &Q , int e){
if ((Q.rear+1) % MAXQSIZE == Q.front) return ERROR;
Q.base[Q.rear] = e;
Q.rear = (Q.rear+1) %MAXQSIZE;
return OK;
}
Status DeQueue (SqQueue &Q, int &e) {
if (Q.front == Q.rear) return ERROR;
e = Q.base[Q.front];
Q.front = (Q.front+1)% MAXQSIZE;
return OK;
}
Status QueueEmpty(SqQueue Q)
{
if(Q.front==Q.rear)
return OK;
else
return ERROR;
}
typedef struct ArcNode
{
int adjvex;
struct ArcNode *nextarc;
char *info;
}ArcNode;
typedef struct VNode
{
int data;
ArcNode * firstarc;
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct
{
AdjList vertices;
int vexnum,arcnum;
}ALGraph;
int FirstAdjVex(ALGraph G,int v)
{
if(G.vertices[v].firstarc)
return G.vertices[v].firstarc->adjvex;
return -1;
}
#include "1.h"
int NextAdjVex(ALGraph G,int v,int w)
{
ArcNode *p = G.vertices[v].firstarc;
while(p)
{
if(p->adjvex == w)
{
if(p->nextarc)
return p->nextarc->adjvex;
}
p=p->nextarc;
}
return -1;
}
int createUDG(ALGraph &G )
{
int i, v1, v2;
cout<<" :";
cin>>G.vexnum; //
cout<<" :";
cin>>G.arcnum; // ( )
for(i=1; i<=G.vexnum ; i++) //
{
G.vertices[i].firstarc =NULL;
G.vertices[i].data=i;
}
for(i=1;i<=G.arcnum ;i++)
{
cout<<" ["<<i<<"] "<<endl;
cin>>v1>>v2;// ( )
//
ArcNode *p;
p=(ArcNode *)malloc(sizeof(ArcNode));
p->adjvex=v2;
p->nextarc=G.vertices[v1].firstarc;
G.vertices[v1].firstarc=p;
}
return OK;
}
void vist_P(ALGraph G)
{
int i;
ArcNode *p;
p=(ArcNode *)malloc(sizeof(ArcNode));
for (i = 1 ; i<= G.vexnum ; i++)
{
cout<<"."<<G.vertices[i].data<<"->";
p=G.vertices[i].firstarc;
while (p)
{
cout<<p->adjvex<<"->";
p= p->nextarc;
}
cout<<"^"<<endl;
}
}
int visited[MAX];
void DFS(ALGraph G,int v)
{
visited[v]=TRUE;
cout<<G.vertices[v].data<<" ";
for (int w=FirstAdjVex(G,v);w>=0;w=NextAdjVex(G,v,w))
{
if (!visited[w]) DFS(G,w);
}
}
void DFSTraverse(ALGraph G)
{
int v;
for (v=1;v<=G.vexnum;v++)
{
visited[v]=ERROR;
}
for (v=1;v<=G.vexnum;v++)
{
if (!visited[v]) DFS(G,v);
}
}
void BFSTraverse(ALGraph G)
{
int w,v;
for (v=1; v<=G.vexnum; ++v)
visited[v] = ERROR; //
InitQueue(Q); // Q
for ( v=1; v<=G.vexnum; ++v )
if ( !visited[v])
{
visited[v] = TRUE;
cout<<G.vertices[v].data<<" ";//
EnQueue(Q,v);
while(!QueueEmpty(Q))
{
int u ;
DeQueue (Q,u);
for(w = FirstAdjVex(G,u); w >= 0; w = NextAdjVex(G,u,w))
if(!visited[w])
{
visited[w] = TRUE ;cout<<G.vertices[w].data<<" " ;
EnQueue(Q,w);
}
}
}
}
void Collections(ALGraph G)
{
int num[20] = {0};
int i;
ArcNode *ptr;
for(i = 1; i <= G.vexnum ; i++)
{
if(G.vertices[i].firstarc)
{
ptr = G.vertices[i].firstarc;
while (ptr)
{
num[i]++;
num[ptr->adjvex]++;
ptr = ptr->nextarc;
}
}
}
for(i = 1 ; i <= G.vexnum; i++)
cout<<i<<" :"<<num[i]<<endl;
}
void main()
{
printf(" 1.
");
printf(" 2.
");
printf(" 3.
");
printf(" 4.
");
printf(" 0. !
");
printf(" :");
ALGraph G ;
int select;
scanf("%d",&select);
while (0<select && select <5)
{
switch (select){
case 1:
createUDG(G );
cout<<" :"<<endl;
vist_P(G);
cout<<endl;
cout<<endl;
break;
case 2:
cout<<" :"<<endl;
BFSTraverse( G);
cout<<endl;
break;
case 3:
cout<<" :"<<endl;
DFSTraverse( G);
cout<<endl;
printf("
");
break;
case 4:
Collections(G);
break;
}
printf(" 1.
");
printf(" 2.
");
printf(" 3.
");
printf(" 4.
");
printf(" 0. !
");
printf(" :");
scanf("%d",&select);
}
}
************************************************************************************************************
************************************************************************************************************
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 10
typedef int KeyType;
typedef int Status;
typedef struct{
KeyType key;
}Elemtype;
typedef struct{
Elemtype *elem;
int length;
}Sstable;
//.cpp
Status InitList_Ss(Sstable &st)
{// st
st.elem=(Elemtype*)malloc(LIST_INIT_SIZE*sizeof(Elemtype));
if(!st.elem) return false;
st.length=0;
return true;
}//InitList_Ss
Sstable &Init_Ss(Sstable &st)
{//
printf(" :
");
int i=0;
int t;
do
{
scanf("%d",&st.elem[i].key);
st.length++;
t=st.elem[i].key;
if(i>0)
{
for(int j=0;j<st.length-1;j++)
{
if(st.elem[i].key<st.elem[j].key)
{
for(int k=st.length-2;k>=j;k--)
{
st.elem[k+1].key=st.elem[k].key;
}
st.elem[j].key=t;
}
}
}
i++;
}while(t<1000);
st.length--;
return st;
}//Init_Ss
void Display(Sstable &st)
{//
printf(" :
");
if(st.length==0)
printf("
");
for(int i=0;i<st.length;i++)
{
printf("%5d",st.elem[i].key);
}
printf("
");
}//Display
int Search_Bin(Sstable st,KeyType k)
{//
int low=0,high=st.length,mid;
while(low<=high)
{
mid=(low+high)/2; //
if(st.elem[mid].key==k)
return (mid+1); //
else if(st.elem[mid].key>k)
high=mid-1; //
else
low=mid+1; //
}
return 0;
}//Search_Bin
void Menu()
{
printf("
");
printf("1,
");
printf("2,
");
printf("0,
");
printf(" :
");
}
void main()
{
Sstable st; //
KeyType k; //
int x,n; //x ,n
Menu();
scanf("%d",&n);
while(n!=0)
{
switch(n)
{
case 1:
{
if(InitList_Ss(st))
{
st=Init_Ss(st);
Display(st);
}
else
printf("
");
break;
}
case 2:
{
printf(" :
");
scanf("%d",&k);
x=Search_Bin(st,k);
if(x!=0)
printf(" , %d
",x);
else
printf("
");
break;
}
default:printf(" , !
");
}
Menu();
scanf("%d",&n);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.