• Support PF! Buy your school textbooks, materials and every day products Here!

Incorporate another function into this problem C++

  • Thread starter heavyc
  • Start date
  • #1
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 dont 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 );
}
}
 

Answers and Replies

  • #2
andrevdh
Homework Helper
2,128
116
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.
 

Related Threads for: Incorporate another function into this problem C++

Replies
13
Views
3K
Replies
1
Views
794
Replies
6
Views
10K
  • Last Post
Replies
18
Views
1K
  • Last Post
Replies
8
Views
1K
Replies
6
Views
3K
  • Last Post
Replies
1
Views
3K
Top