第一步:定义一维指针数组,并赋予十二个月。
第二步:遍历数组,判断若是n在1到12中则将对应的地址赋给它。
第三步:返回a。html
char *getmonth( int n ) { char *mon[12]={"January","February","March","April","May","June","July","August","September","October","November","December"}; char *a; int i; if(n>0&&n<=12) { a=mon[n-1]; }else { a=NULL; } return a; }
中英文符号错误。node
第一步:将一个星期的七天赋给定义的一维指针数组。
第二步:经过strcmp函数判断其与数组元素是否相同,若是相同则返回i,不然返回-1.
第三步:结束。git
int getindex( char *s ) { char *a[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; int i; for(i=0;i<7;i++) { if(strcmp(s,a[i])==0) { return i; } } return -1; }
无数组
第一步:使用strlen函数来计算字符串长度,先定义最长max为0,使用for循环判断其字符串长度是否比max大。
第二步:若是长于max则将这个数赋予max。
第三步:返回max。函数
int max_len( char *s[], int n ) { int i,len=0,max=0; for(i=0;i<n;i++) { len=strlen(s[i]); if(len>max) { max=len; } } return max; }
没有给出i<n的条件。学习
第一步:新定义一个指针temp,若是s[i]与ch1相同则将其地址赋给temp跳出循环。
第二步:判断s[i]与ch2是否相等,若是不相等则输出s[i]不然输出s[i]并换行,返回temp。
第三步:结束。.net
char *match( char *s, char ch1, char ch2 ) { int i,j=strlen(s),k = 0,m=0; char *temp; for(i=0;s[i] != '\0';i++) { if(s[i] == ch1) { temp = &s[i]; j = i; break; } } for(i=j;s[i] != '\0';i++) { if(s[i] != ch2) { printf("%c",s[i]); } else { printf("%c\n",s[i]); return temp; } } printf("\n"); return temp; }
没有考虑s[i]不能是‘/0’的状况。设计
第一步:创建链表,用p->data==-1判链表结束,定义head为头文件,定义链表n放置输入元素。
第二步:创建的链表拆分为两个链表,历遍链表,按单双数将其放置于不一样链表。
第三步:将k链表赋给原链表,并返回h1链表。3d
struct ListNode *readlist() { int number; struct ListNode *p = NULL,*head = NULL,*tail = NULL; scanf("%d",&number); while(number!=-1&&number>0 ) { p = (struct ListNode*)malloc(sizeof(struct ListNode)); p->data = number; if(head == NULL) { head = p; } else { tail->next = p; } tail = p; scanf("%d",&number); } if(head == NULL) { return NULL; } tail->next = NULL; return head; } struct ListNode *getodd( struct ListNode **L ) { struct ListNode *p = *L,*head1 = NULL,*r = NULL,*L1 = NULL,*r1 = NULL; while(p!=NULL&&p->data>0) { if(p->data%2!=0) { if(head1 == NULL) { head1 = p; }else { r->next = p; } r = p; } else { if(L1 ==NULL) { L1 = p; } else { r1->next = p; } r1 = p; } p = p->next; } if(head1==NULL){ return NULL; } else { r->next = NULL; } if(L1==NULL) { *L = NULL; } else { r1->next = NULL; *L = L1; } return head1; }
第一步:创建一个链表并返回。
第二步:对链表进行历遍,当p->score小于min_score时将链表向前推动,去掉要删除项。
第三步:返回链表头文件。指针
struct stud_node *createlist() { struct stud_node *tail=NULL,*head=NULL,*p=NULL; int num=0,score=0; char name[20]; scanf("%d",&num); while(num!=0) { p=(struct stud_node*)malloc(sizeof(struct stud_node)); p->num=num; scanf("%s %d",p->name,&p->score); if(head==NULL) { head=p; }else { tail->next=p; } tail=p; scanf("%d",&num); p->next=NULL; } return head; } struct stud_node *deletelist( struct stud_node *head, int min_score ) { struct stud_node *ptr1=NULL,*ptr2=NULL; for(;head!=NULL;head=head->next) { if(head->score>=min_score) { if(ptr1==NULL) { ptr1=head; }else { ptr2->next=head; } ptr2=head; } } if(ptr1==NULL) { return NULL; }else { ptr2->next=NULL; } return ptr1; }
三、本题调试过程碰到的问题及解决办法
无。
四、提交列表
第一步:定义链表。
第二步:利用链表原为升序链表这一特色进行排序。
第三步:返回链表p。
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2) { struct ListNode *h,*p,*i,*k; h=(struct ListNode*)malloc(sizeof(struct ListNode)); p=h; i=list1; k=list2; while(i!=NULL&&k!=NULL) { if(i->data<k->data) { p->next=i; i=i->next; } else { p->next=k; k=k->next; } p=p->next; } while(i) { p->next=i; i=i->next; p=p->next; } while(k) { p->next=k; k=k->next; p=p->next; } p->next=NULL; return h->next; }
学习了链表。
https://coding.net/u/zhouxuan12/p/123/git?public=true
http://www.cnblogs.com/lixiaojing/p/8760462.html
http://www.cnblogs.com/fengzx/p/8781906.html
http://www.cnblogs.com/dx2017/p/8781858.html