list c++

Hi!
I was trying to write a program which takes in input a list of points of the plane and two integers m and q (that represent the line y=mx+q) and that should shift the points under the line to a second list.


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
PNodo spezzasemipiani(PNodo *l,int m, int q){
PNodo tmp=*l, tmp2, H=NULL;
if(tmp==NULL)
return NULL;

while(tmp!=NULL && m*tmp->x+q<tmp->y){
    *l=tmp->next;
    if(H==NULL){
        H=tmp;
        H->next=NULL;
    }
    else{
        while(H->next!=NULL)
        H=H->next;
        H->next=tmp;
        tmp->next=NULL;
    }
    tmp=*l;
}

if(tmp!=NULL){
while(tmp->next!=NULL){
    tmp2=tmp->next;
if(m*tmp2->x+q<tmp2->y){
    tmp->next=tmp2->next;
    if(H==NULL){
        H=tmp2;
        H->next=NULL;
    }
    else{
        while(H->next!=NULL)
        H=H->next;
        H->next=tmp2;
        tmp2->next=NULL;}
    
}
else tmp=tmp->next;
}}
return H;}


But it doesn't work well: working with the points (0,0)(1,1)(-1,1)(-2,1)(-3,1) and m=q=1 the point (-1,1) simply disappear and it is no more in none of the two lists.
Can you help me please?

The list is so defined:
1
2
3
4
5
6
7
8
struct nodo{
    int x;
    int y;
    struct nodo *next;
};

typedef struct nodo Nodo;
typedef Nodo * PNodo;


Thank you!

I'm gonna add the whole script if anybody needs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main(){
    PNodo pstart=NULL;
    PNodo lista2;
    int m,q;
    pstart=crealista(pstart);
    stampalista(pstart);
    printf("\n\nLine values\n m = ");
    scanf("%d",&m);
    printf(" q = ");
    scanf("%d",&q);
 
    lista2=spezzasemipiani(&pstart,m,q);
    printf("\npoints in the upper half plane:  ");
    stampalista(lista2);
    printf("\npoints in lower half plane:  ");
    stampalista(pstart);
    }


and the functions I used:

to add elements in a list:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void InserisciInCoda(PNodo *l, int x, int y) {
Nodo *temp, *p;
temp = (PNodo) malloc(sizeof(Nodo));
temp->x = x;
    temp->y=y;
temp->next = NULL;
if (*l==NULL)
    *l= temp;
else {
    p=*l;
    while(p->next!=NULL)
       p=p->next ;
    p->next=temp;
}}


to create a list:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
PNodo crealista(PNodo inizio){
    int c=1;
    int x,y;
   
    while(c!=0){   
    printf("inserire punti? (1/0) ");
    scanf("%d",&c);
    if(c==1){
    printf("X= ");
    scanf("%d",&x);
    printf("Y= ");
    scanf("%d",&y);
    if(Ricerca(inizio,x,y)==0)
    InserisciInCoda(&inizio,x,y);}
    else{
        if(c!=0){
        printf("non valido, riprova\n");
       }
    };
    }
return inizio;}


to print a list
1
2
3
4
5
6
7
void stampalista(PNodo p){
while(p!=NULL){
    printf("(%d,%d) --> ",p->x,p->y);
    p=p->next;
}
printf("NULL");
}


to search for an element in a list, used in the function crealista
1
2
3
4
5
6
7
8
9
int Ricerca(PNodo l, int x1, int x2){
    int r=1;
    while(l != NULL && (l->x!= x1 || l->y!=x2))
    l=l->next;
    
    if(l != NULL)
    return 1;
    else return 0;
}
Last edited on
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
#include <iostream>
#include <list>
using namespace std;

struct PT{ int x, y; };
using LIST = list<PT>;

//==========================================================

ostream & operator << ( ostream &out, const LIST &L ){ for ( auto e : L ) cout << e.x << " " << e.y << '\n';   return out; }

//==========================================================

void split( const LIST &input, LIST &above, LIST &below, int m, int q )
{
   above.clear();   below.clear();
   for ( auto e : input )
   {
      if ( e.y < m * e.x + q ) below.push_back( e );
      else                     above.push_back( e );
   }
}

//==========================================================

int main()
{
   int m = 1, q = 1;

   LIST L{ {0,0}, {1,1}, {-1,1}, {-2,1}, {-3,1} };
   cout << "Original:\n" << L;
   cout << "\nDividing line is y = " << m << " x + " << q << '\n';

   LIST above, below;
   split( L, above, below, m, q );
   cout << "\nAbove (or on):\n" << above;
   cout << "\nBelow:        \n" << below;
}

//========================================================= 


Original:
0 0
1 1
-1 1
-2 1
-3 1

Dividing line is y = 1 x + 1

Above (or on):
-1 1
-2 1
-3 1

Below:        
0 0
1 1
Last edited on
Topic archived. No new replies allowed.