c++ - 函数中的堆栈指针会是形参吗?

标签 c++ c stack

    #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);

stack1data 是形式参数。它们在进入函数时需要值(参数)。 请考虑到,由于 stack1 被声明为指向结构体 stack 的指针,因此该指针所指向的对象的任何更改都将在退出函数后保留在该对象中。

关于c++ - 函数中的堆栈指针会是形参吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28778672/

相关文章:

c++ - 想要查看一个 vector 中的任何值是否与另一个 vector 中的值匹配

c++ - 为什么 std::abs(9484282305798401ull) = 9484282305798400?

c++ - 变量嵌套 for 循环

c - malloc分配的内存有边界吗?

c - Stack ADT实现中难以理解的表达

c++ - 使用 4x3 矩阵创建平面阴影?

c - C 中的字符串验证 : Trying to ask for a new string when the old one has an invalid character

c - 如何将 stdin 从我的应用程序重定向到另一个知道其 PID 的应用程序(C,在 Windows 中)

C : Printing a pointer to an array seems to print a junk value also

c - C中函数的返回值