• C

C Program works with Turbu C++ editor, but not with a UNIX machine

Please see the bellow codes, and try to execute the llTest code on a Unix machine . . .

I compiled llTest on the "Turbo C++" and it works successfully, BUT when I  changed the extension from llTest.cpp to llTest.c, the compiler of the Unix machine gave me a lot of errors . . .

Please notify me with the solution to this problem since it is a major part of an Operating Course Project . . .

Thanks in advanced . . .

This is the header code:

/*                                               */
/* queue.c                                          */
/* Demo of dynamic data structures in C                      */

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

#define FALSE 0
#define NULL 0

struct listelement{
    int     dataitem;
    listelement *link;
};

listelement * AddItem (listelement * listpointer, int data);
listelement * RemoveItem (listelement * listpointer);
void PrintQueue (listelement * listpointer);
void ClearQueue (listelement * listpointer);

listelement * AddItem (listelement * listpointer, int data) {

    listelement * lp = listpointer;

    if (listpointer != NULL) {
      while (listpointer -> link != NULL)
          listpointer = listpointer -> link;
      listelement  *new_lp = (struct listelement *)malloc(sizeof(struct listelement));
      listpointer -> link = new_lp;
      listpointer = listpointer -> link;
      listpointer -> link = NULL;
      listpointer -> dataitem = data;
      return lp;
    }
    else {
      listpointer = (struct listelement  *) malloc (sizeof (listelement));
      listpointer -> link = NULL;
      listpointer -> dataitem = data;
      return listpointer;
    }
}

listelement * RemoveItem (listelement * listpointer) {

    listelement * tempp;
    printf ("Element removed is %d\n", listpointer -> dataitem);
    tempp = listpointer -> link;
    free (listpointer);
    return tempp;
}

void PrintQueue (listelement * listpointer) {

    if (listpointer == NULL)
      printf ("queue is empty!\n");
    else
      while (listpointer != NULL) {
          printf ("%d\t", listpointer -> dataitem);
          listpointer = listpointer -> link;
      }
    printf ("\n");
}

void ClearQueue (listelement * listpointer) {

    while (listpointer != NULL) {
      listpointer = RemoveItem (listpointer);
    }
}

This is the code:

/*                                               */
/* queue.c                                          */
/* Demo of dynamic data structures in C                      */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "goodLinkedList.h"

main () {
    listelement listmember, *listpointer;
    int     data,
            choice;

    listpointer = NULL;

            printf ("Enter data item value to add  ");
            scanf ("%d", &data);
            listpointer = AddItem (listpointer, data);

            PrintQueue (listpointer);

            printf ("Enter data item value to add  ");
            scanf ("%d", &data);
            listpointer = AddItem (listpointer, data);

            PrintQueue (listpointer);

            printf ("Enter data item value to add  ");
            scanf ("%d", &data);
            listpointer = AddItem (listpointer, data);

            PrintQueue (listpointer);

            printf ("Enter data item value to add  ");
            scanf ("%d", &data);
            listpointer = AddItem (listpointer, data);

            PrintQueue (listpointer);
             
            if (listpointer == NULL)
                printf ("Queue empty!\n");
            else
                {
                listpointer = RemoveItem (listpointer);
                PrintQueue (listpointer);
            }

            if (listpointer == NULL)
                printf ("Queue empty!\n");
            else
                {
                listpointer = RemoveItem (listpointer);
                PrintQueue (listpointer);
            }

            if (listpointer == NULL)
                printf ("Queue empty!\n");
            else
                {
                listpointer = RemoveItem (listpointer);
                PrintQueue (listpointer);
            }

            if (listpointer == NULL)
                printf ("Queue empty!\n");
            else
                {
                listpointer = RemoveItem (listpointer);
                PrintQueue (listpointer);
            }

            if (listpointer == NULL)
                printf ("Queue empty!\n");
            else
                {
                listpointer = RemoveItem (listpointer);
                PrintQueue (listpointer);
            }

            if (listpointer == NULL)
                printf ("Queue empty!\n");
            else
                {
                listpointer = RemoveItem (listpointer);
                PrintQueue (listpointer);
            }

            PrintQueue (listpointer);
    ClearQueue (listpointer);

    return(0);
}
zaakiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cjjcliffordCommented:
the first thing I see is that you define a structure "listelement" but don't refer to it anywhere as a struct, either change references to it from "listelement" to "struct listelement" or create a typedef of the struct:

e.g. suggestion 1 (struct)

struct listelement{
    int     dataitem;
    struct listelement *link;
};

struct listelement * AddItem (struct listelement * listpointer, int data);
struct listelement * RemoveItem (struct listelement * listpointer);
void PrintQueue (struct listelement * listpointer);
void ClearQueue (struct listelement * listpointer);


e.g. suggestion 2 (typedef)

typedef struct listelement{
    int     dataitem;
    listelement *link;
} listelement;

listelement * AddItem (listelement * listpointer, int data);
listelement * RemoveItem (listelement * listpointer);
void PrintQueue (listelement * listpointer);
void ClearQueue (listelement * listpointer);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.