做c的开发有1年多了,期间写过c++,感觉基础不够好,补上去,不丢人。o(^▽^)o
to better myself.
#include <stdio.h> #include <stdlib.h> typedef struct node { int data; struct node* next; }Node; typedef Node* List; void pInitList(List*); List initList(); void deleteList(List); int isNull(List); void insertNode(Node*, int); void deleteNode(List, Node*); Node* findLast(List); Node* findValue(List, int); void printList(List); void printList(List list) { if(list == NULL) { printf("empty list. printf nothing. "); return; } Node* curNode = list->next; while(curNode != NULL ) { if(curNode->next != NULL) { printf("%d ->", curNode->data); }else{ printf("%d ", curNode->data); } curNode = curNode->next; } } Node* findValue(List list, int tarVal) { if(list == NULL) { printf("empty list. "); return NULL; } Node* curNode = list->next; while((curNode->data != tarVal ) &&(curNode != NULL)) { curNode = curNode->next; } if(curNode == NULL) { printf("not find the value %d ", tarVal); free(curNode); return NULL; } printf(" find the value %d ", tarVal); return curNode; } Node* findLast(List list) { if(list == NULL) { printf("empty list. "); return NULL; } Node* curNode = list->next; while(curNode->next != NULL) { curNode = curNode->next; } printf("find the last node value is %d. ", curNode->data); return curNode; } void deleteNode(List list, Node* delNode) { Node* curNode = (Node*)malloc(sizeof(Node)); Node* freeNode = NULL; if(curNode == NULL) { printf("malloc error at line %d.", __LINE__); return; } curNode = list->next; if(curNode->data == delNode->data) { list->next = NULL; free(curNode); return ; } while((curNode != NULL) && (curNode->next->data != delNode->data) && (curNode->next != NULL)) { curNode=curNode->next; } if(curNode->next == NULL || (curNode ==NULL)) { printf("can not find the given node. "); return ; }else{ freeNode = curNode->next; curNode->next = freeNode->next; free(freeNode); } } void insertNode(Node* curNode, int newValue) { Node *newNode = (Node*) malloc(sizeof(Node)); if(newNode == NULL) { printf("malloc error at line %d.", __LINE__); return; } newNode->data = newValue; newNode->next = curNode->next; curNode->next = newNode; } /*return 0 means is not NULL*/ int isNull(List list ) { //printf("next value %d ", list->next->data); return (list->next == NULL); } #if 1 void pInitList(List* list) { //*list = (Node*) malloc(sizeof(Node)); (*list)->next = NULL; return; } #endif List initList() { List list; list = (Node*) malloc(sizeof(Node)); list->next = NULL; return list; } void deleteList(List list) { Node* freeNode = NULL; if(list == NULL) return; while(list->next != NULL) { //get the free node freeNode = list->next; list->next = freeNode->next; free(freeNode); } return ; } int main() { printf("hello. "); #if 0 List newList = NULL; newList = initList(); #else List *list = NULL; //这里我第一次就没分配1级指针的内存,导致出错,感谢帮助我指出的人。@2016-07-26 00:54:22 list = (List*)malloc(sizeof(List)); *list = (Node*)malloc(sizeof(Node)); pInitList(list); insertNode((*list), 1); insertNode((*list)->next, 2); insertNode((*list)->next->next, 3); findValue(*list, 2); findLast(*list); printList(*list); Node *delNode = (Node*)malloc(sizeof(Node)); delNode->data = 2; deleteNode(*list, delNode); printList(*list); if(isNull(*list)){ printf("list is empty. "); }else{ printf("list is not empty. "); } #endif return 0; }