023. C语言基础

//   221.c
#include
#include
struct node
{
   int num;
   struct node *next;
};
void putfun(struct node *head)
{
   while(head!=NULL)
   {
      printf("%4d",head->num);
      head=head->next;
   }
}
struct node *fun(int n)
{
   struct node *head,*p1,*p2;
   int i=0;
   head=p1=p2=(struct node *)malloc(sizeof(struct node));
   head->num=i+1;
   i++;
   while(inum=i+1;
     p2->next=p1;
     i++;
   }
   p1->next=NULL;
   return head;
}
struct node *insfun(struct node *p,int n)
{
   struct node *head=p,*m,*q;
   q=(struct node *)malloc(sizeof(struct node));
   q->num=n;
   if(n<=head->num)
   {
      q->next=head;
      head=q;
      return head;
   }
   while(p->numnext;
   }
   q->next=p;
   m->next=q;
   return head;
}
void main()
{
   struct node *head;
   int n;
   printf("please input node's num:");
   scanf("%d",&n);
   head=fun(n);
   putfun(head);
   printf("\nplease input insert's node's daxiao:");
   scanf("%d",&n);
   head=insfun(head,n);
   putfun(head);
   getch();
}
//  222.c
#include
#include"lianbiao.h"
//链表的综合应用
void main()
{
   struct node *head;
   int n;
   int m;
   char ans='y';
   printf("please input node's num:");
   scanf("%d",&n);
   head=fun(n);
   putfun(head);
   do{
   printf("\nplease input 1/2/3\n(1 is insert,2 is delet,3 is fanxiang):");
   scanf("%d",&m);
   switch(m)
   {
      case 1:
      printf("\nplease input want insert's num:");
      scanf("%d",&n);
      head=insfun(head,n);
      putfun(head);break;
      case 2:
      printf("\nplease input want delete's num:");
      scanf("%d",&n);
      head=delfun(head,n);
      putfun(head);break;
      case 3:
      head=fxfun(head);
      putfun(head);break;
   }
      printf("\nyou want play again:(Y/N)");
      getchar();
      scanf("%c",&ans);
   }while(ans=='y');
   printf("\nGAME OVER");
   getch();
}
//   223.c
#include
#include"lianbiao.h"
int issfun(struct node *head)
{
   struct node *m,*p;
   m=head;
   p=m->next;
   while(p!=NULL)
   {
      if(p->numnum)return 0;
      m=p;
      p=p->next;
   }
   return 1;
}
int isjfun(struct node *head)
{
   struct node *m,*p;
   m=head;
   p=m->next;
   while(p!=NULL)
   {
      if(p->num>m->num)return 0;
      m=p;
      p=p->next;
   }
   return 1;
}
void main()
{
   struct node *head;
   int n;
   scanf("%d",&n);
   head=fun(n);
   putfun(head);
   if(issfun(head)==1)printf("\nis shenxu");
   else if(isjfun(head)==1)printf("\nis jiangxu");
   else printf("\nnot shengxu and not jiangxu");
   getch();
}
//   224.c
#include
#include"lianbiao.h"
int issfun(struct node *head)
{
   struct node *p;
   p=head;
   head=head->next;
   if(head==NULL)return 1;
   if(head->numnum)return 0;
   issfun(head);
}
int isjfun(struct node *head)
{
   struct node *p;
   p=head;
   head=head->next;
   if(head==NULL)return 1;
   if(head->num>p->num)return 0;
   isjfun(head);
}
void main()
{
   struct node *head;
   int n;
   scanf("%d",&n);
   head=fun(n);
   putfun(head);
   if(issfun(head)==1)printf("\nis shenxu");
   else if(isjfun(head)==1)printf("\nis jiangxu");
   else printf("\nnot shengxu and not jiangxu");
   getch();
}

//    225.c
#include
#include"lianbiao.h"
void main()
{
   struct node *head,*p,*q;
   int t;
   head=fun(5);
   putfun(head);
   for(p=head;p!=NULL;p=p->next)
     for(q=p->next;q!=NULL;q=q->next)
       if(p->num>q->num)
       {
          t=p->num;
          p->num=q->num;
          q->num=t;
       }
   printf("\n");
   putfun(head);
   getch();
}
//   226.c
#include
#include"lianbiao.h"
//链表求耶稣的叛徒
struct node *ptfun(struct node *head,int n,int num,int start)
{
   struct node *p,*m;
   int i=0;
   p=m=head;
   while(start>1)
   {
     p=p->next;
     start--;
   }
   while(n!=1)
   {
      m=p;
      p=p->next;
      i++;
      if(i==num-1)
      {
         m->next=p->next;
         printf("%3d",p->num);
         p=p->next;
         i=0;
         n--;
      }
   }
   p->next=NULL;
   return p;
}
void main()
{
   struct node *head,*p,*m;
   int n;
   scanf("%d",&n);
   head=p=fun(n);
   while(p!=NULL)
   { m=p;
     p=p->next;
   }
   m->next=head;
   printf("\nti's xiaoguo is:\n");
   head=ptfun(head,n,3,2);
   printf("\nzh's pt is:\n");
   putfun(head);
   getch();
}
//   227.c
#include
union node
{
   int a;
   long b;
   char c;
};
void main()
{
   union node x;
   x.a=256;
   printf("%d",x.c);
   getch();
}
//   228.c
#include
union node1
{
   int a;
   char b;
};
union node2
{
   int a;
   char b;
   union node1 c;
};
struct node3
{
   int a;
   char b;
   union node2 c;
};
void main()
{
   struct node3 x;
   x.c.a.b=1;
   printf("%d",x.c.a.a);
   getch();
}
//    229.c
#include
struct node
{
   char name[20];
   char job;
   union
   {
      int ban;
      char zc[20];
   }ban_zc;
};
void main()
{
   struct node x[2],*p;
   int i;
   for(i=0;i<2;i++)
   {
      scanf("%s",x[i].name);
      while(getchar()!='\n');
      getchar();
      scanf("%c",&x[i].job);
      if(x[i].job=='t')
      {
         printf("\nplease input teacher's zhicheng:\n");
         scanf("%s",x[i].ban_zc.zc);
      }
      else
      {
         printf("\nplease input student's class:\n");
         scanf("%d",&x[i].ban_zc.ban);
      }
   }
   printf("\nname job class/zc\n");
   for(p=x;pname,p->job);
       if(p->job=='t')
       {
          printf("%s",p->ban_zc.zc);
       }
       else
          printf("%d",p->ban_zc.ban);
       printf("\n");
   }
   getch();
}
//   230.c
#include
void main()
{
   enum node{a,b,c=6,d,e,f};
   enum node x;
   for(x=a;x<=f;x++)
     printf("%3d",x);
   getch();
}

你可能感兴趣的:(023. C语言基础)