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

学习了一个学期的数据结构(C语言版),把这个学期编过的代码整理一下。

实验一:复数抽象数据类型ADT的实现:

#include

struct fs

{ float e1;

     float e2;

};

struct fs z1,z2,result;

int flag;

void add(struct fs z1,struct fs z2);

void sub(struct fs z1,struct fs z2);

void     mul(struct fs z1,struct fs z2);

void     div(struct fs z1,struct fs z2);

void     shuchu(float a,float b);

/*struct fs show(z1,z2);*/

void main()

{        clrscr();

printf(“\nplease input z1的实部:”);

     scanf(“%f”,&z1.e1);

     printf(“\nplease input z1的虚部:”);

     scanf(“%f”,&z1.e2);

printf(“\nz1=”);

shuchu(z1.e1,z1.e2);

printf(“\nplease input z2的实部:”);

     scanf(“%f”,&z2.e1);

     printf(“\nplease input z2的虚部:”);

     scanf(“%f”,&z2.e2);

printf(“\nz2=”);

shuchu(z2.e1,z2.e2);

printf(“\n\n1.ADD.     2.SUB.     3.MUL.     4.DIV \n\n”);

scanf(“%d”,&flag);

printf(“\n\n”);

switch(flag)

{

     case 1:add(z1,z2);

      break;

     case 2:sub(z1,z2);

      break;

     case 3:mul(z1,z2);

      break;

                case 4:div(z1,z2);

      break;

}

}

void shuchu(float a,float b)

{ /*if(b>0) printf(“%f+%fi\n”,a,b);

     else printf(“%f%fi\n”,a,b);*/

if(a==0)

             {if (b==0)

                  printf(“0\n”);

              else

                  printf(“%fi\n”,b);

             }

else

             {if(b==0)

                 printf(“%f\n”,a);

             else

                if(b>0) printf(“%f+%fi\n”,a,b);

                   else printf(“%f%fi\n”,a,b);

              }

}

void add(struct fs z1,struct fs z2)

{struct fs result;

result.e1=z1.e1+z2.e1;

result.e2=z1.e2+z2.e2;

printf(“z3=”);

shuchu(result.e1,result.e2);

}

void sub(struct fs z1,struct fs z2)

{struct fs result;

result.e1=z1.e1-z2.e1;

result.e2=z1.e2-z2.e2;

printf(“z3=”);

     shuchu(result.e1,result.e2);

}

void mul(struct fs z1,struct fs z2)

{struct fs result;

result.e1=z1.e1*z2.e1-z1.e2*z2.e2;

result.e2=z1.e1*z2.e2+z1.e2*z2.e1;

printf(“z3=”);

shuchu(result.e1,result.e2);

}

void div(struct fs z1,struct fs z2)

{

struct fs result;

         float k;

          k=z2.e1*z2.e1+z2.e2*z2.e2;

      z2.e2=-z2.e2;

        result.e1=z1.e1*z2.e1-z1.e2*z2.e2;

result.e2=z1.e1*z2.e2+z1.e2*z2.e1;

      

shuchu(result.e1/k,result.e2/k);

}

实验二:线性表顺序表示和基本操作

#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

#define null 0

struct slist

     {int coef,exp;

      struct slist *next;

     };

typedef struct slist SLIST;

SLIST *creat()

{ int c,d;

     SLIST *h,*s,*r;

     h=(SLIST*) malloc (sizeof(SLIST));

     r=h;

     printf(“\nplease input xishu and     zhishu:”);

     scanf(“%d,%d”,&c,&d);

     printf(“\n”);

     while(c!=0 )

      {     s=(SLIST*)malloc(sizeof(SLIST));

         s->coef=c;

         s->exp=d;

         r->next=s;

         r=s;

        printf(“\nplease input xishu and     zhishu:”);

       scanf(“%d,%d”,&c,&d);

       printf(“\n”);

      }

     r->next=null;

return h;

}

print(SLIST *h)

{

     SLIST *p;

     p=h;

     while(p->next!=null)

       {

         p=p->next;

         printf(“(%d,%d)”,p->coef,p->exp);

         printf(“\n”);

         printf(“\n”);

       }

     printf(“\n”);

}

SLIST *padd(SLIST *heada,SLIST *headb)

{

     struct slist *headc,*p,*q,*s,*r;

     int x;

     p=heada->next;q=headb->next;

     headc=(SLIST *)malloc(sizeof(SLIST));

     r=headc;

     while(p!=NULL&&q!=NULL)

      {

        if(p->exp==q->exp)

            {

             x=p->coef+q->coef;

             if(x!=0)

                {

                  s=(SLIST *)malloc(sizeof(SLIST));

                  s->coef=x;s->exp=p->exp;

                   r->next=s;r=s;

                   p=p->next;q=q->next;

                 }

             else

               {p=p->next;q=q->next;}

             }

        else

              if(p->expexp)

                 {

                    s=(SLIST *)malloc(sizeof(SLIST));

                    s->coef=p->coef;

                     s->exp=p->exp;

                    r->next=s;

                    r=s;

                    p=p->next;

                  }

             else

                {

                   s=(SLIST *)malloc(sizeof(SLIST));

                   s->coef=q->coef;s->exp=q->exp;

                   r->next=s;r=s;q=q->next;

                 }

        }

      while(p!=NULL)

       { s=(SLIST *)malloc(sizeof(SLIST));

        s->coef=p->coef;

        s->exp=p->exp;

        r->next=s;

        r=s;

        p=p->next;

       }

      while(q!=NULL)

         { s=(SLIST *)malloc(sizeof(SLIST));

          s->coef=q->coef;s->exp=q->exp;

          r->next=s;

          r=s;

          q=q->next;

         }

        r->next=NULL;  

  

       return(headc);

}

main()

{

     SLIST *head1,*head2,*head3;

     clrscr();

      printf(“\nplease intput expres1″);

     head1=creat();

     print(head1);

       printf(“\nplease intput expres2″);

     head2=creat();

     print(head2);

     head3=padd(head1,head2);

     printf(“\nplease output jieguo:”);

print(head3);

getch();

}

实验四:栈的操作,装10进制转换为8进制

#include

#include

#include

#define ok 1

#define overflow -2

#define error 0

typedef struct {

int *base;

int *top;

int stacksize;

}sqstack;

sqstack s,l; int n,e;

sqstack initstack(sqstack s){

     s.base=(int *)malloc(100*sizeof(int));

s.top=s.base;

s.stacksize=100;

}

push(sqstack s,int e){

if(s.top-s.base>=s.stacksize) {

s.base=(int *)realloc(s.base,

(s.stacksize+10)*sizeof(int));

if(!s.base)exit(overflow);

s.top=s.base+s.stacksize;

s.stacksize+=100;

}

*s.top++=e;

return ok;

}

pop(sqstack s,int e){

if(s.top==s.base)return error;

e= *(–s.top);

}

void main(){

initstack(s);

scanf(“%d”,n);

while(n)

{push(s,n%8);

n=n/8;

}

while(!(s.top==s.base)){

pop(s,e);

printf(“%d”,e);

}



发表评论

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

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