I have to swap the node with the entered value with the head node. It must swap the links, not the data. I wrote a code but it does not work. It makes the entered node head and then It deletes the other nodes.
For example, if the linked list is: 0 1 2 3 4 5 and I enter 3, the result is 3 4 5. However, it must be 3 1 2 0 4 5.
Could you please help me find my mistake? Thanks a lot.
Here is my function in the cpp file:
void IntSLList::swap_nodes(int val)
{
if (head == 0)
{
cout << "\nThe linked list is empty.\n";
}
else
{
int control = 0;
for (IntSLLNode *tmp = head; tmp != 0; tmp = tmp->next)
{
if (tmp->info == val)
{
control = 1;
}
}
if (control == 0)
{
"\nThe entered value is not in the linked list.\n";
}
else
{
if ((head == tail) || (head->info == val))
{
cout << "\nThe linked list will be printed exactly the same\n";
}
else
{
IntSLLNode *prev = NULL, *cur = NULL, *head_copy = NULL, *first = NULL;
for (IntSLLNode *tmp = head; tmp->next != 0; tmp = tmp->next)
{
if (tmp->next->info == val)
{
cur = tmp->next;
prev = tmp;
}
}
head_copy = head;
first = head->next;
head = cur;
head->next = cur->next;
cur = head_copy;
cur->next = first;
prev->next = head;
}
}
}
}
Here is the header file:
#ifndef INT_LINKED_LIST
#define INT_LINKED_LIST
class IntSLLNode {
public:
IntSLLNode() {
next = 0;
}
IntSLLNode(int el, IntSLLNode *ptr = 0) {
info = el; next = ptr;
}
int info;
IntSLLNode *next;
};
class IntSLList {
public:
IntSLList() {
head = tail = 0;
}
~IntSLList();
int isEmpty() {
return head == 0;
}
void addToHead(int);
void addToTail(int);
int deleteFromHead(); // delete the head and return its info;
int deleteFromTail(); // delete the tail and return its info;
void deleteNode(int);
bool isInList(int) const;
void printAll() const;
void sort_list();
void swap_nodes(int);
int find_min();
private:
IntSLLNode *head, *tail;
};
#endif
Here is the main.cpp:
#include <iostream>
using namespace std;
#include "intSLList.h"
int main() {
IntSLList list, list2;
for (int i = 0; i < 6; i ) {
list.addToHead(i);
}
list.printAll();
list.sort_list();
for (int i = 0; i < 6; i ) {
list2.addToTail(i);
}
list2.swap_nodes(3);
list2.printAll();
}
CodePudding user response:
It was difficult to understand your code - so I tried to make the logic simple. Hope I haven't missed anything.
Few points to note:
- Don't use 0 for null pointer, instead use
nullptr
- Don't use integer for boolean flags, use
bool
void IntSLList::swap_nodes(int val)
{
if (head == nullptr) {
cout << "Empty list\n";
return;
}
if (head == tail || head->info == val) {
cout << "\nThe linked list will be printed exactly the same\n";
}
IntSLLNode* beg = head;
IntSLLNode* begNext = head->next;
IntSLLNode* swap = nullptr; // node to be swapped
IntSLLNode* swapNext = nullptr; // node's original next ptr
IntSLLNode* swapPrev = nullptr; // node's original prev ptr
for (IntSLLNode* p = head, *prev = p; p != nullptr; prev = p, p = p->next)
{
if (p->info == val) {
swap = p;
swapNext = p->next;
swapPrev = prev;
break;
}
}
if (swap == nullptr) {
cout << "Not found in list\n";
return;
}
swapPrev->next = beg;
beg->next = swapNext;
swap->next = begNext;
head = swap;
if (tail->next != nullptr) {
// val == tail->info
for (IntSLLNode* p = head; p != nullptr; p = p->next)
{
if (p->next == nullptr) {
tail = p;
}
}
}
}
CodePudding user response:
Here is an ASCII rendition of the procedure.
Translating to code left as an exercise.
head->0->1->2->3->4->5
----------
here
|
v
head->0->1->2->3->4->5
----------
prev here
| |
v v
head->0->1->2->3->4->5
^
|
tail
----------
here
----- |
v | v
head->0 1->2 3->4->5
^
|
tail
----------
here
----- |
v | v
head->0 1->2 3->4->5
| ^ ^
| | |
| tail |
| |
-----------
----------
here
----- |
v | v
head->0 1->2 3 4->5
| ^ | ^
| |---- |
| tail |
| |
-----------
----------
------------------
| |
| here |
| ----- |----
| v | v
head 0 1->2 3 4->5
| ^ | ^
| ---- |
| |
| |
-----------
----------
------------------
| |
| ----
| ----- |
| v | v
head 0 1->2 3 4->5
| ^ | ^
| ---- |
| |
| |
-----------