![]() ![]() So we will take a temporary pointer ptr and equate it to the top pointer.The pop operation would be similar to deleting a node from the starting of a linked list.Similarly we can push the values 2 & 3 in the stack which will give us a linked list of three nodes with top pointer pointing to the node containing value 3.Finally we will make top = ptr to point it to the newly created node which will now be the starting of the linked list and top of our stack.Then we will insert the value 1 in the data part of the Node : ptr->data = valueand make link part of the node equal to top : ptr->link=top.So firstly we will create a new Node using the new operator and return its address in temporary pointer ptr.Let's suppose we have to insert the values 1, 2 & 3 in the stack. So initially when the Stack (Linked List) is empty, the top pointer will be NULL.The push operation would be similar to inserting a node at starting of the linked list.Taking this as the basic structure of our Node:Īs we know that we use a head pointer to keep track of the starting of our linked list, So when we are implementing stack using linked list we can simply call the head pointer as top to make it more relatable to stack.So to solve this lets try to implement Stack using Linked list where we will dynamically increase the size of the stack as per the requirement.If we create a very large array, we will be wasting a lot of memory space.We create an array of predefined size & we cannot increase the size of the array if we have more elements to insert.the element at top will be accessed first.Īnd both insertion & deletion takes place at the top. The data/element which is stored last in the stack i.e. Display\n4.Stack is a data structure which follows LIFO i.e. Printf("\n:: Stack using Linked List ::\n") Step 5 - Finally! Display ' temp → data -> NULL'.Repeat the same until temp reaches to the first node in the stack. Step 4 - Display ' temp → data ->' and move it to the next node. ![]() Step 3 - If it is Not Empty, then define a Node pointer 'temp' and initialize with top.Step 2 - If it is Empty, then display 'Stack is Empty!!!' and terminate the function.We can use the following steps to display the elements (nodes) of a stack. 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 major problem with the stack implemented using an array is, it works only for a fixed number of data values. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |