Oct 31, 2013 at 10:06pm UTC
Hi lovelies,
I am a little bit at a dead-end with the project I have been working on. I would just like to be pushed in the right direction on how a linkedlist would work on changing the element already in a list to a different position. Sort of a swap in a way.
For example, I already inserted 1 2 3 4
I want to swap position 1 and 2
1 3 2 4
I thought is was simple enough but for some reason when I try I come up with errors.
Thank you <3!
cout << myList.change(1,2) << endl;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
bool List::change(int first_pos, int second_pos)
{
}
//Header file
#include <iostream>
using namespace std;
typedef int ElementType;
class List
{
public :
List();
~List();
List(const List & original);
void insert(ElementType item, int pos);
void erase(ElementType item);
void traverse();
bool change(int first_pos, int second_pos);
void display(ostream & out) const ;
private :
class Node
{
public :
ElementType data;
Node * next;
Node * value;
Node()
: next(NULL)
{ }
Node(ElementType dataValue)
: data(dataValue), next(NULL)
{ }
};
Node * first;
Node * second;
Node * head;
int mySize;
};
#endif
Last edited on Nov 1, 2013 at 3:50am UTC
Nov 1, 2013 at 2:29am UTC
Thank you I did something like that, but it doesn't affect the code at all. Do I have to do something with Node?
Here is my insert
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
void List::insert(ElementType dataVal, int index)
{
if (index < 0 || index > mySize)
{
cerr << "Illegal location to insert -- " << index << endl;
return ;
}
mySize++;
Node * newPtr = new Node(dataVal);
Node * predPtr = first;
if (index == 0)
{
newPtr->next = first;
first = newPtr;
}
else
{
for (int i = 1; i < index; i++)
predPtr = predPtr->next;
newPtr->next = predPtr->next;
predPtr->next = newPtr;
}
}
Like I did here
Last edited on Nov 1, 2013 at 2:50am UTC
Nov 1, 2013 at 2:49am UTC
Where is value coming from?
Nov 1, 2013 at 3:01am UTC
The errors I was properly figuring out how to use the nodes, and that I could not directly compare them to the positions given. Ha confusing
Nov 1, 2013 at 3:16am UTC
@JLBorges Unfortunately the code still doesn't swap the two numbers
Nov 1, 2013 at 3:41am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <iostream>
#include <algorithm>
struct List
{
struct Node { int value ; Node* next ; };
int mySize = 0 ;
Node* first ;
bool change(int first_pos, int second_pos) ;
};
bool List::change(int first_pos, int second_pos)
{
if ( first_pos > second_pos ) std::swap(first_pos,second_pos) ;
if ( first_pos < 0 || second_pos > mySize) return false ;
if ( first_pos == second_pos ) return true ;
int cnt = 0 ;
Node* n = first ;
for ( ; cnt < first_pos ; ++cnt ) n = n->next ;
Node* first_node = n ;
for ( ; cnt < second_pos ; ++cnt ) n = n->next ;
Node* second_node = n ;
std::swap( first_node->value, second_node->value ) ;
return true ;
}
int main()
{
List lst ;
using Node = List::Node ;
lst.first = new Node{ 0, new Node{ 1, new Node{ 2, new Node{ 3,
new Node{ 4, new Node{5,nullptr } }}}}} ;
lst.mySize = 6 ;
for ( Node* n = lst.first ; n ; n = n->next ) std::cout << n->value << ' ' ;
std::cout << '\n' ;
lst.change(1,4) ;
for ( Node* n = lst.first ; n ; n = n->next ) std::cout << n->value << ' ' ;
std::cout << '\n' ;
lst.change(2,3) ;
for ( Node* n = lst.first ; n ; n = n->next ) std::cout << n->value << ' ' ;
std::cout << '\n' ;
}
http://coliru.stacked-crooked.com/a/97ee2bc051468ef9
Last edited on Nov 1, 2013 at 3:45am UTC
Nov 1, 2013 at 4:20am UTC
Thank you but I can not change my class list, I thank you for your effort. All of you, thank you.
Nov 1, 2013 at 4:34am UTC
He was giving you an example of how one works. So you can learn from it and modify your code from what you learned he didn't want you do copy/paste his so yours would work :P
Nov 1, 2013 at 4:46am UTC
No worries, I have been trying to manipulate it.
Last edited on Nov 1, 2013 at 4:47am UTC
Nov 1, 2013 at 7:20am UTC
Hmm its seems I can't figure it out.