#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct stack
{
node *head;
};
void push(struct stack *stack1,int data)
{
struct node *new_node= new node;
new_node->data=data;
if(stack1->head!=NULL)
new_node->next=stack1->head;
else
new_node->next=NULL;
stack1->head=new_node;
}
void pop(struct stack *stack1)
{
node *temp= new node;
if(stack1->head==NULL)
cout<<"EMPTY"<<endl;
else
{
temp=stack1->head;
stack1->head=(stack1->head)->next;
int t;
t=temp->data;
free(temp);
cout<<t<<" ";
}
}
void show(struct stack *stack1)
{
node *new_node=new node;
new_node=stack1->head;
if(stack1->head==NULL)
{
cout<<"EMPTY"<<endl;
return;
}
while(new_node!=NULL)
{
cout<<new_node->data<<" ";
new_node=new_node->next;
}
}
void peek(struct stack *stack1)
{
if(stack1->head==NULL)
cout<<"EMPTY";
else
cout<<stack1->head->data<<" ";
}
int main()
{
int temp,temp2;
struct stack *stack1=new stack;
stack1->head=NULL;
while(1)
{
cin>>temp;
switch(temp)
{
case 0: exit(0);
case 1: cin>>temp2;
cout<<endl;
push(stack1,temp2);
break;
case 2: pop(stack1);
cout<<endl;
break;
case 3: peek(stack1);
cout<<endl;
break;
case 4: show(stack1);
cout<<endl;
break;
}
}
return 0;
}
最佳答案
来自 C++ 标准
1.3.14 [defns.parameter]
parameter formal argument formal parameter
object or reference declared as part of a function declaration or definition or in the catch clause of an exception handler that acquires a value on entry to the function or handler
来自 C 标准
3.16 1 parameter formal parameter formal argument (deprecated)
object declared as part of a function declaration or definition that acquires a value on entry to the function, or an identifier from the comma-separated list bounded by the parentheses immediately following the macro name in a function-like macro definition
例如在此函数声明中
void push(struct stack *stack1,int data);
stack1
和 data
是形式参数。它们在进入函数时需要值(参数)。
请考虑到,由于 stack1 被声明为指向结构体 stack 的指针,因此该指针所指向的对象的任何更改都将在退出函数后保留在该对象中。
关于c++ - 函数中的堆栈指针会是形参吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28778672/