數據結構6-11

6
#include <stdio.h>
#include <string.h>
#define maxsize 100
int C[maxsize];
char A[maxsize];
void frequency()
{
  char s[maxsize];
  int size,n=0,same=0;
  scanf("%s",&s);
  size=strlen(s);
  for(int j=0,k=j;j<size;j++,k++)
  {
    for(int p=0;p<k;p++)
    {
      if(s[j]==s[p])
      {
    same=1;
    break;
       //    C[p]=+1;
      }
      else
    same=0;
    }
    if(same==0)
    {
      A[n]=s[j];
      n++;
      }
  }
  for(int h=0;h<strlen(A);h++)
  {
    C[h]=0;
    for(int m=0;m<strlen(s);m++)
    {
      if(A[h]==s[m])
    C[h]++;
    }
  }
}
main()
{
  frequency();
  int len=strlen(A) ;
 // printf("%d ",len);
  for(int i=0;i<len;i++)
  {
    printf("%c  %d/n",A[i],C[i]);

  }
  printf("Different number is:%d ",len);
}

7
#include <stdio.h>
#include < malloc.h>
typedef float DataType;
typedef struct node{
  DataType data;
  struct node *next;
}ListNode;
typedef ListNode *LinkList;
void delBet(DataType min,DataType max,LinkList h)
{
  ListNode *p,*b;
  p=h;
  while(p->next!=NULL)
  {
    if(p->next->data>min && p->next->data<max)
    {
      b=p->next;
      p->next=b->next;
      free(b);
    }
    else
    {
      p=p->next;
    }
  }
}
main()
{
  DataType x[10]={1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0};
  LinkList head=(LinkList)malloc(sizeof(ListNode));
  ListNode *s,*r,*t;
  r=head;
  printf("The inital data are:");
  for(int i=0;i<10;i++)
  {
    s=(ListNode *)malloc(sizeof(ListNode));
    s->data=x[i];
    printf("%f ",s->data) ;
    r->next=s;
    r=s;
  }
  r->next=NULL;
  delBet(0.0,9.0,head);
  printf("/nThe result are:");
  t=head->next;
  while(t!=NULL)
  {
    printf("%f ",t->data);
    t=t->next;
  }
}


8
#include <stdio.h>
int pal(char A[],int s,int e)
{
  int i=(e-s)/2;
  for(int j=0;j<i;j++,s++,e--)
  {
    if(A[s-1]!=A[e-1])
      return 0;
    }
  return 1;

}
main()
{
  char Ch[500];
  scanf("%s",Ch);
  printf("%d",pal(Ch,1,5));
}

9
#include <stdio.h>
#include <malloc.h>
typedef struct BinTreeNode{
  char data;
  BinTreeNode *lChild,*rChild;
}BinTreeNode,*BinTree;
//typedef BinTreeNode* BinTree;
int BTreeHeight(BinTree BT)
{
  int dep1,dep2,max;
  if(BT==NULL)
    return 0;
  else
    {
    dep1=BTreeHeight(BT->lChild);
    dep2=BTreeHeight(BT->rChild);
    if(dep1>dep2)
      max=dep1;
    else
      max=dep2;
    return max+1;
    }
}
int CreateBTree(BinTree *b)
{
 char ch;
 ch=getchar();
 if(ch=='.')
  {
    *b=NULL;
  }
  else
  {
    (*b)=(BinTree)malloc(sizeof(BinTreeNode));
    (*b)->data=ch;
    CreateBTree(&((*b)->lChild));
    CreateBTree(&((*b)->rChild));
  }
}
main()
{
  int high;
  BinTree p=NULL;
  CreateBTree(&p);
  high=BTreeHeight(p);
  printf("The high is:%d",high);
}


10
#include <stdio.h>
int BinSearch(long R[],int n,long k)
{
  int low=0,high=n-1,mid;
  while(low<=high)
  {
    mid=(low+high)/2;
    if(k==R[mid])
      return mid;
    else if(k<R[mid])
      high=mid-1;
      else
      low=mid+1;
  }
  return -1;
}
main()
{
  int temp;
  long s[10]={1.2,3.4,5.6,6.8,7.9,8.9,9.0,11.0,12.9,14.0};
  temp=BinSearch(s,10,9.0);
  printf("%d",temp);
}

11
#include <stdio.h>
#include <string.h>
int static s1[10]={2,3,5,4,6,7,9,8,10,1};
int s2[10];
void countSort()
{
  int temp,k;
  for(int i=0;i<10;i++)
  {
    temp=s1[i];
    k=0;
    for(int j=0;j<10;j++)
    {
      if(temp>s1[j])
    k++;
    }
    s2[k]=temp;
  }
}
main()
{
  printf("The  unsorted numbers are:");
  for(int t=0;t<10;t++)
  {
    printf("%d ",s1[t]);
  }
  countSort();
  printf("/nThe sorted numbers are:");
  for(int p=0;p<10;p++)
  {
    printf("%d ",s2[p]);
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章