Incorporate another function into this problem C++

In summary, the conversation discusses a problem with incorporating a new function into a program. The program involves creating a linked list and traversing it, but the last part of the code is causing confusion. It seems that the goal is to traverse a new type of linked list and potentially cut a node out of the original list.
  • #1
heavyc
16
0
i have this problem and i am having trouble how to incorporate another function into this problem here is the problem..

#include <iostream>

using namespace std;

struct node {
int data;
node *next;
};

struct node *push_front ( node *list, int data )
{
node *p = new node;

p->data = data;
p->next = list;
list = p;

return list;
}

int main()
{
node *list = 0;
node *save;

for ( int i = 0; i < 10; i++ )
list = push_front ( list, rand() % 5 + 1 );

while ( list != 0 ) {
save = list->next;
cout<< list->data <<' ';
delete list;
list = save;



}

cout<<'\n';
}

and here is what i am trying to put in.., i have it written but i don't know what varibles to put and where to put it into the program..

for ( i = first; i != last; i = next ( i ) ) {
for ( j = next ( i ); j != last; j = next ( j ) ) {
if ( i == j )
unlink ( j );
}
}
 
Physics news on Phys.org
  • #2
Right up to the last part:
heavyc said:
for ( i = first; i != last; i = next ( i ) ) {
for ( j = next ( i ); j != last; j = next ( j ) ) {
if ( i == j )
unlink ( j );
}
}
makes sense (except for a few minor problems), but nobody on Earth understands this last part. What are you trying to do with this part of the code? It seems you are travesing a new type of linked list (with the first and last nodes named) and at some node (j) you want to break the list into a new list or either cut the node (j) out of the original list.
 
  • #3


One possible way to incorporate another function into this problem is to create a function that checks for duplicates in the linked list and removes them. This function can be called after the for loop that adds random numbers to the list. Here is an example of how the code could look like:

// Function to check for duplicates in the linked list and remove them
void removeDuplicates(node *list) {
node *i, *j, *temp;

// Loop through the list
for (i = list; i != NULL; i = i->next) {
// Loop through the rest of the list
for (j = i->next; j != NULL; j = j->next) {
// Check if the data in the two nodes are the same
if (i->data == j->data) {
// Remove the duplicate node
temp = j;
j = j->next;
i->next = j;
delete temp;
}
}
}
}

// Main function
int main() {
node *list = 0;
node *save;

// Loop to add random numbers to the list
for (int i = 0; i < 10; i++) {
list = push_front(list, rand() % 5 + 1);
}

// Call the removeDuplicates function to remove any duplicates in the list
removeDuplicates(list);

// Loop to print the list
while (list != 0) {
save = list->next;
cout << list->data << ' ';
delete list;
list = save;
}

cout << '\n';
}
 

1. How do I incorporate another function into my C++ code?

To incorporate another function into your C++ code, you can use the function declaration and definition syntax. First, declare the function with its return type, name, and parameters. Then, define the function by writing its code within curly braces. Finally, call the function in your main code using its name and passing in any necessary arguments.

2. Can I use multiple functions in my C++ program?

Yes, you can use multiple functions in your C++ program. Functions allow you to break down your code into smaller, more manageable chunks and improve the overall organization and readability of your code.

3. How do I pass arguments into a function in C++?

You can pass arguments into a function in C++ by specifying the argument types and names in the function declaration, and then passing in the corresponding values when calling the function. These arguments will be used as parameters in the function definition to perform a specific task.

4. Can I call a function from within another function in C++?

Yes, you can call a function from within another function in C++. This is known as function nesting and can be useful when one function relies on the output of another function in order to perform its task.

5. How do I incorporate a library function into my C++ code?

To incorporate a library function into your C++ code, you must first include the corresponding library header file at the top of your code. Then, you can simply call the function by using its name and passing in any necessary arguments. Make sure to also link the library when compiling your code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
Replies
1
Views
803
Back
Top