Linked List Push Function

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

struct stackNode
{
    int data;
    struct stackNode *nextPtr;
};


void instructions()
{
    printf("[1]Push a value on the stack\n");
    printf("[2]Pop a value off the stack\n");
    printf("[3]Display the whole stack\n");
    printf("[4]Exit");
} 

void push(struct stackPtr *topPtr, int info)
{
    struct stackPtr *newPtr;
    newPtr= malloc(sizeof(struct stackNode));
    if(newPtr !=NULL)
    {
        newPtr->data = info;
        newPtr->nextPtr=*topPtr;
        *topPtr=newPtr;
    }
    else
    {
        printf("%d not inserted no memory available");
    }

int main()
{
    struct StackNodePtr *stackPtr;
    stackPtr = NULL;
    int choice, value;
    do
    {   
        instructions();
        printf("\nEnter Your Choice: ");
        scanf("%d",&choice);
        if(choice == 1)
        {
            printf("Enter  a value for the stack");     
        }             
        if(choice == 2)
        {
            printf(" "); 
        }       
        if(choice == 3)
        {
            printf(" ");
        }
        if(choice == 4 )
        {
            printf("bye!");
            return 0;
        }
    } while(choice !=4);
    system("pause");
}

I made a function push for my linked list and stack code but the thing is it's not working there are Enormous errors in the function push what's wrong with it? it's not allowing the malloc to be used why is that?

Upvotes: 0

Views: 5007

Answers (4)

GrahamS
GrahamS

Reputation: 10350

This line:

typedef struct StackNode *StackNodePtr;

is wrong by the way. Your typedefs should be:

typedef struct stackNode StackNode;
typedef StackNode* StackNodePtr;

No idea why you're against using typedef - it tends to make the code a lot more readable.

Upvotes: 1

Heisenbug
Heisenbug

Reputation: 39174

struct stackNode
{
   int data;
   struct stackNode *nextPtr;
};

int main()
{
    struct stackNode *stackPtr = NULL;
}

Upvotes: 2

John K&#228;ll&#233;n
John K&#228;ll&#233;n

Reputation: 7943

Does this work for you?

struct stackNode { int data; struct stackNode *nextPtr; };

int main() { struct stackNode * stackPtr = NULL; }

Upvotes: 2

user166390
user166390

Reputation:

// just precede the struct type with ... struct
struct stackNode* stackPtr = NULL;

Happy coding.

Upvotes: 1

Related Questions