Skip to main content

How to design a LinkedList ?

To design a Linked List we normally need to design three classes one class for Node that contains a data and link to the next node.

Second class contain a big picture as a combination of nodes where one can view the number of nodes  as well as design functions for it.

Third class contains operations or the methods that we want to perform on the linked lists.This class contains main method and is driving whole mechanism.



Basic Structure of Node:-

Data represent the value and next represent the link to other nodes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.problems.list;

public class Node {
 int data;
 Node next;
 


 public Node()
 {
  data=0;
  next=null;
 }
 
 public Node(int data)
 {
  this.data=data;
  next=null;
 }
 
 public Node(int data,Node node)
 {
  this.data=data;
  this.next=node;
 }
 
 public int getData() {
  return data;
 }

 public void setData(int data) {
  this.data = data;
 }

 public Node getNext() {
  return next;
 }

 public void setNext(Node next) {
  this.next = next;
 }

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





Basic Structure of Linked List:-

Length represent the current size of list and head represent the first node.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.problems.list;

public class LinkedList {

 public LinkedList() {
  length = 0;
 }
 
 Node head;
 int length;
 public void addNodeStart(LinkedList list,int data)
 {
  Node temp=new Node(data);
  if(list.head==null)
  {
   System.out.println("head is null");
   head=temp;
   length++;
  }
  else{
  
  temp.next=list.head;
  list.head=temp;
  
  }
 }
 
 public void printList(Node head)
 {
  Node temp =head;
  System.out.println("List Data");
  while(temp!=null)
  {
   System.out.print(+temp.data+" ");
   temp=temp.next;
  }
  
 }
}
Basic Functions:-

Here we are creating one list then passing the head(first node) to function for creating the node.In second method we are printing the list.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package com.problems.list;

public class LinkedListOperations {

 public static void main(String[] args)
 {
  LinkedList list=new LinkedList();
  list.addNodeStart(list, 5);
  list.addNodeStart(list, 15);
  list.addNodeStart(list, 25);
  list.printList(list.head);
 }
}

Comments

.

Popular posts from this blog

Career in software engineering : Roles to know

when someone starts a career in software engineering there are plenty of options. Although most graduates have the same degree, everyone starts in a different role. Developer / Coder - Engineers in this role get a chance to actually implement and write software. At the start of a career mostly it is about fixing defects in existing software then taking small changes to the software and eventually getting to the point to handle and implement more complex changes.  Quality analysts / Quality engineers - Engineers in this role test and automate software. This is a challenging role where one has to know about the full functionality of the software and test accurately so the software built is of high quality. This role also involves the automation of test cases and preparing a test suite that can be run and detect issues automatically. Release Engineers - Once the software is built and tested release engineers are responsible for pushing a set of code to production. A production is a plac

5 books that your college doesn’t tell to read seriously for PLACEMENTS

1.               Let us C - It can be the best book to pick in your first semester itself. The book is written in a very simple style so you can easily grasp most of the concepts of programming by just working hard on it. It will help you in building basics of programming around c language. 2.               HEAD FIRST JAVA- This book you should buy in third semester and invest your whole second year into it by working on all the codes. This book will help you teaching the concepts in a very attractive way. You will never bore while working  through this book. 3.               Data Structure Made Easy In Java - As soon you finished studying core java in the 4 th semester just pick this book to understand the logic’s of data structure and algorithms. It will help you to go deep inside this subject from which questions are frequently asked in all the interviews. This book carries a great set of problems that will help in developing intellectual knowledge around