程序填空题:双链表插入操作
双链表插入操作。
```
#include
using namespace std;
typedef int Status;
typedef int ElemType;
typedef struct DuLNode {
ElemType data;
struct DuLNode *prior;
struct DuLNode *next;
} DuLNode, *DuLinkList;
DuLinkList 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 ListInsert_DuL(DuLinkList &L, int i, ElemType e) {
DuLinkList s, p;
if(!(p=GetElemP_DuL(L,i)))
return 0;
s = new DuLNode;
s->data = e;
@@[s->prior = p->prior; ](2)
@@[p->prior->next = s; ](2)
@@[s->next = p; ](2)
@@[p->prior = s; ](2)
return 1;
}
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;
cin >> e;
ListInsert_DuL(L, a, e);
print(L);
return 0;
}
```
答案:
第1空:s->prior = p->prior;
第2空:p->prior->next = s;
第3空:s->next = p;
第4空:p->prior = s;
```
#include
using namespace std;
typedef int Status;
typedef int ElemType;
typedef struct DuLNode {
ElemType data;
struct DuLNode *prior;
struct DuLNode *next;
} DuLNode, *DuLinkList;
DuLinkList 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 ListInsert_DuL(DuLinkList &L, int i, ElemType e) {
DuLinkList s, p;
if(!(p=GetElemP_DuL(L,i)))
return 0;
s = new DuLNode;
s->data = e;
@@[s->prior = p->prior; ](2)
@@[p->prior->next = s; ](2)
@@[s->next = p; ](2)
@@[p->prior = s; ](2)
return 1;
}
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;
cin >> e;
ListInsert_DuL(L, a, e);
print(L);
return 0;
}
```
答案:
第1空:s->prior = p->prior;
第2空:p->prior->next = s;
第3空:s->next = p;
第4空:p->prior = s;