int lengthNode(Node *p) { int n = 0; Node *q; q = p->next; while(q != NULL) { ++n; q = q->next; } return n; }
void deleteNode(Node *p, int n, int &x) { int length ; length = lengthNode(p); int k = 0; Node *q,*r; if(n<=0 || n> length) { printf("The position is not suitable!\n"); } else{ q = p; while( k != (n-1)) { q = q->next; ++k; } r = q->next; q->next = r->next; x = r->data; free(r); } }
int insertNode(Node *p,int position,int x) { if(position<1 || position>lengthNode(p)) { printf("The position is not suitable, insert failed!\n"); return 0; } Node *r, *q; r = p->next; int n = 1; while(n!= position-1) { ++ n; r = r->next; } q = (Node *)malloc(sizeof(Node)); q ->data = x; q ->next = r->next; r->next = q; return 1; }