C语言程序设计15

问题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

Result_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

Result_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

Result_Q15_3

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值