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

带头列表:

相关推荐
道可到6 分钟前
写了这么多代码,你真的在进步吗??—一个前端人的反思与全栈突围路线
前端
迎風吹頭髮13 分钟前
UNIX下C语言编程与实践59-UNIX TCP 数据传输:send 与 recv 函数的使用与数据处理
c语言·网络·unix
迎風吹頭髮17 分钟前
UNIX下C语言编程与实践55-TCP 协议基础:面向连接的可靠传输机制与三次握手、四次挥手
c语言·网络·unix
凡大来啦26 分钟前
v-for渲染的元素上使用ref
前端·javascript·vue.js
道可到28 分钟前
前端开发的生存法则:如何从“像素工人”进化为价值创造者?
前端
中微子38 分钟前
TypeScript 泛型与 ReturnType 详解
前端
我叫张得帅39 分钟前
从零开始的前端异世界生活--003--“探究Domain,DNS,Hosting”
前端
一大树42 分钟前
H5在不同操作系统与浏览器中的兼容性挑战及全面解决方案
前端·ios
中微子43 分钟前
TypeScript never 类型详解
前端
Strawberry_rabbit44 分钟前
路由配置中的svg图标如何匹配
前端·css