Sunday, 15 February 2015

c - How to determine complexity of these operations? -



c - How to determine complexity of these operations? -

i have implmented stack using these date structures:

typedef struct node{ stackentry entry; struct node *next; }node; typedef struct stack{ node *top; }stack;

how find bigo each of these operations below?

stackempty(stack *s)

stacksize(stack *s)

push(node *e, stack *s)

pop(node *e, stack *s)

time complexity

stackempty(stack *s) = o(1) //we need check top== null or not

stacksize(stack *s) = o(n) //we need pop n time count++ if stack holds n elements until top== null

push(node *e, stack *s) = o(1) //requires 1 operation no explanation need asume

pop(node *e, stack *s) = o(1) //requires 1 operation no explanation need asume

boolean stackempty(stack *s) { if(s.next==null&&s.value==null) homecoming true; //or 1 else homecoming false; //or 0 } int stacksize(stack *s) { if(stackempty!=true) { count = 0 while(stackempty!=true) { pop(); count++; } } homecoming count; }

c

No comments:

Post a Comment