Pages

Tuesday, 10 March 2020

Linked List :- create,delete, display Operations

// Single Linked List create,delete, display Operations
import java.util.*;
class lists
{
int info;
lists next;
}
class rklklist
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
slist sl=new slist();
int ch,n,i,x,y;
while(true)
  {
System.out.printf("\n **** MENU ****\n");
System.out.println("1.Creation");
System.out.println("2.Deletion");
System.out.println("3.Display");
System.out.println("4.exit");
System.out.println("enter ur choice:");
ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("\n Enter how many nodes:");
        n=sc.nextInt();
for(i=0;i<n;i++)
  {
System.out.println("\n Enter the element:");
x=sc.nextInt();
sl.create(x);
}
break;
case 2: System.out.println("\nEnter the element:");
            y=sc.nextInt();
            sl.del(y);
break;
case 3: sl.display();
break;
case 4: System.exit(0);
default:System.out.println("Wrong choice");
}
  }
}
}

  class slist
  {
lists start,q,tmp,head;
void create(int x)
{
tmp=new lists();
tmp.info=x;
tmp.next=null;
if(start==null)
{
start=tmp;
System.out.printf("\n start.next=%h tmp.info=%d",start,tmp.info);
}
else
{
q=start;
System.out.printf("\n q.next=%h q.info=%d",q,q.info);
while(q.next!=null)
{
q=q.next;
System.out.printf("\n q.next=%h q.info=%d",q,q.info);
}
q.next=tmp;
System.out.printf("\n q.next=%h q.info=%d",tmp,tmp.info);
return;
}
}

void del(int y)
{
if(start.info==y)
{
tmp=start;
start=start.next;
}
q=start;
while(q.next!= null)
{
      if(q.next.info==y)
{
tmp=q.next;
q.next=tmp.next;
return;
}
     q=q.next;
  }
    if(q.next.info==y)
{
tmp=q.next;
q.next=null;
      return;
}
System.out.printf("\n Element %d not found ",y);
}
void display()
{
if(start== null)
  System.out.printf("list is empty");
q=start;
System.out.printf("\n\t list is :");
System.out.printf("\n %h --> %d ",start,start.info);
while(q.next != null)
{
q=q.next;
        System.out.printf("%h-->%d || ",q,q.info);
}
System.out.printf("%h",q.next);
return;
 }
 }

 output:

Queue implementation using linked list

// Queue implementation using linked list
import java.util.*;
class qulink
{
int info;
qulink next;
}
class rklinkqueue
   {
public static void main(String []args)
{
queue ql=new queue();
int ch,x;
Scanner sc=new Scanner(System.in);
while(true)
    {
System.out.printf("\n ***MENU***\n");
System.out.println("1.INSERT");
System.out.println("2.DELETE");
System.out.println("3.DISPLAY");
System.out.println("4.EXIT");
System.out.println("Enter Ur Choice:");
ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("Enter any Number into queue :");
x=sc.nextInt();
ql.insert(x);
break;
case 2: ql.delete();
break;
case 3: ql.display();
break;
case 4: System.exit(0);
default:System.out.println("Wrong choice");
       }
    }
  }
}


class queue
{
   qulink front,rear,start,tmp;
   void insert(int x)
    {
tmp=new qulink();
tmp.info=x;
tmp.next=null;
if(front == null)
front=tmp;
else
rear.next=tmp;
rear=tmp;
   }
   void delete()
    {
if(front == null)
  System.out.println("queue underflow");
else
{
tmp=front;
System.out.println("Deleted Element is:"+tmp.info);
front=front.next;
   }
  }
  void display()
  {
start=front;
if(front==null)
System.out.println("queue is empty");
else
{
  System.out.printf("queue linked list is :");
  while(start!=null)
  {
    System.out.printf(" %h -->%d ||",start,start.info);
  start=start.next;
    }
  System.out.printf(" %h",start);
}
 }
}

output:




Monday, 9 March 2020

Stack implementation using linked list

// Stack implementation using linked list
import java.util.*;
class stlink
{
int info;
stlink next;
}
 class rklinkstack
 {
public static void main(String []args)
{
stack sl=new stack();
int ch,x;
Scanner sc=new Scanner(System.in);
while(true)
    {
System.out.printf("\n ***MENU***\n");
System.out.println("1.PUSH");
System.out.println("2.POP");
System.out.println("3.DISPLAY");
System.out.println("4.EXIT");
System.out.println("Enter Ur Choice:");
ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("Enter any Number into stack :");
x=sc.nextInt();
sl.push(x);
break;
case 2: sl.pop();
break;
case 3: sl.display();
break;
case 4: System.exit(0);
default: System.out.println("Wrong choice");
     }
}
 }
}

class stack
{
   stlink start,top,tmp;
   void push(int x)
    {
tmp=new stlink();
  tmp.info=x;
tmp.next=top;
top=tmp;
    }
   void pop()
   {
if(top==null)
       System.out.println("Stack underflow");
else
{
tmp=top;
     System.out.println("Deleted Element is:"+tmp.info);
top=top.next;
}
  }
  void display()
  {
if(top==null)
System.out.println("Stack is empty");
  else
{
     start=top;
      System.out.printf("Stack linked list is : %h --> %d ",start,start.info);
     while(start.next!=null)
       {
       start=start.next;
       System.out.printf(" || %h -->%d ",start,start.info);
      }
     System.out.printf("||%h",start.next);
}
  }
}

output:

Saturday, 7 March 2020

LinkedList

Each element in the LinkedList is called the Node. Each Node of the Linked List contains two items:
1)Content of the element
2)Pointer/Address/Reference to the Next Node in the LinkedList.
Note:
1. Head of the LinkedList only contains the Address of the First element of the List.
2. The Last element of the LinkedList contains null in the pointer part of the node because it is the end of the List so it doesn’t point to anything as shown in the above diagram.
3. The diagram which is shown above represents a singly linked list. There is another complex type variation of LinkedList which is called doubly linked list,node of a doubly linked list contains three parts: 
1) Pointer to the previous node of the linked list 
2) content of the element 
3) pointer to the next node of the linked list.

The following features are provided by Linked List:
1. Linked list allows dynamic memory allocation, which means memory allocation is done at the run time by the compiler and we do not need to mention the size of the list during linked list declaration.

2. Linked list elements don’t need contiguous memory locations because elements are linked with each other using the reference part of the node that contains the address of the next node of the list.

3. Insert and delete operations in the Linked list are not expensive because adding and deleting an element from the linked list does not require element shifting, only the pointer of the previous and the next node requires change.


Types of Linked List:
  • Simple Linked List − Item navigation is forward only.
  • Doubly Linked List − Items can be navigated forward and backward.
  • Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous.

Basic Operations
Following are the basic operations supported by a list.

  • Insertion − Adds an element at the beginning of the list
  • Deletion − Deletes an element at the beginning of the list.
  • Display − Displays the complete list.
  • Search − Searches an element using the given key.
  • Delete − Deletes an element using the given key.
Insertion Operation
Adding a new node in linked list is a more than one step activity. We shall learn this with diagrams here. First, create a node using the same structure and find the location where it has to be inserted.
Imagine that we are inserting a node B (NewNode), between  A (LeftNode) and C (RightNode). Then point B.next to C
NewNode.next −> RightNode;

Now, the next node at the left should point to the new node.
LeftNode.next −> NewNode;
This will put the new node in the middle of the two. The new list should look like this 
Similar steps should be taken if the node is being inserted at the beginning of the list. While inserting it at the end, the second last node of the list should point to the new node and the new node will point to NULL.

Deletion Operation
Deletion is also a more than one step process. We shall learn with pictorial representation. First, locate the target node to be removed, by using searching algorithms.

The left (previous) node of the target node now should point to the next node of the target node −
LeftNode.next −> TargetNode.next;

This will remove the link that was pointing to the target node. Now, using the following code, we will remove what the target node is pointing at.
TargetNode.next −> NULL;

We need to use the deleted node. We can keep that in memory otherwise we can simply deallocate memory and wipe off the target node completely.

//Program to illustrate Single Linked List Operations
import java.util.*;
class lists
{
int data;
lists next;
}
class linklist
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
slist sl=new slist();
int opt,y,z;
while(true)
  {
System.out.println("1.creation");
System.out.println("2.Insertion");
System.out.println("3.Deletion");
System.out.println("4.display");
System.out.println("5.exit");
System.out.println("enter ur choice:");
opt=sc.nextInt();
switch(opt)
{
case 1: System.out.println("Enter the number:");
y=sc.nextInt();
sl.create(y);
break;
case 2: System.out.println("Enter the position:");
y=sc.nextInt();
System.out.println("Enter the Data:");
z=sc.nextInt();
sl.insert(y,z);
break;
case 3: System.out.println("Enter the position:");
y=sc.nextInt();
sl.deletes(y);
break;
case 4: sl.display();
break;
case 5: System.exit(0);
}
  }
}
}

  class slist
  {
lists head,last,new1,temp;
slist()
{
head=null;
}
public void create(int x)
{
new1=new lists();
if(new1==null)
{
System.out.println("creation is not poosible");
return;
}
new1.data=x;
if(head==null)
{
head=last=new1;
last.next=null;
}
else
{
last.next=new1;
last=new1;
last.next=null;
}
}

public void display()
{
if(head==null)
{
System.out.println("Empty list");
return;
}
System.out.println("The Elements are:");
System.out.printf("%h---",head);
for(temp=head;temp!=null;temp=temp.next)
{
System.out.printf(" %d || %h ",temp.data,temp.next);
}
System.out.println();
}
public void insert(int x,int d)
{
int pos,i;
pos=x;
new1=new lists();
if(new1==null)
{
System.out.println("Insufficient Memory");
return;
}
new1.data=d;
if(head==null && pos==1)
{
head=last=new1;
new1.next=null;
}
else if(head!=null && pos==1)
{
new1.next=head;
head=new1;
}
else
{
i=2;
temp=head;
while(i<pos)
{
temp=temp.next;
i++;
}
if(temp==null)
{
last.next=new1;
last=new1;
last.next=null;
}
else
{
new1.next=temp.next;
temp.next=new1;
}
}
}
public void deletes(int p)
 {
int pos,i;
lists curr;
pos=p;
if(head==null)
{
System.out.println("Empty Linked List");
return;
}
if(pos==1)
{
temp=head;
head=head.next;
temp=null;
}
else
{
i=2;
temp=head;
curr=temp.next;
while(i<pos)
{
temp=temp.next;
curr=temp.next;
i++;
}
temp.next=curr.next;
curr=null;
}
 }
}

Thursday, 5 March 2020

Minimum Spanning Tree

What is a Spanning Tree?
Given an undirected and connected graph , a spanning tree of the graph  is a tree that spans  (that is, it includes every vertex of ) and is a subgraph of  (every edge in the tree belongs to )

What is a Minimum Spanning Tree?
The cost of the spanning tree is the sum of the weights of all the edges in the tree. There can be many spanning trees. Minimum spanning tree is the spanning tree where the cost is minimum among all the spanning trees. 


There are two famous algorithms for finding the Minimum Spanning Tree:
  1. Kruskal’s Algorithm
  2. Prim’s Algorithm
Kruskal’s Algorithm
Kruskal's algorithm is a greedy algorithm that finds a minimum spanning tree for a connected weighted graph. It finds a tree of that graph which includes every vertex and the total weight of all the edges in the tree is less than or equal to every possible spanning tree.

Algorithm
  1. Arrange all the edges of the given graph G(V,E) in ascending order as per their edge weight.
  2. Choose the smallest weighted edge from the graph and check if it forms a cycle with the spanning tree formed so far.
  3. If there is no cycle, include this edge to the spanning tree else discard it.
  4.  Repeat Step 2 and Step 3 until (V−1) number of edges are left in the spanning tree.

1. Problem
Suppose we want to find minimum spanning tree for the following graph G using Kruskal’s algorithm.

2. Problem
Suppose we want to find minimum spanning tree for the following graph G using Kruskal’s algorithm.

From the above graph we construct the following table 

Edge No.
Vertex Pair
Edge Weight
E1
(a, b)
20
E2
(a, c)
9
E3
(a, d)
13
E4
(b, c)
1
E5
(b, e)
4
E6
(b, f)
5
E7
(c, d)
2
E8
(d, e)
3
E9
(d, f)
14

Now we will rearrange the table in ascending order with respect to Edge weight 

Edge No.
Vertex Pair
Edge Weight
E4
(b, c)
1
E7
(c, d)
2
E8
(d, e)
3
E5
(b, e)
4
E6
(b, f)
5
E2
(a, c)
9
E3
(a, d)
13
E9
(d, f)
14
E1
(a, b)
20


Since we got all the 5 edges in the last figure, we stop the algorithm and this is the minimal spanning tree and its total weight is (1+2+3+5+9)=20.

Prim's Algorithm
Prim, is a greedy algorithm that finds a minimum spanning tree for a connected weighted graph. It finds a tree of that graph which includes every vertex and the total weight of all the edges in the tree is less than or equal to every possible spanning tree. 

Algorithm
  1. Initialize the minimal spanning tree with a single vertex, randomly chosen from the graph.
  2. Repeat steps 3 and 4 until all the vertices are included in the tree.
  3. Select an edge that connects the tree with a vertex not yet in the tree, so that the weight of the edge is minimal and inclusion of the edge does not form a cycle.
  4. Add the selected edge and the vertex that it connects to the tree.
1. Problem
Suppose we want to find minimum spanning tree for the following graph G using Prim’s algorithm.
2. Problem
Suppose we want to find minimum spanning tree for the following graph G using Prim’s algorithm.
Solution
Here we start with the vertex ‘a’ and proceed.

This is the minimal spanning tree and its total weight is (1+2+3+5+9)=20

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...