Friday, April 1, 2016
Linked List : add function
package blog_linklist;
public class Blog_linklist {
public static void main(String[] args) {
linklist demo = new linklist();
demo.add("Avengers", 2015, "fantasy");
demo.add("Iron Man", 2009, "fantasy");
demo.display();
}
}
----------------------------------------------
package blog_linklist;
public class linklist {
public link firstlink;
public linklist(){
firstlink = null;
}
public void add(String Name, int Year, String Genre){
link newlink = new link(Name, Year,Genre);
newlink.next = firstlink;
firstlink = newlink;
}
public void display(){
link newlink = firstlink;
while(newlink != null){
System.out.println(newlink.name + " "+ newlink.year + " " + newlink.genre);
newlink = newlink.next;
}
}
}
-----------------------------------------------------
package blog_linklist;
public class link {
public int year;
public String genre;
public String name;
// Creating the next link
public link next;
// creating the constructor
public link(String Name, int Year, String Genre){
this.name = Name;
this.year =Year;
this.genre = Genre;
}
}
Java : creatng swaping function
package blog_swap;
import java.util.Scanner;
public class Blog_swap {
/**
* Author #Jacob Kurian#
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x;
int y;
int temp;
System.out.println("Here is the swap function");
System.out.println("-------------------------");
System.out.print("Enter value of x: ");
x = input.nextInt();
System.out.print("Enter value of y: ");
y = input.nextInt();
System.out.println("Details: x =" + x + ", y = " + y);
temp = x;
x = y;
y = temp;
System.out.println("After values swapped");
System.out.println("Details: x =" + x + ", y = " + y);
}
}
import java.util.Scanner;
public class Blog_swap {
/**
* Author #Jacob Kurian#
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x;
int y;
int temp;
System.out.println("Here is the swap function");
System.out.println("-------------------------");
System.out.print("Enter value of x: ");
x = input.nextInt();
System.out.print("Enter value of y: ");
y = input.nextInt();
System.out.println("Details: x =" + x + ", y = " + y);
temp = x;
x = y;
y = temp;
System.out.println("After values swapped");
System.out.println("Details: x =" + x + ", y = " + y);
}
}
Subscribe to:
Posts (Atom)