Memory 如何使堆栈代码使用更少的内存?(C朗) #包括 int-tp=-1; 无效推送(int arr[],int值) { arr[++tp]=值; } 无效pop(整数arr[] { 如果(size()=0) { 看跌期权(“-1”); 返回; } printf(“%d\n”,arr[tp--]); } int size() { 返回tp+1; } void empty() { 如果(size()==0)放入(“1”); 其他认沽期权(“0”); } 内部顶部(内部阵列[]) { 如果(size()=0) { 看跌期权(“-1”); 返回; } printf(“%d\n”,arr[tp]); } int main() { 国际农业资源[10000]; 无符号整数i,重复; char命令[6]; scanf(“%d”,重复(&repeat);//重复 对于(i=0;i

Memory 如何使堆栈代码使用更少的内存?(C朗) #包括 int-tp=-1; 无效推送(int arr[],int值) { arr[++tp]=值; } 无效pop(整数arr[] { 如果(size()=0) { 看跌期权(“-1”); 返回; } printf(“%d\n”,arr[tp--]); } int size() { 返回tp+1; } void empty() { 如果(size()==0)放入(“1”); 其他认沽期权(“0”); } 内部顶部(内部阵列[]) { 如果(size()=0) { 看跌期权(“-1”); 返回; } printf(“%d\n”,arr[tp]); } int main() { 国际农业资源[10000]; 无符号整数i,重复; char命令[6]; scanf(“%d”,重复(&repeat);//重复 对于(i=0;i,memory,stack,clang,Memory,Stack,Clang,这个问题,可以有几种解决方案。由于使用静态数组和顶部变量来跟踪堆栈的最后一个元素,因此每次数组的大小都保持不变,因此算法的空间复杂度是相同的 因此,在向堆栈中添加任何元素时,都应该使用动态内存分配,这是指每次添加位置时都使用C中的malloc或calloc函数从堆中分配内存。而使用free函数弹出元素并释放分配给它的内存 下面是使用链表实现堆栈的代码: #include<stdio.h> int tp=-1; void push(int arr[],int value) {

这个问题,可以有几种解决方案。由于使用静态数组和顶部变量来跟踪堆栈的最后一个元素,因此每次数组的大小都保持不变,因此算法的空间复杂度是相同的

因此,在向堆栈中添加任何元素时,都应该使用动态内存分配,这是指每次添加位置时都使用C中的malloc或calloc函数从堆中分配内存。而使用free函数弹出元素并释放分配给它的内存

下面是使用链表实现堆栈的代码:

#include<stdio.h>

int tp=-1;

void push(int arr[],int value)
{
    arr[++tp]=value;
}

void pop(int arr[])
{
    if(size()==0)
    {
        puts("-1");
        return;
    }
    printf("%d\n",arr[tp--]);
}

int size()
{
    return tp+1;
}

void empty()
{
    if(size()==0)puts("1");
    else puts("0");
}

int top(int arr[])
{
    if(size()==0)
    {
        puts("-1");
        return;
    }   
    printf("%d\n",arr[tp]);
}

int main()
{
    int arr[10000];
    unsigned int i,repeat;
    char command[6];

    scanf("%d",&repeat);                //repeating
    for(i=0;i<repeat;i++)
    {
    scanf("%s",command);
    switch(command[0])
    {
        case 'p':
            if(command[1]=='u')         //push
            {
                int value;
                scanf("%d",&value);
                push(arr,value);
            }
            else pop(arr);              //pop. if stack is empty, output -1
            break;
        case 's':
            printf("%d\n",size());      //print size of stack
            break;
        case 'e':
            empty();                    //if stack is empty, print 1. if not, print 0.
            break;
        case 't':
            top(arr);                   //print value that is on top of stack. if stack is empty, print -1
            break;
    }
}
#包括
结构体类型
{
int数据;
结构节点*下一步;
}*top=NULL;
无效推力(int);
void pop();
void display();
void main()
{
智力选择、价值;
printf(“1.Push\n2.Pop\n3.Display\n4.Exit\n”);
而(1){
printf(“输入您的选择:”);
scanf(“%d”,选择(&C);
开关(选择){
案例1:printf(“\n输入要插入的值:”);
scanf(“%d”,和值);
推(值);
打破
案例2:砰的一声;破裂;
案例3:显示();中断;
案例4:退出(0);
默认值:printf(“\n错误选择!!!\n”);
}
}
}
无效推送(int值)
{
结构节点*newNode;
newNode=(结构节点*)malloc(sizeof(结构节点));
新建节点->数据=值;
if(top==NULL)
newNode->next=NULL;
其他的
新建节点->下一步=顶部;
top=newNode;
printf(“\n插入成功!!!\n”);
}
void pop()
{
if(top==NULL)
printf(“\n堆栈为空!!!\n”);
否则{
结构节点*temp=top;
printf(“\n删除的元素:%d”,临时->数据);
顶部=温度->下一步;
免费(临时);
}
}
无效显示()
{
if(top==NULL)
printf(“\n堆栈为空!!!\n”);
否则{
结构节点*temp=top;
while(临时->下一步!=NULL){
printf(“%d-->”,temp->data);
温度=温度->下一步;
}
printf(“%d”,临时->数据);
}
}   
您还可以验证在使用9432 KB时的内存使用情况

#include<stdio.h>

struct Node
{
   int data;
   struct Node *next;
}*top = NULL;

void push(int);
void pop();
void display();

void main()
{
   int choice, value;
   printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
   while(1){
      printf("Enter your choice: ");
      scanf("%d",&choice);
      switch(choice){
     case 1: printf("\nEnter value to insert: ");
         scanf("%d", &value);
         push(value);
         break;
     case 2: pop(); break;
     case 3: display(); break;
     case 4: exit(0);
     default: printf("\nWrong selection!!! \n");
      }
   }
}
void push(int value)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   if(top == NULL)
      newNode->next = NULL;
   else
      newNode->next = top;
   top = newNode;
   printf("\nInsertion is Success!!!\n");
}
void pop()
{
   if(top == NULL)
      printf("\nStack is Empty!!!\n");
   else{
      struct Node *temp = top;
      printf("\nDeleted element: %d", temp->data);
      top = temp->next;
      free(temp);
   }
}
void display()
{
   if(top == NULL)
      printf("\nStack is Empty!!!\n");
   else{
      struct Node *temp = top;
      while(temp->next != NULL){
     printf("%d--->",temp->data);
     temp = temp -> next;
      }
      printf("%d",temp->data);
   }
}