The following program should create singly cyclic linked list which has n nodes, where data are random generated numbers from 0-99. Create binary search tree which will contain all prime numbers from the list. Print binary tree in text file so that numbers are sorted in descending order and every number is in the new line.

Code:

#include<stdio.h>
#include<stdlib.h>

typedef struct list_node
{
    int info;
    struct list_node *next;
}LIST_NODE;

typedef struct temp
{
    LIST_NODE *head;
    int n;
}TEMP;

typedef struct tree_node
{
    LIST_NODE plist;
    struct tree_node *left;
    struct tree_node *right;
}TREE_NODE;

LIST_NODE *inputData();
void init(TEMP *);
TREE_NODE *formTreeNode(LIST_NODE *);
TREE_NODE *addToTree(LIST_NODE *,TREE_NODE *,int (*prime)(int));
/*LIST_NODE *addFront(LIST_NODE **,int);*/
int isCyclic(TEMP *);
int isPrime(int);
int cmp_int(const void *,const void *);
TREE_NODE *binSearch(TREE_NODE *,int);
void printToFile(TREE_NODE *,FILE *);
void removeTree(TREE_NODE *);

int main()
{
    int (*prime)(int);
    char *fileName;
    LIST_NODE *data;
    TEMP *temp;
    TREE_NODE *root=0;
    LIST_NODE plist;
    int n,i;
    do
    {
        printf("n=");
        scanf("%d",&n);
    }
    while(n<1);
    for(i=0;i<n;i++)
        data=inputData();
    init(&temp);
    TREE_NODE *p=binSearch(root,plist.info);
    if(p)
        p->plist=plist;
    else
        root=addToTree(&plist,root,&prime);
        printf("name of file:");
        scanf("%s",fileName);
    printToFile(root,&fileName);
    removeTree(root);
    return 0;
}

int isCyclic(TEMP *pt)
{
    if(pt->head == NULL)
        return -1;
    LIST_NODE *listElement=pt->head;
    if(pt->head == pt->head->next)
    {
        pt->head=NULL;
        pt->head->next=NULL;
    }
    else
      pt->head->next=pt->head;
    pt->n--;
    int info=listElement->info;
    free(listElement);
    return info;
}

void init(TEMP *temp)
{
    temp->head=NULL;
    temp->n=0;
}

/*LIST_NODE *addFront(LIST_NODE **phead,int info)
{
    LIST_NODE *newListNode=(LIST_NODE *)malloc(sizeof(LIST_NODE));
    newListNode->info=info;
    newListNode->next=*phead;
    *phead=newListNode;
    return newListNode;
}*/

TREE_NODE *formTreeNode(LIST_NODE *plist)
{
  TREE_NODE *newTreeNode=(TREE_NODE *)malloc(sizeof(TREE_NODE));
  newTreeNode->left=newTreeNode->right=0;
  newTreeNode->plist=*plist;
  return newTreeNode;
}

int isPrime(int n)
{
    int d,nd=2;
    if(n == 1)
        return -1;
    for(d=2;d<=n/2;d++)
    {
        if(n % d == 0)
            nd++;
    }
    if(nd == 2)
        return 1;
    else return 0;
}

TREE_NODE *addToTree(LIST_NODE *plist,TREE_NODE *root,int(*prime)(int))
{
    if(root == 0)
        return formTreeNode(plist);
    if(plist->info == (*prime)(plist->info) &&
       root->plist.info == (*prime)(root->plist.info))
    {
       if(plist->info <= root->plist.info)
          root->left=addToTree(plist,root->left,prime);
       else
          root->right=addToTree(plist,root->right,prime);
       return root;
    }
    else return NULL;
}

int cmp_int(const void *a,const void *b)
{
    return (*(int *)b-*(int *)a);
}

LIST_NODE *inputData()
{
    LIST_NODE *data=(LIST_NODE *)malloc(sizeof(LIST_NODE));
    printf("input:");
    scanf("%d",&data->info);
    return data;
}

TREE_NODE *binSearch(TREE_NODE *root,int info)
{
    if(root == 0)
        return 0;
    else if(info == root->plist.info)
        return root;
    else if(info <= root->plist.info)
        return binSearch(root->left,info);
    else
        return binSearch(root->right,info);
}

void printToFile(TREE_NODE *root,FILE *pf)
{
    pf=NULL;
    TEMP *pt;
    char *fileName;
    if((pf=fopen(fileName,"w")) != NULL)
    {
        qsort(&(root->plist.info),(pt->n),sizeof(int),cmp_int);
        fprintf(pf,"%02d\n",(root->plist.info));
        fclose(pf);
    }
    else return;
}

void removeTree(TREE_NODE *root)
{
    if(root == 0)
        return;
    removeTree(root->left);
    removeTree(root->right);
    free(root);
}

Segmentation faults:
|52|warning: passing argument 1 of 'init' from incompatible pointer type [enabled by default]|
|24|note: expected 'struct TEMP ' but argument is of type 'struct TEMP **'|
|57|warning: passing argument 3 of 'addToTree' from incompatible pointer type [enabled by default]|
|26|note: expected 'int (
)(int)' but argument is of type 'int (
)(int)'|
|60|warning: passing argument 2 of 'printToFile' from incompatible pointer type [enabled by default]|
|32|note: expected 'struct FILE *' but argument is of type 'char '|
|39|warning: variable 'data' set but not used [-Wunused-but-set-variable]|
|167|warning: 'fileName' is used uninitialized in this function [-Wuninitialized]|
|169|warning: 'pt' may be used uninitialized in this function [-Wuninitialized]|

Could someone check for possible obvious errors?

Types are critical. You are passing the address of a pointer to init() and not the pointer itself. Also, your variable TEMP* temp in main has not been initialized with a pointer to a real object. If you change the type of temp to a TEMP object, as in TEMP temp; then your call to init(&temp); will work just fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.