#include <iostream>
using namespace std;
struct node {
int data;
node* llink, * rlink;
};
node* createList(int n) {
node* first = new node;
first->llink = first->rlink = first;
first->data = 0;
node* q=first;//q是p的前驱指针。
int i = 0;
for (i = 0; i < n; i++) {
node* p = new node;
cin >> p->data;
p->llink = q; q->rlink = p;
p->rlink = first; first->llink = p;
q = p;
}
return first;
}
void output(node*first) {
if (first == first->llink) {
cout << "此表为空" << endl;
return;
}
cout << "后驱输出:按顺序" << endl;
node* p = first->rlink;
while (p != first) {
cout << p->data << " " << endl;
p = p->rlink;
}
cout << "前驱输出:倒序" << endl;
p = first->llink;
while (p != first) {
cout << p->data << " " << endl;
p = p->llink;
}
}
int main()
{
//创立
cout << "请输入你要创建的节点的个数:" << endl;
int n;
cin >> n;
node* first =createList(n);
//输出
output(first);
}
C++创键带头结点的双向链表进行双向输出
最新推荐文章于 2023-05-16 16:17:24 发布