1. 段错误调试
1.1 打印法
在可能出现错误的位置加入打印,前一句能够打印出来,后一句打印不出来,问题就可以定位到两次打印中间的代码
1.2 gbd调试法
1. 编译代码时加入-g选项
gcc filename.c -g
2. 使用gdb调试生成的代码
gdb a.out
3. gdb调试命令
l 查看代码
b 函数名/行号 设置断点
r 运行代码
n 单步运行
c 直接运行到下一处断点
s 进入函数内部调试
p 变量名 查看变量对应的值
q 退出
1.3 core文件调试方法
1. 配置core文件
ubuntu 产生core文件-CSDN博客文章浏览阅读251次。ubuntu 产生core文件https://blog.csdn.net/weixin_43847267/article/details/130546569?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522169243011316800225536999%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=169243011316800225536999&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-2-130546569-null-null.142%5Ev93%5EchatgptT3_2&utm_term=Ubuntu%E4%BA%A7%E7%94%9Fcore%E6%96%87%E4%BB%B6&spm=1018.2226.3001.4187 2. ulimit -c unlimited
3. 编译代码加入-g选项
4. 运行代码使其产生段错误,段错误产生后会生成一个包含错误信息的core文件
5. gdb a.out core 找到错误产生的位置
注意:打印法和gdb法可以调试任意错误,但core文件调试法只能调试段错误
2. 链表以及链表的增删改查
2.1 创建空链表
typedef struct node
{int data;struct node *pnext;
}linknode;linknode *createlinklist(void)
{linknode *ptmpnode = NULL;ptmpnode = malloc(sizeof(linknode));if (NULL == ptmpnode){printf("malloc failed!\n");return NULL;}ptmpnode->pnext = NULL;return ptmpnode;
}
2.2 头插法
int insertheadlinklist(linknode *phead, int tmpdata)
{linknode *ptmpnode = NULL;ptmpnode = malloc(sizeof(linknode));if (NULL == ptmpnode){printf("malloc failed!\n");return -1;}ptmpnode->data = tmpdata;ptmpnode->pnext = phead->pnext;phead->pnext = ptmpnode;return 0;
}
2.3 打印链表信息
int showlinklist(linknode *phead)
{linknode *ptmp = NULL;ptmp = phead->pnext;while (ptmp != NULL){printf("%d ", ptmp->data);ptmp = ptmp->pnext;}printf("\n");return 0;
}
2.4 寻找链表中的元素
linknode *findlinklist(linknode *phead, int tmpdata)
{linknode *ptmpnode = NULL;ptmpnode = phead->pnext;while (ptmpnode != NULL){if (ptmpnode->data == tmpdata){return ptmpnode;}ptmpnode = ptmpnode->pnext;}return NULL;
}
2.5 链表元素的替换
int replacelinklist(linknode *phead, int oldata, int newdata)
{linknode *ptmpnode = NULL;ptmpnode = phead->pnext;while (ptmpnode != NULL){if (ptmpnode->data == oldata){ptmpnode->data = newdata;}ptmpnode = ptmpnode->pnext;}
2.6 头插法
int insertheadlinklist(linknode *phead, int tmpdata)
{linknode *ptmpnode = NULL;ptmpnode = malloc(sizeof(linknode));if (NULL == ptmpnode){printf("malloc failed!\n");return -1;}ptmpnode->data = tmpdata;ptmpnode->pnext = phead->pnext;phead->pnext = ptmpnode;return 0;
}
2.7 尾插法
int inserttaillinklist(linknode *phead, int tmpdata)
{linknode *ptmpnode = NULL;linknode *plastnode = NULL;ptmpnode = malloc(sizeof(linknode));if (NULL == ptmpnode){printf("malloc failed!\n");return -1;}ptmpnode->data = tmpdata;ptmpnode->pnext = NULL;plastnode = phead;while (plastnode->pnext != NULL){plastnode = plastnode->pnext;}plastnode->pnext = ptmpnode;return 0;
}
2.8 链表的删除
int deletelinklist(linknode *phead, int tmpdata)
{linknode *ptmpnode = NULL;linknode *pprenode = NULL;pprenode = phead;ptmpnode = phead->pnext;while (ptmpnode != NULL){if (ptmpnode->data == tmpdata){pprenode->pnext = ptmpnode->pnext;free(ptmpnode);ptmpnode = pprenode->pnext;}else {ptmpnode = ptmpnode->pnext;pprenode = pprenode->pnext;}}return 0;
}
2.9 链表的摧毁和回收
int destroylinklist(linknode **pphead)
{linknode *ptmpnode = NULL;linknode *pfreenode = NULL;ptmpnode = pfreenode = *pphead;while (ptmpnode != NULL){ptmpnode = ptmpnode->pnext;free(pfreenode);pfreenode = ptmpnode;}*pphead = NULL;return 0;
}
2.10 链表的冒泡排序法
int bubblesortlinklist(linknode *phead)
{linknode *ptmpnode1 = NULL;linknode *ptmpnode2 = NULL;linknode *pend = NULL;int tmp = 0;while (1){ptmpnode1 = phead->pnext;ptmpnode2 = phead->pnext->pnext;if (ptmpnode2 == pend){break;}while (ptmpnode2 != pend){if (ptmpnode1->data > ptmpnode2->data){tmp = ptmpnode1->data;ptmpnode1->data = ptmpnode2->data;ptmpnode2->data = tmp;}ptmpnode1 = ptmpnode1->pnext;ptmpnode2 = ptmpnode2->pnext;}pend = ptmpnode1;}return 0;
}
2.11 链表的选择排序法
int selectsortlinklist(linknode *phead)
{linknode *pselectnode = NULL;linknode *pminnode = NULL;linknode *ptmpnode = NULL;int tmp;pselectnode = phead->pnext;while (pselectnode->pnext != NULL){pminnode = pselectnode;ptmpnode = pselectnode->pnext;while (ptmpnode != NULL){if (ptmpnode->data < pminnode->data){pminnode = ptmpnode;}ptmpnode = ptmpnode->pnext;}if (pminnode != pselectnode){tmp = pminnode->data;pminnode->data = pselectnode->data;pselectnode->data = tmp;}pselectnode = pselectnode->pnext;}return 0;
}