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.

Seighart0
  • 13
  • 2

1 Answers1

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));
}
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466