仅交换双链接列表中的前2个节点
Swap first 2 nodes only in a doubly linked list
在反向函数中,我试图只交换双链表中的前两个节点,而不交换数据。它运行,但不会交换列表中的前2个节点。有人能告诉我我做错了什么吗?
struct node {
int data;
node * p; // FORWARD LINK
node * rp; // REVERSE LINK
};
ostream & operator<<(ostream &, const node *);
void addFront(node * & start, int);
void cleanUp(node *);
void reverse(node * &);
void main()
{
node * a = NULL;
cout << "EMPTY LIST CASE:n";
cout << "BEFORE a isn" << a << endl;
reverse(a);
cout << "AFTER a isn" << a << endl;
cleanUp(a);
a = NULL;
addFront(a, 10);
cout << "nONE ELEMENT LIST CASE:n";
cout << "BEFORE a isn" << a << endl;
reverse(a);
cout << "AFTER a isn" << a << endl;
cleanUp(a);
a = NULL;
addFront(a, 30);
addFront(a, 20);
cout << "nTWO ELEMENT LIST CASE:n";
cout << "BEFORE a isn" << a << endl;
reverse(a);
cout << "AFTER a isn" << a << endl;
cleanUp(a);
a = NULL;
addFront(a, 60);
addFront(a, 50);
addFront(a, 40);
cout << "nTHREE ELEMENT LIST CASE:n";
cout << "BEFORE a isn" << a << endl;
reverse(a);
cout << "AFTER a isn" << a << endl;
cleanUp(a);
a = NULL;
addFront(a, 400);
addFront(a, 300);
addFront(a, 200);
addFront(a, 100);
cout << "nFOUR ELEMENT LIST CASE:n";
cout << "BEFORE a isn" << a << endl;
reverse(a);
cout << "AFTER a isn" << a << endl;
cleanUp(a);
}
void reverse(node * & s)
{
node * n1 = s;
node * n2 = s;
if (n1 == NULL) return;
node * temp = new node;
temp->rp = n1->rp;
temp->p = n1->p;
n1->rp = n2->rp;
n1->p = n2->p;
n2->rp = temp->rp;
n2->p = temp->p;
if (n1->p != NULL)
n1->p->rp = n1;
if (n1->rp != NULL)
n1->rp->p = n1;
if (n2->p != NULL)
n2->p->rp = n2;
if (n2->rp != NULL)
n2->rp->p = n2;
delete temp;
}
void addFront(node * & start, int x)
{
node * t = new node;
t->data = x;
if (start != NULL)
{
t->p = start;
t->rp = NULL;
start->rp = t;
}
else
{
t->p = NULL;
t->rp = NULL;
}
start = t;
}
void cleanUp(node * s)
{
node * walker, *prev;
walker = s;
while (walker != NULL)
{
prev = walker;
walker = walker->p;
delete prev;
}
}
ostream & operator<<(ostream & w, const node * s)
{
const node * walker = s;
const node * trailer = walker;
w << "Forward Print " << endl;
if (s == NULL)
{
w << "EMPTY LIST";
}
else
{
while (walker != NULL)
{
w << walker->data << ' ';
trailer = walker;
walker = walker->p;
}
}
w << endl << "Reverse Print " << endl;
if (trailer == NULL)
{
w << "EMPTY LIST";
return w;
}
while (trailer != NULL)
{
w << trailer->data << ' ';
trailer = trailer->rp;
}
return w;
}
更新了完整的程序。
以下是更新的reverse
:
void reverse(node * & s)
{
node * n1 = s;
if (n1 == NULL) return;
node * n2 = n1->p;
if (n2 == NULL) return;
node *temp;
node *n3;
n3 = n2->p;
temp = n1->rp;
n1->p = n3;
n1->rp = n2;
n2->rp = temp;
n2->p = n1;
if (n3 != NULL)
n3->rp = n1;
s = n2;
}
注意:即使temp
必须保持您定义的状态,也没有必要分配和删除它。我会使用node temp
并用temp.blah
替换temp->blah
相关文章:
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- 为什么我的双向链表删除函数会删除多个节点?
- 检查两个节点在子节点上是否具有相同状态的更优雅的方法
- 比较两个节点坐标的最佳方法是什么?
- 生成所有具有 n 个节点的无向图
- 使用 Dijkstra 算法跟踪两个节点之间的最短路径
- 只有我的"链接列表"中的第一个节点正在打印
- 双向链表 - 无法删除第一个节点
- 如何仅在 2 个节点之间获取最短路径,给定邻接列表有向图?
- 图问题:找出两个节点是否在每个节点的O(1)时间和O(2)存储中共享同一分支
- 如何在单个链表中交换两个节点的位置,只修改指针
- 如何检查一行中的3个节点是否相同C++?
- 如何遍历具有两个节点的链接节点
- 链表中头/第一个节点的声明
- 查找树中两个节点之间的最大成本边
- 链表.如何删除列表中的第一个节点?
- 链表 C++中的多个节点
- C MPI,使用多个节点,首先在节点级别上降低,然后减小到头节点
- 查找树(不属于任何特定类型的简单连接树)中两个节点之间的路径
- 链接列表正在抛出异常,而不是打印第一个节点