广工 AnyviewC C语言习题 第九章

Anyview 第九章


/**********

【习题9.023】结构体类型定义如下:

struct date{int year; int month; int day;}; //定义日期结构体类型
struct student
{  char name[20]; 
   struct date birth; //出生日期
};

结构体数组s存储了n个人的名字和出生日期。写一函数,求这n个人中年龄最大
(即出生日期最小)者的姓名。

char *oldest(student s[], int n)
{
    struct student *s0,*od;
    
    for (od=s,s0=s+1;s0<s+n;s0++)
    {
        if (od->birth.year>(s0)->birth.year)
        {
            *od=*(s0);
        }
        else if (od->birth.year == (s0)->birth.year)
        {
            if (od->birth.month>(s0)->birth.month)
                *od=*(s0);
            else if (od->birth.month == (s0)->birth.month)
            {
                if (od->birth.day>(s0)->birth.day)
                    *od=*(s0);
            }
        }
    }
    return (od->name);
}

【习题9.033】日期和链表结点的结构体类型定义如下:

    struct date{int year; int month; int day;}; //日期结构体类型
    struct studentNode    //链表结点的结构体类型
    {  char name[10];     //人名
       struct date birth; //出生日期
       struct studentNode *next;
    };

结构体链表L存储了n个人的名字和出生日期。写一函数,求这n个人中
年龄最大(即出生日期最小)者的名字。

char *oldest(struct studentNode *L)
/* 若L是空表,则返回空指针null
   否则返回表中年龄最大者的名字
 */
{
    studentNode *p,*od;
    p=L;od=L;
    while (p!=NULL)
    {
        if (od->birth.year>p->birth.year)
        {
            *od=*p;
        }
        else if (od->birth.year == p->birth.year)
        {
            if (od->birth.month>p->birth.month)
                *od=*p;
            else if (od->birth.month == p->birth.month)
            {
                if (od->birth.day>p->birth.day)
                    *od=*p;
            }
        }
        p=p->next;
    }
    return (od->name);
}
#include <stdio.h> 
#include <string.h>

struct date
{
	int year; 
	int month; 
	int day;
}; //定义日期结构体类型

struct studentNode    //链表结点的结构体类型
    {  char name[10];     //人名
       struct date birth; //出生日期
       struct studentNode *next;
    };

char *oldest(struct studentNode *L)
/* 若L是空表,则返回空指针null
   否则返回表中年龄最大者的名字
 */
{
	 studentNode *p,*od;
    p=L+1;od=L;
    while (p->next!=NULL)
    {
        if (od->birth.year>p->birth.year)
		{
			*od=*p;
		}
		else if (od->birth.year == p->birth.year)
		{
			if (od->birth.month>p->birth.month)
				*od=*p;
			else if (od->birth.month == p->birth.month)
			{
				if (od->birth.day>p->birth.day)
					*od=*p;
			}
		}
        p=p->next;
    }
    return (od->name);
}
 
 //#define n 10
 
int main()
{
	int i;
	char o[20];
	char *old=o;
	struct studentNode a[3];
	for (i=0;i<3;i++)
	{
		printf("请输入同学%d的姓名\n",i+1);
		scanf("%s",&a[i].name);
		printf("请输入同学%d的生日\n",i+1);
		scanf("%d%d%d",&a[i].birth.year,&a[i].birth.month,&a[i].birth.day); 
		a[i]
	}
	
	old=oldest(a,3);
	
	printf("%s\n",old);
	return 0;
}

/**********

【习题9.063】结构体类型定义如下:

struct course
{  int   cID;       //课程号,取值0~99
   char  name[10];  //课程名
   float credit;    //学分,取值0~5
   int   semester;  //学期,取值1~8
};

结构体数组c存储了n门课程的信息。写一函数,求学期s的总学分。
**********/

float creditSum(struct course c[], int n, int s)
{
    int i=0;
	float sum=0;
	
	 for (i=0;i<n;i++)
	 {
	 	if (c[i].semester==s)
		 	sum+=c[i].credit;	
	 }
	 return sum;
}

/**********

【习题9.073】课程链表结点的结构体类型定义如下:

    struct courseNode   //课程链表结点的结构体类型
    {  int   cID;       //课程号,取值0~99
       char  name[10];  //课程名
       float credit;    //学分,取值0~5
       int   semester;  //学期,取值1~8
       struct courseNode *next;
    };

结构体链表Lc存储了各学期多门课程的信息。写一函数,求学
期s的总学分。
**********/

float creditSum(struct courseNode *Lc, int s)
/* 若Lc是空表,则返回0;
   否则返回学期s的总学分
 */
{
     struct courseNode *p;
     float sum=0.0;
    if (NULL==Lc)
    {
        return 0.0; //注意这里如果是0可能会报错(类型不匹配)
    }        
    else
    {
        p=Lc;
        while (p!=NULL)
        {
            if (p->semester==s)
            sum+=p->credit;
            p=p->next;
        }
    }
    return sum;
}

/**********

【习题9.133】日期和结构体类型定义如下:

struct date{int year; int month; int day;}; //日期结构体类型
struct student    //结构体类型
{  char name[10];     //人名
   struct date birth; //出生日期
};

结构体数组s存储了n个人的名字和出生日期。写一函数,由数组s中n个人
的信息及其顺序构造相应的链表。链表的结点的结构体类型定义如下:
struct studentNode //结构体类型
{ char name[10]; //人名
struct date birth; //出生日期
struct studentNode *next
};
**********/

struct studentNode *CreateLinkList(struct student s[], int n)
{
    struct studentNode* L,*p0;
    L = (studentNode*) malloc (sizeof(studentNode));
    L->next=NULL;
    
    struct student *x;
    if (NULL==s || n==0) return NULL;    
     x=s;       
        L->birth.year=x->birth.year;
        L->birth.month=x->birth.month;
        L->birth.day=x->birth.day;
        strcpy(L->name,x->name);
        x++;
        p0=L;
    for (;x<s+n;x++)
    {
        struct studentNode* p;
        p = (studentNode*) malloc (sizeof(studentNode));
        p->birth.year=x->birth.year;
        p->birth.month=x->birth.month;
        p->birth.day=x->birth.day;
        strcpy(p->name,x->name);
        p->next=p0->next;
        p0->next=p;
        p0=p0->next;        
    }   
    return L;
}

/**********

【习题9.173】课程链表结点的结构体类型定义如下:

struct courseNode   //课程链表结点的结构体类型
{  int   cID;       //课程号,取值0~99
   char  name[10];  //课程名
   float credit;    //学分,取值0~5
   int   semester;  //学期,取值1~8
   struct courseNode *next;
};

结构体链表Lc存储了多门课程的信息。写一函数,将课程号为c的
课程的学分修改为t。
**********/

struct courseNode *creditChange(struct courseNode *Lc, int c, float t)
/* 若课程c不存在,则修改不成功,返回null;
   否则修改该课程的学分为t,返回指向该课程结点的指针。
 */
{
    struct courseNode*p;
    p=Lc;
    while (p!=NULL)
    {
        if (p->cID==c)
        {
           p->credit=t;
           return p;
        }
        p=p->next;
    }
    return NULL;
}

/**********

【习题9.183】课程链表结点的结构体类型定义如下:

struct courseNode   //课程链表结点的结构体类型
{  int   cID;       //课程号,取值0~99
   char  name[10];  //课程名
   float credit;    //学分,取值0~5
   int   semester;  //学期,取值1~8
   struct courseNode *next;
};

结构体链表Lc存储了多门课程的信息。写一函数,将课程号为c的
课程结点删除。
**********/

struct courseNode *deleteCourse(struct courseNode **Lc, int c)
/* 若在链表Lc中课程c不存在,则删除不成功,返回null;
   否则从链表Lc中删除该课程结点,并返回指向该课程结点的指针。
 */
{
    struct courseNode*p;
    p=*Lc;
    while (p!=NULL)
    {
        if (p->next->cID==c)
        {
            struct courseNode*t=p->next;
           p->next=p->next->next;
           return t;
        }
        if (p->next==NULL)
        {
            if (p->cID==c)
            {               
               Lc[0]=NULL;
               return p;
            }
        }
        p=p->next;
    }
    return NULL;
}

/**********

【习题9.302】单向链表的结点类型定义如下:

struct node{
  char  ch;
  struct node *next;
};

编写函数,对单向链表L实现就地逆置,即将所有结点
的指针反向,原链头当作链尾,原链尾当作链头,并返
回逆置后链表的头指针。
**********/
struct node *inverse(struct node *L)
{
struct node *t,*p;
p=L->next;
L->next=NULL;

while (p!=NULL)
{

    t=p->next;
    p->next=L;
    L=p;
    p=t;
}
return L;

}

/**********

【习题9.352】单向链表的结点类型定义如下:

struct node{
  char  ch;
  struct node *next;
};

编写函数,对单向链表L实现排序,即按结点的ch值,
从小到大重构链表L,并返回排序后的链表的头指针。
**********/

struct node *sorting(struct node *L)
/* 对单向链表L实现从小到大排序,
   并返回重构后的链表的头指针。
 */
{
   struct node *p=L,*tq;
   int n=0,i=0;
   if (NULL==L) return NULL;
char t;
   while (p!=NULL)
   {
        p=Lp->next;
    
    n++;
   }
   p=L;
   for (t=L;t!=NULL;t;i<n-1;i++)
    {
         forp=L;
        while (p=L->next;p!=NULL;p++)
         {
            q=p->next;
            if (t(p->ch) > p(q->ch)
)
            {
            char tmp=t    t=p->ch;
            t    p->ch=pq->ch;
            p    q->ch=tmp;
            }
         p=p->next;
         t=p 
                p=p->next;
         }
    }
    return L;
    
}
  • 6
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
9.25实现下列函数: int Search(SSTable s, KeyType k); /* Index the element which key is k */ /* in StaticSearchTable s. */ /* Return 0 if x is not found. */ 静态查找表的类型SSTable定义如下: typedef struct { KeyType key; ... ... // 其他数据域 } ElemType; typedef struct { ElemType *elem; int length; } SSTable; int Search(SSTable a, KeyType k) /* Index the element which key is k */ /* in StaticSearchTable s. */ /* Return 0 if x is not found. */ { int i; if(a.length==0) return ERROR; //先判断a是否为空 a.elem[a.length+1].key=k; for(i=1;a.elem[i].key>k;i++) if(i>=a.length||a.elem[i].key<k) //不能忽略i=a.length这种情况 return ERROR; return i; } /* { int i; a.elem[a.length+1].key=k; for(i=1;a.elem[i].key>k;i++) if(i>a.length||a.elem[i].key<k) return ERROR; return i; } */ 9.26② 试将折半查找算法改写成递归算法。 实现下列函数: int BinSearch(SSTable s, int low, int high, KeyType k); /* Index the element which key is k */ /* in StaticSearchTable s. */ /* Return 0 if x is not found. */ 静态查找表的类型SSTable定义如下: typedef struct { KeyType key; ... ... // 其他数据域 } ElemType; typedef struct { ElemType *elem; int length; } SSTable; int BinSearch(SSTable s, int low, int high, KeyType k) /* Index the element which key is k */ /* in StaticSearchTable s. */ /* Return 0 if x is not found. */ { int mid; if(low<=high) { mid=(low+high)/2; if(s.elem[mid].key==k) return mid; if(s.elem[mid].key<k) return BinSearch(s,mid+1,high,k); if(s.elem[mid].key>k) return BinSearch(s,low,high-1,k); } return 0; } /* { int mid; if(low<=high) { mid=(low+high)/2; if(s.elem[mid].key<k) return BinSearch(s,mid+1,high,k); else return BinSearch(s,low,high-1,k); } return 0; } */ 9.31④ 试写一个判别给定二叉树是否为二叉排序树的算法,设此二叉树以二叉链表作存储结构。且树中结点的关键字均不同。 实现下列函数: Status IsBSTree(BiTree t); /* 判别给定二叉树t是否为二叉排序树。*/ /* 若是,则返回TRUE,否则FALSE */ 二叉树的类型BiTree定义如下: typedef struct { KeyType key; ... ... // 其他数据域 } ElemType; typedef struct BiTNode { ElemType data; BiTNode *lchild,*rchild; }BiTNode, *BiTree; KeyType predt=-32767; Status IsBSTree(BiTree t) /* 判别给定二叉树t是否为二叉排序树。*/ /* 若是,则返回TRUE,否则FALSE */ { if( t )//&& ! ( t->lchild || t->rchild ) )//空树和叶子不用判断 { if( t->lchild && ( t->data.key < t->lchild->data.key ) )//左孩子不空,左孩子的key比本身的大 return FALSE; else if( t->rchild && ( t->data.key > t->rchild->data.key ) )//右孩子不空,右孩子的key比本身的大 return FALSE; else if( !IsBSTree( t->lchild ) )//判断左子树 return FALSE; else if( !IsBSTree( t->rchild ) )//判断右子树 return FALSE; } return TRUE; } /* { if(!t) return OK; if(t&&!t->lchild&&!t->rchild) return OK; else { if(t->lchild->data.key<t->data.key) IsBSTree(t->lchild); if(t->lchild->data.key>=t->data.key) return ERROR; if(t->rchild->data.key>t->data.key) IsBSTree(t->rchild); else return ERROR; return OK; } } */ 9.33③ 编写递归算法,从大到小输出给定二叉排序树中所有关键字不小于x的数据元素。要求你的算法的时间复杂度为O(log2n+m),其中n为排序树中所含结点数,m为输出的关键字个数。 实现下列函数: void OrderOut(BiTree t, KeyType x, void(*visit)(TElemType)); /* Output is to use visit(t->data); */ 二叉树的类型BiTree定义如下: typedef struct { KeyType key; ... ... // 其他数据域 } ElemType; typedef struct BiTNode { ElemType data; BiTNode *lchild,*rchild; }BiTNode, *BiTree; void OrderOut(BiTree t, KeyType x, void(*visit)(TElemType)) /* Output is to use visit(t->data); */ { if(t->rchild) OrderOut(t->rchild,x,visit); if(t->data.key>=x) visit(t->data); if(t->lchild)OrderOut(t->lchild,x,visit); } /* { if(t->rchild) OrderOut(t->rchild,x); if(t->data<x) exit(); visit(x); if(t->lchild)OrderOut(t->lchild,x); } */ 9.44④ 已知某哈希表的装载因子小于1,哈希函数 H(key)为关键字(标识符)的第一个字母在字母表中的序号,处理冲突的方法为线性探测开放定址法。试编写一个按第一个字母的顺序输出哈希表中所有关键字的算法。 实现下列函数: void PrintKeys(HashTable ht, void(*print)(StrKeyType)); /* 依题意用print输出关键字 */ 哈希表的类型HashTable定义如下: #define SUCCESS 1 #define UNSUCCESS 0 #define DUPLICATE -1 typedef char StrKeyType[4]; typedef struct { StrKeyType key; void *any; } HElemType; int hashsize[] = { 7,11,17,23,29,37,47 }; typedef struct { HElemType elem[MAXLEN]; int count; int sizeindex; } HashTable; int H(char *s)//求Hash函数 { if( s[0] ) return s[0]-'A'+1; //求关键字第一个字母的字母序号(小写) else return 0; } void PrintKeys(HashTable ht, void(*print)(StrKeyType)) /* 依题意用print输出关键字 */ { int i,j; for( i = 1; i <= 26; i++ ) { for( j = (i-1)%hashsize[ht.sizeindex]; ht.elem[j].key[0]; ) { if( H ( ht.elem[j].key ) == i ) print(ht.elem[j].key); j = (j+1)%hashsize[ht.sizeindex]; } } } /* void PrintKeys(HashTable ht, void(*print)(StrKeyType)) /* 依题意用print输出关键字 { int i,j; for(i=1;i<=26;i++) for(j=i;ht.elem[j].key;j=(j+1)%MAXLEN) if(H(ht.elem[j].key)==i) print(ht); } int H(char *s) { if(s) return s[0]-96; else return 0; } */ 9.45③ 假设哈希表长为m,哈希函数为H(x),用链地址法处理冲突。试编写输入一组关键字并建造哈希表的算法。 实现下列函数: int BuildHashTab(ChainHashTab &H, int n, HKeyType es[]) /* 直接调用下列函数 */ /* 哈希函数: */ /* int Hash(ChainHashTab H, HKeyType k); */ /* 冲突处理函数: */ /* int Collision(ChainHashTab H, HLink &p); */ { int i = 0,l,flag; HLink p,node; while( es[i] ) { l = Hash( H, es[i] ); node = ( HLink )malloc( sizeof( HNode ) ); node->data = es[i]; node->next = NULL; i++; if( !H.elem[l] ) H.elem[l] = node; else { flag = 0; p = H.elem[l]; if( p->data == node->data ) flag = 1; while( Collision( H, p ) ) if( p->data == node->data ) { flag = 1; break; } if( !flag ) { p = H.elem[l]; node->next = p; H.elem[l] = node; } } } } "+userLink+""; $('miniAd').show(); } }, onFailure: function(){} }}); } showMiniAd();

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值