17周刷题(6~10)

编写int  fun(char  s[])函数,将八进制参数串s转换为十进制整数返回,若传入"77777"返回32767。

#include<string.h>
int  fun(char  s[]) {
	int i = strlen(s)-1, h = 0, q = 1;
	while (i>=0) {
		h += (s[i] - '0') * q;
		q *= 8;
		i--;
	}
	return h;
}

 初始化单列表附加头结点的两种方式:

编写void  init(struct  xs  **hd)函数,初始化单链表附加的头结点。
struct  xs{
        int  cj;
        struct  xs  *next;
};
void  main()
{
        struct  xs  *head=NULL;
        init(&head);
        create(head);
}

struct  xs* init() {
    struct xs* p = (struct xs*)malloc(sizeof(struct xs));
    p->next = NULL;
    return p;
}

编写struct  xs  *init()函数,初始化单链表附加的头结点。

struct  xs{
        int  cj;
        struct  xs  *next;
};
void  main()
{
        struct  xs  *head=init();
        create(head);
}

struct  xs* init() {
    struct xs* p = (struct xs*)malloc(sizeof(struct xs));
    p->next = NULL;
    return p;
}

编写void  create(struct  xs  *hd,int  a[],int  n)函数,根据数组a采用尾插法创建带附加头结点的单链表。
struct  xs{
        int  cj;
        struct  xs  *next;
};
void  main()
{
        int  a[10]={1,2,3,4,5,10,6,7,8,9};
        struct  xs  *head=(struct  xs  *)malloc(sizeof(struct  xs));
        head->next=NULL;
        create(head,a,10);
}

void  create(struct  xs* hd, int  a[], int  n) {
	struct xs* s = hd;
	int i = 0;
	for (i = 0; i < n; i++) {
		s->next = (struct xs*)malloc(sizeof(struct xs) * n);
		s->next->cj = a[i];
		s = s->next;
	}
	s = s->next=NULL;
}

编写int  fun(struct  xs  *hd)函数,返回带头结点单链表所有数据结点的和。
struct  xs{
        int  cj;
        struct  xs  *next;
};
void  main()
{
        ......
        printf("%d\n",fun(head));
        ......
}

int  fun(struct  xs* hd) {
    int s = 0;
    struct  xs* p = hd->next;
    while (p != NULL) {
        s += p->cj;
        p = p->next;
    }
    //p->next = NULL;
    return s;
}

带头列表:

相关推荐

  1. 13 数组

    2024-01-06 17:30:06       35 阅读
  2. 第1 Python语法基础

    2024-01-06 17:30:06       21 阅读
  3. 第2 Python列表、元组

    2024-01-06 17:30:06       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-06 17:30:06       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-06 17:30:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-06 17:30:06       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-06 17:30:06       20 阅读

热门阅读

  1. Node.js + Mysql 防止sql注入的写法

    2024-01-06 17:30:06       38 阅读
  2. 第一章 随机事件和概率

    2024-01-06 17:30:06       27 阅读
  3. 对象转成json,由于数据量过大压缩成.json.zip格式

    2024-01-06 17:30:06       33 阅读
  4. git 使用场景 本地分支 推送到 远程分支

    2024-01-06 17:30:06       50 阅读
  5. CentOS:安装gitlab

    2024-01-06 17:30:06       42 阅读
  6. 2023.12.31力扣每日一题——一年中的第几天

    2024-01-06 17:30:06       43 阅读
  7. pytest常用的第三方插件介绍

    2024-01-06 17:30:06       35 阅读
  8. 根据具体时间转换为一周前、几小时前格式

    2024-01-06 17:30:06       38 阅读
  9. Redis过期清理策略和内存淘汰机制

    2024-01-06 17:30:06       44 阅读
  10. 华为云服务介绍(二)

    2024-01-06 17:30:06       46 阅读