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

带头列表:

相关推荐
疯狂的沙粒2 分钟前
在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?
前端·uni-app·html
草莓熊Lotso5 分钟前
【数据结构初阶】--算法复杂度的深度解析
c语言·开发语言·数据结构·经验分享·笔记·其他·算法
小妖6666 分钟前
html 滚动条滚动过快会留下边框线
前端·html
KyollBM11 分钟前
【CF】Day75——CF (Div. 2) B (数学 + 贪心) + CF 882 (Div. 2) C (01Trie | 区间最大异或和)
c语言·c++·算法
heroboyluck20 分钟前
Svelte 核心语法详解:Vue/React 开发者如何快速上手?
前端·svelte
海的诗篇_21 分钟前
前端开发面试题总结-JavaScript篇(二)
开发语言·前端·javascript·typescript
琹箐31 分钟前
ant-design4.xx实现数字输入框; 某些输入法数字需要连续输入两次才显示
前端·javascript·anti-design-vue
程序员-小李32 分钟前
VuePress完美整合Toast消息提示
前端·javascript·vue.js
CodeOfCC1 小时前
c语言 封装跨平台线程头文件
linux·c语言·windows
Uyker1 小时前
从零开始制作小程序简单概述
前端·微信小程序·小程序