2 2 votes Consider a stack and we wish to perform an operation StackDelete() in which we wish to delete all the elements of the stack .What is the worst case time complexity of doing this if stack is implemented: 1) Using an array 2)Using a Linked list Programming in C data-structures stack + – VS 1.7k views answer comment Share Follow Print See all 11 Comments 11 11 Comments reply joshi_nitish commented Dec 26, 2017 reply Follow flag it will be O(n) time in an array, in linked list, it can take O(1) time, if you simply remove pointer from top of stack, but if you want that memory also be needed to free up, then it will take O(n) time. 2 2 replyShare Hradesh patel commented Dec 26, 2017 reply Follow flag array takes worst time .... 0 0 replyShare VS commented Dec 26, 2017 reply Follow flag @joshi_nitish Linked list point , I agree with you Array I was thinking like this : Suppose in array implementation we have 2 variables one pointing to starting and one ending of stack(TOS variable).Now, as you mentioned 2 cases here: 1) Set TOS=-1 2) If memory needs to be deallocated we could use free() in C , Delete() in C++ , and in java set arr=null(as we have garbage collection in it) In any case O(1) time :) 0 0 replyShare joshi_nitish commented Dec 26, 2017 reply Follow flag i was answering in accordance with C, assuming no predefined functions are available and assuming that to delete each element of array simply means setting $A[i]=0$ $\forall i\epsilon [0,n]$ 0 0 replyShare pradeepdeepu commented Dec 26, 2017 reply Follow flag In the question asked to delete the elements. So even we change the top of the stack the array is not empty, we can access the elements of the array So they are not deleted untill we again replace with some other elements or delete them. But in case of linked lists if we change the pointer once we cant again access them so in this case the elements are deleted even they may be in the memory but we cant access them 0 0 replyShare VS commented Dec 26, 2017 reply Follow flag @joshi_nitish Agreed ! But, if a question like this is asked in gate , shouldn't we consider functions like free() are by default there ? 0 0 replyShare joshi_nitish commented Dec 26, 2017 reply Follow flag in case of linked list, free() is trivial function, but for array i don't think predefined function is to be taken until mentioned exlplicitly. 0 0 replyShare VS commented Dec 26, 2017 reply Follow flag @joshi_nitish https://stackoverflow.com/questions/15298822/delete-whole-array-in-c 0 0 replyShare joshi_nitish commented Dec 26, 2017 reply Follow flag @VS there array is dynamic array, where memory is allocated using malloc(). but generally by default we take static array in which memory is binded up throughout lifetime of a function(in which array is declared) or throughout lifetime of program(if array declared globally). in case of static allocation deallocation of a array memory is not possible(i think so) because memory is allocated during compile time itself rather than runtime. 1 1 replyShare VS commented Dec 26, 2017 reply Follow flag @joshi_nitish I think then the answer depends on how memory is allocated to an array. 1) Dynamic --> StackDelete() -->O(1) 2)Static --> StackDelete() -->O(n) But, what is the default case then ? 1 1 replyShare joshi_nitish commented Dec 26, 2017 reply Follow flag @VS if asked in GATE, qsn will be properly framed avoiding any type of abguity that may arise 1 1 replyShare Please log in or register to add a comment.