数据结构 C版

作者在 2008-05-02 12:15:27 发布以下内容

#include<stdio.h>
#include<stdlib.h>
typedef struct Complex
{
float real;
float image;
}Complex;
Complex InitComplex(float a,float b)
{
  Complex c;
  c.real=a;
  c.image=b;
  return c;
}
Complex Add(Complex c1,Complex c2)
{
  Complex sum;
  sum.real=c1.real+c2.real ;
  sum.image=c1.image+c2.image  ;
  return sum;
}
Complex Sub(Complex c1,Complex c2)
{
  Complex dif;
  dif.real=c1.real-c2.real ;
  dif.image=c1.image-c2.image ;
  return dif;
}
Complex Mul(Complex c1,Complex c2)
{
  Complex product;
  product.real=c1.real*c2.real-c1.image*c2.image;
  product.image=c1.real*c2.image+c1.image*c2.real;
return product;
}
Complex Div(Complex c1,Complex c2)
{

  Complex quotient;
  quotient.real=0;
  quotient.image=0;
  if (c2.real<1e-38&&c2.image<1e-38)
     {printf("c2 is 0!error!\n");return quotient;}
  quotient.real=
(c1.real*c2.real+c1.image*c2.image)/(c2.real*c2.real+c2.image*c2.image);
 quotient.image=
(c1.image*c2.real-c1.real*c2.image)/(c2.real*c2.real+c2.image*c2.image);
  return quotient;
}
float GetReal(Complex c)
{
  return c.real;
}
float GetImag(Complex c)
{
  return c.image;
}
void Print_C(Complex c)
{
   if(GetImag(c)==0.0)   printf("%5.2f\n",GetReal(c));
   else  if(GetReal(c)==0.0)  printf("%5.2fi\n",GetImag(c));
         else   printf("%5.2f+%5.2fi\n",GetReal(c),GetImag(c));
}
void main()
{
   Complex z1,z2,sum,dif,pro,quo;
   float e1,e2;
   char sym;
   printf("Input the operation symbol(+,-,*,/):");
   scanf("%c",&sym);
   printf("Input z1 like: 3.0+2.1i or -3.0+-2.1i\nz1=");
   scanf("%f+%fi",&e1,&e2);
   z1=InitComplex(e1,e2);
   printf("Input z2 like: 3.0+2.1i or -3.0+-2.1i\nz2=");
   scanf("%f+%fi",&e1,&e2);
   z2=InitComplex(e1,e2);
   switch(sym)
    {
     case '+': sum=Add(z1,z2);Print_C(sum);break;
     case '-': dif=Sub(z1,z2);Print_C(dif);break;
     case '*': pro=Mul(z1,z2);Print_C(pro);break;
     case '/': quo=Div(z1,z2);Print_C(quo);break;
     default: printf("error symbol!!");

    }
getch();
}

 

#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
/* 设立无头结点的单循环链表 */
typedef struct LNode{
  int id;
  int pw;
  struct LNode *next;
}LNode,*LinkList;

void main()
{
  int m,n,i=1,pw,j=0;
  LinkList L,p,q; 
  printf("输入人数n(n>0):");
  scanf("%d",&n);
  printf("输入第 1 人密码:");
  scanf("%d",&pw);
  L=(LinkList)malloc(sizeof(struct LNode));
  L->id=i;
  L->pw=pw;
  L->next=L;
  q=L;  
  for(i=2;i<=n;i++)
    {
       printf("输入第 %d 人密码:",i);
       scanf("%d",&pw);
       p=(LinkList)malloc(sizeof(struct LNode));
       p->id=i;
       p->pw=pw;      
       while(q->next!=L) q=q->next;
       q->next=p;
       p->next=L;
    }
  printf("输入约瑟夫环的初始报数值m:");
  scanf("%d",&m);
  printf("出列顺序为:");
  p=L;
  for(i=1;i<=n;i++)
    {     
       for(j=1;j<m-1;j++)   p=p->next;
       if (m==1) p=p->next;
       q=p->next; /* q指向待删除结点 */
       p->next=q->next;
       printf("%d\t",q->id);
       m=q->pw;
       p=q->next;
       free(q);
    }
  getch();
}

 

 

#include"stdio.h"
#include"stdlib.h"
struct people{
 int id;
 int pw;
 struct people *next;
};

struct people * play(struct people *p1,int n,int m);
void printpoint(struct people *p1);

void main()
{
 int n,m=20,i;
 struct people *top,*p;
 printf("输入人数:");
 scanf("%d",&n);
 printf("输入m的初值:");
 scanf("%d",&m);
 top=(struct people *)malloc(sizeof(struct people));
 if(NULL==top)
 {
  printf("内存分配失败!");
  getchar();
  exit(0);
 }
 p=top;
 p->id=1;
 p->next=NULL;
 printf("输入第1个人的密码:");
 scanf("%d",&(p->pw));
 for(i=1;i<n;i++)
 {
  p->next=(struct people *)malloc(sizeof(struct people));
  if(NULL==top)
  {
   printf("分配失败!");
   getchar();
   exit(0);
  }
  p=p->next;
  p->id=i+1;
  printf("输入第%d个人的密码:",i+1);
  scanf("%d",&(p->pw));
  p->next=NULL;
 }
 p->next=top;      //首尾连接成循环链表;
 top=play(top,n,m);      //报数出对程序;
 printpoint(top);    //打印出对次序;
}

struct people * play(struct people * p1,int n,int m)
{
 int i;
 struct people *pk=NULL,*pk1=NULL;
 if(1==m&&n>1)
 {
  for(i=1;i<n;i++)
  {
   p1=p1->next;
  }
 }
 else
 {
  for(i=1;i<m-1;i++)
  {
   p1=p1->next;
  }
 }
  pk1=pk=p1->next;
  m=p1->next->pw;
  n--;
  p1->next=p1->next->next;
  p1=p1->next;
 while(n!=1)       //即n>1时;
 {
  if(1==m&&n>1)
  {
   for(i=1;i<n;i++)
   {
    p1=p1->next;
   }
  }
  else
  for(i=1;i<m-1;i++)
  {
   p1=p1->next;
  }

  pk1->next=p1->next;
  m=p1->next->pw;
  n--;
  p1->next =p1->next->next;
  p1=p1->next;
  pk1=pk1->next;
 }
 pk1->next=p1;
 p1->next=NULL;     //执行这句之前p1->next等于p1;
 return(pk);
}

void printpoint(struct people *p1)
{
 int i=1;
 printf("出列顺序为:\n");
 while(p1!=NULL&&i<1000)
 {
  printf("%d,",p1->id);
  p1=p1->next;
  i++;
 }
 printf("\n");
}

文章评论,共0条
游客请输入验证码