Anda di halaman 1dari 7

Topic:Program To Implement SLL import java.io.

*; class node { int data; node next; public node() { data = 0; next = null; } } class SLL { node start, last, current; int nodecount; public void insertAt(int x, int position) { nodecount=0; if (position > 0) { if (start == null) { insertFirst(x); return; } else { current = start; nodecount++; while (current !=null) { if (position-1 == 0) { insertFirst(x); return; } else if ( nodecount == position-1) { node p = new node(); p.data = x; p.next = current.next; current.next =p; last=p; return; } else if (nodecount == position) { insertLast(x); return; } else { current=current.next; nodecount++; }

} } } } public void insertFirst(int x) { if (start == null) { node p = new node(); p.data = x; start = p; last = p; } else { node p = new node(); p.data = x; p.next = start; start = p; } } public void insertLast(int x) { if (start == null) { node p = new node(); p.data = x; start = p; last = p; } else { node p = new node(); p.data = x; last.next =p; last = p; } } public int removeFirst() { int x=-1; if (start != null && start == last) { if () { x = start.data; start = null; last = null; } } else if (start != null && start != last) { x = start.data; start = start.next; } return x;

//

} public int removeLast() { int x=-1; if (start != null && start == last) { x = start.data; start = null; last = null; } else if (start != null && start != last) { current = start; x= last.data; while (current.next != last) { current=current.next; } if (current.next == last) { current.next = null; last = current; } } return x; } public void display() { if (start != null) { current = start; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(""); } } } class SLLDemo { public static void main(String args[])throws IOException { int choice,val,position; char ans='y'; SLL obj = new SLL(); do { System.out.println("\nProgram to perform varios operatio ns on Linked list"); System.out.println("1. Insert As First"); System.out.println("2. Insert As Last"); System.out.println("3. Insert At position"); System.out.println("4. Display"); // System.out.println("3. Search for an Item");

// t");

System.out.println("4. Insert an element in a list"); System.out.println("5. Delete First element from the lis System.out.println("6. Delete Last element from the list

"); System.out.println("\n Enter your choice :"); choice=getChar(); switch (choice) { case '1':System.out.println("\nEnter the element"); val=getInt(); obj.insertFirst(val); break; case '2':System.out.println("\nEnter the element"); val=getInt(); obj.insertLast(val); break; case '3':System.out.println("\nEnter the element"); val=getInt(); System.out.println("\nEnter the Position"); position=getInt(); obj.insertAt(val, position); break; case '4':obj.display(); break; case '5':System.out.println("Deleted the element " + obj .removeFirst() + " from the linked list"); break; case '6':System.out.println("Deleted the element " + obj .removeLast() + " from the linked list"); break; // case '3':System.out.println("Enter the element to be sea rched in the linked list "); // key=getInt(); // node temp=obj.search(obj.head,key); // if (temp==null) // System.out.println("The element is not present in the li st"); // break; // // case '4':obj.head=obj.insert(obj.head); // break; // case '5':System.out.println("Enter the element to be del eted from the linked list"); // key=getInt(); // obj.head=obj.dele(obj.head,key); // break; } System.out.println("Do you want to go to main menu(y/n)? "); ans=getChar();

}while(ans=='y'); } public static String getString()throws IOException { InputStreamReader input = new InputStreamReader(System.i n); BufferedReader b = new BufferedReader(input); String s=b.readLine(); return s; } public static char getChar() throws IOException { String s=getString(); return s.charAt(0); } public static int getInt() throws IOException { String s=getString(); return Integer.parseInt(s); } //} } OUTPUT: c:\jdk1.3\bin\java.exe SLLDemo Working Directory - D:\301\ Class Path - .;c:\KawaEnt5.0\kawaclasses.zip;c:\jdk1.3\lib\tools.jar;c:\jdk1.3\j re\lib\rt.jar;c:\jdk1.3\jre\lib\i18n.jar Program to perform varios operations on Linked list 1. Insert As First 2. Insert As Last 3. Insert At position 4. Display 5. Delete First element from the list 6. Delete Last element from the list Enter your choice : 1 Enter the element 11 Do you want to go to main menu(y/n)? y Program to perform varios operations on Linked list 1. Insert As First 2. Insert As Last 3. Insert At position 4. Display 5. Delete First element from the list 6. Delete Last element from the list Enter your choice : 2 Enter the element 33 Do you want to go to main menu(y/n)? y

Program to perform varios operations on Linked list 1. Insert As First 2. Insert As Last 3. Insert At position 4. Display 5. Delete First element from the list 6. Delete Last element from the list Enter your choice : 3 Enter the element 22 Enter the Position 2 Do you want to go to main menu(y/n)? y Program to perform varios operations on Linked list 1. Insert As First 2. Insert As Last 3. Insert At position 4. Display 5. Delete First element from the list 6. Delete Last element from the list Enter your choice : 4 11 22 33 Do you want to go to main menu(y/n)? y Program to perform varios operations on Linked list 1. Insert As First 2. Insert As Last 3. Insert At position 4. Display 5. Delete First element from the list 6. Delete Last element from the list Enter your choice : 5 Deleted the element 11 from the linked list Do you want to go to main menu(y/n)? y Program to perform varios operations on Linked list 1. Insert As First 2. Insert As Last 3. Insert At position 4. Display 5. Delete First element from the list 6. Delete Last element from the list Enter your choice : 4 22 33 Do you want to go to main menu(y/n)?

y Program to perform varios operations on Linked list 1. Insert As First 2. Insert As Last 3. Insert At position 4. Display 5. Delete First element from the list 6. Delete Last element from the list Enter your choice : 6 Deleted the element 33 from the linked list Do you want to go to main menu(y/n)? y Program to perform varios operations on Linked list 1. Insert As First 2. Insert As Last 3. Insert At position 4. Display 5. Delete First element from the list 6. Delete Last element from the list Enter your choice : 4 22 Do you want to go to main menu(y/n)?

Anda mungkin juga menyukai