2017-07-18 18 views
0
#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; 
    } 
} 

}如何讓這個堆棧代碼使用更少的內存? (C浪)

我想使此代碼使用較少的內存... 這個代碼使用1116KB, 但相同的算法代碼使用1000KB。 我怎樣才能讓這段代碼使用更少的內存?

這個代碼是這樣工作的 -

此代碼具有5個命令:

1.push X:在堆疊中添加X

2.pop:從棧和打印移除項它。

3.size:打印

4.empty堆疊的元件的數量:堆棧是否爲空的,打印1.如果不是打印0

5.top:打印是項在堆棧的頂部

步驟

  1. 輸入值(repeat循環量)

  2. 輸入命令

  3. 利潤!

回答

0

這個問題可以有幾種解決方案。因爲您正在使用靜態數組和頂部變量來跟蹤堆棧的最後一個元素,所以最終算法的空間複雜度會與數組的大小每次保持相同。因此,在向堆棧添加任何元素時,應該使用動態內存分配,這指的是使用C中的malloc或calloc函數在每次添加位置時從堆中分配內存。而使用免費功能彈出一個元素並釋放分配給它的內存。

下面是實現使用鏈表棧的代碼:

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

你也可以,因爲它是用9432 KB驗證在codechef's online IDE內存的使用情況。