Sunday, September 13, 2015

Java Function Code to create Reverse Linked List of Singly Linked List (In memory pointer changes)

Here is a sample java Function Code to create Reverse Linked List of Singly Linked List (In memory pointer changes). Approach
  • 1) Maintain a previous node pointer
  • 2) maintain a current node pointer
  • 3) store current-> next in temporary variable
  • 4) replace the current node-> next to previous pointer
  • 5) do step 3 and 4 till you reach current node is your end node
  • 6) at the end update the head->next to null as the old linked list head will be your end node now
  • 7) return previous node as this will be the head of your new linked list.
You can see the image below - before and after structure of linked list: java Function Code to create Reverse Linked List
package com.sample.linkedlist;

public class ReverseLinkedList {

 static class Node {
  int data;
  Node next;

  public Node(int data) {
   this.data = data;
  }

  @Override
  public String toString() {
   return "Node[data=" + data + "]";
  }

 }

 public static Node reverse(Node head) {

  if (head == null)
   return null;

  Node currentNode = head.next;
  Node previousNode = head;
  while (currentNode != null) {
   System.out.println("previous:" + previousNode +" current:"+currentNode);
   
   Node tempCurrent = currentNode.next;

   currentNode.next = previousNode;
   previousNode = currentNode;

   /* move to next node */
   currentNode = tempCurrent;
  }
  head.next = null;

  return previousNode;
 }

 static void print(Node head) {
  while (head != null) {
   System.out.print(head + " ");
   head = head.next;
  }
  System.out.println();

 }

 public static void main(String[] args) {
  
  Node head = new Node(1);
  head.next = new Node(2);
  head.next.next = new Node(3);
  head.next.next.next=new Node(4);
  
  /*print actual linked list*/
  print(head);

  Node newHead = reverse(head);

  /*print reversed linked list*/
  print(newHead);
 }
}

Output
Node[data=1] Node[data=2] Node[data=3] Node[data=4] 
previous:Node[data=1] current:Node[data=2]
previous:Node[data=2] current:Node[data=3]
previous:Node[data=3] current:Node[data=4]
Node[data=4] Node[data=3] Node[data=2] Node[data=1] 


Please share/ask more data structure problems with us. We will try to solve your problem in coming posts. Also share this post with your friends/classmates.

Monday, December 17, 2012

Principles of Testing - Software Testing Principles

Testing is a process to expose hidden defects. It is detecting errors and deviations from specification. It is verifying that a system satisfies its specified requirements or not. Here discuss principles of testing.

Software Testing Principles

Seven General principles of Software testing

Principle 1. Exhaustive Input Testing
Exhaustive input testing means test all possible input condition as test cases. Test for all valid and invalid input conditions. But it is impossible to test a system for all input test cases.

Principle 2. Testing is creative and difficult
Second principle of testing is "testing is creative and difficult". Yes it requires creativity. It requires extensive domain knowledge. It also requires good testing methodology.

Principle 3. Prevention of defects
It is a procedure to fixing errors. If we find defects in early stages of development then its cost of fixing is less. Cost of fixing of any defect is too higher in later stages. So it's better to go with preventive approach.

Principle 4. Testing is risk based
Testing is risk based process. A risk is a loss associated with an event. Risk can be economical.
Suppose I try to test one module of system. I got some defects. I solved defects but due to modification some more defects appear in same module or other modules. So it is risky process. Sometime it increases the cost of testing.

Principle 5. Testing must be planned
Test planning is essential. Test Planning helps to solve many problems in system. Test plan covers points like requirement of testing, test priority, cost of testing, test team, test strategy, test tools etc. These factors effect on testing.

Principle 6. Testing requires independence
Testing must be unbiased. Unbiased testing is essential to objectively test quality of software. If testing is done by same developer who develop it then it may be biased. Developer can be an emotional attachment with its development. Developer who has to test his or her program parts will tend to be too optimistic. Chances of “blindness to their own errors”. So require to testing done by other than developer to make unbiased.

Principle 7. Provide expected results
It is important principle of testing. Test is done to check system is fulfilling user requirement or not. System must provide expected results. Testing checks that system pre defined specifications achieved or not.

 

Tuesday, May 8, 2012

String Reverse Example in Java Programming

It is a sample string reverse example. In this program read array of string from user and give output array of reverse string.
Here use StringBuffer class and create object name[] of StringBuffer class. Use method reverse() to reverse inserted string.

Program Code

//developed by Om Prakash
import java.util.*;
class StringReverse
{
public static void main(String argv[])
{
int size;
Scanner console=new Scanner(System.in);
System.out.println("*******STRING REVERSE*******");
System.out.print("How many string do you want to enter : ");
size=console.nextInt();
StringBuffer name[]=new StringBuffer[size+1];
String temp;
int i;
System.out.println("Enter "+size+" string : ");
for(i=0;i<=size;i++)
{
temp=console.nextLine();
name[i]=new StringBuffer(temp);
}
System.out.println("String after Reverse");
for(i=0;i<=size;i++)
{
name[i].reverse();
System.out.println(name[i]);
}
}
}

Output





Monday, May 7, 2012

Java AWT vs SWING

Difference between Java AWT and SWING
1. AWT(Abstract Window Toolkit) is a heavy weight but Swing is a light weight.

2. Swing feel and look better than AWT components.

3. Swing is a pure java components but AWT native components.

4. AWT not have complex components but Swing has additional components like JTable, JProgressBar, JSlider, JTree etc.

5. Applet list has scrollbar but Swing JList does not support scrolling but this can be done using scrollPane.

6. Applet not support MDI(multiple document interface) window but Swing support MDI.

7. Applet default layout is FlowLayout but Swing default layout is BorderLayout.

8. AWT menu item cannot have images or radio buttons or checkboxes but Swing menu item can have images or radio buttons or checkboxes.

9. AWT do not have JMV(Java Model Viewport) but All swing components have JMV.

10. In AWT, components can be added directly on the Frame or window but in Swing while adding components on Frame or window, they have to be added on its content pane.

11. Swing has more powerful and flexible components than AWT.