【C语言提高52】链表的基本操作
2016-01-13 15:20
627 查看
</pre><h1><span style="color:#ff0000">链表分类:</span></h1><div><span style="color:#ff0000"> <img src="http://img.blog.csdn.net/20160113155144640?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></span></div><div><span style="color:#ff0000"></span></div><div><span style="color:#ff0000"></span></div><div><span style="color:#ff0000"></span></div><div><span style="color:#ff0000"></span></div><div><span style="color:#ff0000"></span></div><div><span style="color:#ff0000"></span></div><div><span style="color:#ff0000"></span></div><div><span style="color:#ff0000"></span></div><div><span style="color:#ff0000"></span></div><div><span style="color:#ff0000"></span><pre code_snippet_id="1555535" snippet_file_name="blog_20160113_2_396671" name="code" class="cpp">typedef struct Node { int data; struct Node*next; }SLIST; //创建链表 返回头结点 SLIST*SList_Creat(); //创建链表 //顺序访问链表中各结点的数据域 int SList_Print(SLIST*pHead); //遍历链表 //在单向链表中插入节点 int SList_NodeInsert(SLIST*pHead,int x,int y); //插入值 在x值之前插入y //在单向链表中删除节点 int SList_NodeDel(SLIST*pHead,int y); //删除值 //链表逆置 int SList_Reverse(SLIST*pHead); //删除链表 int Slist_Destory(SLIST*pHead);
创建链表:
SLIST*SList_Creat() { SLIST *pHead, *pM, *pCur; //创建头结点 并初始化 pHead = (SLIST*)malloc(sizeof(SLIST)); if (pHead == NULL) { return NULL; } pHead->data = 0; pHead->next = NULL; printf("\n please enter you data"); int dat; scanf_s("%d",&dat,50); pCur = pHead; //当前结点指向头结点 while (dat!=-1) { //创建业务结点 并初始化 //1.不断的接收输入 malloc新结点 pM pM= (SLIST*)malloc(sizeof(SLIST)); if (pM == NULL) { return NULL; } pM->data = dat; pM->next = NULL; //当前结点next置空 //2.新结点 入链表 pCur->next = pM; //通过pCur来操纵上一个结点的next指向当前创造的结点 //3.新结点变成当前结点 pCurrent pCur = pM; //链表结点的尾部追加 改变pCur指向 使其指向当前创造的结点 //提前用户输入下一个结点dat值 printf("\n please enter you data"); scanf_s("%d", &dat, 50); } return pHead; }
遍历链表:
int SList_Print(SLIST*pHead) { SLIST*tmp = NULL; if (pHead == NULL) { return -1; } tmp = pHead;//tmp的next指向phead printf("\nBegin\t"); while (tmp) { printf("%d ",tmp->data); tmp = tmp->next;//改变tmp的指向 使其指向当前所代表结点的next 也就是下一个的结点的起始地址 } printf("\tEnd\t"); return 0; }
在单向链表中插入节点:
int SList_NodeInsert(SLIST*pHead, int x, int y) { //链表的单向的 当前结点的位置 保存在前驱结点的next域中 SLIST *pM, *pCur,*pPre; int data; //创建要插入的新的业务结点 并且初始化 pM pM = (SLIST*)malloc(sizeof(SLIST)); if (pM==NULL) { return -1; } pM->data = y; pM->next = NULL; //遍历链表 pPre = pHead; //初始化 起始位置 pCur = pHead->next; while (pCur) { if (pCur->data = x) { break; } //移动辅助指针指向 依次指向下两个结点 pPre = pCur; pCur = pCur->next; } //必须按照这个顺序来是因为 后驱结点的位置保存在前驱结点的next域中 //让 新结点 链接 后驱结点 pM->next = pPre->next; //让 前驱结点 链接 新结点 pPre->next = pM; return 0; }
在单向链表中删除节点:
int SList_NodeDel(SLIST*pHead, int y) { SLIST *pCur, *pPre; //初始化状态 pPre = pHead; pCur = pHead->next; //遍历 while (pCur) { if (pCur->data == y) { break; } pPre = pCur; pCur = pCur->next; } //删除操作 if (pCur == NULL) { printf("没有找到结点值为%d的结点\n",y); return -1; } pPre->next = pCur->next; if (pCur != NULL) { free(pCur); } return 0; }
链表逆置:
<span style="font-size:14px;">int SList_Reverse(SLIST*pHead)
{
SLIST *pPre=NULL; //前驱指针
SLIST *pCur=NULL;// 当前指针
SLIST *tmp = NULL;//缓存下一个结点
if (pHead == NULL||pHead->next==NULL||pHead->next->next==NULL)
{
return -1;
}
pPre = pHead->next;
pCur = pHead->next->next;
//一个结点 一个结点的逆置
while (pCur)
{
tmp = pCur->next; //缓存后面的链表地址
pCur->next = pPre; //逆置
pPre = pCur; //让pPre下移一个结点
pCur = tmp;
}
//头结点 变成 尾部结点后 置NULL
pHead->next->next = NULL;
pHead->next = pPre;
return 0;</span>
删除链表:
int Slist_Destory(SLIST*pHead) { SLIST*tmp = NULL; if (pHead == NULL) { return -1; } tmp = pHead; while (pHead!=NULL) //删pHead之前 缓存下来 { tmp = pHead->next; free(pHead); pHead = tmp->next; } return 0; }
相关文章推荐
- c/c++笔记
- C/C++ void及void指针
- C、C++动态分配函数暨几种参数传递疑难解析
- C++中的 virtual destructor
- C++中的虚函数表
- C++ Primer Plus 学习笔记(三)--编程练习6
- c++内存分配(new和delete)
- C++中的多态与虚函数的内部实现
- LevelDB windows vs2013 c++编译和测试
- 【C语言提高51】链表基本概念与静态链表
- CodeForces 413A Data Recovery
- C语言学习之字符串、字符和字节第三讲
- 【C语言提高50】Win32环境下动态链接库(DLL)编程原理
- 游戏里C++,JAVA,quick交互
- C/C++内联函数(inline)
- 【30天学习C++ primer系列】day2:基础拾忆之变量和基本类型(1/2)。-20160111
- cpp题目
- C语言版本历史
- 值得学习的C语言开源项目
- Visual Studio2013的C语言编译器对C99标准的支持情况