package LinkedList;
public class FullLinkedList {
static Node head = null;
public static void insert(int data) {
Node node = new Node(data);
if (null == head) {
head = node;
} else {
node.next = head;
head = node;
}
}
public void deleteNode(int data) {
System.out.println("removing node is "+data);
Node temp = head;
while(temp!=null && temp.next!=null) {
if(temp.next.data == data) {
temp.next = temp.next.next;
}
temp= temp.next;
}
}
public static void insertAtLast(int data) {
Node temp = head;
if (null == head) {
temp = new Node(data);
;
} else {
while (temp.next != null) {
temp = temp.next;
}
Node node = new Node(data);
temp.next = node;
}
}
public static void insertAtSpecPos(int data, int pos) {
Node temp = head;
for (int i = 0; i < pos; i++) {
temp = temp.next;
}
Node node = new Node(data);
node.next = temp.next;
temp.next = node;
}
public static void insertAtMiddle(int data) {
Node fast = head;
Node slow = head;
Node prev = null;
while (fast != null && fast.next != null) {
fast = fast.next.next;
prev = slow;
slow = slow.next;
}
System.out.println(slow.data);
Node node = new Node(data);
prev.next = node;
node.next = slow;
// node.next = slow.next;
// slow.next = node;
}
public static void deleteAtFirst() {
head = head.next;
}
public static void deleteAtLast() {
Node temp = head;
while (temp.next.next != null) {
temp = temp.next;
}
temp.next = null;
}
public void deleteMiddleNode() {
if(head == null && head.next ==null) {
return;
}
Node slow = head;
Node fast = head;
Node prev = null;
while(fast!=null && fast.next!=null) {
fast = fast.next.next;
prev = slow;
slow = slow.next;
}
prev.next = slow.next;
}
public boolean hasLoop() {
Node temp = head;
Node slow = temp;
Node fast = temp;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
System.out.println("Hasloop");
return true;
}
}
System.out.println("No loop");
return false;
}
public void findStartNode() {
Node temp = head;
Node slow = temp;
Node fast = temp;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
slow = temp;
while(slow != fast) {
slow = slow.next;
fast = fast.next;
}
System.out.println(slow.data);
}
}
}
public static void reverseList() {
Node current = head;
Node next, prev = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
}
public static void sortList() {
Node temp = head;
Node current = temp;
temp.next = head;
Node newNode = head;
while (current != null && newNode.data < current.next.data) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
head = temp.next;
}
public static void print() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
}
public Node fromNthToLast(int data) {
Node first = head;
Node second = head;
for(int i=0;i first = first.next;
}
while(first!=null) {
first = first.next;
second = second.next;
}
return second;
}
static void rotate(int k)
{
Node current = head;
int count =1;
while(count current= current.next;
count++;
}
Node KthNode = current;
while(current.next!= null){
current= current.next;
}
current.next= head;
head = KthNode.next;
KthNode.next= null;
}
public static Node removeDuplicates() {
Node temp = head;
while(temp.next!=null) {
if(temp.data==temp.next.data) {
Node current = temp.next.next;
temp.next = current;
}
else {
temp = temp.next;
}
//temp = temp.next;
}
return head;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
class implemntStack1 {
Node top = null;
final static int RogueValue = -999999;
public boolean empty() {
return top == null;
}
public void push(int n) {
Node p = new Node(n);
p.next = top;
top = p;
} // end push
public int pop() {
if (this.empty())
return RogueValue; // a symbolic constant
int hold = top.data;
top = top.next;
return hold;
} // end pop
public void display()
{
Node tempDisplay = top; // start at the beginning of linkedList
while (tempDisplay != null){ // Executes until we don't find end of list.
System.out.println(tempDisplay.data);
tempDisplay = tempDisplay.next; // move to next Node
}
}
}
class Node1 {
int data;
Node1 next;
public Node1(int data) {
this.data = data;
this.next = null;
}
}
class CurcularLinkList {
private Node tail;
CurcularLinkList(){
tail = null;
}
public static void main(String args[]){
CurcularLinkList list = new CurcularLinkList();
list.insertAtBegin(30);
list.insertAtBegin(20);
list.insertAtBegin(10);
list.insertAtLast(40);
list.removeFromStart();
list.printList();
}
private void removeFromStart() {
// TODO Auto-generated method stub
Node temp = tail.next;
if(null == temp){
tail = null;
}
else{
tail.next=temp.next;
}
}
private void printList() {
// TODO Auto-generated method stub
Node current = tail.next;
while(current!=tail){
System.out.println(current.data);
current=current.next;
}
System.out.println(current.data);
}
private void insertAtLast(int data) {
Node node = new Node(data);
insertAtBegin(data);
tail = tail.next;
}
private void insertAtBegin(int data) {
// TODO Auto-generated method stub
Node node = new Node(data);
if (null == tail) {
tail = node;
tail.next = tail;
} else {
node.next = tail.next;
tail.next = node;
}
}
}
class DoublyNode {
int data;
DoublyNode prev;
DoublyNode next;
public DoublyNode(int data) {
super();
this.data = data;
this.prev = null;
this.next = null;
}
}
class DoublyLinkList {
DoublyNode head;
public DoublyLinkList() {
// TODO Auto-generated constructor stub
this.head = null;
}
public void insertAtBegining(int data){
DoublyNode node = new DoublyNode(data);
if(null == head){
head=node;
}else{
node.next=head;
head.prev=node;
head= node;
}
}
public void insertAtLast(int data){
DoublyNode node = new DoublyNode(data);
if(null == head){
head=node;
}else{
DoublyNode current = head;
while(current.next!=null){
current = current.next;
}
current.next=node;
node.prev=current;
}
}
public void insertAfterData(int data1, int data2) {
if(null == head) {
System.out.println("List is empty");
}else {
DoublyNode current = head;
while(current!= null) {
if(current.data == data1) {
DoublyNode node = new DoublyNode(data2);
DoublyNode next = current.next;
if(null!=next) {
current.next = node;
node.next= next;
node.prev=current;
next.prev=node;
} else {
current.next=node;
node.prev=current;
}
}
current=current.next;
}
}
}
public void deleteFromStart(){
if(null == head){
System.out.println("List is Empty");
}else{
head=head.next;
}
}
public void deleteFromLast(){
if(null == head){
System.out.println("List is Empty");
}else{
DoublyNode current =head;
while(current.next.next!=null){
current=current.next;
}
current.next=null;
}
}
public void printForward(){
DoublyNode currentNode= head;
while(null!=currentNode){
System.out.println(currentNode.data);
currentNode=currentNode.next;
}
}
public void printBackword(){
DoublyNode currentNode= head;
while(null!=currentNode.next){
currentNode=currentNode.next;
}
while(null!=currentNode){
System.out.println(currentNode.data);
currentNode=currentNode.prev;
}
}
}
class LinkList {
Node head;
Node tail;
public LinkList() {
// TODO Auto-generated constructor stub
this.head = null;
this.tail = null;
}
public void addNodeBegining(String data) {
if (null == head) {
head = new Node(data);
} else {
Node temp = new Node(data);
temp.next = head;
head = temp;
}
}
public void printList() {
Node current = head;
while (current != null) {
System.out.println(current.data);
current = current.next;
}
}
public void insertAtLast(String data) {
if (null == tail) {
tail = new Node(data);
head = tail;
} else {
Node temp = new Node(data);
tail.next = temp;
tail = temp;
}
}
public void insertAtLastWithouTail(String data) {
if (null == head) {
head = new Node(data);
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = new Node(data);
}
}
public void insetAfter(String data, String newData) {
Node current = head;
Node nextAdd = null;
while (current.data != data) {
current = current.next;
}
nextAdd = current.next;
Node temp = new Node(newData);
current.next = temp;
temp.next = nextAdd;
}
public void deleteFromStart() {
if (null == head) {
System.out.println("List is Empty");
} else {
head = head.next;
}
}
public void deleteFromLast() {
if (null == head) {
System.out.println("List is Empty");
} else {
Node current = head;
if (current.next == null) {
current = null;
} else {
while (current.next.next != null) {
current = current.next;
}
}
current.next = null;
}
}
public void deleteNode(String data) {
Node current = head;
Node prev = null;
while(current.data!=data) {
prev = current;
current = current.next;
}
prev.next=current.next;
}
}
public class FullLinkedList {
static Node head = null;
public static void insert(int data) {
Node node = new Node(data);
if (null == head) {
head = node;
} else {
node.next = head;
head = node;
}
}
public void deleteNode(int data) {
System.out.println("removing node is "+data);
Node temp = head;
while(temp!=null && temp.next!=null) {
if(temp.next.data == data) {
temp.next = temp.next.next;
}
temp= temp.next;
}
}
public static void insertAtLast(int data) {
Node temp = head;
if (null == head) {
temp = new Node(data);
;
} else {
while (temp.next != null) {
temp = temp.next;
}
Node node = new Node(data);
temp.next = node;
}
}
public static void insertAtSpecPos(int data, int pos) {
Node temp = head;
for (int i = 0; i < pos; i++) {
temp = temp.next;
}
Node node = new Node(data);
node.next = temp.next;
temp.next = node;
}
public static void insertAtMiddle(int data) {
Node fast = head;
Node slow = head;
Node prev = null;
while (fast != null && fast.next != null) {
fast = fast.next.next;
prev = slow;
slow = slow.next;
}
System.out.println(slow.data);
Node node = new Node(data);
prev.next = node;
node.next = slow;
// node.next = slow.next;
// slow.next = node;
}
public static void deleteAtFirst() {
head = head.next;
}
public static void deleteAtLast() {
Node temp = head;
while (temp.next.next != null) {
temp = temp.next;
}
temp.next = null;
}
public void deleteMiddleNode() {
if(head == null && head.next ==null) {
return;
}
Node slow = head;
Node fast = head;
Node prev = null;
while(fast!=null && fast.next!=null) {
fast = fast.next.next;
prev = slow;
slow = slow.next;
}
prev.next = slow.next;
}
public boolean hasLoop() {
Node temp = head;
Node slow = temp;
Node fast = temp;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
System.out.println("Hasloop");
return true;
}
}
System.out.println("No loop");
return false;
}
public void findStartNode() {
Node temp = head;
Node slow = temp;
Node fast = temp;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
slow = temp;
while(slow != fast) {
slow = slow.next;
fast = fast.next;
}
System.out.println(slow.data);
}
}
}
public static void reverseList() {
Node current = head;
Node next, prev = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
}
public static void sortList() {
Node temp = head;
Node current = temp;
temp.next = head;
Node newNode = head;
while (current != null && newNode.data < current.next.data) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
head = temp.next;
}
public static void print() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
}
public Node fromNthToLast(int data) {
Node first = head;
Node second = head;
for(int i=0;i first = first.next;
}
while(first!=null) {
first = first.next;
second = second.next;
}
return second;
}
static void rotate(int k)
{
Node current = head;
int count =1;
while(count
count++;
}
Node KthNode = current;
while(current.next!= null){
current= current.next;
}
current.next= head;
head = KthNode.next;
KthNode.next= null;
}
public static Node removeDuplicates() {
Node temp = head;
while(temp.next!=null) {
if(temp.data==temp.next.data) {
Node current = temp.next.next;
temp.next = current;
}
else {
temp = temp.next;
}
//temp = temp.next;
}
return head;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
class implemntStack1 {
Node top = null;
final static int RogueValue = -999999;
public boolean empty() {
return top == null;
}
public void push(int n) {
Node p = new Node(n);
p.next = top;
top = p;
} // end push
public int pop() {
if (this.empty())
return RogueValue; // a symbolic constant
int hold = top.data;
top = top.next;
return hold;
} // end pop
public void display()
{
Node tempDisplay = top; // start at the beginning of linkedList
while (tempDisplay != null){ // Executes until we don't find end of list.
System.out.println(tempDisplay.data);
tempDisplay = tempDisplay.next; // move to next Node
}
}
}
class Node1 {
int data;
Node1 next;
public Node1(int data) {
this.data = data;
this.next = null;
}
}
class CurcularLinkList {
private Node tail;
CurcularLinkList(){
tail = null;
}
public static void main(String args[]){
CurcularLinkList list = new CurcularLinkList();
list.insertAtBegin(30);
list.insertAtBegin(20);
list.insertAtBegin(10);
list.insertAtLast(40);
list.removeFromStart();
list.printList();
}
private void removeFromStart() {
// TODO Auto-generated method stub
Node temp = tail.next;
if(null == temp){
tail = null;
}
else{
tail.next=temp.next;
}
}
private void printList() {
// TODO Auto-generated method stub
Node current = tail.next;
while(current!=tail){
System.out.println(current.data);
current=current.next;
}
System.out.println(current.data);
}
private void insertAtLast(int data) {
Node node = new Node(data);
insertAtBegin(data);
tail = tail.next;
}
private void insertAtBegin(int data) {
// TODO Auto-generated method stub
Node node = new Node(data);
if (null == tail) {
tail = node;
tail.next = tail;
} else {
node.next = tail.next;
tail.next = node;
}
}
}
class DoublyNode {
int data;
DoublyNode prev;
DoublyNode next;
public DoublyNode(int data) {
super();
this.data = data;
this.prev = null;
this.next = null;
}
}
class DoublyLinkList {
DoublyNode head;
public DoublyLinkList() {
// TODO Auto-generated constructor stub
this.head = null;
}
public void insertAtBegining(int data){
DoublyNode node = new DoublyNode(data);
if(null == head){
head=node;
}else{
node.next=head;
head.prev=node;
head= node;
}
}
public void insertAtLast(int data){
DoublyNode node = new DoublyNode(data);
if(null == head){
head=node;
}else{
DoublyNode current = head;
while(current.next!=null){
current = current.next;
}
current.next=node;
node.prev=current;
}
}
public void insertAfterData(int data1, int data2) {
if(null == head) {
System.out.println("List is empty");
}else {
DoublyNode current = head;
while(current!= null) {
if(current.data == data1) {
DoublyNode node = new DoublyNode(data2);
DoublyNode next = current.next;
if(null!=next) {
current.next = node;
node.next= next;
node.prev=current;
next.prev=node;
} else {
current.next=node;
node.prev=current;
}
}
current=current.next;
}
}
}
public void deleteFromStart(){
if(null == head){
System.out.println("List is Empty");
}else{
head=head.next;
}
}
public void deleteFromLast(){
if(null == head){
System.out.println("List is Empty");
}else{
DoublyNode current =head;
while(current.next.next!=null){
current=current.next;
}
current.next=null;
}
}
public void printForward(){
DoublyNode currentNode= head;
while(null!=currentNode){
System.out.println(currentNode.data);
currentNode=currentNode.next;
}
}
public void printBackword(){
DoublyNode currentNode= head;
while(null!=currentNode.next){
currentNode=currentNode.next;
}
while(null!=currentNode){
System.out.println(currentNode.data);
currentNode=currentNode.prev;
}
}
}
class LinkList {
Node head;
Node tail;
public LinkList() {
// TODO Auto-generated constructor stub
this.head = null;
this.tail = null;
}
public void addNodeBegining(String data) {
if (null == head) {
head = new Node(data);
} else {
Node temp = new Node(data);
temp.next = head;
head = temp;
}
}
public void printList() {
Node current = head;
while (current != null) {
System.out.println(current.data);
current = current.next;
}
}
public void insertAtLast(String data) {
if (null == tail) {
tail = new Node(data);
head = tail;
} else {
Node temp = new Node(data);
tail.next = temp;
tail = temp;
}
}
public void insertAtLastWithouTail(String data) {
if (null == head) {
head = new Node(data);
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = new Node(data);
}
}
public void insetAfter(String data, String newData) {
Node current = head;
Node nextAdd = null;
while (current.data != data) {
current = current.next;
}
nextAdd = current.next;
Node temp = new Node(newData);
current.next = temp;
temp.next = nextAdd;
}
public void deleteFromStart() {
if (null == head) {
System.out.println("List is Empty");
} else {
head = head.next;
}
}
public void deleteFromLast() {
if (null == head) {
System.out.println("List is Empty");
} else {
Node current = head;
if (current.next == null) {
current = null;
} else {
while (current.next.next != null) {
current = current.next;
}
}
current.next = null;
}
}
public void deleteNode(String data) {
Node current = head;
Node prev = null;
while(current.data!=data) {
prev = current;
current = current.next;
}
prev.next=current.next;
}
}
Comments
Post a Comment