Pages

Saturday 14 March 2020

Double Linked List


In a single linked list, every node has a link to its next node in the sequence. So, we can traverse from one node to another node only in one direction and we can not traverse back. We can solve this kind of problem by using a double linked list.
      
  Double linked list is a sequence of elements in which every element has links to its previous element and next element in the sequence.
       
   In a double linked list, every node has a link to its previous node and next node. So, we can traverse forward by using the next field and can traverse backward by using the previous field. 

  Every node in a double linked list contains three fields and they are shown in the following figure
Here, 'link1' field is used to store the address of the previous node in the sequence, 'link2' field is used to store the address of the next node in the sequence and 'data' field is used to store the actual value of that node
   

Important Points to be Remembered
  1. In double linked list, the first node must be always pointed by head.
  2. Always the previous field of the first node must be NULL.
  3. Always the next field of the last node must be NULL.

Operations on Double Linked List
In a double linked list, we perform the following operations
  1. Insertion
  2. Deletion
  3. Display

1.Insertion
In a double linked list, the insertion operation can be performed in three ways as follows
  • Inserting At Beginning of the list
  • Inserting At End of the list
  • Inserting At Specific location in the list

1.Inserting At Beginning of the list
We can use the following steps to insert a new node at beginning of the double linked list...

Step 1 - Create a newNode with given value and newNode → previous as NULL.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, assign NULL to newNode → next and newNode to head.
Step 4 - If it is not Empty then, assign head to newNode → next and newNode to head.

2.Inserting At End of the list
We can use the following steps to insert a new node at end of the double linked list...

Step 1 - Create a newNode with given value and newNode → next as NULL.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty, then assign NULL to newNode → previous and newNode to head.
Step 4 - If it is not Empty, then, define a node pointer temp and initialize with head.
Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (until temp → next is equal to NULL).
Step 6 - Assign newNode to temp → next and temp to newNode → previous.

3.Inserting At Specific location in the list (After a Node)
We can use the following steps to insert a new node after a node in the double linked list...

Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, assign NULL to both newNode → previous & newNode → next and set newNode to head.
Step 4 - If it is not Empty then, define two node pointers temp1 & temp2 and initialize temp1 with head.
Step 5 - Keep moving the temp1 to its next node until it reaches to the node after which we want to insert the newNode (until temp1 → data is equal to location, here location is the node value after which we want to insert the newNode).
Step 6 - Every time check whether temp1 is reached to the last node. If it is reached to the last node then display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the function. Otherwise move the temp1 to next node.
Step 7 - Assign temp1 → next to temp2, newNode to temp1 → next, temp1 to newNode → previous, temp2 to newNode → next and newNode to temp2 → previous.

2.Deletion
In a double linked list, the deletion operation can be performed in three ways as follows.
  • Deleting from Beginning of the list
  • Deleting from End of the list
  • Deleting a Specific Node

1.Deleting from Beginning of the list
We can use the following steps to delete a node from beginning of the double linked list...

Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4 - Check whether list is having only one node (temp → previous is equal to temp → next)
Step 5 - If it is TRUE, then set head to NULL and delete temp (Setting Empty list conditions)
Step 6 - If it is FALSE, then assign temp → next to head, NULL to head → previous and delete temp.

2.Deleting from End of the list
We can use the following steps to delete a node from end of the double linked list...

Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty, then display 'List is Empty!!! Deletion is not possible' and terminate the function.
Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4 - Check whether list has only one Node (temp → previous and temp → next both are NULL)
Step 5 - If it is TRUE, then assign NULL to head and delete temp. And terminate from the function. (Setting Empty list condition)
Step 6 - If it is FALSE, then keep moving temp until it reaches to the last node in the list. (until temp → next is equal to NULL)
Step 7 - Assign NULL to temp → previous → next and delete temp.

3.Deleting a Specific Node from the list
We can use the following steps to delete a specific node from the double linked list...

Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with head.
Step 4 - Keep moving the temp until it reaches to the exact node to be deleted or to the last node.
Step 5 - If it is reached to the last node, then display 'Given node not found in the list! Deletion not possible!!!' and terminate the fuction.
Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having only one node or not
Step 7 - If list has only one node and that is the node which is to be deleted then set head to NULL and delete temp (free(temp)).
Step 8 - If list contains multiple nodes, then check whether temp is the first node in the list (temp == head).
Step 9 - If temp is the first node, then move the head to the next node (head = head → next), set head of previous to NULL (head → previous = NULL) and delete temp.
Step 10 - If temp is not the first node, then check whether it is the last node in the list (temp → next == NULL).
Step 11 - If temp is the last node then set temp of previous of next to NULL (temp → previous → next = NULL) and delete temp (free(temp)).
Step 12 - If temp is not the first node and not the last node, then set temp of previous of next to temp of next (temp → previous → next = temp → next), temp of next of previous to temp of previous (temp → next → previous = temp → previous) and delete temp (free(temp)).

3.Displaying a Double Linked List
We can use the following steps to display the elements of a double linked list...

Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the function.
Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with head.
Step 4 - Display 'NULL <--- '.
Step 5 - Keep displaying temp → data with an arrow (<===>) until temp reaches to the last node
Step 6 - Finally, display temp → data with arrow pointing to NULL (temp → data ---> NULL).

Thursday 12 March 2020

ABSTRACT DATA TYPE


Steps to run ABSTRACT DATA TYPE (ADT)

·        Name the program number(1) as add.c

·        Name the program number(2) as add.h

·        Name the program number(3) as add.app

·        .C file with calling function

·        .H file is with  function declaration

·        .App file is with called function

·        Now Compile Program Number(1)


Program Number (1): save this file has add.c
#include<stdio.h>
#include<conio.h>
#include"add.h"
#include"add.app"
void main()
{
int a,b;
clrscr ();
printf("enter any number");
scanf("%d %d",&a,&b);
add(a,b);    // calling function 
getch();
}

Program Number (2): save this file has add.h
void add (int,int);  // function declaration 

Program Number (3):save this file has add.app
#include "add.h"
void add (int a, int b)   // called function 
{
int tmp;
tmp=a+b;
printf("addition of two numbers is : %d",tmp);
}


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;
}
 }
}

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...