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

带头列表:

相关推荐
李火火的安全圈5 分钟前
基于Yakit、Wavely实现CVE-2025-55182(React Server Components(RSC)) 反序列化漏洞挖掘和POC编写
前端·react.js
枫叶丹411 分钟前
【Qt开发】Qt系统(十二)-> Qt视频
c语言·开发语言·c++·qt·音视频
浅念-14 分钟前
C语言文件操作
c语言·c++·经验分享·笔记·学习
Orange_sparkle15 分钟前
dify的web页面如何传入user用户信息进行对话,而不是uuid
前端·人工智能
你怎么知道我是队长23 分钟前
C语言---排序算法5---迭代归并排序法
c语言·算法·排序算法
Amumu1213824 分钟前
Vue Router 和 常用组件库
前端·javascript·vue.js
BackCatK Chen28 分钟前
第十三章 C 语言中的存储类别、链接与 内存管理
c语言·内存管理·static·extern·存储类别·malloc 动态内存
霍理迪29 分钟前
CSS移动端开发及less使用方法
前端·css
2601_9498574329 分钟前
Flutter for OpenHarmony Web开发助手App实战:HTML参考
前端·flutter·html
爱内卷的学霸一枚32 分钟前
现代前端工程化实践:从Vue到React的架构演进与性能优化(7000字深度解析)
前端·vue.js·react.js