鏈表的操作


#include "stdio.h"
#include "string.h"
#include "stdlib.h"


typedef struct Node
{
int data;
struct Node *next;
}SLIST;






//編寫函數SList_Creat,建立帶有頭結點的單向鏈表。循環創建結點,
//結點數據域中的數值從鍵盤輸入,以-1作爲輸入結束標誌。鏈表的頭結點地址由函數值返回。


SLIST *SList_Creat();
int SList_Print(SLIST *pHead);
int SList_NodeInsert(SLIST *pHead, int x, int y);
int SList_NodeDel(SLIST *pHead, int x);
int SList_Destory(SLIST *pHead);
int SList_Resve(SLIST *pHead);


//能寫出穩健的、長期運算的工作代碼  1年工作經驗那
SLIST *SList_Creat()
{
SLIST *pHead = NULL, *pM =NULL, *pCur = NULL;
int data = 0;
//1 創建頭結點並初始化
pHead = (SLIST *)malloc(sizeof(SLIST));
if (pHead == NULL)
{
return NULL;
}
pHead->data = 0;
pHead->next = NULL;


//2 從鍵盤輸入數據,創建業務結點
printf("\nplease enter the data of node(-1:quit) ");
scanf("%d", &data);


//3 循環創建
//初始化當前節點,指向頭結點
pCur = pHead;
while(data != -1)
{
//新建業務結點 並初始化
//1 不斷的malloc 新的業務節點 ===PM
pM = (SLIST *)malloc(sizeof(SLIST));
if (pM == NULL)
{
SList_Destory(pHead); //
return NULL;
}
pM->data = data;
pM->next = NULL;


//2、讓pM節點入鏈表
pCur->next = pM;


//3 pM節點變成當前節點
pCur = pM; //pCur = pCur->next;




//2 從鍵盤輸入數據,創建業務結點
printf("\nplease enter the data of node(-1:quit) ");
scanf("%d", &data);
}


return pHead;
}


// 2年工作經驗那
int SList_Creat2(SLIST **mypHead)
{
int ret = 0;
SLIST *pHead = NULL, *pM =NULL, *pCur = NULL;
int data = 0;
//1 創建頭結點並初始化
pHead = (SLIST *)malloc(sizeof(SLIST));
if (pHead == NULL)
{
ret = -1;
printf("func SList_Creat2() err:%d ", ret);
return ret;
}
pHead->data = 0;
pHead->next = NULL;


//2 從鍵盤輸入數據,創建業務結點
printf("\nplease enter the data of node(-1:quit) ");
scanf("%d", &data);


//3 循環創建
//初始化當前節點,指向頭結點
pCur = pHead;
while(data != -1)
{
//新建業務結點 並初始化
//1 不斷的malloc 新的業務節點 ===PM
pM = (SLIST *)malloc(sizeof(SLIST));
if (pM == NULL)
{
SList_Destory(pHead); //
ret = -2;
printf("func SList_Creat2() err:%d ", ret);
return ret;
}
pM->data = data;
pM->next = NULL;


//2、讓pM節點入鏈表
pCur->next = pM;


//3 pM節點變成當前節點
pCur = pM; //pCur = pCur->next;


//2 從鍵盤輸入數據,創建業務結點
printf("\nplease enter the data of node(-1:quit) ");
scanf("%d", &data);
}


*mypHead = pHead;
return ret;
}


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 *p = NULL, *tmp = NULL;
if (pHead == NULL)
{
return -1;
}
p = pHead;
while(p)
{
//緩存下一個結點位置
tmp = p->next;
free(p);//刪除當前節點
p = tmp; //節點指針後移
}
return 0;
}


//功能:在值爲x的結點前,插入值爲y的結點;若值爲x的結點不存在,則插在表尾。
int SList_NodeInsert(SLIST *pHead, int x, int y)
{
SLIST *pCur = NULL, *pPre = NULL, *pM = NULL;


//根據y的值malloc新結點
pM = (SLIST *)malloc(sizeof(SLIST));
if (pM == NULL)
{
return -1;
}
pM->data = y;
pM->next = NULL;


//準備pCur Pre環境
pPre = pHead;
pCur = pHead->next;


while (pCur)
{
if (pCur->data == x)
{
//插入操作
break;
}


pPre = pCur; //讓前驅結點後移
pCur = pCur->next; //讓當前結點後移
}


//1 pM結點連接後繼鏈表
//pM->next = pCur;
pM->next = pPre->next;
//2 讓前驅結點連接pM 
pPre->next = pM; 


return 0;
}


int SList_NodeDel(SLIST *pHead, int x)
{
SLIST *pCur = NULL, *pPre = NULL;


//準備pCur Pre環境
pPre = pHead;
pCur = pHead->next;


while (pCur)
{
if (pCur->data == x)
{
//插入操作
break;
}


pPre = pCur; //讓前驅結點後移
pCur = pCur->next; //讓當前結點後移
}


if (pCur == NULL)
{
printf("沒有找到要刪除的結點\n");
return -1;
}
//從鏈表上刪除結點
pPre->next =   pCur->next;
//釋放內存
free(pCur);
return 0;
}


//刪除節點值爲偶數的
int SList_NodeSpecialDel(SLIST *pHead)
{
SLIST *pCur = NULL, *pPre = NULL;


//準備pCur Pre環境
pPre = pHead;
pCur = pHead->next;


while (pCur)
{
if (pCur->data %2 == 0)
{
//插入操作
break;
}


pPre = pCur; //讓前驅結點後移
pCur = pCur->next; //讓當前結點後移
}


if (pCur == NULL)
{
printf("沒有找到要刪除的結點\n");
return 0;
}
//從鏈表上刪除結點
pPre->next =   pCur->next;
//釋放內存
free(pCur);
return 0;
}




int SList_NodeDelOueve(SLIST *pHead)
{
SLIST *p = NULL;
if (pHead == NULL)
{
return -1;
}
p = pHead->next;
while (p)
{
SList_NodeSpecialDel(p);
p = p->next;
}
return 0;
}




int SList_Resve(SLIST *pHead)
{
SLIST *t = NULL, *p = NULL, *q = NULL;


if (pHead == NULL)
{
return -1;
}
if (pHead->next == NULL || pHead->next->next == NULL)
{
return -2;
}


//初始化逆置環境
p = pHead->next;
q = pHead->next->next;


while (q != NULL)
{
//逆置之前把q的後繼結點保存
t = q->next;
//逆置操作
q->next = p;


//3讓前驅節點後移  //3 4是打造下一次while循環逆置的環境
p = q;


//4讓逆置節點後移
q = t;
}


//逆置完成以後p指向最後一個結點。q指向null
pHead->next->next = NULL; //
pHead->next = p;


return 0;
}


void main()
{
int ret = 0;
SLIST *pHead = NULL;
pHead = SList_Creat();
ret = SList_Print(pHead);
if (ret != 0)
{
printf("func SList_Print() err:%d\n", ret);
return ;
}


//刪除結點值爲偶數的結點
ret = SList_NodeDelOueve(pHead);
if (ret != 0)
{
printf("func SList_NodeDelOueve(pHead)() err:%d\n", ret);
return ;
}
ret = SList_Print(pHead);
if (ret != 0)
{
printf("func SList_Print() err:%d\n", ret);
return ;
}

ret = SList_Destory(pHead);
if (ret != 0)
{
printf("func SList_Destory() err:%d\n", ret);
return ;


system("pause");

}


發佈了61 篇原創文章 · 獲贊 7 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章