C语言简单实现单链表的一般操作
/** * author:soarHu verison:1.0 date:2016/05/01 description:"this is a linkedList operation..." */ #include "stdio.h" #include "stdlib.h" typedef struct Node{ int value; struct Node *pNext; }Node,*PNode; PNode createLinkedList(void); void tranverse(PNode pHead); int getLength(PNode pHead); int insertNode(PNode pHead,int positon,int value); int deleteNode(PNode pHead,int position); void appendList(PNode pHead,int value); void getElem(PNode pHead,int position); void priorElem(PNode pHead,int position); void nextElem(PNode pHead); int main(int argc, char const *argv[]){ PNode pHead = NULL;//define root pointer to point headNode. pHead = createLinkedList(); tranverse(pHead); if(insertLinkedList(pHead,3,88)){ printf("insert success!"); tranverse(pHead); }else{ printf("%s\n", " insert error"); } printf("what you wanna to deleted value is %d\n", deleteNode(pHead,4)); tranverse(pHead); free(pHead); return 0; } /** * [createLinkedList: create the linkedList] * @return [the pointer to the headNode] */ PNode createLinkedList(void){ int length =0;//length of list. int value; PNode pHead = NULL;//the root pointer. PNode pTail = NULL;//the tail pointer or understand as the current node pointer. PNode pNew = NULL;//the pointer to point new node. printf("%s\n","please input the length you wanna to create the linkedList:"); scanf("%d",&length); pHead = malloc( sizeof(Node) ); pHead->pNext = NULL; pTail = pHead; if (NULL== pHead){ printf("%s\n", "out of memory"); exit(-1); } printf("%s\n", "please input the value of every node"); for (int i = 0; i < length; ++i){ scanf("%d",&value); pNew = malloc( sizeof(Node) ); if (NULL==pNew){ printf("%s\n", "out of memory"); exit(-1); }else{ pNew->value = value;//set value to the current node. pTail->pNext = pNew;//in order to previous node point next node. pTail = pNew;//in order to the tail pointer point new node. pTail->pNext = NULL;//if is the last node,set the pointer null. } } return pHead; } /** * [tranverse :tranverse the linkedList] * @param pHead [the pointer to the headNode*(the root pointer)] */ void tranverse(PNode pHead){ if (NULL == pHead){ printf("%s\n", "the linkedList is empty"); } PNode pCurrent = pHead->pNext; while(NULL!=pCurrent){ printf("%d ", pCurrent->value); pCurrent = pCurrent->pNext; } printf("%s\n", "tranverse over!"); } /** * [getLength: get length of list] * @param pHead [the root pointer] * @return [the length] */ int getLength(PNode pHead){ int length = 0; if (NULL == pHead || NULL== pHead->pNext){ return 0; }else{ PNode pCurrent = pHead->pNext; while(NULL!=pCurrent){ length++; pCurrent = pCurrent->pNext; } return length; } }; /** * [insertLinkedLise :insert the value to the linkedList] * @param pHead [the root pointer] * @param positon [the new node wanna to insert positon] * @param value [the value wanna to insert] * @return [if success return 1,other return 0] */ int insertNode(PNode pHead,int position,int value){ if (position<=0 || position>getLength(pHead)){ return 0; } PNode pCurrent = pHead; PNode pPrevious = NULL; PNode pNew = NULL; for (int i = 0; i < position; ++i){ pPrevious = pCurrent; pCurrent = pCurrent->pNext; } /*now the pPrevious has point to the previous node of position node and the current pointer point to the positon node. */ pNew = (PNode)malloc( sizeof(Node)); pNew->value =value; pNew->pNext = pPrevious->pNext; pPrevious->pNext = pNew; return 1; } /** * [deleteNode :delete the node which locaiton has given] * @param pHead [root pointer] * @param position [the position you wanna to delete] * @return [return the value of which you deleted] */ int deleteNode(PNode pHead,int position){ int value = 0; if (position<=0 || position>getLength(pHead)){ exit(-1); }else{ PNode pCurrent = pHead; PNode pPrevious = NULL; for (int i = 0; i < position; ++i){ pPrevious = pCurrent; pCurrent = pCurrent->pNext; } value = pCurrent->value; pPrevious->pNext = pCurrent->pNext; pCurrent=NULL; return value; } }