![]() ![]() To check if the stack is empty or not we can simply check if top = NULL, it means that the stack is empty.b) In push operation, if new nodes are inserted at the end of linked list, then in pop operation, nodes must. After we pop once our stack will look something like this: To keep the Last In First Out order, a stack can be implemented using linked list in two ways: a) In push operation, if new nodes are inserted at the beginning of linked list, then in pop operation, nodes must be removed from beginning.Finally, we will delete the node using delete operator and pointer ptr i.e delete(ptr).Then we will move the top pointer to the next node i.e.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.Inserting a node to a given position in a linked list. Custom linked list implementing Iterable used as stack. Let's suppose we have to insert the values 1, 2 & 3 in the stack. Designing function prototypes for a singly-linked list API in C. So initially when the Stack (Linked List) is empty, the top pointer will be NULL. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |