程序填空题:创建双链表
创建双链表。
```
#include
using namespace std;
typedef int ElemType;
typedef struct DuLNode {
ElemType data;
struct DuLNode *prior;
struct DuLNode *next;
} DuLNode, *DuLinkList;
void CreateDuList(DuLinkList &L,int n) {
DuLinkList r, p;
int length = 0;
L = new DuLNode;
L->next = NULL;
L->prior=L;
r = L;
while (length p = new DuLNode;
cin >> p->data;
p->next = NULL;
@@[r->next = p; ](2)
r = p;
@@[p->prior = L->prior; ](2)
L->prior = p;
length++;
}
}
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 n;
cin >> n;
DuLinkList L;
CreateDuList(L,n);
print(L);
return 0;
}
```
答案:
第1空:r->next = p;
第2空:p->prior = L->prior;
```
#include
using namespace std;
typedef int ElemType;
typedef struct DuLNode {
ElemType data;
struct DuLNode *prior;
struct DuLNode *next;
} DuLNode, *DuLinkList;
void CreateDuList(DuLinkList &L,int n) {
DuLinkList r, p;
int length = 0;
L = new DuLNode;
L->next = NULL;
L->prior=L;
r = L;
while (length
cin >> p->data;
p->next = NULL;
@@[r->next = p; ](2)
r = p;
@@[p->prior = L->prior; ](2)
L->prior = p;
length++;
}
}
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 n;
cin >> n;
DuLinkList L;
CreateDuList(L,n);
print(L);
return 0;
}
```
答案:
第1空:r->next = p;
第2空:p->prior = L->prior;