Suppose we have a Queue as follows: E -> D -> C -> B
We maintain a head pointer to E and a tail pointer to B.
To add an element, we make a new queue item (say A), we set B->next = A, and set tail = A.
To delete an element, we let a temp pointer to the head (say tempHead = head), set head = E->next and deallocate tempHead.
Assume the Linked list has following operations 1.node* insert(node* node, bool head); /* head = true if insertion at head else insertion at tail */ 2. node* remove(bool head); /* head = true if removal at head else removal at tail */ Now implement the stack functions as below. STK_RET push(node* pnode) { node* head; head = insert(pnode, true); /* insert the node at head */ if (head) return STK_OK; else return STK_NOK; } similarly pop can be implemented to remove the node at head Also the conventioin of inserting and removal from the tail can be used.
examples:- delete this node (identified by a pointer)- insert a new node before this node- replace this node with another node
void pointer
Well, in a singly linked list you can only move forward, if the pointer you seek is behind your current position you'll have to cross the hole list to get there. In a doubly linked list you can simply move back as well as forward.... hope this helps...
In a doubly linked list, you can iterate backwards as easily as forwards, as each element contains links to both the prior and the following element. You can also insert or delete an element without needing to iterate and remember the prior element's link. This comes at a cost. You are adding storage to each element for the second link, and you are adding processing overhead to the insert and delete operation. You have to determine the tradeoff.
Assume the Linked list has following operations 1.node* insert(node* node, bool head); /* head = true if insertion at head else insertion at tail */ 2. node* remove(bool head); /* head = true if removal at head else removal at tail */ Now implement the stack functions as below. STK_RET push(node* pnode) { node* head; head = insert(pnode, true); /* insert the node at head */ if (head) return STK_OK; else return STK_NOK; } similarly pop can be implemented to remove the node at head Also the conventioin of inserting and removal from the tail can be used.
examples:- delete this node (identified by a pointer)- insert a new node before this node- replace this node with another node
Yes it is possible to implement stack and queue using linked list
It is easier to insert into a singly linked list.
Some common operations that can be performed on a linked list include inserting a node, deleting a node, searching for a specific node, traversing the list, and updating a node's value. Other operations may include reversing the list, merging two lists, sorting the list, and finding the length of the list.
Yes, it is easy.
void pointer
Macro operations are those not linked to individual types and invoked on objetcs - but rather general operations associated with the database as a whole.
Well, in a singly linked list you can only move forward, if the pointer you seek is behind your current position you'll have to cross the hole list to get there. In a doubly linked list you can simply move back as well as forward.... hope this helps...
One advantage of a linked list (with pointers) is that it is fairly cheap to insert or delete an element - once you know where it is. A disadvantage is that getting a specific element - for example, the 1000th. element - is expensive.
In a doubly linked list, you can iterate backwards as easily as forwards, as each element contains links to both the prior and the following element. You can also insert or delete an element without needing to iterate and remember the prior element's link. This comes at a cost. You are adding storage to each element for the second link, and you are adding processing overhead to the insert and delete operation. You have to determine the tradeoff.
You copy a singly linked list into a doubly linked list by iterating over the singly linked list and, for each element, calling the doubly linked list insert function.