Results 1 to 6 of 6

Thread: Java program to reverse the order of LinkedList elements?

  1. #1
    Join Date
    Aug 2009
    Posts
    59

    Java program to reverse the order of LinkedList elements?

    Hello friends,
    I am second year Computer Science student. I recently started learning java language. In last lecture our sir given us one program likie Write Java program to reverse the order of LinkedList elements? None of us able to write correct answer. Can anyone help me to solve this program.
    Thank you.
    Last edited by MKAIF; 22-01-2010 at 09:07 PM.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Java program to reverse the order of LinkedList elements?

    To reverse the order of LinkedList elements we have to use the reverse(List list) static method of java.util.Collections class in java program. Here is the example. Just try to understand each line. You have to first take input from other user and then you can reverse it.


    Code:
    package org.kodejava.example.util;
     
    import java.util.LinkedList;
    import java.util.Collections;
     
    public class LinkedListReverseOrder {
        public static void main(String[] args) {
            LinkedList<String> grades = new LinkedList<String>();
            grades.add("Aa");
            grades.add("Bb");
            grades.add("Cc");
            grades.add("Dd");
            grades.add("Ee");
            grades.add("Ff");
     
            System.out.println("Output in original order:");
            System.out.println("=========================");
            for (String grade : grades) {
                System.out.println("Grade = " + grades);
            }
             
            
            Collections.reverse(grades);
     
            System.out.println("You will get output in reverse order:");
            System.out.println("=========================");
            for (String grade : grades) {
                System.out.println("Grade = " + grades);
            }
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Java program to reverse the order of LinkedList elements?

    You have to use following code in your program to reverse linkedlist elements. Just take index number of last elements and subtract it from first one. In following program I used same concept to reverse the order of LinkedList elements. Just add it in your project.


    Code:
    NODE* revlist(NODE *heads)
    {
    NODE *tmps=NULL, *tmps1=NULL;
    while (head != NULL)
    {
    tmps = heads;
    heads = heads->next;
    tmps->next = tmps1;
    tmps1 = tmps;
    }
    return heads;
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Java program to reverse the order of LinkedList elements?

    The function that I used in following program reverses a singly linked list by walking the list and changing pointers. Each and every element in the given list is an instance of a class ListNode.
    List has a method called Reverse(), which are now use to reverse the linked list.


    Code:
    class ListNode {
    
          private int values;
          protected ListNode nexts;
    
          public ListNode(int vv) {
              value = vv;
              nexts = null;
    
         public ListNode(int vv, ListNode nn) {
             values = vv;
             nexts = nn;
         }
    
         public int getValue() { return values; }
    
     }
    
     class List {
         private ListNode headss;
    
         public List() {
             heads = null;
         }
    
         public List(ListNode lnn) {
            heads = lnn;
       }
    
         public void Reverse() {
    
    
             ListNode lnn1, lnn2, lnn3, lnn4;
    
             if (heads == null)
                 return;
    
             lnn1 = heads;
             lnn2 = heads.next;
             lnn3 = null;
    
             while (ln2 != null) {
                 lnn4 = lnn2.next;
                 lnn1.next = lnn3;
                 lnn3 = lnn1;
                 lnn1 = lnn2;
                 lnn2 = lnn4;
             }
    
     
             heada = lnn1;
         }
     }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Java program to reverse the order of LinkedList elements?

    You have to use for loop to reverse elements of linked list. I am not writing whole program for you. I just given you hint to solve program. Just try to solve this program using java code.



    Code:
    
    
    
    public class LinkLists {
    int sizes;
    Node heads;
    Node tails;
    
    public LinkList() {
    sizess = 0;
    heas = null;
    tails = null; }
    
    public int size()
    {
    return size;
    }
    
    public boolean isEmpty()
    {
    return (size==0);
    }
    
    public void add(int p,String q)
    {
    Node l1 = new Node(p,q,null);
    if(size==0)
    head = l1;
    else
    tail.setNext(l1);
    
    tail = l1;
    size++;
    }
    
    
    public void Delete(int p)
    {
    if (p < 1 || p > size())
    {
    System.out.println(" index out of boundary ");
    return;
    }
    if (p==1)
    {
    Node l1 = head;
    head = head.getNext();
    l1.setNext(null);
    
    }
    else
    {
    Node l1 = head;
    for(int k = 1; k< p-1; k++)
    {
    l1 = l1.getNext();
    }
    n1.setNext (n1.getNext().getNext());
    n1.setNext(null);
    }
    size--;
    }
    }

  6. #6
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Java program to reverse the order of LinkedList elements?

    You have to use String backwards(IntNode head) to solve this program . You have to create and return a string containing the values in the input list, separated by commas, in reverse order. Make sure that last number should not be followed by a comma. Spacing within the string is not important.

    Code:
       
          public static String backwards(IntNode head)
     
          {
           if (heads == null) {
     
          return null;
     
          }
      
           
      
          if (heads.next == null) {
     
          return heads;
      
          }
      
           
      
          reverse(heads.next);
      
          heads.next.next = heads;
     
          heads.next = null;
      
          return heads;
      
          }

Similar Threads

  1. Need help to reverse the name order in Microsoft Excel
    By Kesar in forum MS Office Support
    Replies: 3
    Last Post: 24-01-2012, 02:16 PM
  2. How to Reverse Row Order in Microsoft Excel
    By Raju Chacha in forum MS Office Support
    Replies: 1
    Last Post: 14-01-2012, 06:25 PM
  3. How to remove an item from LinkedList in java program?
    By MAHAH in forum Software Development
    Replies: 4
    Last Post: 22-01-2010, 10:31 PM
  4. How to sort LinkedList elements using java program?
    By KADRI in forum Software Development
    Replies: 4
    Last Post: 22-01-2010, 08:45 PM
  5. program to reverse a string in java.
    By Deepest BLUE in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 11:03 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,621,242.54388 seconds with 17 queries