17周刷题(6~10)

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

cpp 复制代码
#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);

}

cpp 复制代码
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);

}

cpp 复制代码
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);

}

cpp 复制代码
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));

......

}

cpp 复制代码
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;
}

带头列表:

相关推荐
css趣多多6 分钟前
ctx 上下文对象控制新增 / 编辑表单显示隐藏的逻辑
前端
_codemonster12 分钟前
Vue的三种使用方式对比
前端·javascript·vue.js
寻找奶酪的mouse13 分钟前
30岁技术人对职业和生活的思考
前端·后端·年终总结
梦想很大很大21 分钟前
使用 Go + Gin + Fx 构建工程化后端服务模板(gin-app 实践)
前端·后端·go
We་ct23 分钟前
LeetCode 56. 合并区间:区间重叠问题的核心解法与代码解析
前端·算法·leetcode·typescript
张3蜂30 分钟前
深入理解 Python 的 frozenset:为什么要有“不可变集合”?
前端·python·spring
无小道30 分钟前
Qt——事件简单介绍
开发语言·前端·qt
广州华水科技32 分钟前
GNSS与单北斗变形监测技术的应用现状分析与未来发展方向
前端
code_YuJun1 小时前
corepack 作用
前端
千寻girling1 小时前
Koa.js 教程 | 一份不可多得的 Node.js 的 Web 框架 Koa.js 教程
前端·后端·面试