void reverse( node * & s)
{
// NOTE: YOU CAN NOT MOVE DATA FROM ONE NODE TO ANOTHER
// YOU CAN ONLY MOVE POINTERS FROM ONE NODE TO ANOTHER
// Grab the first node`
// Create
startNode = s;
node *guest,*secondToLast,*secondElem;
guest=secondToLast=s;
// Return true if list is only 1 element long
if (s->next == NULL)
return true;
// Grab the second element in the list
secondElem = s->next;
// Grab the second to last element in the list
while(guest->next!=NULL)
{
secondToLast=guest;
guest=guest->next;
}
// Set the second to last node to now point to the original first node
secondToLast->next=startNode;
guest->next=secondElem;
return true;
5 2469
What is this function supposed to do?
In what way is it not doing what you want it to do?
Donbock,
I am just trying to change the syntax from psudo to actual code, I have included the rest of the code in this email
The fuction is supposed to switch the first and last node in a linked list by just changing the pointers
The code is shooting errors not the least of which cannot convert from node * to int
Thanks again for any help,
Goldenteeplanet - #include <iostream>
-
using std::cout;
-
using std::cin;
-
using std::endl;
-
using std::ostream;
-
-
struct node {
-
int data;
-
node * p;
-
};
-
-
void reverse(node * & ); // Student Written
-
-
void addFront( node * & start, int);
-
-
void print( ostream & , node *);
-
-
void cleanUp( node *);
-
-
ostream & operator<<( ostream &, const node *);
-
-
void build( node * & );
-
-
void main()
-
{
-
-
node * a = NULL;
-
build(a);
-
cout << "Test of general case" << endl;
-
cout << "a is " << a << '\n';
-
reverse(a);
-
cout << "after reverse, a is " << a << endl << endl;
-
-
cleanUp(a);
-
a = NULL;
-
cout << "Test of empty list" << endl;
-
cout << "a is " << a << '\n';
-
reverse(a);
-
cout << "after reverse, a is " << a << endl << endl;
-
-
cout << "Test of 1 element list" << endl;
-
addFront(a,100);
-
cout << "a is " << a << '\n';
-
reverse(a);
-
cout << "after reverse, a is " << a << endl << endl;
-
-
-
addFront(a,200);
-
cout << "Test of 2 element list" << endl;
-
cout << "a is " << a << '\n';
-
reverse(a);
-
cout << "after reverse, a is " << a << endl;
-
cleanUp(a);
-
}
-
-
-
-
-
-
void reverse( node * & s)
-
{
-
// NOTE: YOU CAN NOT MOVE DATA FROM ONE NODE TO ANOTHER
-
// YOU CAN ONLY MOVE POINTERS FROM ONE NODE TO ANOTHER
-
-
// Grab the first node1 `
-
int startNode;
-
// Create
-
startNode = s;
-
node *guest,*secondToLast,*secondElem;
-
guest=secondToLast=s;
-
-
// Return true if list is only 1 element long
-
if (s->next == NULL)
-
return true;
-
-
// Grab the second element in the list
-
secondElem = s->next;
-
-
// Grab the second to last element in the list
-
while(guest->next!=NULL)
-
{
-
secondToLast=guest;
-
guest=guest->next;
-
}
-
-
// Set the second to last node to now point to the original first node
-
secondToLast->next=startNode;
-
guest->next=secondElem;
-
return true;
-
}
Please read our forum posting guidelines located at this link: http://bytes.com/faq.php?faq=posting_guidelines
You will find that you will receive much more high quality help by adhering to these simple rules and suggestions. For example using [code] ... [ \code] tags.
Code tags put line numbers in your code snippet -- making it much easier for helpers to point at specific problems in the code.
@goldenteeplanet
The compiler error messages typically provide all kinds of detail to help you find and fix the errors. You will get quicker and more helpful assistance if you clearly state what is going wrong with your code. This includes providing the full text of any error messages. Most compiler error messages specify a line number. Frequently the code snippet you post is a subset of your original program. It is especially helpful if you edit the error message so that it specifies the line number in your posted snippet that corresponds to that same line in your original program.
Here is a quick list of problems I see with your code. No doubt there are others. - Compare lines 6 and 8 against 20 and 35. The function is defined to not have a return value, but you try to return a boolean value.
- Compare lines 6 and 8 against 11 and 14. Argument s is of type node * &, but you assign it to int variable startNode. In line 33 you do the reverse.
- I don't understand C++ reference syntax, so I don't know if it is ok to assign node * &s to node *guest (line 16).
- Compare lines 3 and 19. The node structure does not have a field called next.
- On line 19 you should have verified that s isn't NULL before you dereferenced the pointer. (No compiler error for this one.)
Did you cut and paste this code from somewhere else?
Another problem is that there is no list. All I see is a struct for the nodes. Where is the struct for the list?
You know, the struct where you keep the address of the first and last node of the list plus the address of the current node used by functions that traverse the list.
This missing struct is causing a lot of trouble.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Philipp Lenssen |
last post by:
I'm looking for a way to grab *all* XML of a DOM node (in PHP5).
Something like the following, only it doesn't work! Thanks.
/* I want everything (all XHTML) in <div id="hell=">...</div> */
...
|
by: ComputerSmith |
last post by:
Hi all.
I have programmed VB6 apps before, ones that use dropdown listboxes, text
boxes, etc... normal stuff.
I was asked by a friend to write a "simple" app that I am unsure how to
proceed...
|
by: hzgt9b |
last post by:
Using visual studio .NET 2003, VB:
I'm experiencing an issue with a TreeView object when it gets focus - the
first root node in the tree always gets selected when the control gets focus
- even if...
|
by: News |
last post by:
I am new in using Python
Anyone know how to implement breadth first search using Python? Can Python
create list dynamically, I want to implement a program which will read data
from a file and...
|
by: David |
last post by:
Hi Everyone,
I'm trying to grab all of the elements of a DL, specifically the <a href>'s
grouping them by the DD's. I suppose if I can just get them into groups I
can get the href's later. The...
|
by: David |
last post by:
I wonder if someone could tell me where I am going wrong with this script.
The original implementation of finding a DL list and separating it by dt |
dd groups and making new DL's from them worked...
|
by: Arielle |
last post by:
This is an example of what I'm trying to accomplish but anyway..
<table id="myTable1">
<tr>
<td><input type="checkbox" onClick="doSomething(this)" value="1"/></td>
<td>One</td>
</tr>
<tr>...
|
by: ofuuzo1 |
last post by:
Hei,
I have the following xml file and I have tried to write xslt to
extract only the values of the first "record" node. It does not
work. I need some help. I used ---- to represent indent.
...
|
by: SM |
last post by:
Hello,
I've done some research on how to grab the first key of a
multidimensional array. I've came up with a couple of solutions. Wich
is the best?. If possible, can it be done in just one line of...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |