第三次作业

输出月份英文名

char *getmonth( int n )
{
    if(n<=0||n>12)
    {
        return NULL;
    }else
    {
        if(n==1)
        {
            return "January";
        }
        if(n==2)
        {
            return "February";
        }
        if(n==3)
        {
            return "March";
        }
        if(n==4)
        {
            return "April";
        }
        if(n==5)
        {
            return "May";
        }
        if(n==6)
        {
            return "June";
        }
        if(n==7)
        {
            return "July";
        }
        if(n==8)
        {
            return "August";
        }
        if(n==9)
        {
            return "September";
        }
        if(n==10)
        {
            return "October";
        }
        if(n==11)
        {
            return "November";
        }
        if(n==12)
        {
            return "December";
        }
    }
}

查找星期

int getindex( char *s )
{
    int i;
    char a[7][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    for(i=0;i<7;i++)
    {
        if(strcmp(s,a[i])==0)
        {
            return i;
        }
    }
   if(i==7)
    {
        return -1;
    }
}

计算最长的字符串长度

int max_len( char *s[], int n )
{
    int i;
    int max=strlen(s[0]);
    for(i=1;i<n;i++)
    {
        if(strlen(s[i])>max)
        {
            max=strlen(s[i]);
        }
    }
    return max;
}

指定位置输出字符串

char *match( char *s, char ch1, char ch2 )
{  
    int i=0,j=0,k=0,x=0;  
    char *p=NULL;  
    x = strlen(s);  
    for(i=0;i<x;i++)
    {  
        if(s[i]==ch1)
        {  
            p=&s[i];  
            for(j=i;j<x;j++)
            {  
                if(s[j]!=ch2)
                {  
                    printf("%c", s[j]);  
                }  
                if(s[j]==ch2)
                {  
                              printf("%c\n", s[j]);  
                    return p;  
                }     
            }  
            printf("\n");  
            return p;  
        }  
    }  
    p=&s[x];
    printf("\n");  
    return p;  
} 

奇数值结点链表

  struct ListNode *getodd(struct ListNode **L)
  {
      int flag=0;
      struct ListNode *p=*L,*head1=NULL,*tail1=NULL,*q=NULL,*tail2=NULL,*ptr=NULL,*head2=NULL;
      while(p!=NULL)
      {
          q=(struct ListNode*)malloc(sizeof(struct ListNode));
          if(p->data%2!=0)
          {
              q->data=p->data;
              q->next=NULL;
              if(head1==NULL)
              {
                  head1=q;
              }else
              {
                  tail1->next=q;
              }
              tail1=q;
              ptr=p;
              p=p->next;
              free(ptr);
          }else
          {
              if(head2==NULL)
              {
                  head2=p;
              }else
              {
                  tail2->next=p;
              }
              tail2=p;
              p=p->next;
              flag=1;

          }
          if(flag==1)
          {
              tail2->next=NULL;
          }
          *L=head2;
          return head1;
      }
  }

错误:

改正:

  struct ListNode *readlist()
  {
      int data;
      struct ListNode *head=NULL, *tail=NULL, *p=NULL;
  
      scanf("%d", &data);
      while (data != -1)
      {
          p = (struct ListNode*) malloc(sizeof(struct ListNode));
          p->data = data;
          p->next = NULL;
          if (head == NULL)
              head = p;
          else
              tail->next = p;
          tail = p;
          scanf("%d", &data);
      }
      return head;
  }
  
  struct ListNode *getodd(struct ListNode **L)
  {
      int flag = 0;
      struct ListNode *head=NULL, *tail=NULL, *q=NULL, *p=*L, *p1=NULL, *p2=NULL, *ptr=NULL;
  
      while (p!=NULL)
      {
          if (p->data % 2 != 0)
          {
              q = (struct ListNode *) malloc(sizeof(struct ListNode));
              q->data = p->data;
              q->next = NULL;
              if (head == NULL)
                  head = q;
              else
                  tail->next = q;
              tail = q;
              ptr = p;
              p = p->next;
              free(ptr);
          }
          else
          {
              if (p1 == NULL)
                  p1 = p;
              else
                p2->next = p;
              p2 = p;
                p = p->next;
                flag = 1;
          }
      }
      if (flag==1)
          p2->next = NULL;
      *L = p1;
      return head;
  }

学生成绩链表处理

struct stud_node *deletelist( struct stud_node *head, int min_score )
{
    struct stud_node *p=head,*p1=head;
    if(head==NULL)
    {
        return NULL;
    }else
    {
        for(p=head;p!=NULL;p=p->next)
        {
        
            if(p->score<min_score)
            {
              
                if(p==head)
                {
                    head=p->next;
                }else
                {
                    if(p->next == NULL )
                    {
                      
                        p1->next = NULL;
                        free(p);
                    }
                    p1->next=p->next;
                }
                free(p);
            }
            p1=p;
        }
    }
    return head;
}

错误:

只能通过本题的案例,其他都不能过
改正

struct stud_node *createlist()
{
    struct stud_node *head= NULL, *tail= NULL, *q= NULL;
    int num;
    char name[200];
    int score;
    scanf ("%d", &num);
    while (num != 0)
    {
        q = (struct stud_node *)malloc (sizeof (struct stud_node));
        scanf ("%s%d", q->name, &q->score);
        q->num = num;
        q->next = NULL;
        if (head == NULL)
            head = q;
        else
            tail->next = q;
        tail = q;
        scanf ("%d", &num);
    }
    return head;
}
struct stud_node *deletelist( struct stud_node *head, int min_score )
{
    struct stud_node *p=head,*p1=head;
    if(head==NULL)
    {
        return NULL;
    }else
    {
        for(p=head;p!=NULL; )
        {
            if(p->score<min_score)
            {
                if(p==head)
                {
                    head=p->next;
                    p1 = head;
                    free(p);
                    p = head;
continue;
                }
                if(p->next == NULL )
                {

                    p1->next = NULL;
                    free(p);
                    return head;
                }
                p1->next=p->next;
                free(p);
                p=p->next;  continue;
            }
            p1=p;
            p=p->next;
        }
    }
    return head;
}

链表拼接

struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2)
{

    int n=0,a[10000];
    struct ListNode *p=list1;
    while(p!=NULL)
    {
        a[n]=p->data;
        p=p->next;
        n++;
    }
    p=list2;
    while(p!=NULL)
    {
        a[n]=p->data;
        p=p->next;
        n++;
    }
    int i,j,t;
    for(j=0;j<n-1;j++)
    {
        for(i=0;i<n-1-j;i++)
        {
              if(a[i]>a[i+1])
            {
            t=a[i];
            a[i]=a[i+1];
            a[i+1]=t;
            }
        }
    }
    struct ListNode *q=NULL,*head=NULL,*tail=NULL;
for(i=0;i<n;i++)
    {
            q=(struct ListNode *)malloc(sizeof(struct ListNode));
            q->data=a[i];
            q->next=NULL;
            if(head==NULL)
            {
                head=q;
            }else
            {
                tail->next=q;
            }
            tail=q;
    }
    return head;

}
posted on 2018-04-22 13:58  可乐汽水1  阅读(155)  评论(3编辑  收藏  举报