您的位置:首页 > 编程语言 > C语言/C++

【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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: