【问题标题】:How can I make this stack code use less memory? (C lang)如何使此堆栈代码使用更少的内存? (C 语言)
【发布时间】:2017-12-23 05:15:27
【问题描述】:
#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;
    }
}

}

我想让这段代码使用更少的内存... 此代码使用 1116KB, 但是具有相同算法的代码使用 1000KB。 我怎样才能让这段代码使用更少的内存?

这段代码是这样工作的 -

这段代码有 5 个命令:

1.push X : 在栈中添加 X

2.pop :从堆栈中删除一个项目并打印它。

3.size : 打印栈的元素个数

4.empty : 如果此堆栈为空,则打印 1。如果不是,则打印 0

5.top : 打印栈顶的元素

步骤

  1. 输入值(循环次数)

  2. 输入命令

  3. 利润!!

【问题讨论】:

    标签: memory stack clang


    【解决方案1】:

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

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

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

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

    您还可以在codechef's online IDE 验证内存的使用情况,因为它正在使用 9432 KB。

    【讨论】:

      猜你喜欢
      • 2018-05-02
      • 2012-12-24
      • 2017-12-21
      • 2018-03-30
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 1970-01-01
      相关资源
      最近更新 更多