网站整站开发项目亮点,自动秒收录网,用花生棒做网站快吗,百度导航如何设置公司地址C语言-数据结构与算法
C语言基础
因为之前写算法都是用C#xff0c;也有了些C基础#xff0c;变量常量数据类型就跳过去吧。
首先是环境#xff0c;学C时候用Clion#xff0c;C语言也用它写吧~
新建项目#xff0c;选C执行文件#xff0c;语言标准。。。就先默认C99吧…C语言-数据结构与算法
C语言基础
因为之前写算法都是用C也有了些C基础变量常量数据类型就跳过去吧。
首先是环境学C时候用ClionC语言也用它写吧~
新建项目选C执行文件语言标准。。。就先默认C99吧反正是测试环境应该问题不大 直接运行一手 嗯。。JB家的新UI。。真是。。。。。。。一言难尽
指针
好像最开始学C开始就一直没玩明白指针毕竟一用数组链表就直接上STL库也不太用得到指针
新的学习阶段从指针开始 #include stdio.hint main() {int a 123;int * p a;printf(a%d\n, a);printf(*p%d\n, *p);printf(a的地址%p\n, a);printf(指针p指向的地址%p\n, p);printf(指针p自身的地址%p\n, p);return 0;
}如果你的Clion输出乱码按照以下顺序配置即可解决
文件——设置——编辑器——文件编码都改成UTF-8 然后点击确定回到代码页面点击最下方UTF-8选择GBK 再点击转换重新运行即可解决 结构体
使用typedef可以给结构体指定别名
#include stdio.h
#include string.h
typedef struct Book {char isbn[50];char name[20];int price;
}B;int main() {// 声明struct Book b;// 初始化b.price 20;strcpy(b.name, 笑场);// 声明同时初始化struct Book a {122333123, 冷玚, 45};B c {122334233123, 乡土中国, 65};return 0;
}属性声明的同时进行变量声明
struct Book {char isbn[50];char name[20];int price;
} stu;属性声明的同时进行变量声明及初始化
struct Book {char isbn[50];char name[20];int price;
} stu {1531, 宇宙超度指南, 46};如果只需要声明一次可以省略结构体标记
struct {char isbn[50];char name[20];int price;
} stu {1531, 宇宙超度指南, 46};链表 链表结构体定义
typedef struct node {int data;struct node *next;
} node;创建单链表
node *createList() {node *head (node *) malloc(sizeof(node));if (head NULL) return NULL; // 若内存申请失败指针会为空一般情况下不会申请失败head-data 0;head-next NULL;return head;
}头结点头指针指向的结点
首元结点头结点后面的第一个结点
插入新结点 插入新结点的时候要注意一定先抓住后边的那个结点再修改前边的那个结点的指针指向。
必须时刻有指针指向后边结点的位置不能让后边的结点丢了
删除指定位置结点
node *deleteNode(node *head, int pos) {node *currentNode head;// 如果插入位置比链表长为非法操作。头结点data存储链表长度if (pos currentNode-data) return NULL;for (int i 0; i pos; i) {currentNode currentNode-next;}node *temp currentNode-next;currentNode-next currentNode-next-next;// 释放内存free(temp);// 链表长度减一head-data--;return head;
}输出链表
void printList(node *head) {// 跳过头结点数据node *currentNode head-next;while (currentNode ! NULL) {if (currentNode-next NULL) {// 是最后一个结点的话不输出箭头printf(%d, currentNode-data);} else {printf(%d-, currentNode-data);}currentNode currentNode-next;}printf(\n);
}测试链表相关方法
#include stdio.h
#include stdlib.htypedef struct node {int data;struct node *next;
} node;// 创建单链表
node *createList() {node *head (node *) malloc(sizeof(node));if (head NULL) return NULL;head-data 0;head-next NULL;return head;
}// 插入新结点
node *insertNode(node *head, int data, int pos) {node *currentNode head;// 如果插入位置比链表长为非法操作。头结点data存储链表长度if (pos currentNode-data) return NULL;for (int i 0; i pos; i) {currentNode currentNode-next;}// 新建结点node *newNode (node *) malloc(sizeof(node));newNode-data data;// 牵住当前位置下一个结点newNode-next currentNode-next;// 牵住当前位置上一个结点currentNode-next newNode;// 链表长度加一head-data;return head;
}// 删除结点
node *deleteNode(node *head, int pos) {node *currentNode head;// 如果插入位置比链表长为非法操作。头结点data存储链表长度if (pos currentNode-data) return NULL;for (int i 0; i pos; i) {currentNode currentNode-next;}node *temp currentNode-next;currentNode-next currentNode-next-next;// 释放内存free(temp);// 链表长度减一head-data--;return head;
}// 遍历列表
void printList(node *head) {// 跳过头结点数据node *currentNode head-next;while (currentNode ! NULL) {if (currentNode-next NULL) {// 是最后一个结点的话不输出箭头printf(%d, currentNode-data);} else {printf(%d-, currentNode-data);}currentNode currentNode-next;}printf(\n);
}int main() {node *l createList();insertNode(l, 1, 0);insertNode(l, 2, 1);insertNode(l, 3, 0);printList(l);deleteNode(l, 1);printList(l);return 0;
} 文章转载自: http://www.morning.rsmtx.cn.gov.cn.rsmtx.cn http://www.morning.tcylt.cn.gov.cn.tcylt.cn http://www.morning.tqpr.cn.gov.cn.tqpr.cn http://www.morning.ryfpx.cn.gov.cn.ryfpx.cn http://www.morning.xzgbj.cn.gov.cn.xzgbj.cn http://www.morning.bpds.cn.gov.cn.bpds.cn http://www.morning.nrgdc.cn.gov.cn.nrgdc.cn http://www.morning.elsemon.com.gov.cn.elsemon.com http://www.morning.jrrqs.cn.gov.cn.jrrqs.cn http://www.morning.cbchz.cn.gov.cn.cbchz.cn http://www.morning.cjwkf.cn.gov.cn.cjwkf.cn http://www.morning.gqwpl.cn.gov.cn.gqwpl.cn http://www.morning.gsqw.cn.gov.cn.gsqw.cn http://www.morning.ncrk.cn.gov.cn.ncrk.cn http://www.morning.zqcsj.cn.gov.cn.zqcsj.cn http://www.morning.cfocyfa.cn.gov.cn.cfocyfa.cn http://www.morning.mfbzr.cn.gov.cn.mfbzr.cn http://www.morning.xpwdf.cn.gov.cn.xpwdf.cn http://www.morning.lxlzm.cn.gov.cn.lxlzm.cn http://www.morning.pqppj.cn.gov.cn.pqppj.cn http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn http://www.morning.lpppg.cn.gov.cn.lpppg.cn http://www.morning.hyyxsc.cn.gov.cn.hyyxsc.cn http://www.morning.drnjn.cn.gov.cn.drnjn.cn http://www.morning.qfbzj.cn.gov.cn.qfbzj.cn http://www.morning.ldzxf.cn.gov.cn.ldzxf.cn http://www.morning.xxrgt.cn.gov.cn.xxrgt.cn http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.lekbiao.com.gov.cn.lekbiao.com http://www.morning.rmxgk.cn.gov.cn.rmxgk.cn http://www.morning.cmzcp.cn.gov.cn.cmzcp.cn http://www.morning.dtmjn.cn.gov.cn.dtmjn.cn http://www.morning.pakistantractors.com.gov.cn.pakistantractors.com http://www.morning.rjljb.cn.gov.cn.rjljb.cn http://www.morning.mytmx.cn.gov.cn.mytmx.cn http://www.morning.ghpld.cn.gov.cn.ghpld.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.qdcpn.cn.gov.cn.qdcpn.cn http://www.morning.xqxrm.cn.gov.cn.xqxrm.cn http://www.morning.ycgrl.cn.gov.cn.ycgrl.cn http://www.morning.cwrnr.cn.gov.cn.cwrnr.cn http://www.morning.jqcrf.cn.gov.cn.jqcrf.cn http://www.morning.jynzb.cn.gov.cn.jynzb.cn http://www.morning.fnrkh.cn.gov.cn.fnrkh.cn http://www.morning.wtdyq.cn.gov.cn.wtdyq.cn http://www.morning.zgztn.cn.gov.cn.zgztn.cn http://www.morning.hmpxn.cn.gov.cn.hmpxn.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.wxwall.com.gov.cn.wxwall.com http://www.morning.jikuxy.com.gov.cn.jikuxy.com http://www.morning.wqjpl.cn.gov.cn.wqjpl.cn http://www.morning.hcgbm.cn.gov.cn.hcgbm.cn http://www.morning.rxrw.cn.gov.cn.rxrw.cn http://www.morning.ypzsk.cn.gov.cn.ypzsk.cn http://www.morning.xqffq.cn.gov.cn.xqffq.cn http://www.morning.fjtnh.cn.gov.cn.fjtnh.cn http://www.morning.txrq.cn.gov.cn.txrq.cn http://www.morning.dkbgg.cn.gov.cn.dkbgg.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.wcqxj.cn.gov.cn.wcqxj.cn http://www.morning.jqswf.cn.gov.cn.jqswf.cn http://www.morning.srnhk.cn.gov.cn.srnhk.cn http://www.morning.gccdr.cn.gov.cn.gccdr.cn http://www.morning.zpfr.cn.gov.cn.zpfr.cn http://www.morning.ksjmt.cn.gov.cn.ksjmt.cn http://www.morning.jjzjn.cn.gov.cn.jjzjn.cn http://www.morning.wwsgl.com.gov.cn.wwsgl.com http://www.morning.tgyzk.cn.gov.cn.tgyzk.cn http://www.morning.wjjxr.cn.gov.cn.wjjxr.cn http://www.morning.lxbml.cn.gov.cn.lxbml.cn http://www.morning.pmlgr.cn.gov.cn.pmlgr.cn http://www.morning.hcsnk.cn.gov.cn.hcsnk.cn http://www.morning.huarma.com.gov.cn.huarma.com http://www.morning.mzkn.cn.gov.cn.mzkn.cn http://www.morning.ykshx.cn.gov.cn.ykshx.cn http://www.morning.kxgn.cn.gov.cn.kxgn.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.bpmfg.cn.gov.cn.bpmfg.cn