// #include <iostream> // using namespace std; // // typedef struct Node // { // int data; // struct Node* next; // }Node, *pNode; // // pNode Init(int* a, int len) // { // int i; // pNode head, p, q; // head = new Node(); // head->data = a[0]; // q = head; // // for (i = 1; i < len; ++i) // { // p = new Node(); // p->data = a[i]; // q->next = p; // q = p; // } // q->next = NULL; // // return head; // } // // void deleteNode(pNode p, int n) // { // pNode q; // if (p == NULL) // { // return; // } // while(p->data != n) // { // q = p; // if (p->next != NULL) // { // p = p->next; // } // else // return; // } // if (p->data == n) // { // q->next = p->next; // delete p; // } // } // // void printList(pNode p) // { // while(p) // { // cout<<p->data<<" "; // if (p->next) // { // p = p->next; // } // else // { // cout<<endl; // break; // } // } // } // // int main(void) // { // pNode head; // int a[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; // int N = sizeof(a) / sizeof(int); // // head = Init(a, N); // printList(head); // deleteNode(head, 11); // printList(head); // // return 0; // } #include <iostream> using namespace std; typedef struct Node { int data; struct Node* next; }Node, *pNode; pNode Init(int* a, int len) { int i; pNode head, p, q; head = new Node(); head->data = a[0]; q = head; for (i = 1; i < len; ++i) { p = new Node(); p->data = a[i]; q->next = p; q = p; } q->next = NULL; return head; } void deleteNode2(pNode p) { pNode q; if (p == NULL) { return; } while(p->next) { p->data = p->next->data; q = p; p = p->next; } q->next = NULL; delete p; } void printList(pNode p) { while(p) { cout<<p->data<<" "; if (p->next) { p = p->next; } else { cout<<endl; break; } } } int main(void) { pNode p, head; int a[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; int N = sizeof(a) / sizeof(int); head = Init(a, N); p = head; printList(head); while(p->data != 11) { p = p->next; } deleteNode2(p); printList(head); return 0; }