If we have one pointer pointing at the last element of link list and we don't have any other pointer pointing to any other node of the link list then is it possible to delete that last node safely?
As per my opinion its not possible.
This is not possible if you do not have reference to the second last element of the list. You need to make the pointer part of the second last element null. So as per my knowledge, if you only have a reference to the last element, then deleting it is not possible.
An exceptional case will be when there is only one element in the list which is the first as well as the last element. In such a case, freeing that element will not cause any harm.
When you have a linked-list, you need to have a reference to the first element using which you traverse the entire list. But when you say you do not have any reference to the list except the last element, this is a very unusual case.
Related
I am implementing a stack in C. The task i have been given requires me to use the first element of the array as top.
For example:
If i have the struct:
struct stack {
int arr[MAX];
int top
};
I need to assign the first element of arr(i.e. arr[0]) to top and then implement the stack.
I don't get the question as top is usually assigned the value -1 and is accordingly incremented or decremented. What exactly do I need to do here?
This is a rather strange requirement, because the most natural way to implement the stack is using the last element of the array as the top by adding new elements at the end of the array and removing them from the end.
This way you will satisfy the LIFO requirement of the stack and also avoid moving the rest of the data in the array as you would have if you were adding and removing elements from the beginning of the array.
Also I'm not sure what is the top member of your struct is supposed to do, but the most natural thing to have here is some way to indicate what is the last element of the array currently is (i.e. the top of the stack). So it seems to me that the top member should contain the index of the last element in the array and by doing stack.arr[stack.top] you will retrieve the top element of the stack.
if I have a linked list in C and I need to write a function that will delete one of it's elements in the the list (not the first or last one). The list is a global variable, so anywhere I use it it will be changed. If I do a while loop to get to the correct spot and then delete the element, wont the whole list change because I had to move the head to that point?
While doing head = head -> next in the while loop, the pointer is moving element by element and I have no way of going back to the first element once I have finished.
So how can I delete an element without erasing part of the list?
You shouldn't move the global head but rather make a new pointer that will work as a temporary head while iterating through the list.
Lets say the list looks like:
A -> B -> C -> D
If you want to delete C for example, you need to iterate to B (and see that it points to C). Then save it's pointer to C lets call it pC, move on to C and save it's pointer to D lets call it pD.
Then you can simply re-link the list by doing pC = pD so that B now links to D and skips C entirely. That way you remove an element from a linked list.
Your new list will be A -> B -> D.
Just remember to dump the variable before you lose the pointer to it.
Two options, off the top of my head :
Have the last element point to the first element. This way, no matter what node of your linked list you delete, you can always "go back".
Always keep a reference to your first element.
This being said, I don't think having a linked list as a global is a good idea ; is there no other way? Furthermore : is a linked list the right thing for what you're trying to achieve?
I'm trying to make a link list and I'm having trouble with the concept with linking the middle part, I'm just doing a little pseudo-code right now, haven't actually coded anything.
(struct pointers) *current, *ahead, *behind, *begin;
(behind)-->(current)-->(ahead) //This is what I want to do
behind->next = current;
current->next = ahead;
Is this the proper way to break and connect the list? Without losing anything..
What you have looks correct but rather incomplete. One of the unwritten rules of programming is that you cannot write a linked list implementation correctly the first time. There are four cases you need to deal with:
Insert into an empty list
Insert into a non-empty list
Removing the first element from the list
Removing any other element from the list
There are also doubly-linked lists, where each element has a pointer to both the previous element and the next element. That makes it easier to handle things like removal of a random element without traversing the list, but can be trickier to get right.
I am trying to convert an array to an linked list.
so basically, im gonna have a structure called "head" which will be the first element
and node, which will be the other elements.
any ideas so i can it started?
I don't see any solution simpler than just iterating through the array and appending the elements to the list.
The standard way to implement linked lists in C is with a single node structure containing a data member and a next pointer. Every time you want a new node, malloc space for it and set the next pointer of the last node in the list to point to it. The last node's next pointer should point to NULL.
You only have to hold onto a regular pointer to the first element. That's your head pointer.
Without using malloc you won't be able to easily add a new node to store data in so its best to just use the array to avoid the mess (but why can't you use malloc now?)
I have tried a lot to learn linked list.But all my efforts were wasted.Please can some one help me understand linked list by providing his/her own code?Thanks in advance.
A linked list is simply a list of elements (usually called nodes) where each node has a reference (or pointers, in C) to the next node:
http://img837.imageshack.us/img837/5613/ll1s.png
You keep track of the list by having a pointer to the first node (the "head"), and by having the last node point to null
Linked lists where each element points to both the next and previous nodes are called doubly-linked lists.
By following these references, you can traverse the list and get any node.
A common advantage of linked lists over arrays is that you can insert and remove the elements in O(1) (constant) time. The disadvantage is that you have O(N) random-access.
See Wikipedia for more.
Have you play some of these rally games? The organizer left this hints all around the city and you must get one hint and then solve the riddle for getting the position of the next hint. Now, imagine every hint comes with a little prize.
Well, linked list are like that: every element has "content" on it AND the memory address (the hint) for getting the next item. The next item, of course, has another prize and another hint.
A linked list is a series of object each pointing to the next one in the list. The last element in the list has NULL as it's next pointer.
You keep track of the head of the list in your program so you can traverse the list from the start.
You might want to keep track of the current position in the list, but that will depend on your application.
A doubly linked list has a previous element pointer too enabling you to traverse the list in both directions.
This:
typedef struct tagProp
{
rtPropertyKey key;
rtProperty property;
struct tagProp *next;
} TProperty;
defines a property key/value lookup list.
A linked list is implemented as a list of "nodes". The nodes are linked together using pointers. The following code describes one node. The pointer to the next node is called next. In my example, each node contains an integer value as its data.
struct node {
int val;
struct node * next;
};
The fun is how to actually create a list. You have to use malloc to create new nodes. When you malloc the new node, you have to tie into the list using the next pointer.
We can help you more if you specifically tell us what your issues are...