First of all the best thing to do for check something if you didn't sure is to see it - print it. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Array of Linked Lists in C: initializing and inserting? Ask Question. Asked 4 years, 3 months ago. Active 4 years, 3 months ago. Viewed 16k times. I want to initialize it to point to NULL.
Improve this question. The insertion is invalid. There is again a memory leak. See my answer. Will edit ASAP : — zuzu. May I ask why a memory leak occurs? Add a comment. Active Oldest Votes. Improve this answer. Vlad from Moscow Vlad from Moscow k 18 18 gold badges silver badges bronze badges.
Especially with the purpose of the function being collecting data for it anyway? We also keep a track of last node created while iteration to update its next pointer.
Besides this, we will also store the first node created, that we will return in end. Your email address will not be published. This site uses Akismet to reduce spam. Learn how your comment data is processed. We are going to use the element at the index 1 of the array as the bottom of the stack and the last element of the stack as the top of the stack as described in the picture given below. Since we need to add and remove elements from the top of the stack, we are going to use a pointer which is always going to point the topmost element of the stack.
It will point to the element at index 0 when the stack is empty. We are going to consider only the elements from 1 to S. It might be possible that there are other elements also in the array but we are not going to consider them as stack. To add an item to a stack push , we just need to increment the top pointer by 1 and add the element there. So, if S. So, we will first check for the overflowing of the stack and then accordingly add the element to it. Similarly to remove an item from a stack pop , we will first check if the stack is empty or not.
If it is empty, then we will throw an error of "Stack Underflow", otherwise remove the element from the stack and return it. A stack using a linked list is just a simple linked list with just restrictions that any element will be added and removed using push and pop respectively. In addition to that, we also keep top pointer to represent the top of the stack. This is described in the picture given below.
If the stack is empty, we will make the new node head of the linked list and also point the top pointer to it. If the stack is not empty, we will add the new node at the last of the stack. For that, we will point next of the top to the new node - S. Similarly, to remove a node pop , we will first check if the stack is empty or not as we did in the implementation with array. In the case when the stack is not empty, we will first store the value in top node in a temporary variable because we need to return it after deleting the node.
Now if the stack has only one node top and head are same , we will just make both top and head null. If the stack has more than one node, we will move to the node previous to the top node and make the next of point it to null and also point the top to it. POP S We first iterated to the node previous to the top node and then we marked its next to null - tmp. After this, we pointed the top pointer to it - S.
In the next chapter, we are going to study about queues and implement it also using an array and a linked list. Username Password min 6 characters Password again Email. Go to Sign Up By signing up or logging in, you agree to our Terms of service and confirm that you have read our Privacy Policy.
0コメント