00:01
We're going to start with this question is asking us to do a swap within a link list and a dual link list.
00:07
I'm going to start by showing the coding example for the single link list.
00:13
So within our node class, we just have int data and then node.
00:20
Next, which is going to be our pointer to say, okay, this node is next in the list.
00:25
We have a method built out here, print list, and it takes a node.
00:30
At the head of the list and then just traverses the list until it gets to the end and we can tell that by the next node pointing to null and then we have our swap node method here which is actually going to perform the swap we actually don't need this temp line here but what's going to happen is node there's going to be two nodes passed as parameters node one is going to point to what node two is currently pointing to and then node two is just going to point to node 1.
01:03
So that's how the swap is going to be handled there.
01:06
And with a single link list, that's all that has to be performed.
01:09
So it's relatively easy.
01:10
Here in our main code, we have our declarations of our actual node objects.
01:14
We have node 1, node 2, node 3.
01:18
We have, again, declaring the new node.
01:22
Then for n1, we're declaring the data set.
01:26
And then we're telling it to point to node 2 first.
01:29
And node 2 is going to take 2 as its dataset and going to point to node 3 thereafter.
01:37
Node 3 is just going to take the dataset since it's at the tail of the link list.
01:41
It's not going to point to anything.
01:43
It's just going to point to null.
01:45
So we're going to perform a print of the list prior to the swap, and then we're going to do the swap, and then thereafter list the actual, the list thereafter starting with the n2 node that's going to be putting at the header of the list.
02:02
So i'm going to run this guy.
02:08
And as we could see, 1, 2, 3 is the list before the swap, and afterwards is 2 .13.
02:15
Now it's important to notice we didn't just change the dataset here.
02:19
We actually changed the pointer node, meaning we actually did the swap of the nodes.
02:24
We did not just change the data within the nodes.
02:27
If you wanted to do that, it'd be probably a little easier, but that's not what we're we're asked to do here.
02:32
Next we're us to do a dual -linked list and this is practically the same with a little more complication.
02:40
So here we have our example for the dual -linked list.
02:43
The class node is going to be set up a little differently because we have a previous node to be considered.
02:50
That's how dual links are set up.
02:51
We take a next node pointer and a previous node pointer and then we have our two methods here.
02:57
We're going to be printing the list forward and then printing the list backward, practically the same method except it's going to start at the tail node and traverse this way to the front of the list.
03:11
The swap node method is going to be different in that it takes three node arguments or parameters, and what's going to happen is node two is going to point to node two's previous pointer is going to accept a point.
03:29
It's going to accept n1's previous pointer, then n1 is going to, n1's next pointer is going to point to n2's next pointer...