#ifndef _LIST_h_ #define _LIST_h_ //鏈表中的數據結構 typedef struct Link_data { int a; int b; }Node_data; //鏈表節點結構 typedef struct Link_node { Node_data data; struct Link_node *pNext; }Node; Node* CreateList(void); Node* FindNodeByGlobalIndex(Node *pHead, int iGlobalIndex); Node* Insert2ListTail(Node *pHead, Node_data *pAutoInfo); int RemoveList(Node *pHead); void PrintList(Node *pHead); int DeleteList (Node *pHead,int x); void ReverseList(Node *pHead); void SortList(Node *pHead); #endif
#include <string.h> #include <malloc.h> #include<stdio.h> #include"list.h" /************************************************* Function : CreateList Description : 創建鏈表頭節點 Return : 鏈表的頭指針 *************************************************/ Node* CreateList(void) { Node *pHead = NULL; //申請的頭指針 pHead = (Node *)malloc(sizeof(Node)); //判斷是否申請成功 if (NULL == pHead) { return NULL; } //針對具體結構進行初始化 pHead->data.a = 0; pHead->data.b = 0; pHead->pNext = NULL; return pHead; } /************************************************* Function : FindNodeByGlobalIndex Description : 根據指定參數,查找某個節點 Input : pHead 鏈表的頭節點指針 要查找的學生ID Return : 正確:返回指定節點的指針 失敗:返回空指針 *************************************************/ Node* FindNodeByGlobalIndex(Node *pHead, int iGlobalIndex) { Node *pNode = NULL; if ((NULL == pHead) || (iGlobalIndex < 0)) { return NULL; } pNode = pHead->pNext; while ((NULL != pNode)) { if (pNode->data.a == iGlobalIndex) { break; } pNode = pNode->pNext; } return pNode; } /************************************************* Function : Insert2ListTail Description : 向鏈表中尾部插入某個節點 Input : pHead 鏈表的頭節點指針 pStudentInfo 學生信息 Return : 正確:返回頭節點指針 失敗:返回空指針 *************************************************/ Node* Insert2ListTail(Node *pHead, Node_data *pAutoInfo) { Node* pNode = NULL; Node* pNewNode = NULL; if ((NULL == pHead) || (NULL == pAutoInfo)) { return NULL; } pNode = pHead; while (pNode->pNext != NULL) { pNode = pNode->pNext; } pNewNode = (Node *)malloc(sizeof(Node)); if (NULL == pNewNode) { return NULL; } pNode->pNext = pNewNode; pNewNode->pNext = NULL; memcpy(&(pNewNode->data), pAutoInfo, sizeof(Node_data )); return pHead; } /************************************************* Function : RemoveList Description : 刪除整個鏈表 Input : pHead 鏈表的頭節點指針 Return : 正確: 1 失敗: 0 *************************************************/ int RemoveList(Node *pHead) { Node *pNode = NULL; Node *pb = NULL; if (NULL == pHead) { return 0; } pNode = pHead; pb = pNode->pNext; if (NULL == pb) { free(pNode); } else { while (NULL != pb) { free(pNode); pNode = pb; pb = pb->pNext; } free(pNode); } pNode = NULL; return 1; } /************************************************* Function : PrintList Description : 打印整個鏈表 Input : pHead 鏈表的頭節點指針 Return : *************************************************/ void PrintList(Node *pHead) { Node *pNode = NULL; if (NULL == pHead) { return ; } pNode = pHead->pNext; while ((NULL != pNode)) { printf("\r\n a is %d b is %d",pNode->data.a,pNode->data.b); pNode = pNode->pNext; } return ; } /************************************************* Function : DeleteList Description : 刪除鏈表的一個結點,刪除條件該節點的a值與x相同 Input : Return : *************************************************/ int DeleteList (Node *pHead,int x) { Node *pNode = NULL; Node *pre = NULL; if (NULL == pHead ) { return 0; } pNode = pHead->pNext; pre = pHead; while(pNode) { if(pNode->data.a == x)//刪除條件 { pre->pNext = pNode->pNext; free(pNode); return 1; } else { pre = pNode; } pNode = pNode->pNext; } return 0; } /************************************************* Function : ReverseList Description : 鏈表反轉 Input : Return : *************************************************/ void ReverseList(Node *pHead) { Node* p = pHead->pNext; Node* q = p->pNext; Node* t = NULL; if(NULL == pHead || NULL == pHead->pNext) { return; } while(NULL != q) { t = q->pNext; q->pNext = p; p = q; q = t; } pHead->pNext->pNext = NULL; pHead->pNext = p; } /************************************************* Function : SortList Description : 按a值排序 Input : Return : *************************************************/ void SortList(Node *pHead) { Node* pi = pHead->pNext; Node* pj = pi->pNext; Link_data temp; memset(&temp,0,sizeof(Link_data)); if(NULL == pHead || NULL == pHead->pNext) { return; } for(;pi != NULL;pi=pi->pNext) { for(pj = pi->pNext;pj != NULL;pj=pj->pNext) { if(pj->data.a < pi->data.a) { temp = pj->data; pj->data = pi->data; pi->data = temp; } } } }
#include<stdio.h> #include<string.h> #include"list.h" Node * g_LinkHead = NULL; int main() { Node_data data1; Node_data data2; Node_data data4; Node *data3 = NULL; memset(&data1, 0, sizeof(Node_data)); memset(&data2, 0, sizeof(Node_data)); memset(&data4, 0, sizeof(Node_data)); data1.a=3; data1.b=3; data2.a=2; data2.b=4; data4.a=5; data4.b=6; g_LinkHead=CreateList(); Insert2ListTail(g_LinkHead,&data1); Insert2ListTail(g_LinkHead,&data2); Insert2ListTail(g_LinkHead,&data4); PrintList(g_LinkHead); //data3 = FindNodeByGlobalIndex(g_LinkHead, 2); //printf("\r\n data3.a %d data3.b %d",data3->data.a,data3->data.b); printf("\n\n"); //(void) ReverseList(g_LinkHead); (void) SortList(g_LinkHead); PrintList(g_LinkHead); /*if(DeleteList (g_LinkHead,4)) { PrintList(g_LinkHead); } PrintList(g_LinkHead);*/ /*if(RemoveList(g_LinkHead)) { g_LinkHead = NULL; } PrintList(g_LinkHead);*/ return 0; }