Home > Back-end >  Chain stack problem in C language
Chain stack problem in C language

Time:09-15

Program runs to the "initStack (TOP);//initialization chain stack "this line is always an error, stop, don't know if structure definition there is something wrong with the

#include
#include

# define OK 1
# define the ERROR 0

Typedef struct node
{
Int num.
Struct node * next;
} LinkStackNode LinkStack;//list of each node list

Void initStack (LinkStack * top);//initialization stack chain
Int a push (LinkStack * top, int n);//chain into the stack operation
Pop (LinkStack is void * top);//chain of stack operation
Int getTop (LinkStack * top, int * s);//read chain stack stack element

Int main ()
{
LinkStack * TOP;
int n;
Int * data;

InitStack (TOP);//initialization stack chain

Getchar ();
Getchar ();
return 0;
}

Void initStack (top) LinkStack *//initializes the chain stack
{
Top=(LinkStack *) malloc (sizeof (LinkStack));
Top - & gt; Next=NULL;
}

CodePudding user response:

 
#include
#include

# define OK 1
# define the ERROR 0

Typedef struct node
{
int num;
Struct node * next;
} LinkStackNode LinkStack;//list of each node list

Void initStack (LinkStack * top);//initialization stack chain
Int a push (LinkStack * top, int n);//chain into the stack operation
Pop (LinkStack is void * top);//chain of stack operation
Int getTop (LinkStack * top, int * s);//read chain stack stack element

Int main ()
{
LinkStack * TOP;
int n;
Int * data;

InitStack (TOP);//initialization stack chain

Getchar ();
Getchar ();
return 0;
}

Void initStack (top) LinkStack *//initializes the chain stack
{
Top=(LinkStack *) malloc (sizeof (LinkStack));
Top - & gt; Next=NULL;
if (! Top)
{
Printf (" allocates memory failed! \n");
exit(1);
}
Printf (" the initial success! \n");
}

No error, the error information screenshot you look at the

CodePudding user response:

CodePudding user response:

Didn't do anything, there should be no wrong

CodePudding user response:

But the usage is wrong, parameters can be changed to * & amp; Or return to the top, so can't modify the top

CodePudding user response:

 # include & lt; stdio.h> 
#include

# define OK 1
# define the ERROR 0

Typedef struct node
{
int num;
Struct node * next;
} LinkStackNode LinkStack;//list of each node list

Void initStack (LinkStack * * top);//initialization stack chain
Int a push (LinkStack * top, int n);//chain into the stack operation
Pop (LinkStack is void * top);//chain of stack operation
Int getTop (LinkStack * top, int * s);//read chain stack stack element

Int main ()
{
LinkStack * TOP;
int n;
Int * data;

InitStack (& amp; TOP);//initialization stack chain

Getchar ();
Getchar ();
return 0;
}

Void initStack (LinkStack * * top)//initializes the chain stack
{
* top=(LinkStack *) malloc (sizeof (LinkStack));
Top (*) - & gt; Next=NULL;
if (! Top) (*)
{
Printf (" allocates memory failed! \n");
exit(1);
}
Printf (" the initial success! \n");
}


Need to use a double pointer, a pointer cannot return the malloc space,

When I want to application in the use of the problem, because the TOP is a pointer to the wild, even call after initialization stack or a wild pointer, so a double pointer, or in the c + + compiler, you can use * & amp; The top as a parameter, the arguments can use top directly without having to add & amp;
  • Related