#include "stdafx.h" #include "stdio.h" #include <stdlib.h> #include "string.h" typedef int elemType ; /************************************************************************/ /* 以下是關於線性錶鏈接存儲(單鏈表)操作的18種算法 */ /* 1.初始化線性表,即置單鏈表的表頭指針爲空 */ /* 2.創建線性表,此函數輸入負數終止讀取數據*/ /* 3.打印鏈表,鏈表的遍歷*/ /* 4.清除線性表L中的所有元素,即釋放單鏈表L中所有的結點,使之成爲一個空表 */ /* 5.返回單鏈表的長度 */ /* 6.檢查單鏈表是否爲空,若爲空則返回1,否則返回0 */ /* 7.返回單鏈表中第pos個結點中的元素,若pos超出範圍,則停止程序運行 */ /* 8.從單鏈表中查找具有給定值x的第一個元素,若查找成功則返回該結點data域的存儲地址,否則返回NULL */ /* 9.把單鏈表中第pos個結點的值修改爲x的值,若修改成功返回1,否則返回0 */ /* 10.向單鏈表的表頭插入一個元素 */ /* 11.向單鏈表的末尾添加一個元素 */ /* 12.向單鏈表中第pos個結點位置插入元素爲x的結點,若插入成功返回1,否則返回0 */ /* 13.向有序單鏈表中插入元素x結點,使得插入後仍然有序 */ /* 14.從單鏈表中刪除表頭結點,並把該結點的值返回,若刪除失敗則停止程序運行 */ /* 15.從單鏈表中刪除表尾結點並返回它的值,若刪除失敗則停止程序運行 */ /* 16.從單鏈表中刪除第pos個結點並返回它的值,若刪除失敗則停止程序運行 */ /* 17.從單鏈表中刪除值爲x的第一個結點,若刪除成功則返回1,否則返回0 */ /* 18.交換2個元素的位置 */ /* 19.將線性表進行快速排序 */ /************************************************************************/ typedef struct Node{ /* 定義單鏈表結點類型 */ elemType element; Node *next; }Node; /* 1.初始化線性表,即置單鏈表的表頭指針爲空 */ void initList(Node **pNode) { *pNode = NULL; printf("initList函數執行,初始化成功\n"); } /* 2.創建線性表,此函數輸入負數終止讀取數據*/ Node *creatList(Node *pHead) { Node *p1; Node *p2; p1=p2=(Node *)malloc(sizeof(Node)); //申請新節點 if(p1 == NULL || p2 ==NULL) { printf("內存分配失敗\n"); exit(0); } memset(p1,0,sizeof(Node)); scanf("%d",&p1->element); //輸入新節點 p1->next = NULL; //新節點的指針置爲空 while(p1->element > 0) //輸入的值大於0則繼續,直到輸入的值爲負 { if(pHead == NULL) //空表,接入表頭 { pHead = p1; } else { p2->next = p1; //非空表,接入表尾 } p2 = p1; p1=(Node *)malloc(sizeof(Node)); //再重申請一個節點 if(p1 == NULL || p2 ==NULL) { printf("內存分配失敗\n"); exit(0); } memset(p1,0,sizeof(Node)); scanf("%d",&p1->element); p1->next = NULL; } printf("creatList函數執行,鏈表創建成功\n"); return pHead; //返回鏈表的頭指針 } /* 3.打印鏈表,鏈表的遍歷*/ void printList(Node *pHead) { if(NULL == pHead) //鏈表爲空 { printf("PrintList函數執行,鏈表爲空\n"); } else { while(NULL != pHead) { printf("%d ",pHead->element); pHead = pHead->next; } printf("\n"); } } /* 4.清除線性表L中的所有元素,即釋放單鏈表L中所有的結點,使之成爲一個空表 */ void clearList(Node *pHead) { Node *pNext; //定義一個與pHead相鄰節點 if(pHead == NULL) { printf("clearList函數執行,鏈表爲空\n"); return; } while(pHead->next != NULL) { pNext = pHead->next;//保存下一結點的指針 free(pHead); pHead = pNext; //表頭下移 } printf("clearList函數執行,鏈表已經清除\n"); } /* 5.返回單鏈表的長度 */ int sizeList(Node *pHead) { int size = 0; while(pHead != NULL) { size++; //遍歷鏈表size大小比鏈表的實際長度小1 pHead = pHead->next; } printf("sizeList函數執行,鏈表長度 %d \n",size); return size; //鏈表的實際長度 } /* 6.檢查單鏈表是否爲空,若爲空則返回1,否則返回0 */ int isEmptyList(Node *pHead) { if(pHead == NULL) { printf("isEmptyList函數執行,鏈表爲空\n"); return 1; } printf("isEmptyList函數執行,鏈表非空\n"); return 0; } /* 7.返回單鏈表中第pos個結點中的元素,若pos超出範圍,則停止程序運行 */ elemType getElement(Node *pHead, int pos) { int i=0; if(pos < 1) { printf("getElement函數執行,pos值非法\n"); return 0; } if(pHead == NULL) { printf("getElement函數執行,鏈表爲空\n"); return 0; //exit(1); } while(pHead !=NULL) { ++i; if(i == pos) { break; } pHead = pHead->next; //移到下一結點 } if(i < pos) //鏈表長度不足則退出 { printf("getElement函數執行,pos值超出鏈表長度\n"); return 0; } return pHead->element; } /* 8.從單鏈表中查找具有給定值x的第一個元素,若查找成功則返回該結點data域的存儲地址,否則返回NULL */ elemType *getElemAddr(Node *pHead, elemType x) { if(NULL == pHead) { printf("getElemAddr函數執行,鏈表爲空\n"); return NULL; } if(x < 0) { printf("getElemAddr函數執行,給定值X不合法\n"); return NULL; } while((pHead->element != x) && (NULL != pHead->next)) //判斷是否到鏈表末尾,以及是否存在所要找的元素 { pHead = pHead->next; } if((pHead->element != x) && (pHead != NULL)) { printf("getElemAddr函數執行,在鏈表中未找到x值\n"); return NULL; } if(pHead->element == x) { printf("getElemAddr函數執行,元素 %d 的地址爲 0x%x\n",x,&(pHead->element)); } return &(pHead->element);//返回元素的地址 } /* 9.把單鏈表中第pos個結點的值修改爲x的值,若修改成功返回1,否則返回0 */ int modifyElem(Node *pNode,int pos,elemType x) { Node *pHead; pHead = pNode; int i = 0; if(NULL == pHead) { printf("modifyElem函數執行,鏈表爲空\n"); } if(pos < 1) { printf("modifyElem函數執行,pos值非法\n"); return 0; } while(pHead !=NULL) { ++i; if(i == pos) { break; } pHead = pHead->next; //移到下一結點 } if(i < pos) //鏈表長度不足則退出 { printf("modifyElem函數執行,pos值超出鏈表長度\n"); return 0; } pNode = pHead; pNode->element = x; printf("modifyElem函數執行\n"); return 1; } /* 10.向單鏈表的表頭插入一個元素 */ int insertHeadList(Node **pNode,elemType insertElem) { Node *pInsert; pInsert = (Node *)malloc(sizeof(Node)); memset(pInsert,0,sizeof(Node)); pInsert->element = insertElem; pInsert->next = *pNode; *pNode = pInsert; printf("insertHeadList函數執行,向表頭插入元素成功\n"); return 1; } /* 11.向單鏈表的末尾添加一個元素 */ int insertLastList(Node **pNode,elemType insertElem) { Node *pInsert; Node *pHead; Node *pTmp; //定義一個臨時鏈表用來存放第一個節點 pHead = *pNode; pTmp = pHead; pInsert = (Node *)malloc(sizeof(Node)); //申請一個新節點 memset(pInsert,0,sizeof(Node)); pInsert->element = insertElem; while(pHead->next != NULL) { pHead = pHead->next; } pHead->next = pInsert; //將鏈表末尾節點的下一結點指向新添加的節點 *pNode = pTmp; printf("insertLastList函數執行,向表尾插入元素成功\n"); return 1; } /* 12.向單鏈表中第pos個結點位置插入元素爲x的結點,若插入成功返回1,否則返回0 */ /* 13.向有序單鏈表中插入元素x結點,使得插入後仍然有序 */ /* 14.從單鏈表中刪除表頭結點,並把該結點的值返回,若刪除失敗則停止程序運行 */ /* 15.從單鏈表中刪除表尾結點並返回它的值,若刪除失敗則停止程序運行 */ /* 16.從單鏈表中刪除第pos個結點並返回它的值,若刪除失敗則停止程序運行 */ /* 17.從單鏈表中刪除值爲x的第一個結點,若刪除成功則返回1,否則返回0 */ /* 18.交換2個元素的位置 */ /* 19.將線性表進行快速排序 */ /******************************************************************/ int main() { Node *pList=NULL; int length = 0; elemType posElem; initList(&pList); //鏈表初始化 printList(pList); //遍歷鏈表,打印鏈表 pList=creatList(pList); //創建鏈表 printList(pList); sizeList(pList); //鏈表的長度 printList(pList); isEmptyList(pList); //判斷鏈表是否爲空鏈表 posElem = getElement(pList,3); //獲取第三個元素,如果元素不足3個,則返回0 printf("getElement函數執行,位置 3 中的元素爲 %d\n",posElem); printList(pList); getElemAddr(pList,5); //獲得元素5的地址 modifyElem(pList,4,1); //將鏈表中位置4上的元素修改爲1 printList(pList); insertHeadList(&pList,5); //表頭插入元素12 printList(pList); insertLastList(&pList,10); //表尾插入元素10 printList(pList); clearList(pList); //清空鏈表 system("pause"); }
|