程序设计15
- 问题15_1
- 代码15_1
- 结果15_1
 
- 问题15_2
- 代码15_2
- 结果15_2
 
- 问题15_3
- 代码15_3
- 结果15_3
 
问题15_1
在 m a i n main main 函数中将多次调用 f u n fun fun 函数,每调用一次,输出链表尾部结点中的数据,并释放该结点,使链表缩短。
代码15_1
#include<stdio.h>
#include<stdlib.h>
#define N 8
typedef struct list{
	int data;
	struct list *next;
}SLIST;
void fun(SLIST *p){
	SLIST *t, *s;
	t = p->next;
	s = p;
	while(t->next!=NULL){
		s = t;
		t = t->next;
	}
	printf("%d", t->data);
	s->next = NULL;
	free(t);
}
SLIST *creatlist(int *a){
	SLIST *h, *p, *q;
	int i;
	h = p = (SLIST *)malloc(sizeof(SLIST));
	for(i=0; i<N; i++){
		q = (SLIST *)malloc(sizeof(SLIST));
		q->data = a[i];
		p->next = q;
		p = q;
	}
	p->next = 0;
	return h;
}
void outlist(SLIST *h){
	SLIST *p;
	p = h->next;
	if(p==NULL)
		printf("\nThe list is NULL!\n");
	else
	{
		printf("\nHead");
		do{
			printf("->%d", p->data);
			p = p->next;
		}while(p!=NULL);
		printf("->End\n");
	}
}
void main(void){
	SLIST *head;
	int a[N] = {11, 12, 15, 18, 19, 22, 25, 29};
	head = creatlist(a);
	printf("\nOutput from head:\n");
	while(head->next!=NULL){
		fun(head);
		printf("\n\n");
		printf("\nOutput from head again:\n");
		outlist(head);
	}
}
结果15_1

问题15_2
        函数  
     
      
       
       
         f 
        
       
         u 
        
       
         n 
        
       
      
        fun 
       
      
    fun 的功能是:将字符串中的字符逆序输出,但不改变字符串中的内容。
         例如,若字符串为  
     
      
       
       
         a 
        
       
         b 
        
       
         c 
        
       
         d 
        
       
      
        abcd 
       
      
    abcd,则应输出  
     
      
       
       
         : 
        
       
         d 
        
       
         c 
        
       
         b 
        
       
         a 
        
       
      
        :dcba 
       
      
    :dcba。
代码15_2
#include<stdio.h>
void fun(char *a){
	if(*a){
		fun(a+1);
		printf("%c", *a);
	}
}
void main(void){
	char s[10] = "abcd";
	printf("处理前字符串 = %s,\n处理后字符串 = ", s);
	fun(s);
	printf("\n");
}
结果15_2

问题15_3
         编写函数  
     
      
       
       
         f 
        
       
         u 
        
       
         n 
        
       
      
        fun 
       
      
    fun ,功能是:比较字符串的长度,不得使用  
     
      
       
       
         C 
        
       
      
        C 
       
      
    C 语言提供的求字符串的长度函数,函数返回较长的字符串。若两个字符长度相同,则返回第一个字符串。
         例如,输入  
     
      
       
       
         " 
        
       
         b 
        
       
         e 
        
       
         i 
        
       
         j 
        
       
         i 
        
       
         n 
        
       
         g 
        
       
         " 
        
       
         < 
        
       
         C 
        
       
         R 
        
       
         > 
        
       
         " 
        
       
         s 
        
       
         h 
        
       
         a 
        
       
         n 
        
       
         g 
        
       
         h 
        
       
         a 
        
       
         i 
        
       
         " 
        
       
         < 
        
       
         C 
        
       
         R 
        
       
         > 
        
       
      
        "beijing"<CR>"shanghai"<CR> 
       
      
    "beijing"<CR>"shanghai"<CR> ( 
     
      
       
       
         < 
        
       
         C 
        
       
         R 
        
       
         > 
        
       
      
        <CR> 
       
      
    <CR> 为  
     
      
       
       
         E 
        
       
         n 
        
       
         t 
        
       
         e 
        
       
         r 
        
       
      
        Enter 
       
      
    Enter 键),函数将返回  
     
      
       
       
         " 
        
       
         s 
        
       
         h 
        
       
         a 
        
       
         n 
        
       
         g 
        
       
         h 
        
       
         a 
        
       
         i 
        
       
         " 
        
       
      
        "shanghai" 
       
      
    "shanghai" 。
代码15_3
#include<stdio.h>
char *fun(char *s, char *t){
	int i, j;
	for(i=0; s[i]!='\0'; i++);
	for(j=0; t[j]!='\0'; j++);
	if(i<=j)
		return t;
	else
		return s;
}
void main(void){
	char a[20], b[20];
	printf("Input 1th string:");
	gets(a);
	printf("Input 2th string:");
	gets(b);
	printf("%s", fun(a, b));
}
结果15_3



















