Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Java Doubly Linked List Insert

Uploaded by

9923008050
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java Doubly Linked List Insert

Uploaded by

9923008050
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

DOUBLY LINKED LIST:

[Implementation]

public class DoublyLinkedList {


class Node{
int data;
Node previous;
Node next;
public Node(int data) {
this.data = data;
}
}
Node head=null;
Node tail=null;
public void addingNode(int data) {
Node newNode = new Node(data);
if(head == null) {
head = tail = newNode;
head.previous = null;
tail.next = null;
}
else {
tail.next = newNode;
newNode.previous = tail;
tail = newNode;
tail.next = null;
}
}
public void display() {
Node current = head;
if(head == null) {
System.out.println("List empty");
return;
}
while(current != null) {
System.out.print(current.data + "<==> ");
current = current.next;
}
}
public static void main(String[] args) {
DoublyLinkedList obj = new DoublyLinkedList();
obj.addingNode(1);
obj.addingNode(2);
obj.addingNode(3);
obj.addingNode(4);
obj.addingNode(5);
obj.display();
}
}
-------------------------------------------------
INSERT AT START:

public class InsertStart {


class Node{
int data;
Node previous;
Node next;
public Node(int data) {
this.data = data;
}
}
Node head, tail = null;
public void addAtStart(int data) {
Node newNode = new Node(data);
if(head == null) {
head = tail = newNode;
head.previous = null;
tail.next = null;
}
else {
head.previous = newNode;
newNode.next = head;
newNode.previous = null;
head = newNode;
}
}
public void display() {
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
while(current != null) {
System.out.print(current.data + "<==>");
current = current.next;
}
System.out.println();
}

public static void main(String[] args) {


InsertStart dList = new InsertStart();
dList.addAtStart(1);
dList.display();
dList.addAtStart(2);
dList.display();
dList.addAtStart(3);
dList.display();
dList.addAtStart(4);
dList.display();
dList.addAtStart(5);
dList.display();
}
}
=========================================
dll - insert at last:

public class InsertStart {


class Node{
int data;
Node previous;
Node next;
public Node(int data) {
this.data = data;
}
}
Node head, tail = null;
public void addAtStart(int data) {
Node newNode = new Node(data);
if(head == null) {
head = tail = newNode;
head.previous = null;
tail.next = null;
}
else {
Node temp=head;
while(temp.next!=null)
{
temp=temp.next;

}
temp.next=newNode;
newNode.previous=temp;

}
}
public void display() {
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
while(current != null) {
System.out.print(current.data + "<==>");
current = current.next;
}
System.out.println();
}

public static void main(String[] args) {


InsertStart dList = new InsertStart();
dList.addAtStart(1);
dList.display();
dList.addAtStart(2);
dList.display();
dList.addAtStart(3);
dList.display();
dList.addAtStart(4);
dList.display();
dList.addAtStart(5);
dList.display();
}
}
=======================================
dll - insert at given position - excercise
========================================
delete - all methods in one prg:
---------

public class DLL {


Node head;
class Node {
int data;
Node prev;
Node next;
Node(int d) { data = d; }
}
public void push(int new_data)
{
Node new_Node = new Node(new_data);
new_Node.next = head;
new_Node.prev = null;
if (head != null)
head.prev = new_Node;
head = new_Node;
}
public void printlist(Node node)
{
Node last = null;
while (node != null) {
System.out.print(node.data + " ");
last = node;
node = node.next;
}
System.out.println();
}
void deleteNode(Node del)
{
if (head == null || del == null) {
return;
}
if (head == del) {
head = del.next;
}
if (del.next != null) {
del.next.prev = del.prev;
}
if (del.prev != null) {
del.prev.next = del.next;
}
return;
}
public static void main(String[] args)
{
DLL dll = new DLL();
dll.push(2);
dll.push(4);
dll.push(8);
dll.push(10);
System.out.print("Original Linked list ");
dll.printlist(dll.head);
dll.deleteNode(dll.head); /*delete first node*/
dll.deleteNode(dll.head.next); /*delete middle node*/
dll.deleteNode(dll.head.next); /*delete last node*/
System.out.print(
"\nFinal Linked list ");
dll.printlist(dll.head);
}
}
=======================================
circular linked list: implementation:

public class CreateList {


public class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
}
}
public Node head = null;
public Node tail = null;
//insert at last
public void add(int data){
Node newNode = new Node(data);
if(head == null) {
head = newNode;
tail = newNode;
newNode.next = head;
}
else {
tail.next = newNode;
tail = newNode;
//this line is important
tail.next = head;
}
}
public void display() {
Node current = head;
if(head == null) {
System.out.println("List is empty");
}
else {
do{
System.out.print("<--> "+ current.data);
current = current.next;
}while(current != head);
System.out.println();
}
}

public static void main(String[] args) {


CreateList cl = new CreateList();
//Adds data to the list
cl.add(1);
cl.add(2);
cl.add(3);
cl.add(4);
cl.display();
}
}
=======================================
cll - insert at start:

public class InsertAtStart {


//Represents the node of list.
public class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
}
}

//Declaring head and tail pointer as null.


public Node head = null;
public Node tail = null;

//This function will add the new node at the end of the list.
public void addAtStart(int data){
//Create new node
Node newNode = new Node(data);
//Checks if the list is empty.
if(head == null) {
//If list is empty, both head and tail would point to new node.
head = newNode;
tail = newNode;
newNode.next = head;
}
else {
//Store data into temporary node
Node temp = head;
//New node will point to temp as next node
newNode.next = temp;
//New node will be the head node
head = newNode;
//Since, it is circular linked list tail will point to head.
tail.next = head;
}
}

//Displays all the nodes in the list


public void display() {
Node current = head;
if(head == null) {
System.out.println("List is empty");
}
else {
System.out.println("Adding nodes to the start of the list: ");
do{
//Prints each node by incrementing pointer.
System.out.print(" "+ current.data);
current = current.next;
}while(current != head);
System.out.println();
}
}

public static void main(String[] args) {


InsertAtStart cl = new InsertAtStart();

//Adding 1 to the list


cl.addAtStart(1);
cl.display();
//Adding 2 to the list
cl.addAtStart(2);
cl.display();
//Adding 3 to the list
cl.addAtStart(3);
cl.display();
//Adding 4 to the list
cl.addAtStart(4);
cl.display();
}
}
=========================================
CLL - INSERT AT END:

public class InsertAtEnd {


//Represents the node of list.
public class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
}
}

//Declaring head and tail pointer as null.


public Node head = null;
public Node tail = null;

//This function will add the new node at the end of the list.
public void addAtEnd(int data){
//Create new node
Node newNode = new Node(data);
//Checks if the list is empty.
if(head == null) {
//If list is empty, both head and tail would point to new node.

head = newNode;
tail = newNode;
newNode.next = head;
}
else {
//tail will point to new node.
tail.next = newNode;
//New node will become new tail.
tail = newNode;
//Since, it is circular linked list tail will points to head.
tail.next = head;
}
}

//Displays all the nodes in the list


public void display() {
Node current = head;
if(head == null) {
System.out.println("List is empty");
}
else {
System.out.println("Adding nodes to the end of the list: ");
do{
//Prints each node by incrementing pointer.
System.out.print(" "+ current.data);
current = current.next;
}while(current != head);
System.out.println();
}
}

public static void main(String[] args) {


InsertAtEnd cl = new InsertAtEnd();
//Adding 1 to the list
cl.addAtEnd(1);
cl.display();
//Adding 2 to the list
cl.addAtEnd(2);
cl.display();
//Adding 3 to the list
cl.addAtEnd(3);
cl.display();
//Adding 4 to the list
cl.addAtEnd(4);
cl.display();
}
}
------------------------------------------------
cll - GIVEN POSITION delete- EXCERCISE
----------------------------------------------------
public class DeleteEnd {
//Represents the node of list.
public class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
}
}

//Declaring head and tail pointer as null.


public Node head = null;
public Node tail = null;

//This function will add the new node at the end of the list.
public void add(int data){
//Create new node
Node newNode = new Node(data);
//Checks if the list is empty.
if(head == null) {
//If list is empty, both head and tail would point to new node.
head = newNode;
tail = newNode;
newNode.next = head;
}
else {
//tail will point to new node.
tail.next = newNode;
//New node will become new tail.
tail = newNode;
//Since, it is circular linked list tail will point to head.
tail.next = head;
}
}

//Deletes node from end of the list


public void deleteEnd() {
//Checks whether list is empty
if(head == null) {
return;
}
else {
//Checks whether contain only one element
if(head != tail ) {
Node current = head;
//Loop will iterate till the second last element as current.next is
pointing to tail
while(current.next != tail) {
current = current.next;
}
//Second last element will be new tail
tail = current;
//Tail will point to head as it is a circular linked list
tail.next = head;
}
//If the list contains only one element
//Then it will remove it and both head and tail will point to null
else {
head = tail = null;
}
}
}

//Displays all the nodes in the list


public void display() {
Node current = head;
if(head == null) {
System.out.println("List is empty");
}
else {
do{
//Prints each node by incrementing pointer.
System.out.print(" "+ current.data);
current = current.next;
}while(current != head);
System.out.println();
}
}

public static void main(String[] args) {


DeleteEnd cl = new DeleteEnd();
//Adds data to the list
cl.add(1);
cl.add(2);
cl.add(3);
cl.add(4);
//Printing original list
System.out.println("Original List: ");
cl.display();
while(cl.head != null) {
cl.deleteEnd();
//Printing updated list
System.out.println("Updated List: ");
cl.display();
}
}
}
===============================================
stack implementation using array:

class Stack {
private int arr[];

private int top;

private int capacity;

Stack(int size)
{

arr = new int[size];

capacity = size;

top = -1;

public void push(int x)


{

if (isFull())
{

System.out.println("Stack OverFlow");

System.exit(1);

System.out.println("Inserting " + x);

arr[++top] = x;

public int pop()


{

if (isEmpty())
{

System.out.println("STACK EMPTY");

System.exit(1);

return arr[top--];

public int getSize()


{

return top + 1;

}
public Boolean isEmpty()
{

return top == -1;

public Boolean isFull()


{

return top == capacity - 1;

public void printStack()


{

for (int i = 0; i <= top; i++)


{

System.out.print(arr[i] + ", ");

}
}

public static void main(String[] args)


{

Stack stack = new Stack(5);

stack.push(1);

stack.push(2);

stack.push(3);

System.out.print("Stack: ");

stack.printStack();

stack.pop();

System.out.println("\nAfter pop");

stack.printStack();

}
==============================

You might also like