Incorporate another function into this problem C++

  • Thread starter Thread starter heavyc
  • Start date Start date
  • Tags Tags
    C++ Function
Click For Summary
SUMMARY

The discussion revolves around incorporating an additional function into a C++ program that manipulates a linked list. The existing code defines a linked list structure and includes a function, push_front, to add nodes to the front of the list. The user is attempting to integrate a nested loop structure to traverse and potentially unlink nodes from the list but is unclear about variable definitions and their placement within the program. The main challenge lies in understanding how to implement the logic for unlinking nodes correctly.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with linked list data structures
  • Knowledge of dynamic memory management in C++
  • Experience with function implementation and variable scope in C++
NEXT STEPS
  • Research C++ linked list traversal techniques
  • Learn about memory management and deletion of nodes in linked lists
  • Study the implementation of nested loops in C++ for data structure manipulation
  • Explore the concept of unlinking nodes in linked lists and its implications
USEFUL FOR

C++ developers, computer science students, and anyone interested in linked list manipulation and memory management in programming.

heavyc
Messages
16
Reaction score
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;
count<< list->data <<' ';
delete list;
list = save;



}

count<<'\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
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
8K