第三次作业

6-1 输出月份英文名
1.思路
char十二个月的英文名指针数组
如果数字在一到十二输出月份名,else return null
2.代码

char *getmonth( int n )
{
	char *month[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
  if(n<1||n>12)
  {
    return NULL;
  }
  else{
    return *(month+n-1);
  }
}

3.错误
数组首下标为0
6-2 查找星期
1.思路
char星期数组,day【i】如果s=day[i],a=i
2.代码

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

3.错误

6-3 计算最长的字符串长度
1.思路
for循环遍历数组,比较最长串
2.代码

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

3.错误

6-4 指定位置输出字符串
1.思路

6-1 奇数值结点链表

struct ListNode *readlist()
{
    int data;
    struct ListNode *head=NULL;
    struct ListNode *p;
    while(scanf("%d",&data)&&data!=-1)
    {
        struct ListNode *q=(struct ListNode*)malloc(sizeof(struct ListNode));
        if(q!=NULL)
        {
            q->data=data;
            q->next=NULL;
        }
        else exit(1);
        if(head!=NULL)
        {
             p->next=q;
        }
        else head=q;
        p=q;
    }
    return head;
}
struct ListNode *getodd( struct ListNode **L )
{
    struct ListNode *head0=NULL,*head1=NULL,*p0,*p1;
    while((*L)!=NULL)
    {
        int data=(*L)->data;
        struct ListNode *q=(struct ListNode*)malloc(sizeof(struct ListNode));
        if(data%2)
        {
            if(q!=NULL)
            {
                q->data=data;
                q->next=NULL;
            }
            else exit(1);
            if(head1!=NULL)
            {
                p1->next=q;
            }
            else head1=q;
            p1=q;
        }
        else
        {
            if(q!=NULL)
            {
                q->data=data;
                q->next=NULL;
            }
            else exit(1);
            if(head0!=NULL)
            {
             p0->next=q;
            }
            else head0=q;
            p0=q;
        }
        *L=(*L)->next;
    }
    *L=head0;
    return head1;
}

6-2 学生成绩链表处理

struct stud_node *createlist()
{
    struct stud_node *head, *tail, *q;
    head = tail = NULL;
    int num;
    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 *ptr1, *ptr2;
    while (head != NULL && head->score < min_score)
    {
        ptr2 = head;
        head = head->next;
        free(ptr2);
    }
    if (head == NULL)
        return NULL;
    ptr1 = head;
    ptr2 = head->next;
    while (ptr2 != NULL)
    {
        if (ptr2->score < min_score) {
            ptr1->next = ptr2->next;
            free(ptr2);
        }
        else
            ptr1 = ptr2;
        ptr2 = ptr1->next;
    }
    return head;
} 

6-3 链表拼接

struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2)
{
    int num = 0;
    int temp[100];
    struct ListNode  *p = list1;
    while(p != NULL)
    {
        temp[num] = p->data;
        num++;
        p = p->next;
    }
    p = list2;
    while(p != NULL)
    {
        temp[num] = p->data;
        num++;
        p = p->next;
    }
    int i,j;
    for(i = 0; i < num; i++)
        for(j = i + 1; j < num; j++)
        {
            if(temp[i] > temp[j])
            {
                int t;
                t = temp[i];
                temp[i] = temp[j];
                temp[j] = t;
            }
        }
      struct ListNode  *newlist = NULL;
      struct ListNode  *endlist = NULL;
      struct ListNode  *q;
      for(i = 0; i < num; i++)
      {
          q = (struct ListNode  *)malloc(sizeof(struct ListNode));
          q->data = temp[i];
          if(newlist == NULL)
          {
              newlist = q;
              newlist->next = NULL;
          }
            if(endlist != NULL)
         {
            endlist->next = q;
         }
         endlist = q;
         endlist->next = NULL;
      }
      return newlist;
}

总结
本周学习了二维数组
链表没太学明白
点评
http://www.cnblogs.com/wwb986187/p/8906109.html
http://www.cnblogs.com/qinbaoyan/
http://www.cnblogs.com/moxiaoshuai/p/8893220.html

posted on 2018-04-22 21:22  江北宋天霸  阅读(132)  评论(3编辑  收藏  举报