数据结构各实验源码二(C语言版)

实验六:矩阵的压缩存储

)

    

         {

           if(M.data[total].i==row&&M.data[total].j==col)

           {printf(“%-5d “,M.data[total].e);

            total++;

           }

           else printf(“%-5d “,0);

        }

         printf(“\n”);

     }

}

TSMatrix TransposeSMatrix(TSMatrix A)           /*转置函数;*/

{TSMatrix B;

     int col,k,p=0;

     B.mu=A.nu;

     B.nu=A.mu;

     B.tu=A.tu;

     if(A.tu)

     {

      for(col=1;col<=A.nu;col++)
         for(k=0;k           if(A.data[k].j==col)

               {B.data[p].i=A.data[k].j;

                B.data[p].j=A.data[k].i;

                B.data[p].e=A.data[k].e;

                p++;

               }

    

        }

       return B;  

}

    

TSMatrix AddTriTuple(TSMatrix A,TSMatrix B)  

     {      TSMatrix C;                                 /*三元组表表示的稀疏矩阵A,B相加;*/

         int k=0,l=0,t=0;    

         int temp;

         C.mu=A.mu;

         C.nu=A.nu;

         while(k         {if((A.data[k].i==B.data[l].i)&&(A.data[k].j==B.data[l].j))

           {temp=A.data[k].e+B.data[l].e;

               if(temp)

                  { C.data[t].i=A.data[k].i;

                    C.data[t].j=A.data[k].j;

                    C.data[t].e=temp;

                    k++;l++;t++;

                   }

               else{k++;l++; }

           }

        

           else if((A.data[k].i==B.data[l].i)&&(A.data[k].j          {C.data[t].i=A.data[k].i;

           C.data[t].j=A.data[k].j;

           C.data[t].e=A.data[k].e;

           k++;t++;

         }

           else     /*if((A.data[k].i==B.data[l].i)&&(A.data[k].j>B.data[l].j)||(A.data[k].i>B.data[l].i)) */

         {C.data[t].i=B.data[l].i;

           C.data[t].j=B.data[l].j;

           C.data[t].e=B.data[l].e;

           l++;t++;

         }

        }

      

         while(k         {C.data[t].i=A.data[k].i;

          C.data[t].j=A.data[k].j;

          C.data[t].e=A.data[k].e;

          k++;t++;

         }

         while(l         {C.data[t].i=B.data[l].i;

          C.data[t].j=B.data[l].j;

          C.data[t].e=B.data[l].e;

          l++;t++;

         }

      

         C.tu=t;

         return C;

     }    

  

main()

{TSMatrix M,T,H,F,K1,K2;

M=creattsm();

printf(“juzheng\n”);

     printtsm(M);

     printf(“\n\n”);

     T=TransposeSMatrix(M);

      printf(“zhuan zhi ju zheng :\n”);

      printtsm(T);

/*      M=creattsm();

printf(“\njuzheng\n\n”);

     printtsm(M);

     printf(“\n\n”);

        T=creattsm();

printf(“juzheng\n”);

     printtsm(T);

     printf(“\n\n”);

     H=AddTriTuple(M,T);

printf(“\n\nadd juzheng :\n”);

printtsm(H);       */

getch();

}

实验七:二叉树的遍历

#include

#define NULL 0

#include

struct tree

{int      e;

struct tree *lchild,*rchild;

};

struct tree*creat()

{int c;

struct tree *t;

printf(“\nplease input a number:”);

scanf(“%d”,&c);

if(c==0) t=NULL;

else

{

t=(struct tree*)malloc(sizeof(struct tree));

t->e=c;

t->lchild=creat();

t->rchild=creat();

}

return t;

}

void traver1(struct tree*t)

{if(t!=NULL)

{

printf(“%d”,t->e);

traver1(t->lchild);

traver1(t->rchild);

}

}

void traver2(struct tree*t)

{if(t!=NULL)

{

traver2(t->lchild);

printf(“%d”,t->e);

traver2(t->rchild);

}

}

void traver3(struct tree*t)

{if(t!=NULL)

{

traver3(t->lchild);

traver3(t->rchild);

printf(“%d”,t->e);

}

}

main()

{struct tree *t;

printf(“\n xian xu creat bitree:”);

t=creat();

printf(“\n xian xu bianli:”);

traver1(t);

printf(“\n”);

printf(“\n zhong xu bianli:”);

traver2(t);

printf(“\n”);

printf(“\n hou xu bianli:”);

traver3(t);

printf(“\n”);

getch();

}

实验八:查找

#include “stdio.h”

#include “conio.h”

#include “malloc.h”

#define     LIST_INIT_SIZE     100

#define     LISTINCREMENT     10

#define OVERFLOW -2

     #define OK 1

        #define ERROR 0

#define ElemType int

typedef     struct

{

     ElemType *elem;

     int length;

     int listsize;

}Sqlist;

Sqlist InitList_Sq()

{      Sqlist L;

      L.elem=(ElemType *)malloc( LIST_INIT_SIZE*sizeof( ElemType));

      if(!L.elem)exit(OVERFLOW);

      L.length=0;

      L.listsize=LIST_INIT_SIZE;

     return L;

}

void shuru(int n,Sqlist L)

{     int i;

       for(i=0;i<=n-1;i++)
          { printf(“\nplease input L.elem[%d]=”,i);     scanf(“%d”,L.elem++);}

       L.length=n;

}

void shuchu(Sqlist L)

{

        int i;

       printf(“\nplease output Sqlist L=(“);

       for(i=0;i<=L.length-1;i++)
         {      printf(“%5d”,*L.elem++);}

          printf(“)”);

}

void sharu(Sqlist L,int i,ElemType e)

{

     int *newbase,*p,*q;

       if (i<1 ||i>=L.length+1)return ERROR;

     if(L.length>=L.listsize)

         {

         newbase=(ElemType *)realloc( L.elem,(L.listsize+ LISTINCREMENT)*sizeof( ElemType));

             if(! L.elem)exit(OVERFLOW);

            L.elem=newbase;

          L.listsize+=LISTINCREMENT;

         }

      q=&(L.elem[i-1]);

      for (p=&(L.elem[L.length-1]); p>=q;–p) *(p+1)=*p;

       *q=e;

      L.length=L.length+1;

}

void shanchu(Sqlist L,int i,ElemType e)

{

      int *p,*q;

      if((i<1)||(i>L.length)) return ERROR;

      p=&(L.elem[i-1]);

      e=*p;

       printf(“\ndelete L.elem[%d]=%d”,i,e);

      q=L.elem+L.length-1;

      for(++p;p<=q;++p)*(p-1)=*p;         L.length=L.length-1;
}

main()

{ int n,i,e,*p,*q,*newbase;

     Sqlist L;

     clrscr();

     L=InitList_Sq();

       printf(“please input n=”);

       scanf(“%d”,&n);

      shuru(n,L);

     L.length=n;

      shuchu(L);

       printf(“\n\n\nplease insert weizhi i=”);

        scanf(“%d”,&i);

        printf(“\nplease insert e=”);

        scanf(“%d”,&e);

       sharu(L,i,e);     L.length=n+1;

          shuchu(L);

        printf(“\n\n\nplease delete weizhi i=”);

        scanf(“%d”,&i);

         shanchu(L,i,e);

         L.length=L.length-1;

       shuchu(L);

       getch();

}

实验九:内部排序

#include

#include

#define GT(a,b) ((a)>(b))

typedef int KeyType ;

typedef struct

{

       KeyType key;

}RedType;

typedef struct

{

       RedType r[21];

       int length;

}Sqlist;

int main()

{

    

       Sqlist L;

       int i,j;

        printf(“please input 10 number:\n”);

       for(i=1;i<=10;i++)
           scanf(“%d”,&L.r[i].key);

       L.length=10;

       for(i=1;i<=L.length;i++)
           printf(“%d\t”,L.r[i].key);

       printf(“\n”);

       for(i=2;i<=L.length;i++)
           if(GT(L.r[i].key,L.r[i-1].key))

           {

               L.r[0]=L.r[i];

               L.r[i]=L.r[i-1];

               for(j=i-2;GT(L.r[0].key,L.r[j].key);–j)

                   L.r[j+1]=L.r[j];

               L.r[j+1]=L.r[0];

           }

    

       for(i=1;i<=L.length;i++)
           printf(“%d\t”,L.r[i].key);

       printf(“\n”);

       getch();

       return 0;

}

说明:以上程序由本人整理,错误是难免的,特别是实验四,编译的时候并没有完全通过 ,望高手能帮助修改一下。



发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>