嵌入式工程师掌握编程的几个要素
来源:嵌入式专栏 发布时间:2024-05-11
分享至微信
![](/_nuxt/img/wechaticon.317e48d.png)
要用C语言的思维方式来进行程序的构架构建 要有良好的C语言算法基础,以此来实现程序的逻辑构架 灵活运用C语言的指针操作
用C语言的思维方式进行程序构架构建
用C语言进行逻辑实现
/* 读入问题条件 */
printf("input total num:");
scanf("%d", &n);
printf("from which num begin:");
scanf("%d", &k);
if(k>n||k==0)
{
printf("please input the right begin num");
return 1;
}
printf("input the out num:");
scanf("%d", &m);
if(m>n||m==0)
{
printf("please input the right del num");
return 2;
}
typedef struct node
{
int data;
struct node *next;
}linklist;
/* 创建循环链表,头节点也存信息 */
head = (linklist*) malloc(sizeof(linklist));
p = head;
p->data = 1;
p->next = p;
/* 初始化循环链表 */
for (i = 2; i <= n; i++)
{
s = (linklist*) malloc(sizeof(linklist));
s->data = i;
s->next = p->next;
p->next = s;
p = p->next;
}
p=head;
for (i = 1; i <= k; i++)
{
p = p->next;
}
/*保存节点总数*/
total = n;
printf("\nthe out num:");
q = head;
/* 只剩一个节点时停止循环 */
while (total != 1)
{
/* 报数过程,p指向要删除的节点 */
for (i = 1; i < m; i++)
{
p = p->next;
}
/* 打印要删除的节点序号 */
printf("[%d] ", p->data);
/* q 指向 p 节点的前驱 */
while (q->next != p)
{
q = q->next;
}
/* 删除 p 节点 */
q->next = p->next;
/* 保存被删除节点指针 */
s = p;
/* p 指向被删除节点的后继 */
p = p->next;
/* 释放被删除的节点 */
free(s);
/* 节点个数减一 */
total--;
}
/* 打印最后剩下的节点序号 */
printf("\n\nthe last num:[%d] \n\n", p->data);
free(p);
}
使用C语言的指针
linklist *head, *p, *s, *q;
![](https://img.icspec.com/article/9a1fc6140f3e11ef9f3400163e1eb85a.jpeg)
![](https://img.icspec.com/article/9a76911a0f3e11ef884400163e1eb85a.png)
[ 新闻来源:嵌入式专栏,更多精彩资讯请下载icspec App。如对本稿件有异议,请联系微信客服specltkj]
存入云盘 收藏
举报
全部评论
暂无评论哦,快来评论一下吧!
![](https://img.icspec.com/common/images/artbrand.png)
![](https://img.icspec.com/xxt/avatar/9e8c32953d3a4ac887530700d0df3e22.png)
嵌入式专栏
作者黄工【strongerHuang】,专注分享嵌入式软件、硬件、工具等相关内容,通过专栏形式精选并整理更多嵌入式相关教程。
查看更多
相关文章
TI:印度拥有丰富的工程师人才库
一周前
阻碍AGI 研究,谷歌工程师指责OpenAI!
2024-06-13
嵌入式项目需要写哪些文档?
2024-05-06
嵌入式项目延期有哪些原因?
2024-05-11
嵌入式项目常见管理模式
2024-05-06
热门搜索