answersLogoWhite

0

In this case recursion is not necessary, an iterative process is more efficient.

Start by pointing at the head node. While this node has a next node, detach its next node and insert that node at the head. Repeat until the original head node has no next node. At that point the head has become the tail and all the nodes are completely reversed.

The following example shows how this can be implemented, where the list object contains a head node (which may be NULL), and each node has a next node. The tail node's next node is always NULL.

void reverse(list& lst)

{

if( node* p=lst.head )

{

while(p->next)

{

node* n=p.next; // point to the next node

p.next=n.next; // detach the next node

n.next=lst.head; // insert the detached node at the head

lst.head=n; // set the new head node

}

}

}

User Avatar

Wiki User

11y ago

What else can I help you with?

Continue Learning about Engineering

Which of the following data structures can be randomly accessed giving loc A. linked list implemented using array B. singly linked list C. double linked list D. both single and double linked list?

Which of the following data structures can be randomly accessed giving loc?A. linked list implemented using arrayB. singly linked listC. double linked listD. both single and double linked listThe answer is A.


How do you solve josephus problem using circular linked list?

The Josephus problem is a problem to locate the place for the last survivour. It shows the power of the circular linked list over the singly linked lists.


What is the C plus plus programs to implement the stack ADT using a singly linked list?

#include<iostream> #include<time.h> #include<forward_list> int main() { // seed random number generator srand ((unsigned) time(NULL)); // a forward_list is a singly-linked list std::forward_list<int> stack; // push 10 integers onto stack for (int loop=0; loop<10; ++loop) { int num=rand(); std::cout<<"Pushing "<<num<<std::endl; stack.push_front (num); } // pop all integers while (!stack.empty()) { std::cout<<"Popping "<<stack.front()<<std::endl; stack.pop_front(); } }


What is the C plus plus coding for implementation of queue using circular linked list?

The implementation is the same as that for a singly-linked list, the only difference being that rather than maintaining separate pointers to both the head and tail, you only need to maintain a pointer to the tail, since the tail's next node is always the head, which will be itself if there is only one node. As with all queues, insertions occur at the tail and extractions occur at the head.


Disadvantages of circular link list as compared to singly link list?

As compared with doubly-linked lists, one disadvantage is that singly-linked lists can only be efficiently traversed in one direction. Finding the (n - 1)th element, given only a pointer to the nth, requires scanning the list from the first element up to the (n - 1)th. Thus (if memory is too limited to permit tricks such as creating a reversed list) to iterate over the entire list in reverse order would require n + (n - 1) + ... + 1 = n(n + 1)/2 link traversals, which grows quadratically with n. Obviously for a doubly-linked list the time merely grows linearly with n.A down-side of all linked lists versus arrays is that random access can be inefficient; the time taken to find an element with a randomly chosen index grows in direct proportion to the list's length (since we must scan from one of the ends). In contrast, an array allows us to index directly to the elements with simple pointer arithmetic, in a time independent of the array's size - at least in an idealised environment.

Related Questions

Which of the following data structures can be randomly accessed giving loc A. linked list implemented using array B. singly linked list C. double linked list D. both single and double linked list?

Which of the following data structures can be randomly accessed giving loc?A. linked list implemented using arrayB. singly linked listC. double linked listD. both single and double linked listThe answer is A.


How do you implement a doubly linked list by using singly linked list?

Add another pointer to the nodes for the previous node: struct node { struct node *next; struct node *previous; void *data; }; typedef struct node node; Then change the logic for insertion and removal to make sure you set the previous pointer as well as the next one.


Evaluate the polynomial expression using linked list using c?

Evaluating a Polynomial expression using a singly linked list visit : http://myfundatimemachine.blogspot.in/2012/06/polynomial-evaluation-in-c.html


How do you solve josephus problem using circular linked list?

The Josephus problem is a problem to locate the place for the last survivour. It shows the power of the circular linked list over the singly linked lists.


What is a singly linked linear list?

A singly linked list is a linked list which only provides links in "one direction". Using a metaphor, a singly linked list is a one way street, while a doubly linked list is a two way street. Once you move forward in a singly linked list, there is no way to go backwards unless you kept your reference/pointer from before. A singly linked list would look like this: start ----> node1---->node2---->node3 ----> NULL You will see that node2 only has a link forward to node3 - it does not have a link backwards to node1, even though node1 has a link forwards to node2. To prevent us from permanently losing access to portions of the linked list, we generally keep a reference/pointer to "start". A doubly linked list would have twice the number of pointers/references as a singly linked list - making it very inefficient to store small datatypes. On the other hand, it would be possible to move both forwards and backwards with a doubly linked list because you have links pointing both forwards and backwards.


Is it possible for Breadth-First Search (BFS) to be implemented recursively"?

Yes, Breadth-First Search (BFS) can be implemented recursively, but it is not the most efficient method compared to using a queue-based iterative approach.


Using recursion reverse a singly linked list?

Let's suppose your list consists of elements that have an integer part (the data) and a reference (or pointer) to the next list element.Now your function would look like this:void Reverse(listitem){if(listitem.next exists) //you have to check this in a way depending on your implementationReverse(listitem.next); //the function goes right onto the next elementprint(listitem.data); //or do anything that is needed}In a nutshell, this function first explores the whole linked list, and when it reaches the end, the individual function calls reach the data processing block, and then terminate, thus giving the control back to the previous listelement's function call.


How can you prove that the set of all languages that are not recursively enumerable is not countable?

One way to prove that the set of all languages that are not recursively enumerable is not countable is by using a diagonalization argument. This involves assuming that the set is countable and then constructing a language that is not in the set, leading to a contradiction. This contradiction shows that the set of all languages that are not recursively enumerable is uncountable.


Can you skip using a reverse card in the game?

No, you cannot skip using a reverse card in the game.


What is the advantage of doubly linked list over singly linked list?

It's not that one is better than the other. They are used in different circumstances. A linear linked list is used like an array, with the added benefits of random insertion/removal of elements, etc. A circular linked list is often used as a buffer where one portion of the program produces data and another consumes it, such as in communications.


Can you implement Breadth-First Search (BFS) recursively?

Yes, Breadth-First Search (BFS) can be implemented recursively by using a queue data structure to keep track of the nodes to visit next. The algorithm involves visiting each node at the current level before moving on to the next level.


How do you make sentences using 'strongly linked'?

Obesity has been strongly linked to diabetes.