I'd suggest different classes for the stack, the nodes referencing/holding the data and the data (items) themselves. Since you have only one end to insert a unique element on top of the stack, it inserts the new element at the top of the stack. It supports the following methods: -setElement(Object e) - setNext(Object newNext) -setPrev(Object newPrev) - getElement() - getNext() - getPrev() By using a doubly linked list to, all the methods of a deque have constant (that is, O(1)) running time. If that all works, then work on the code. Push operation involves inserting new elements in the stack. These operations are: Push The push operation adds a new element to the stack. That means, stack implemented using linked list works for variable size of data. A stack supports few basic operations and we need to implement all these operations (either with a linked list or an array) to make a stack. Is the stack state correct? If so, now pop off all of the 3 items, one at a time, and again move the arrows (pointers) and verify that everything behaves correctly, especially in the case of popping off the last item (because that's where you currently are having trouble). The stack implemented using linked list can work for unlimited number of values. To implement a stack using a linked list, we need to set the following things before implementing actual operations. Erase and redraw the pointers as they change. To debug your algorithm (not the code, the algorithm if this isn't correct, the code will never be correct) I'd take a pencil and paper and walk through the steps of pushing 3 items onto the stack: draw boxes to represent the stack nodes and arrows to represent the pointers. I have made a push function for a simple stack implemented using doubly linked list but the pointer always moves on to the else condition, even when the starting node is empty. If your objective is just to implement a stack (LIFO queue), then a doubly-linked list is more complexity than you want.Ī stack can be implemented with a singly linked list. if ( size > CAPACITY), then print Stack overflow error message. Step by step descriptive logic to push elements in stack. I am not able to understand why I am getting a segmentation fault.As nv3 pointed out in Solution 1, using the same class for the stack and for the items you are managing is leading to confusion. Push() - function, we use this function to insert data into the stack, so first we check if topfull i.e stack is full and data cannot be inserted. How to push elements in stack using linked list Insertion of new element to stack is known as push operation in stack. As for any given node, we have both previous and next node addresses information available. Doubly Linked List can traverse both in forwards and backwards direction. I am yet to debug the entire code but I am getting stuck at the read() function where the input is an Assignment1.txt which includes: 2,3 For Doubly Linked list in Data Structure in C, unlike singly Linked List, which only traverses in one direction. Printf("Enter the number of vertices: ") Void push(int source, int num, struct node *head)Ĭhar a = "Assignment1.txt" ,c push () : Insert the element into Stack and assign the top pointer to the element. It has some theoretical advantages over contiguous storage options. I have made a push function for a simple stack implemented using doubly linked list but the pointer always moves on to the else condition, even when the starting node is empty. A linked list is a collection of values arranged in a linear, unidirectional sequence. I am trying to implement Hierholzer's algorithm using C.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |