单链表相关操作之C语言实现:插入,删除,倒转,复制,查找。。。
来源:互联网 发布:游戏充值网站源码 编辑:程序博客网 时间:2024/06/14 11:15
typedef struct node { node *next; /* pointer to next element in list */ char data; }node, *pnode, **ppnode; // define two types // create an empty node pnode list_createNullNode() { pnode p1; if (p1 = (pnode)malloc(sizeof(node))) { p1->next = NULL; } return p1; } // create a node with data pnode list_createNode(char c) { pnode p1; if (p1 = (pnode)malloc(sizeof(node))) { p1->data = c; p1->next = NULL; } return p1; } // delete head void list_removeHead(node **p) { pnode n = *p; if (*p == NULL) return; *p = (*p)->next; free(n); } // delete the tail void list_removeTail(node **p) { pnode n = *p; if (*p == NULL) return; if ((*p)->next == NULL) { *p = NULL; free(n); return; } while (n->next->next) n = n->next; free(n->next); n->next = NULL; } // delete a node bool list_deleteNode(node **p, pnode n) { pnode m = *p; pnode prem; if ((*p == NULL)||(n == NULL)) return false; if (*p == n) { *p = (*p)->next; free(m); return true; } prem = m; m = m->next; while ((m != n)&&(m != NULL)) { prem = m; m = m->next; } if (m == NULL) return false; else { prem->next = m->next; free(m); return true; } } // reverse a list using iteration void list_reverseList(node **p) { if (*p == NULL) return; pnode pren = *p; pnode n = (*p)->next; pnode m; while (n != NULL) { m = n->next; pren->next = n->next; n->next = *p; *p = n; n = m; } } node* list_reverseList(node* &head){ node* p = head; node* prep = NULL; node* postp = NULL; while (p != NULL) { postp = p->next; // keep a record of next node p->next = prep; prep = p; p = postp; } head = prep; return head;}// free the whole list bool list_freeList(pnode head) { pnode n; if (head == NULL) return true; while( head != NULL) { n = head; head = head->next; free(n); } return true; } // search data in a list pnode list_searchData(pnode head, char c) { pnode n = head; if (head == NULL) return NULL; while (n != NULL) { if (n->data == c) { return n; } n = n->next; } return NULL; } // duplicate list pnode list_duplicateList(pnode head) { pnode newhead; pnode newnode; if (head==NULL) return NULL; newhead = list_createNode(head->data); if (newhead == NULL) return newhead; pnode p = head; pnode q = newhead; q->data = p->data; q->next = NULL; while(p->next != NULL) { newnode = list_createNode(p->next->data); if (newnode == NULL) { // memory allocation failed in the middle, return NULL and free all allocated memory. // list_emptyList(newhead); } q->next = newnode; p = p->next; q = q->next; } return newhead; } // print the whole list void list_printList(node *p) { if (p == NULL) { printf("list is empty!\n"); return; } printf("The current list is: "); while (p != NULL) { printf("%c ",p->data); p = p->next; } printf("\n"); } // append a data item to the end of the list bool list_appendData(node **p, char c) { pnode n = list_createNode(c); if (n==NULL) return false; // pnode q = *p; // if (q==NULL){q = n; return true;} // this doesn't work since I use local variable if (*p==NULL) { *p = n; return true; } pnode m = *p; while (m->next != NULL) m = m->next; m->next = n; return true; } // append a node to the end of the list bool list_appendNode(node **p, node *q) { if (q==NULL) return false; if (*p==NULL) { *p = q; return true; } pnode m = *p; while (m->next != NULL) m = m->next; m->next = q; return true; } // insert a data item to the head of list bool list_addData2Head(node **p, char c) { pnode n = list_createNode(c); if (n == NULL) return false; n->next = *p; n->data = c; *p = n; return true; } // insert a node to the head of list bool list_addNode2Head(node **p, pnode n) { if (n == NULL) return false; n->next = *p; *p = n; return true; } int main(void) { node *head = NULL; //pnode head = NULL; //pnode = 0; pnode newhead; pnode p1, p2; char c; p1 = list_createNode('d'); p2 = list_createNullNode(); p2->data = 'e'; // newhead = list_duplicateList(head); list_printList(newhead); if (!list_appendData(&head, 'a')) return 0; if (!list_appendData(&head, 'b')) return 0; if (!list_appendData(&head, 'c')) return 0; if (!list_addData2Head(&head, 'd')) return 0; if (!list_addNode2Head(&head, p2)) return 0; list_printList(head); //e d a b c /* c = 'a'; if (list_searchData(head, c)) printf("It is in the list!/n"); else printf("It is not in the list!/n"); */ c = 'f'; if (list_deleteNode(&head,list_searchData(head,c))) printf("The node has been deleted!\n"); else printf("Deleting node failed!\n"); list_printList(head); list_reverseList(&head); list_printList(head); list_printList(list_reverseList(head)); list_printList(head);/* newhead = list_duplicateList(head); list_printList(newhead); if (list_freeList(newhead)) printf("List has been freed!/n"); else printf("List cannot be freed!/n"); */ return 0; }