# include <stdio.h>
# include <stdlib.h>
struct stackNode
{
int data;
struct stackNode *neatPtr;
};
typedef struct stackNode STACKNODE;
typedef STACKNODE *STACKNODEPTR;
void push (STACKNODEPTR*,int);
int pop (STACKNODEPTR*);
int isEmpty (STACKNODEPTR);
void printStack(STACKNODEPTR);
void instructions(void);
main()
{
STACKNODEPTR stackPtr=NULL;
int choice,value;
instructions();
printf("? ");
scanf("%d",&choice);
while (choice!=3)
{
switch(choice)
{
case 1:
printf("Enter an intager: "); //запихиваем в стех
scanf("%d",&value);
push(&stackPtr,value);
printStack (stackPtr);
break;
case 2: //берем из стека
if (!isEmpty(stackPtr)) printf("The popper value is %d.\n",pop(&stackPtr));
printStack (stackPtr);
break;
default:
printf("Invalid choice.\n\n");
instructions();
break;
}
printf("? ");
scanf("%d",&choice);
}
printf("End of r.\n");
return 0;
}
// Распечатка инструкций
void instructions (void)
{
printf("Enter choice:\n"
"1 to push a value on the stack\n"
"2 to pup a value off the stack\n"
"1 to end program\n"
);
}
// Помещаем в стек
void push (STACKNODEPTR *topPtr,int info)
{
STACKNODEPTR newPtr;
newPtr=malloc(sizeof(STACKNODE));
if (newPtr!=NULL) {
newPtr->data=info;
newPtr->neatPtr=*topPtr;
*topPtr=newPtr;
}
else printf("%d not inserted. No memory available.\n",info);
}
// Берем из стека
int pop (STACKNODEPTR *topPtr)
{
STACKNODEPTR tempPtr;
int popValue;
tempPtr=*topPtr;
popValue=(*topPtr)->data;
*topPtr=(*topPtr)->neatPtr;
free(tempPtr);
return popValue;
}
// распечатка стека
void printStack(STACKNODEPTR currentPtr)
{
if (currentPtr==NULL) printf("The stack is empty.\n\n");
else {
printf("the srack is:\n");
while (currentPtr!=NULL){
printf("%d->",currentPtr->data);
currentPtr=currentPtr->neatPtr;
}
printf("NULL\n\n");
}
}
// пустой ли стек
int isEmpty(STACKNODEPTR topPtr)
{
return topPtr==NULL;
}