段错误调试:
方法一:
1.打印法
在可能出现错误的位置加入打印,前一句能够打印出来,后一句打印不出来,问题就可以定位
到两次打印中间的代码
方法二:
1.gdb调试法
1.编译代码时加入-g选项
gcc filename.c -g
2.使用gdb调试生成的代码
gdb a.out
3.gdb调试命令:
l 查看代码
b 函数名/行号 设置断点
r 运行代码
n 单步运行
c 直接运行到下一处断点
s 进入函数内部调试
p 变量名 查看变量对应的值
q 退出
方法三:
1.core文件调试法
1.配置core文件
2.ulimit -c unlimited
3.编译代码加入-g选项
gcc filename.c -o filename
4.运行代码使其产生段错误,段错误产生后会生成一个包含错误信息的core文件
5.gdb a.out core 找到错误产生的位置
链表的创建,插入,删除,打印,排序以及销毁。
#include <stdio.h>
#include <stdlib.h>
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;
}
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;
}
int showlinklist(linknode *phead)
{
linknode *ptmp = NULL;
ptmp = phead->pnext;
while (ptmp != NULL)
{
printf("%d ", ptmp->data);
ptmp = ptmp->pnext;
}
printf("\n");
return 0;
}
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;
}
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;
}
return 0;
}
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;
}
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;
}
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;
}
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;
}
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;
}
int main(void)
{
linknode *plinklist = NULL;
linknode *ptmpnode = NULL;
plinklist = createlinklist();
#if 1
insertheadlinklist(plinklist, 1);
insertheadlinklist(plinklist, 2);
insertheadlinklist(plinklist, 3);
insertheadlinklist(plinklist, 4);
#endif
inserttaillinklist(plinklist, 5);
inserttaillinklist(plinklist, 6);
inserttaillinklist(plinklist, 7);
inserttaillinklist(plinklist, 8);
inserttaillinklist(plinklist, 1);
inserttaillinklist(plinklist, 1);
inserttaillinklist(plinklist, 2);
inserttaillinklist(plinklist, 2);
showlinklist(plinklist);
ptmpnode = findlinklist(plinklist, 3);
if (ptmpnode != NULL)
{
printf("%p, %d\n", ptmpnode, ptmpnode->data);
}
printf("-------------------\n");
replacelinklist(plinklist, 3, 30);
showlinklist(plinklist);
deletelinklist(plinklist, 1);
showlinklist(plinklist);
// bubblesortlinklist(plinklist);
selectsortlinklist(plinklist);
showlinklist(plinklist);
destroylinklist(&plinklist);
printf("plinklist:%p\n", plinklist);
return 0;
}