It follows the principle LIFO (Last In- First Out), which implies that the element which is inserted last would be the first one to be deleted. There are two similarities between the stack and queue:īoth the stack and queue are the linear data structure, which means that the elements are stored sequentially and accessed in a single run.īoth the stack and queue are flexible in size, which means they can grow and shrink according to the requirements at the run-time.ĭifferences between stack and queue The following are the differences between the stack and queue: Basis for comparison Its structure contains two pointers front pointer and rear pointer, where the front pointer is a pointer that points to the element that was first added in the queue and the rear pointer that points to the element inserted last in the queue. In Queue, the technical words for insertion and deletion are enqueue() and dequeue(), respectively whereas, in the case of the stack, the technical words for insertion and deletion are push() and pop(), respectively. The deletion is performed from another end, and that end is known as a front end. In the case of Queue, insertion is performed from one end, and that end is known as a rear end. A Queue is a structure that follows some restrictions on insertion and deletion. It is an ordered list that follows the principle FIFO (First In -First Out). Dynamic: The dynamic implementation of the stack can be done with the help of a linked list.Ī Queue is a linear data structure.Static: The static implementation of the stack can be done with the help of arrays.There are two ways to implement the stack: We need to allocate the memory to get the size of the stack. To implement the stack, we should know the size of the stack. In stack, the top is a pointer which is used to keep track of the last inserted element. isFull(): If the stack is full, then this function will return a true value or else it will return a false value. isEmpty(): If the stack is empty, then this function will return a true value or else it will return a false value.Like pop(), it returns the value of the topmost element but does not remove that element from the stack. peek()/top(): This function returns the value of the topmost element available in the stack.In the pop() function, we do not have to pass any argument. pop(): It is an operation in which the elements are deleted from the top of the stack.In the push function, we need to pass an element which we want to insert in a stack.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |