1

following is the code I have written to implement stack using linked list data structure. I understand the concepts of Data Structure but am fairly new to using pointers and therefore having trouble in implementation.

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

    typedef struct NODE{
        int data;
        struct NODE *next;
    }NODE;

    NODE *top=NULL;
    NODE *newNode=NULL;

    NODE createNode();
    void push(NODE *newNode, int element);
    int pop();
    void display();

    int main()
    {
        int element,choice,p_item;

        do
        {
            printf("\nWhat operation would you like to perform on stack?\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
            printf("\nEnter your choice: ");
            scanf("%d",&choice);
            switch(choice)
            {
                case 1: printf("\nScan an element to push on stack: ");
                        scanf("%d",&element);
                        push(newNode, element);
                        break;

                case 2: p_item=pop();
                        printf("\nThe popped item is %d.\n",p_item);
                        break;

                case 3: display();
                        break;

                case 4: exit(0);
                        break;

                default: printf("\nWrong Choice!!Enter Again!\n");
            }
        }while(choice!=4);

        return 0;
    }

int pop()
{   
    if(top==NULL)
    {
        printf("\nStack UnderFlow!\n");
        return 0;
    }
    NODE *temp=top;
    int p_item;

        p_item=top->data;
        top=top->next;
        free(temp);

    return p_item; 
}

void display()
{
   if(top == NULL)
        printf("\nStack UnderFlow!\n");
   else
   {
        NODE* temp = top;
        while(temp->next != NULL)
        {
            printf("%d--->",temp->data);
            temp=temp->next;
        }  
        printf("%d--->NULL",temp->data);
   }
}

For most part the above written code is correct, the only place where am facing trouble is following section:

    NODE createNode()
    {   NODE *node;
        node=(NODE*)malloc(sizeof(NODE));
        return node;  //line 54
    }

In above function, what I believe I am doing is; declaring a node, allocating it memory and returning the pointer. But apparently the code is incorrect and my knowledge is limited to understand, what I am doing wrong.

    void push(NODE *newNode, int element)
    {
        newNode=createNode();  //line 59
        // newNode=(NODE*)malloc(sizeof(NODE));
        newNode->data=element;
        if(top==NULL)
        {
            newNode->next=NULL;
            top=newNode;
            return;
        }
        newNode->next=top;
        top=newNode;
    }

In above code, if I omit

    newNode=createNode();

and uncomment following line,

    newNode=(NODE*)malloc(sizeof(NODE));

Then, I can perfectly execute the whole program without the trouble, but for implementing other Data Structure concepts I would like to understand where am going wrong.

Following is the error:

stack_using_linked_list.c: In function ‘createNode’:
stack_using_linked_list.c:54:12: error: incompatible types when returning type ‘NODE * {aka struct NODE *}’ but ‘NODE {aka struct NODE}’ was expected
     return node;
            ^
stack_using_linked_list.c: In function ‘push’:
stack_using_linked_list.c:59:12: error: incompatible types when assigning to type ‘NODE * {aka struct NODE *}’ from type ‘NODE {aka struct NODE}’
     newNode=createNode();

Thank You, for taking time to read.

4
  • NODE createNode() => NODE *createNode() (look at the '*') Commented May 18, 2018 at 23:35
  • Please pay attention to compiler warnings. Commented May 18, 2018 at 23:36
  • You know how to declare variables with a pointer type (using e.g. NODE *). Why do you think it would be different for return types? This should be covered by basically any book or tutorial. Commented May 18, 2018 at 23:38
  • don't cast malloc Commented May 18, 2018 at 23:44

1 Answer 1

3

In above function, what I believe I am doing is; declaring a node, allocating it memory and returning the pointer. But apparently the code is incorrect and my knowledge is limited to understand, what I am doing wrong.

The error I see is that you declared the function as returning a NODE, but your code returns a NODE* (a pointer to a NODE).

You can change the declaration of your function to show the correct return type.

NODE* createNode()
{
    NODE *node;
    node = (NODE*)malloc(sizeof(NODE));
    return node;
}

Or you could shorten it to this.

NODE* createNode()
{
    return (NODE*)malloc(sizeof(NODE));
}
Sign up to request clarification or add additional context in comments.

4 Comments

*NODE should be NODE*.
@Barmar: Where did I use *NODE?
It worked, I didn't know that we could create pointer functions. I will read more about it. Thank you very much.
@Seighart0: It's not a pointer function. It is simply a function that returns a pointer to a NODE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.