刪除節點是插入操作的逆過程。在雙向循環鏈表中,若要刪除連續三個節點 s、p、q 中的 p,只需將 s->rlink 指向 q,q->llink 指向 s,然後釋放 p 的記憶體即可。
void del(stud *p) {
(p->rlink)->llink = p->llink;
(p->llink)->rlink = p->rlink;
free(p);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
typedef struct node {
char name[20];
struct node *llink, *rlink;
} stud;
stud *creat(int n) { /* 建表代碼同上,略 */ }
stud *search(stud *h, char *x) { /* 查找代碼同上,略 */ }
void print(stud *h) { /* 列印代碼同上,略 */ }
void del(stud *p) { /* 刪除實現見上 */ }
int main() {
int number = N;
char studname[20];
stud *head, *searchpoint;
head = creat(number);
print(head);
printf("請輸入要刪除的人的姓名:");
scanf("%s", studname);
searchpoint = search(head, studname);
if (searchpoint) {
del(searchpoint);
printf("刪除後:\n");
print(head);
}
return 0;
}