0
#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?

2
  • 2
    OMG, this is like the sixth question about pretty much the same thing ... You're really grinding on that list, aren't you? Commented Apr 7, 2011 at 10:09
  • possible duplicate of typedef and linked list Commented Apr 7, 2011 at 11:06

4 Answers 4

2

Does this work for you?

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

int main() { struct stackNode * stackPtr = NULL; }
Sign up to request clarification or add additional context in comments.

2 Comments

is this the same as this ?? struct stackNode *stackPtr; stackPtr = NULL;??
It is, but I prefer the style in my answer as I like to explicitly initialize my definitions.
2
struct stackNode
{
   int data;
   struct stackNode *nextPtr;
};

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

1 Comment

You're creating a global variable.
1
// just precede the struct type with ... struct
struct stackNode* stackPtr = NULL;

Happy coding.

2 Comments

how about this? can anyone transcribe this too? typedef struct stackNode StackNode; typedef struct StackNode *StackNodePtr;
Just take them out? If they aren't being used, they aren't being used.
1

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.

3 Comments

I just want to keep it simple and it confuses me a lot ooppp sorry how do I fix this ?? typedef struct stackNode StackNode; typedef StackNode* StackNodePtr; without any typdefs?
@Kyel: I think the other answers from @John Källén, @pst and @0verbose already show you how to write your code without typedef - if that is not what you want then you need to reword your question.
soo to sum it all up, it's only like that??

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.