Wednesday, January 5, 2011

Article | Linked List In Java

Here is a Source Code for Linked List In Java.



  1. import java.io.*;

  2. class Node

  3. {

  4. Student data;

  5. Node link;

  6. Node()

  7. {

  8. data=null;

  9. link=null;

  10. }

  11. public Node(Student data)

  12. {

  13. this.data=data;

  14. link=null;

  15. }

  16. }

  17. class LinkedList

  18. {

  19. Node head,newNode;

  20. LinkedList()

  21. {

  22. head=null;

  23. }

  24. public void initList()

  25. {

  26. head=null;

  27. }

  28. public Node createNode(Student data)

  29. {

  30. Node node = new Node(data);

  31. return node;

  32. }

  33. public int size()

  34. {

  35. int count=0;

  36. Node p;

  37. if(head==null)

  38. {

  39. return 0;

  40. }

  41. else

  42. {

  43. p=head;

  44. while(p!=null)

  45. {

  46. count++;

  47. p=p.link;

  48. }

  49. }

  50. return count;

  51. }

  52. public void beforeX(Node newNode,Node x)

  53. {

  54. Node p=head;

  55. while(p!=x.link)

  56. {

  57. p=p.link;

  58. }

  59. newNode.link=p;

  60. x.link=newNode;

  61. }

  62. public void afterX(Node newNode,Node x)

  63. {

  64. Node p=head;

  65. while(p!=x.link)

  66. {

  67. p=p.link;

  68. }

  69. newNode.link=p;

  70. x.link=newNode;

  71. }

  72. public void insertAsFirst(Node newNode)

  73. {

  74. newNode.link=head;

  75. head=newNode;

  76. }

  77. public void insertAsLast(Node newNode)

  78. {

  79. Node p = head;

  80. while(p.link!=null)

  81. p=p.link;

  82. p.link=newNode;

  83. }

  84. public void relativeToX(Node newNode,int rel,Node x)

  85. {

  86. switch(rel)

  87. {

  88. case 1 : beforeX(newNode,x);

  89. break;

  90. case 2 : afterX(newNode,x);

  91. break;

  92. default: System.out.print("\n\nInvalid Option..... Check Syntax of relativeToX()");

  93. }

  94. }

  95. public void insert(Node newNode,int pos,int reltox,Node x)

  96. {

  97. if(head==null)

  98. {

  99. head=newNode;

  100. }

  101. else

  102. {

  103. switch(pos)

  104. {

  105. case 0 : relativeToX(newNode,reltox,x);

  106. break;

  107. case 1 : insertAsFirst(newNode);

  108. break;

  109. case 2 : insertAsLast(newNode);

  110. break;

  111. }

  112. }

  113. }

  114. public Node deleteAny(Node x)

  115. {

  116. Node p=head;

  117. Node tmp;

  118. tmp=x;

  119. if(x==head)

  120. {

  121. head=x.link;

  122. return tmp;

  123. }

  124. else

  125. {

  126. while(p.link!=x)

  127. {

  128. p=p.link;

  129. }

  130. p.link=x.link;

  131. return tmp;

  132. }

  133. }

  134. public Node deleteFirst()

  135. {

  136. Node tmp;

  137. tmp=head;

  138. head=head.link;

  139. return tmp;

  140. }

  141. public Node deleteLast()

  142. {

  143. System.out.print("\n\n........Under Development.........");

  144. return null;

  145. }

  146. public Node delete(int opt,Node x)

  147. {

  148. Node p=head;

  149. Node tmp=null;

  150. if(p==null)

  151. {

  152. System.out.print("\n\n!!!!!! LIST DOES NOT EXIST !!!!!!");

  153. }

  154. else

  155. {

  156. switch(opt)

  157. {

  158. case 1 : tmp=deleteFirst();

  159. break;

  160. case 2 : tmp=deleteLast();

  161. break;

  162. case 3 : tmp=deleteAny(x);

  163. break;

  164. default: System.out.print("\n\nDelete !!-- Wrong Option");

  165. }

  166. }

  167. return tmp;

  168. }

  169. public Node search(int admno)

  170. {

  171. Node p = head;

  172. Student s=null;

  173. if(p==null)

  174. System.out.print("\n\n!!!!!! LIST DOES NOT EXIST !!!!!!");

  175. else

  176. {

  177. while(p!=null)

  178. {

  179. if(p.data.admno==admno)

  180. return p;

  181. p=p.link;

  182. }

  183. }

  184. return null;

  185. }

  186. public void display()

  187. {

  188. Node p=head;

  189. if(p==null)

  190. System.out.print("\n\n!!!!!! LIST DOES NOT EXIST !!!!!!");

  191. while(p!=null)

  192. {

  193. p.data.printData();

  194. p=p.link;

  195. }

  196. }

  197. }

  198. class Student

  199. {

  200. public int admno;

  201. private String name;

  202. Student()

  203. {

  204. this.admno=0;

  205. this.name="";

  206. }

  207. Student(int admno,String name)

  208. {

  209. this.admno=admno;

  210. this.name=name;

  211. }

  212. public int getAdmno()

  213. {

  214. return admno;

  215. }

  216. public void printData()

  217. {

  218. System.out.print("\n\nAdmission Number : "+admno);

  219. System.out.print("\n\nName : "+name);

  220. }

  221. }

  222. class MyStudentListH

  223. {

  224. public static LinkedList ll=new LinkedList();

  225. public static void deleteSubMenu()

  226. {

  227. int choice=0;

  228. while(true)

  229. {

  230. System.out.print("\n\t\t\n\n\tDELETE\n\n");

  231. System.out.print("\n\t1.FIRST\t\t:");

  232. System.out.print("\n\t2.LAST\t\t:");

  233. System.out.print("\n\t3.ANY NODE\t:");

  234. System.out.print("\n\t4.MAIN MENU\t:");

  235. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  236. try

  237. {

  238. System.out.print("\n\nEnter Your Choice : ");

  239. choice=Integer.parseInt(br.readLine());

  240. }

  241. catch(Exception ex)

  242. {

  243. System.out.print("\nInput - Output Error !!!");

  244. System.exit(1);

  245. }

  246. switch(choice)

  247. {

  248. case 1 : ll.delete(1,null);

  249. break;

  250. case 2 : ll.delete(2,null);

  251. break;

  252. case 3 : Node tmp=search();

  253. if(tmp!=null)

  254. {

  255. ll.delete(3,tmp);

  256. }

  257. break;

  258. case 4 : menu();

  259. break;

  260. default: System.out.print("\n\nInvalid Menu Option !!!!!!!!!");

  261. }

  262. }

  263. }

  264. public static void insertSubMenu()

  265. {

  266. int choice=0;

  267. Student s=null;

  268. Node newNode=null;

  269. Node tmp;

  270. while(true)

  271. {

  272. System.out.print("\n\t\t\n\n\tINSERT\n\n");

  273. System.out.print("\n\t1.FIRST\t\t:");

  274. System.out.print("\n\t2.LAST\t\t:");

  275. System.out.print("\n\t3.BEFORE NODE X\t:");

  276. System.out.print("\n\t4.AFTER NODE X\t:");

  277. System.out.print("\n\t5.MAIN MENU\t:");

  278. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  279. try

  280. {

  281. System.out.print("\n\nEnter Your Choice : ");

  282. choice=Integer.parseInt(br.readLine());

  283. if(choice!=5)

  284. {

  285. System.out.print("\n\n---Data to new Record-----");

  286. System.out.print("\n\nEnter The Admission Number : ");

  287. int admno=Integer.parseInt(br.readLine());

  288. System.out.print("\n\nEnter Name : ");

  289. String name = br.readLine();

  290. s=new Student(admno,name);

  291. newNode=ll.createNode(s);

  292. }

  293. }

  294. catch(Exception ex)

  295. {

  296. System.out.print("\nInput - Output Error !!!");

  297. System.exit(1);

  298. }

  299. switch(choice)

  300. {

  301. case 1 : System.out.print("\n\nInsert as First Node ");

  302. ll.insert(newNode,1,0,null);

  303. break;

  304. case 2 : System.out.print("\n\nInsert as Last Node ");

  305. ll.insert(newNode,2,0,null);

  306. break;

  307. case 3 : System.out.print("\n\nInsert before Node X ");

  308. tmp=search();

  309. if(tmp!=null)

  310. {

  311. ll.insert(newNode,0,1,tmp);

  312. }

  313. break;

  314. case 4 : System.out.print("\n\nInsert after Node X ");

  315. tmp=search();

  316. if(tmp!=null)

  317. {

  318. ll.insert(newNode,0,2,tmp);

  319. }

  320. break;

  321. case 5 : System.out.print("\n\nTo Main Menu ");

  322. menu();

  323. break;

  324. default: System.out.print("\n\nInvalid menu option !!!!!");

  325. }

  326. }

  327. }

  328. public static Node search()

  329. {

  330. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  331. int admno=0;

  332. Node res;

  333. try

  334. {

  335. System.out.print("\n\n----Search Data-----");

  336. System.out.print("\n\nEnter Admission number : ");

  337. admno=Integer.parseInt(br.readLine());

  338. }

  339. catch(Exception ex)

  340. {

  341. System.out.print("\nInput - Output Error !!!");

  342. System.exit(1);

  343. }

  344. res=ll.search(admno);

  345. if(res==null)

  346. {

  347. System.out.print("\n\n--->Student Record Not Found");

  348. }

  349. else

  350. {

  351. System.out.print("\n\n--->Student Record Found");

  352. res.data.printData();

  353. }

  354. return res;

  355. }

  356. public static void menu()

  357. {

  358. int choice=0;

  359. while(true)

  360. {

  361. System.out.print("\n\t\t\n\n\tLINKED LIST\n");

  362. System.out.print("\n\t1.INSERT\t:");

  363. System.out.print("\n\t2.DELETE\t:");

  364. System.out.print("\n\t3.DISPLAY\t:");

  365. System.out.print("\n\t4.SEARCH\t:");

  366. System.out.print("\n\t5.INITIALIZE\t:");

  367. System.out.print("\n\t6.SIZE\t\t:");

  368. System.out.print("\n\t7.EXIT\t\t:");

  369. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  370. try

  371. {

  372. System.out.print("\n\nEnter Yor Choice : ");

  373. choice=Integer.parseInt(br.readLine());

  374. }

  375. catch(Exception ex)

  376. {

  377. System.out.print("\nInput - Output Error !!!");

  378. System.exit(1);

  379. }

  380. switch(choice)

  381. {

  382. case 1 : insertSubMenu();

  383. break;

  384. case 2 : deleteSubMenu();

  385. break;

  386. case 3 : ll.display();

  387. break;

  388. case 4 : Node tmp=search();

  389. break;

  390. case 5 : ll.initList();

  391. System.out.print("\n\nList is Initialized");

  392. break;

  393. case 6 : System.out.print("\n\nNumber of items in the List : "+ll.size());

  394. break;

  395. case 7 : System.out.print("\nGOOD BYE.....\n\n");

  396. System.exit(1);

  397. default: System.out.print("\n\nInvalid menu Option !!!!!");

  398. }

  399. }

  400. }

  401. public static void main(String[] arg)

  402. {

  403. int n,i;

  404. String name;

  405. menu();

  406. }

  407. }

Loading...

Comments :

0 comments to “ Linked List In Java ”

Top Views

BloggerTipsTemplates

CHICKLETS

Enter your email address:

Delivered by FeedBurner

Followers