<span style="font-size:32px;"><strong style="background-color: rgb(51, 255, 51);">链表的基本操作之_增删改查</strong></span>
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct Node { int data; struct Node *next; }SLIST; SLIST *Creat_SList(); int SList_Print(SLIST *pHead); //在节点数值为x的前面插入y int SList_NodeInsert(SLIST *pHead,int x,int y); //删除节点为y的链表节点。 int SList_NodeDel(SLIST *pHead,int y); //销毁链表 int SList_Destory(SLIST *pHead); SLIST *Creat_SList() { //1.创建头结点并初始化 SLIST *pHead =NULL; SLIST *pM=NULL;//辅助指针变量 SLIST *pCur;//指向最后一个节点位置的指针 int data=0; pHead=(SLIST*)malloc(sizeof(SLIST)); pHead->data=0; pHead->next=NULL; //2循环创建结点,结点数据域中的数值从键盘输入 //以-1作为输入结束标志 printf("please input the data of node(-1 quit)"); scanf("%d",&data); //准备环境 让pCur指向pHead pCur=pHead;//一开始链表的开始也是链表的结尾; while (data!=-1) { //不断的malloc新节点,并且数据域赋值 pM=(SLIST*)malloc(sizeof(SLIST)); pM->data=data; pM->next=NULL; //不断地新节点进入链表 //1.新节点入链表 pCur->next=pM; //2.当前节点下移,(新节点变成当前节点) pCur=pCur->next; //如果合法,让用户再次输入需要插入的节点。 printf("please input the data of node(-1 quit)"); scanf("%d",&data); } return pHead;//一定要返回,不然在以后的输出,增删查改等操作就无法利用。 } <img src="http://img.blog.csdn.net/20150420192944110?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjk4OTUzNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /> //链表的打印: int SList_Print(SLIST *pHead) { SLIST *p=NULL;//辅助指针,让它移动,依次输出。 if(pHead==NULL) { return -1; } //准备环境 p=pHead->next; printf("\nBegin"); while(p) { printf("%d",p->data); p=p->next; } printf("End"); return 0; } //销毁链表 int SList_Destory(SLIST *pHead) { //删除当前节点前,需要把后继节点位置缓存 SLIST *pTem=NULL; SLIST *pCur=pHead; if(pHead==NULL) { return -1; } while(pCur) { //缓存后继节点位置 pTem=pCur->next; //删除当前节点 free(pCur); //当前节点下移 pCur=pTem; } return 0; }
<img src="http://img.blog.csdn.net/20150420193014499?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjk4OTUzNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
int SList_NodeInsert(SLIST *pHead,int x,int y)//在节点数值为x的前面插入y { SLIST *pCur = NULL,*pPre=NULL,*pM=NULL; if(pHead==NULL) { return -1; } //环境准备; pPre=pHead; pCur=pHead->next; //不断的malloc新节点,并且数据域赋值 pM=(SLIST *)malloc(sizeof(SLIST)); pM->data=y;//???? pM->next=NULL; while(pCur) { if(pCur->data==x) { break; } //让pPre下移 pPre=pCur;//////////////两个节点同时下移 //让当前节点下移 pCur=pCur->next; } //让新节点链接后继节点 pM->next=pPre->next; //让前驱节点连接pM pPre->next=pM; return 0; }
<img src="http://img.blog.csdn.net/20150420192920984?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjk4OTUzNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
//删除结点为y的链表结点 int SList_NodeDel(SLIST *pHead, int y) { SLIST *pCur = NULL, *pPre = NULL; if (pHead == NULL) { return -1; } //环境准备 pPre = pHead; pCur = pHead->next; while(pCur) { if (pCur->data == y) { break; } //让pPre下移 pPre = pCur; //让当前节点下移 pCur = pCur->next; } if (pCur == NULL) { printf("没有找到节点 y:%d", y); return -2; } //执行操作 pPre->next = pCur->next;//让指针的指向越过将要删除的节点。 free(pCur); return 0; }
<img src="http://img.blog.csdn.net/20150420193118475?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjk4OTUzNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
int main() { int ret = 0; SLIST *pHead = NULL; //创建 并打印 pHead = Creat_SList(); ret = SList_Print(pHead); //插入操作 ret = SList_NodeInsert(pHead, 20, 19); ret = SList_Print(pHead); ret = SList_NodeDel(pHead, 19); ret = SList_Print(pHead); ret = SList_Destory(pHead); return 0; }