Question

If If you have a normal singly linked list, how could you print the items backward?

If If you have a normal singly linked list, how could you print the items backward?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Print items Backwards in a Normal Singly Linked List-

Given a linked list, can print its items backwards using a recursive function. For example, if tthe given linked list is

1->2->3->4 , then output should be4->3->2->1.

Algorithm-

1.Call print reverse for hed->next.

2. Print head ->data

Implementation:

   #include <stdio.h> #include<stdlib.h> struct node{ int data; struct node*next; }; struct node *head = NULL; struct node *current = NULL; void reverse_print(struct node *list){ if(list == NULL){ printf("[null] => "); return; } reverse_print (list->next); printf(" %d =>",list ->data); } //create linked list void insert (int data){ //allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); link-> data = data; link-> next = NULL; // if head is empty , create new list if (head == NULL) { head = link ; return; } current = head //move to the end of the list while (current ->next! = NULL) current = current ->next; // Insert to the end of the list current ->next = link; } int main (){ insert(10); insert(20); insert(30); insert(1); insert(40); insert(56); reverse_print(head); return 0; } 

OUTPUT:

Output of the program should be

[null] => 56 => 40 => 1 => 30 => 20 => 10 =>

Add a comment
Know the answer?
Add Answer to:
If If you have a normal singly linked list, how could you print the items backward?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 3: Reversing a singly-linked list of integers Suppose that you have a singly-linked list detined...

    Question 3: Reversing a singly-linked list of integers Suppose that you have a singly-linked list detined via the following data type (30) Integet itemt net: et stem in 1ist Complete the following function so that it reverses the list pointed to by the arguwent void reverse linkedlist(list itom tx shead)

  • Hello, how do I create a new singly linked list by concatenating two existing singly linked...

    Hello, how do I create a new singly linked list by concatenating two existing singly linked list in c++? Thank you!

  • You are given a pointer to a singly linked list. The singly linked list has cycles...

    You are given a pointer to a singly linked list. The singly linked list has cycles due to a programming error. Write a C program to detect whether the linked list has cycles without storing all the pointers to the nodes. The function detect_cycles should return 1 when a cycle is found and 0 when there are no cycles. Your code should handle all corner cases carefully and should not cause segmentation fault. Figure shows various examples of cycles for...

  • Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying...

    Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying structure. Include the following methods: 1. insert an element within the linked list.(this should also work for the front and the rear of the list) 2. Remove an element from the linked list 3. Display (print) the elements of the linked list in order. 4. A method to check if the list is "empty". Test your solution using a linked list that initially has...

  • I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas....

    I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas. public class Node ! int id; Node next: public Node (int id) ( this.id id: Write program codes for the traditional: 1) append int id), prepend(int id), removeFirstNodeO. displayAlINodesO, and findById(int id) operations for a singly linked list 2) pushint id), pop), peek0, displayAllNodes0 operations for a stack 3) enQueue(int id), deQueuel), displayAINodes() operations for a gueue Please make sure that you declare separate...

  • How do you implement a stack using a singly linked list in Java? Can you make...

    How do you implement a stack using a singly linked list in Java? Can you make it a simple implementation with just push and pop methods.

  • Write a C function that iterates through a singly linked list and converts it to a...

    Write a C function that iterates through a singly linked list and converts it to a doubly linked list. Your solution must NOT create a second list. You must come up with the appropriate parameters and return type. Note: the nodes in the singly linked list have a field for a previous pointer, but that field has not initially been set in any of the nodes. You must do this as part of your solution!

  • how to implement a linked list object using only singly linked list toolkit. Then implement a...

    how to implement a linked list object using only singly linked list toolkit. Then implement a FREQUENCY function to count the ovcurrence of each element in the list. task#1: Add = operator to node: implement the assignment operator for the node such that setting a node = overwrites the value in the node with the value. task#2:Linked List class implement the methods of linked list. insert search and locate remove node* operator [] task#3: Implement the Frequency

  • ngu Cons eY Ja Question 1 a) Write pseudo code to output a singly-linked list in...

    ngu Cons eY Ja Question 1 a) Write pseudo code to output a singly-linked list in reverse order when you are NOT allowed to allocate memory dynamically. What is the running time of the algorithm? b) Write pseudo code to output a singly-linked list in reverse order when you are ALLOWED to allocate memory dynamically. What is the running time of the algorithm? c) You have an increasingly-sorted circular list (using an array) of n elements that is full. The...

  • You implemented a stack as a singly linked list (you add at the head and remove...

    You implemented a stack as a singly linked list (you add at the head and remove from the head). Perform the following operations on the stack: push(50), push(90), push(30), push(53), push(52), pop(), push(51), pop(), pop(), push(100), and push(15). After all the operations are performed, draw the resulting linked list. Indicate which node is the top of the stack in the resulting linked list.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT