![]() Step 3 - If it is Not Empty, then define a Node pointer ' temp' and set it to ' top'.Step 2 - If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and terminate the function.Step 1 - Check whether stack is Empty ( top = NULL).We can use the following steps to delete a node from the stack. Step 4 - If it is Not Empty, then set newNode → next = top.Step 3 - If it is Empty, then set newNode → next = NULL.Step 2 - Check whether stack is Empty ( top = NULL).Step 1 - Create a newNode with given value.We can use the following steps to insert a new node into the stack. Push(value) - Inserting an element into the Stack Step 4 - Implement the main method by displaying Menu with list of operations and make suitable function calls in the main method.Step 3 - Define a Node pointer ' top' and set it to NULL.Step 2 - Define a ' Node' structure with two members data and next.And declare all the user defined functions. Step 1 - Include all the header files which are used in the program.To implement a stack using a linked list, we need to set the following things before implementing actual operations. The order of elements inserted is 25, 32,50 and 99. In the above example, the last inserted node is 99 and the first inserted node is 25. The next field of the first element must be always NULL. ![]() ![]() Whenever we want to remove an element from the stack, simply remove the node which is pointed by ' top' by moving ' top' to its previous node in the list. That means every newly inserted element is pointed by ' top'. In linked list implementation of a stack, every new element is inserted as ' top' element. The Stack implemented using linked list can organize as many data values as we want. So, there is no need to fix the size at the beginning of the implementation. That means, stack implemented using linked list works for the variable size of data. The stack implemented using linked list can work for an unlimited number of values. A stack data structure can be implemented by using a linked list data structure. Stack implemented using an array is not suitable, when we don't know the size of data which we are going to use. That means the amount of data must be specified at the beginning of the implementation itself. The ‘dequeue’ option deletes the element from the queue.īased on the input/choice by user, the respective operations are performed.The major problem with the stack implemented using an array is, it works only for a fixed number of data values. The ‘enqueue’ option adds a specific value to the stack. Three options are given, such as ‘enqueue’, ‘dequeue’, and ‘quit’. It has an ‘init’ function that is used to initialize the first element, i.e the ‘head’ to ‘None’.Ī method named enqueue_operation’ is defined, that helps add a value to the queue.Īnother method named ‘dequeue_operation’ is defined, that helps delete a value from the queue, and returns the deleted value.Īn instance of the ‘Queue_structure’ is created. What operation would you like to perform ? quit ExplanationĪnother ‘Queue_structure’ class with required attributes is created. ![]() What operation would you like to perform ? dequeue What operation would you like to perform ? enqueue 12 What operation would you like to perform ? enqueue 45 Print('The deleted element is : ', int(dequeued)) My_instance.enqueue_operation(int(my_input))ĭequeued = my_queue_operation() My_input = input('What operation would you like to perform ? ').split() When it is required to implement a queue data structure using a linked list, a method to add (enqueue operation) elements to the linked list, and a method to delete (dequeue operation) the elements of the linked list are defined.īelow is a demonstration for the same − Example ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |