数据结构————栈(力扣)

来源:1381. 设计一个支持增量操作的栈
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-a-stack-with-increment-operation

数据结构————栈(力扣)_第1张图片

typedef struct {   
	 int *a;    //用数组表示一个栈
	 int size;    //大小
	 int count;//当前大小
} CustomStack;

CustomStack* customStackCreate(int maxSize) 
{   
	 CustomStack *obj = (CustomStack *)malloc(sizeof(CustomStack));	//分配内存
	 obj->a=(int*)malloc(sizeof(int)*maxSize);    
	 obj->size = maxSize;    
	 obj->count=0;   
	  return obj;
}
void customStackPush(CustomStack* obj, int x) 
{   
	 if(obj->count>=obj->size)//如果栈满 				
		return; 
	(obj->a)[obj->count] = x;    
	obj->count++;
}
int customStackPop(CustomStack* obj) 
{    
	if(obj->count==0)        
		return -1;    
	int n = obj->a[obj->count-1];    
	obj->count--;
    	return n;
}
void customStackIncrement(CustomStack* obj, int k, int val) 
{
    int i =0;
    if(obj->count <=k) 
    {       
    	for(i;i<obj->count;i++)        
    	{            
    		obj->a[i] = obj->a[i]+val;        
    	}
      }    
      else{        
      	for(i=0;i<k;i++)        
      	{           
      		 obj->a[i] = obj->a[i]+val;        
      	}    
     }
}
void customStackFree(CustomStack* obj) {    
	free(obj);
}

你可能感兴趣的:(数据结构)