Dear programmers,
i have the following problem.
The homework ask to create 3 linked lists and an array of pointers to them which is NULL at the beginning.
I m stuck when it asks to enter values to the lists until the user enters a number >=1000.
Also we have to make sure that every value in each list is distinct(no duplicate numbers are allowed).
This is my code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct node
{
int temperature;
struct node *next;
}set;
int main(void)
{
set *ap[]={NULL,NULL,NULL}; //array of pointers to linked lists
set *p;
int n;
while(scanf("%d",&n)==1 && n<1000)
{
p=(set*)malloc(sizeof(set));
p->temperature=n;
p->next=ap[0];
ap[0]=p;
}
for (p=ap[0];p;p=p->next)
printf("%d ",p->temperature);
return 0;
}
That code is without preventing duplicate values...
i've made an function in order to search for duplicate values but i can't attach it to the code.. i call it like that exist(ap[0],n) but i think its wrong..
Code:
int exists(set *t , int value)
{
if(t==NULL)return 0;
while (t!=NULL)
{
if(t->temperature==value)return 1;
t=t->next;
}
return 0;
}
Please help me...