xbubbax
27-02-2008, 17:19
Non capisco perchč non funziona questo esercizio sulle liste, in pratica dovrei inserire un intero n da input, poi inserire in coda gli n elementi e ritornare 1 se ci sono elementi compresi tra un intervallo i,j altrimenti 0.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct nodo{
int dato;
struct nodo *next;};
typedef struct nodo nodo;
typedef struct nodo *listaPtr;
int Intervallo(listaPtr L, int i, int j);
listaPtr Inserisci(listaPtr L, int n);
int main(void){
listaPtr L;
int n=0;
int x=0;
scanf("%d", &n);
Inserisci(L,n);
x=Intervallo(L,3,7);
printf("%d\n", x);
system("PAUSE");
return 0;}
listaPtr Inserisci(listaPtr L, int n){
listaPtr prev, curr, temp;
L=NULL;
curr=L;
prev=NULL;
int i=0;
int x=0;
for(i=0;i<n;i++){
scanf("%d", &x);
temp=(listaPtr)malloc(sizeof(nodo));
temp->dato=x;
if(L==NULL){
L=temp;
curr=L;
curr->next=NULL;}else{
curr->next=temp;
curr=temp;
curr->next=NULL;}}
return L;
}
int Intervallo(listaPtr L, int i, int j){
listaPtr prev, curr, temp;
prev=NULL;
curr=L;
int contatore=0;
while(curr!=NULL){
if((curr->dato>=i)&&(curr->dato<=j)){
contatore=1;
curr=curr->next;}else{
curr=curr->next;}}
return contatore;}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct nodo{
int dato;
struct nodo *next;};
typedef struct nodo nodo;
typedef struct nodo *listaPtr;
int Intervallo(listaPtr L, int i, int j);
listaPtr Inserisci(listaPtr L, int n);
int main(void){
listaPtr L;
int n=0;
int x=0;
scanf("%d", &n);
Inserisci(L,n);
x=Intervallo(L,3,7);
printf("%d\n", x);
system("PAUSE");
return 0;}
listaPtr Inserisci(listaPtr L, int n){
listaPtr prev, curr, temp;
L=NULL;
curr=L;
prev=NULL;
int i=0;
int x=0;
for(i=0;i<n;i++){
scanf("%d", &x);
temp=(listaPtr)malloc(sizeof(nodo));
temp->dato=x;
if(L==NULL){
L=temp;
curr=L;
curr->next=NULL;}else{
curr->next=temp;
curr=temp;
curr->next=NULL;}}
return L;
}
int Intervallo(listaPtr L, int i, int j){
listaPtr prev, curr, temp;
prev=NULL;
curr=L;
int contatore=0;
while(curr!=NULL){
if((curr->dato>=i)&&(curr->dato<=j)){
contatore=1;
curr=curr->next;}else{
curr=curr->next;}}
return contatore;}