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?

Dani AI

Generated

This program crashes for a handful of predictable reasons: several functions are being called with the wrong pointer types, important pointers are never initialized or allocated, the linked list is never actually linked into a cyclic list, the prime-test and tree-insertion logic are incorrect, and file handling is done with an uninitialized filename pointer. is correct that the TEMP object is misused — that fixes one class of warnings, but more fixes are needed before the segfaults go away.

Key faults and why they break things (with what to do instead):

  • init/main mismatch and uninitialized TEMP: passing an address of an uninitialized pointer leads to undefined behavior. Either allocate a TEMP structure or declare it as a local object and pass its address. Always initialize pointers to NULL after allocation/assignment.
  • list construction loop: repeatedly assigning data = inputData() loses previously created nodes. Maintain head and tail, link each new node into the list, and set tail->next = head to make it cyclic.
  • TREE_NODE stores a LIST_NODE by value and the tree code compares numbers to the prime-function result. Simplify ownership: store the integer value (e.g., int info) in tree nodes. Call the prime function and test its boolean result — do not compare the value to the return of the prime test.
  • isPrime implementation: current logic returns confusing values. Return 1 for prime, 0 otherwise, and test divisibility up to sqrt(n).
  • printToFile / fileName: scanning into a char * that never points to allocated storage causes a crash. Read into a sized buffer (or allocate), open FILE* via fopen, check the return, then pass that FILE* to the printing routine. Do not call qsort on a tree node field; either collect primes into an array and sort, or traverse the BST in reverse in-order (right, node, left) to write descending values.

Practical debugging checklist: compile with -Wall -Wextra, run under Valgrind to find invalid reads/writes, check every malloc/fopen return, initialize pointers, and test each unit (list building, isPrime, tree insertion, file output) independently. Fixing the pointer/ownership issues and simplifying the tree to hold plain integers will resolve most of the reported warnings and the segmentation faults.

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.