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;
}

带头列表:

相关推荐
IT_陈寒7 小时前
JavaScript性能优化:7个V8引擎内部原理帮你减少90%内存泄漏的实战技巧
前端·人工智能·后端
narukeu7 小时前
聊下 rewriteRelativeImportExtensions 这个 TypeScript 配置项
前端·javascript·typescript
开压路机7 小时前
模拟实现反向迭代器
前端·c++
San30.7 小时前
从 0 到 1 打造 AI 冰球运动员:Coze 工作流与 Vue3 的深度实战
前端·vue.js·人工智能
枫叶丹47 小时前
【Qt开发】Qt事件(一)
c语言·开发语言·数据库·c++·qt·microsoft
xiangzhihong87 小时前
Visual Studio 2026 正式发布,带来 AI 原生 IDE 和提升性能
前端
安_7 小时前
为什么 Vue 要用 npm run dev 启动
前端·vue.js·npm
LYFlied7 小时前
【每日算法】LeetCode 437. 路径总和 III
前端·算法·leetcode·面试·职场和发展
六便士的理想7 小时前
el-table实现滑窗列
前端·vue.js
阿蓝灬7 小时前
Chrome Lighthouse优化
前端·chrome