answersLogoWhite

0


Best Answer

Starting with a pointer to the pointer to the first element (p1**), verify the first pointer is not null (*p1 != null), retrieve the pointer to the second element (*p2 = *p1.next) and verify it is also not null (*p2 != null), and then retrieve the pointer to the third element (*p3 = *p2.next). Note that *p3 might be null, but *p1 and *p2 must not be null, or the swap can not be performed. Note that using pointer to pointer syntax allows you to treat pointer to first element the same as pointer to subsequent element, i.e. to not need to handle the special case. Also, since you do need to modify the pointer, you need a pointer to the pointer. Set *p1 = *p2, *p3 = *p1, and *p2 = *p3. Note that these assignments must be done using the original values, not the intermediate value, so you will need some temp pointers. The end result is that **p1 will be ordered after **p2.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

#include<iostream>

#include<cassert>

int main()

{

int a = 42;

int b = 24;

int* pa = &a;

int* pb = &b;

(*pa) ^= (*pb) ^= (*pa) ^= (*pb);

assert (a==24);

assert (b==42);

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

function swap(head, p)

inputs: pointer to the head of the list

pointer p to a node to swap with its neighbor

will exchange nodes p and p->next

local variables - pointers to list nodes : q = head, temp

if (p->next != NULL)

while (q->next != p) q = q->next;

q->next = p->next;

temp = p->next->next;

p->next->next = p;

p->next = temp;

end if p is not at the end of the list

end function swap

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

You don't. This is a classic mistake perpetuated through the misconception that adjusting node pointers is more efficient than moving huge amounts of data around. While that is undoubtedly true, there is no rule that states that the data must be placed in the node itself. The data can be stored externally to the list, and the nodes can simply point at that data, thus you only need to swap the data pointers. There is no need to deal with the special case when nodes are adjacent (which also requires working out which comes first) as well as the more general case when they are non-adjacent. Your swap code essentially reduces to a normal swap with a temporary pointer:

void list::swap( node &x, node &y)

{

data_pointer* temp = x.data;

x.data = y.data;

y.data = temp;

}

Note that there's no need to test if x and y refer to the same node because in the vast majority of cases the nodes will be different nodes. The actual swap is so efficient that it more than compensates for the odd case where the two nodes might refer to each other, but the calling code should ensure that's never the case anyway.

As a general rule, if the data size is less than or equal to a pointer (4 bytes on a 32-bit system), then you can store the data in the node and swap the data directly. Otherwise you should point to it and swap the data pointers (not the node pointers). But if you know in advance that the data will never be moved around the list then you can store the data in the nodes themselves, regardless of the size of the data.

The node pointers in any list only need adjusting during an insertion or an extraction of an individual node. Even then, an extraction simply adjusts the node pointers of the nodes on either side of the extracted node, never the node itself. This is known as detaching. The node still knows its correct place in the list meaning it can be easily re-attached. this is handy when you want to temporarily remove nodes without affecting their order. If you detach several nodes, then you must re-attach them in reverse order (last out, first in).

Obviously if the nodes point at the data rather than store the data there is a cost in memory consumption (4 bytes per node on a 32-bit system). However, you only need to do that when you need to move data around the list (such as sorting). The speed benefit more than compensates for the extra memory consumed because there is no need to test if the nodes are adjacent, which requires far more code than is necessary.

In the interest of completeness, here's the code you need to swap two adjacent node pointers:

void CList::swapadjacent( CNode &x, CNode &y )

{

if( x.prev ) x.prev->next = &y;

if( y.next ) y.next->prev = &x;

y.prev = x.prev;

x.next = y.next;

x.prev = &y;

y.next = &x;

}

Note the this method assume x is previous to y. This is catered for by the main swap method but before we get to that, here's the code to handle non-adjacent nodes:

void CList::swapnonadjacent( CNode &x, CNode &y )

{

CNode temp;

temp.prev = x.prev;

temp.next = x.next;

x.prev = y.prev;

x.next = y.next;

y.prev = temp.prev;

y.next = temp.next;

}

Finally, the main swap method, which makes the correct call and adjusts the head and tail if necessary.

void CList::swap( CNode &x, CNode &y )

{

if( &x &y ) ? &x : tail;

}

Note that all methods should be declared private to the list. Only the sorting method and any other methods that need to sort nodes need be public.

Clearly this is a great deal of effort compared with swapping data pointers. But each to his own.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Voila:

typedef float DataType;

void swap(DataType* const a, DataType* const b) {

DataType c = *a;

*a = *b;

*b = c;

}

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

you don't you don't

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How swapping two numbers using pointer in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

Call by reference using pointer in c plus plus?

Example: void foo( MyClass&amp; object ){} // function with call by reference signature MyClass* p = new MyClass(); // instantiate a pointer to MyClass foo( *p ); // call by reference using the pointer


Swapping of two nos using pointer in c plus plus?

There is no benefit in swapping two numbers using a pointer. It's actually quicker to swap the two numbers themselves. However the following shows how to swap values by changing the pointers to those value. #include &lt;iostream&gt; int main() { int X = 5; int Y = 6; // Point to the values in X and Y. int *pX = &amp;X; int *pY = &amp;Y; // Print current values and the values being pointed at: std::cout &lt;&lt; "Before swapping:" &lt;&lt; std::endl; std::cout &lt;&lt; " X = " &lt;&lt; X &lt;&lt; ", Y = " &lt;&lt; Y &lt;&lt; std::endl; std::cout &lt;&lt; "pX = " &lt;&lt; *pX &lt;&lt; ", pY = " &lt;&lt; *pY &lt;&lt; std::endl; // Swap the pointers. pX = &amp;Y; pY = &amp;X; // Print current values and the values being pointed at: std::cout &lt;&lt; "After swapping:" &lt;&lt; std::endl; std::cout &lt;&lt; " X = " &lt;&lt; X &lt;&lt; ", Y = " &lt;&lt; Y &lt;&lt; std::endl; std::cout &lt;&lt; "pX = " &lt;&lt; *pX &lt;&lt; ", pY = " &lt;&lt; *pY &lt;&lt; std::endl; return( 0 ); }


Add two numbers in pointer in using c plus plus?

int* a = new int(40); int* b = new int(2); int x = *a + *b; // x = 42 delete b; delete a;


What is Dazzling Pointer in c plus plus?

The pointer that points to a block of memory that does not exist is called a dazzling pointer or wild pointer


Find the greater no between two nos using pointer in c plus plus programming?

if (*&amp;a &gt; *&amp;b) *&amp;max = *&amp;a; else *&amp;max = *&amp;b;


What is 'this' pointer in c plus plus?

Address of the current object.


Why pointer is used to reference a string of characters write C plus plus statements that display the text hello using pointer to a string notation?

We use a pointer to reference a string because a string is an array of characters where every element is a char (or a wchar_t if using UNICODE strings). Passing arrays by value would require the entire array to be copied, but passing a pointer variable to an array only copies the pointer, which is effectively the same as passing the array by reference. #include &lt;iostream&gt; int main() { char * psz = "hello"; // pointer to a null-terminated string. std::cout &lt;&lt; psz; // pass the pointer (by value) to the insertion operator. return( 0 ); }


How do you alternate the input numbers using for loop in c plus plus?

Input a variable.


What is null object in c plus plus?

a pointer that is not pointing to anything


How do you get the number 13 using only numbers 1 and 5 and 7 and 8 using the numbers only once?

8 plus 1 , minus 5, plus 7 + 13


What changes the pointer to a plus icon in excel?

The application does that when the pointer is over the cells region of the spreadsheet.


Coding in c plus plus to swap two nos using pointer?

void swap (int &amp;pa, int &amp;pb) { *pa ^= *pb; *pb ^= *pa; *pa ^= *pb; }