程序填空题:双链表删除操作
双链表删除操作。
```
#include
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct DuLNode {
ElemType data;
struct DuLNode *prior;
struct DuLNode *next;
} DuLNode, *DuLinkList;
DuLNode *GetElemP_DuL(DuLinkList L, int i) {
int j;
DuLinkList p;
p = L->next;
j = 1;
while (j < i && p) {
p = p->next;
++j;
}
if (!p || j > i)
return NULL;
return p;
}
Status ListDelete_DuL(DuLinkList &L, int i) {
DuLinkList p;
if (!(p = GetElemP_DuL(L, i)))
return ERROR;
@@[p->prior->next = p->next; ](2)
@@[p->next->prior = p->prior;](2)
delete p;
return OK;
}
void CreateDuList(DuLinkList &L,int n) //该函数未显示细节
void print(DuLinkList &L)
{
DuLinkList p;
int flag=1;
p = L->next;
while (p) {
if(flag)
cout << p->data;
else
cout << " "<< p->data;
flag=0;
p = p->next;
}
}
int main() {
int a;
ElemType e;
int n;
DuLinkList L, p;
cin >>n;
CreateDuList(L,n);
cin >> a;
ListDelete_DuL(L, a);
print(L);
return 0;
}
```
答案:
第1空:p->prior->next = p->next;
第2空:p->next->prior = p->prior;
```
#include
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct DuLNode {
ElemType data;
struct DuLNode *prior;
struct DuLNode *next;
} DuLNode, *DuLinkList;
DuLNode *GetElemP_DuL(DuLinkList L, int i) {
int j;
DuLinkList p;
p = L->next;
j = 1;
while (j < i && p) {
p = p->next;
++j;
}
if (!p || j > i)
return NULL;
return p;
}
Status ListDelete_DuL(DuLinkList &L, int i) {
DuLinkList p;
if (!(p = GetElemP_DuL(L, i)))
return ERROR;
@@[p->prior->next = p->next; ](2)
@@[p->next->prior = p->prior;](2)
delete p;
return OK;
}
void CreateDuList(DuLinkList &L,int n) //该函数未显示细节
void print(DuLinkList &L)
{
DuLinkList p;
int flag=1;
p = L->next;
while (p) {
if(flag)
cout << p->data;
else
cout << " "<< p->data;
flag=0;
p = p->next;
}
}
int main() {
int a;
ElemType e;
int n;
DuLinkList L, p;
cin >>n;
CreateDuList(L,n);
cin >> a;
ListDelete_DuL(L, a);
print(L);
return 0;
}
```
答案:
第1空:p->prior->next = p->next;
第2空:p->next->prior = p->prior;