Monday, December 1, 2014

Query of products and stock

This is a java program to print out the quantity and stock when asked by a user. The index value is then sent to the server and the corresponding product and quantity is returned.
This is just a basic outline for people with doubts. You'll have to refine it for your own personal purposes.

CLIENT

import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
public class cli implements Runnable{
Thread th1;
String str1;
int str2;
PrintWriter pw;
int a=0;
BufferedReader br,br1;
String[] prod=new String[100];
int[] quan=new int[100];
public static void main(String args[])
{
cli sup=new cli();
sup.th1=new Thread(sup);
sup.th1.start();
}
public cli()
{
try{
Socket s=new Socket("localhost",3000);
br=new BufferedReader(new InputStreamReader(System.in));
br1=new BufferedReader(new InputStreamReader(s.getInputStream()));
pw=new PrintWriter(s.getOutputStream(),true);
}catch(IOException e){}
}
public void run()
{
int str;
String ans;
try{
while(true)
{
if(a==1)
{
ans=(br1.readLine());
System.out.println(ans);
}
a=1;
str=Integer.parseInt(br.readLine());
pw.println(str);
System.out.println("Sent");
}}catch(IOException e){}
}
public void prinnt()
{
pw.println(str1+str2);
}
}


SERVER


import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
public class serv implements Runnable{
Thread th1;
String str1;
int str2;
PrintWriter pw;
BufferedReader br;
String[] prod=new String[100];
int[] quan=new int[100];
public static void main(String args[])
{
serv sup=new serv();
sup.th1=new Thread(sup);
sup.th1.start();
}
public serv()
{
try{
ServerSocket ss=new ServerSocket(3000);
Socket s=ss.accept();

prod[0]="Carrot";
quan[0]=5;
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
pw=new PrintWriter(s.getOutputStream(),true);
}catch(IOException e){}
}
public void run()
{
int str;
try{
while(true)
{
str=Integer.parseInt(br.readLine());
str1=prod[str];
str2=quan[str];
prinnt();
}}catch(IOException e){}
}
public void prinnt()
{
pw.println(str1+str2);
}
}

Sunday, November 30, 2014

Teacher Student Chat (With BroadCast and Multicast)

Here's a program to implement Broadcast and Multicast Query System Between a student and a teacher.
You'll have to change the IP address per student logged in.
Just a basic outline for those who have doubts. UI isn't great.

TEACHER


import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;

public class teacher extends Frame implements ActionListener{
TextField tf2;
TextArea ta;
Button b1,b2;
TextField tf1;
public static void main(String args[])
{
teacher teach=new teacher();
}

public teacher()
{
Frame f1=new Frame();
Panel p1=new Panel();
Panel p2=new Panel();
GridLayout gl=new GridLayout(2,2);
FlowLayout fl=new FlowLayout();
p1.setLayout(gl);
tf1=new TextField();
b1=new Button("MULTICAST");
tf2=new TextField();
b2=new Button("BROADCAST");
b2.addActionListener(this);
b1.addActionListener(this);
p1.add(tf1);
p1.add(tf2);
p1.add(b1);
p1.add(b2);
ta=new TextArea();
p2.add(ta);
f1.setLayout(fl);
f1.add(p1,BorderLayout.NORTH);
f1.add(p2,BorderLayout.SOUTH);
f1.setSize(600,600);
f1.setVisible(true);
receiverfunc();


}

public void itsbroadcast()
{
try{
MulticastSocket ms=new MulticastSocket(3000);
InetAddress iads=InetAddress.getByName("255.255.255.255");
System.out.println("HI");
String str=tf2.getText().toString();
byte[] b=str.getBytes();
DatagramPacket dp=new DatagramPacket(b,str.length(),iads,3000);
ms.send(dp);
}catch(IOException e){}
}

public void itsmulticast()
{
try{
MulticastSocket ms=new MulticastSocket(3000);
InetAddress iads=InetAddress.getByName("231.4.2.3");
System.out.println("HI");
String str=tf1.getText().toString();
byte[] b=str.getBytes();
DatagramPacket dp=new DatagramPacket(b,str.length(),iads,3000);
ms.send(dp);
}catch(IOException e){}
}

public void receiverfunc()
{
try{
System.out.println("hi");
MulticastSocket ms=new MulticastSocket(3000);
InetAddress iads=InetAddress.getByName("231.4.2.2");
ms.joinGroup(iads);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
byte[] b=new byte[100];
DatagramPacket dp=new DatagramPacket(b,b.length);
ms.receive(dp);
String str=new String(dp.getData());
ta.append(str+"\n");
receiverfunc();
}catch(IOException e){}

}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b2)
itsbroadcast();
else if(e.getSource()==b1)
itsmulticast();
}
}




STUDENT

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;

public class student1 extends Frame implements ActionListener{
TextArea ta;
TextField tf1;
public static void main(String args[])
{
student1 teach=new student1();
}

public student1()
{
Frame f1=new Frame();
Panel p1=new Panel();
Panel p2=new Panel();
GridLayout gl=new GridLayout(2,2);
FlowLayout fl=new FlowLayout();
p1.setLayout(gl);
tf1=new TextField();
Button b1=new Button("SEND");
p1.add(tf1);
p1.add(b1);
b1.addActionListener(this);
ta=new TextArea();
p2.add(ta);
f1.setLayout(fl);
f1.add(p1,BorderLayout.NORTH);
f1.add(p2,BorderLayout.SOUTH);
f1.setSize(600,600);
f1.setVisible(true);
itsbroadcast();

}
public void itsbroadcast()
{
try{
System.out.println("hi");
MulticastSocket ms=new MulticastSocket(3000);
InetAddress iads=InetAddress.getByName("231.4.2.3");
ms.joinGroup(iads);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
byte[] b=new byte[100];
DatagramPacket dp=new DatagramPacket(b,b.length);
ms.receive(dp);
String str=new String(dp.getData());
ta.append(str+"\n");
itsbroadcast();
}catch(IOException e){}

}

public void itssent()
{
try{
MulticastSocket ms=new MulticastSocket(3000);
InetAddress iads=InetAddress.getByName("231.4.2.2");
System.out.println("HI");
String str=tf1.getText().toString();
byte[] b=str.getBytes();
DatagramPacket dp=new DatagramPacket(b,str.length(),iads,3000);
ms.send(dp);
}catch(IOException e){}
}

public void actionPerformed(ActionEvent e)
{
itssent();
}
}

Tuesday, October 21, 2014

Optimization hashing into arrays

So I came across this problem of basically finding n(n+1)/2 of numbers in an array where n is the frequency of the number.

Eg. 1 2 3 4
would equate to 1+1+1+1=4

Eg. 1 2 1

would equate to 3+1=4   (since 1 occurs twice , n(n+1)/2=3)

Now the input can range from -1000001 and 1000001.

My original method of doing this caused me to time out, even though I was coding it in python.

I finally found a way to organize my data. I simply hashed the numbers present in the input into 2 arrays, one for positive numbers and another for negative numbers.

Then I simply ordered them in ascending order (I'll explain the benefits of arranging your numbers in a sorted order and then working on it in another blog post. But apparently, it can improve the running time up to 6 times!)

Every time a number occurs, I simply incremented the occurrence since my array/list was initialized to 0 for every item.

If 2 occurred thrice then list[2]++ would occur thrice causing its value to be 3

Then I ran a for loop, and checked if the occurrence was greater than 0. If so, I applied the formula and kept adding.

If you have any doubts let me know.




def read():
isum=0
fisum=0
freq=0
lex=[0]*1000001
lexneg=[0]*1000001
s=raw_input("")
numbers = map(int, s.split())
for y in numbers:
if(y>=0):
lex[y]+=1
else:
y=abs(y)
lexneg[y]+=1
for y in xrange(0,1000001):
if(lex[y]>0):
isum=(lex[y]*(lex[y]+1))/2
fisum=fisum+isum
if(lexneg[y]>0):
isum=(lexneg[y]*(lexneg[y]+1))/2
fisum=fisum+isum

print fisum


n=raw_input("")
n=int(n)
for z in xrange(0,n):
x=raw_input("")
read()

Thursday, October 2, 2014

Finding nth prime number (large number) (Optimization)

This was something handy I learned. Actually the problem is pretty easy, but when finding the nth prime where n>1000000 it takes way to long in the conventional way youre thinking. Here is an optimized solution to it, that reduces the time complexity. If you have any doubts, ask!

I think you can even optimize it further by doing p=p+2 and num=num+2. Try it out and see

def prime(chk):
if(chk%2==0):
return False
else:
p=3
while(p<chk**0.5+1):
if(chk%p==0):
return False
p=p+1
return True



def is_prime(x):
count=2
num=4
while(count<x):
if prime(num):
count+=1
num1=num
num=num+1
return num1

result=is_prime("Enter the nth term here")
print result

Saturday, September 27, 2014

Largest Palindrome of 3 Numbers

If you have any doubts, let me know
#include <stdio.h>
#include <string.h>
#include<limits.h>

main()
{
    int i=999,j=999,sum=0,r=0,y,x,sum1=0,a[3000],k=0,temp;
    for(i=999;i>100;i--)
    {
        for(j=999;j>100;j--)
        {
            sum=i*j;
            x=sum;
            while(sum>0)
            {
             sum1=sum1*10;
             sum1=sum1+sum%10;
             sum=sum/10;
            }
            if(sum1==x)
         {
             a[k]=x;
             k++;
         }
            sum1=0;
        }
    }
   
    for(i=0;i<k;i++)
    for(j=0;j<k;j++)
    {
        if(a[i]<a[j])
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
    }
printf("%d",a[k-1]);

}

Tuesday, September 23, 2014

Program to print out the letters present in one word and absent in the other.

If you have any doubts, please do ask.

#include<stdio.h>
main()
{
    int i,j,flag=0;
char name1[10],name2[10];
scanf("%s",name1);
scanf("%s",name2);
for(i=0;i<strlen(name2);i++)
{
for(j=0;j<strlen(name1);j++)
{
if(name2[i]==name1[j])
flag=1;
}
if(flag==0)
{printf("%c",name2[i]);

}
else
    flag=0;
}}

Program to print out numbers separated by stars with the occurrence defined by the number printed.

This will print out something like this
1
2*2
3*3*3
4*4*4*4
and then the reverse image.
If you have any doubt, please do ask.

#include<stdio.h>
main()
{
int i=1,j=2,z=4,k;
printf("%d\n",i);
for(k=0;k<3;k++)
{
for(i=2;i<z-1;i++)
{
printf("%d*",j);
}
printf("%d\n");
j++;
z++;
}
j=4;
for(k=0;k<3;k++)
{
for(i=2;i<z-2;i++)
{
printf("%d*",j);
}
printf("%d\n");
j--;
z--;
}
printf("%d",1);
}

String reversal without string.h

This is a program to reverse a string without the string.h header. If you have any doubts, please do ask.

#include<stdio.h>
main()
{
int i,count=0,j=0;
char string[10],final[10];
scanf("%s",string);
for(i=0;string[i]!='\0';i++)
count++;
count--;
for(i=count;i>=0;i--)
{
final[j]=string[i];
j++;
}
final[j]='\0';
printf("%s",final);
}

Monday, September 15, 2014

Decrypting a message program

Feel free to use it, modify it or anything.
Try using the input

y!sr@xo#dt$pa%or^ko*vb(da&ql

You'll get the output laboratory
I've wrote in the comments what each function does. According to that, modify it to make a suitable input


#include <stdio.h>
#include <string.h>
char string[50],t;
int l,k;
main()
{
int i,j;
  
  gets(string);
   printf(" string is %s\n",string);
    l=strlen(string);
    k=l;
    letsreverse();
}
letsreverse() //function reverses a string
{
    
int i=0,j,q;
char *ptr;
ptr=string; 
q=l;
q--;
for(i=0;i<=(l/2)-1;i++)
  {
 t=*(ptr+i);
 *(ptr+i)= *(ptr+q);
 *(ptr+q)=t;
 q--;
 }
 printf("Reverse string is %s\n",string);
 wedontlikejunk();

}
wedontlikejunk()     // kills off junk
 {
    int i=0,z,c=0,x;
    char clean[20];
    for(x=0;x<k;x++)
    {
         if(string[c] >= 'a' && string[c] <= 'z')
         {
         clean[i]=string[c];
         i++;
         }
         c++;
         
    }
    clean[i]='\0';
    printf("semi string is %s\n",clean);
    for(i=0;clean[i]!='\0';i++)
    string[i]=clean[i];
    string[++i]='\0';
    letskillsomeletters();
}
letskillsomeletters()     //kills alternate letters
{
    int i=0,z;
    char final[20];
    for(z=(10%10);z<k;z++)
    {
        final[i]=string[z];
        i++;
        z++;
        
    
    }
    printf("Final string is %s\n",final);
}

Thursday, August 14, 2014

IP address program

Really short C program to print out your ip address. Run it as an .exe. 


#include<stdlib.h>
main()
{
system("C:\\Windows\\System32\\ipconfig");
}

Wednesday, August 13, 2014

Decimal to Binary Program

Simple program to Convert a decimal number to binary. If you have doubts, let me know
#include <stdio.h>
main()
{
int num,count,temp;
printf("Enter the number");
scanf("%d",&num);
for(count=31;count>= 0;count--)
{
temp=num>>count;
if(temp&1)
printf("1");
else
printf("0");
}
}

Tuesday, August 12, 2014

Program to copy a file

C program to copy file. If you have any doubts, let me know

#include <stdlib.h>
#include <stdio.h>
main()
{
   
FILE *source, *target;
char x, sourcef[100], targetf[100];
printf("File to copy?"); gets(sourcef);   source=fopen(sourcef, "r");  printf("Target File?"); gets(targetf);   target=fopen(targetf, "w");   while((x=fgetc(source))!=EOF) fputc(x, target);   printf("The file has been copied."); }

Program to read from file

Program to read from a file. If you have any doubts, let me know

#include <stdlib.h>
#include <stdio.h>
main()
{
char x, name[100];
FILE *pf;
    printf("Name of file?");
    gets(name);
    pf = fopen(name,"r");
    printf("Contents are :");
         while((x=fgetc(pf))!= EOF)
         printf("%c",x);
    }

Saturday, August 9, 2014

Program to find twin prime numbers.

Program to find twin prime numbers. Please comment if you need help.

#include<stdio.h>
void main( )
{
int  i,n,k,r,a[50],p;
printf("nEnter the range:  ");
scanf("%d", &r);
i=1;
p=0;
while(i<=r)
{
k=0;
n=1;
while(n<=i)
{
if( i%n==0 )
k++;
n++;
}
if(k==2)
{
a[p]=i;
p++;
}
i++;
}
for(n=0;n<p;n++)
{
if(a[n+1] - a[n]==2)
printf("\n%d and %d are twin primes", a[n], a[n+1]);
}
}

Program to implement Fibonacci series

Program to implement Fibonacci series. Comment if you need any help.

#include<stdio.h>
int fibo(int x);
main()
{
int n,i,ar[50];
printf("\n  enter the number of terms:");
scanf("%d",&n);
ar[0]=0;
for(i=1;i<n;i++)
{
ar[i]=fibo(i);
}
printf("\n");
for(i=0;i<n;i++)
printf("%d ",ar[i]);
printf("\n");
}
int fibo(int c)
{
int fib=0;
if((c==1)||(c==2))
return 1;
else
{
fib=fibo(c-1)+fibo(c-2);
return fib;
}
}





Program to write to a file and display the vowel which occurred the most

Program to write to a file and display the vowel which occurred the most often. If you need help comment below

#include<stdio.h>
main()
{
  FILE *f1;
  char c;
  int a,e,i,o,u;
  a=0;
  e=0;
  i=0;
  u=0;
  o=0;
  printf("\nEnter the letters to the file:\n");
  f1=fopen("TEXT.txt","w");
  while((c=getchar())!='\n')
    putc(c,f1);
  fclose(f1);
  f1=fopen("TEXT.txt","r");
  while((c=getc(f1))!=EOF)
   {
     if(c=='a')
       a++;
     else if(c=='e')
       e++;
     else if(c=='i')
       i++;
     else if(c=='o')
       o++;
     else if(c=='u')
       u++;
   }
  fclose(f1);
  if((a>=e)&&(a>=i)&&(a>=o)&&(a>=u))
   printf("\n\n'a'is the vowel which occured the most.\n%d times",a);
  if((e>=a)&&(e>=i)&&(e>=u)&&(e>=o))
   printf("\n\n'e' is the vowel which occured the most.\n%d times",e);
  if((i>=a)&&(i>=e)&&(i>=o)&&(i>=u))
   printf("\n\n'i' is the vowel which occured the most.\n%d times",i);
  if((o>=a)&&(o>=e)&&(o>=i)&&(o>=u))
   printf("\n\n'o'is the vowel which occured the most.\n%d times",o);
  if((u>=a)&&(u>=e)&&(u>=i)&&(u>=o))
   printf("\n\n'u' is the vowel which occured the most.\n%d times",u);
  printf("\n\n");
}

Program to alphabetically sort names.

Program to alphabetically sort names. If you need help, comment below

#include<stdio.h>
#include<string.h>
void sort(int);
void disp(int);
struct student
       {char name[25];
       }a[50],*ptr;
int main()
       {ptr=a;
        int i,j,n;
        printf("\nEnter the number of students:");
        scanf("%d",&n);
        n++;
        printf("\nEnter the names:\n");
        for(i=0;i<n;i++)
               gets((ptr+i)->name);
        sort(n);
        disp(n);
        return 0;
       }
void sort(int n)
       {

        int i,j;
        char copy[25];
        for(i=0;i<n;i++)
               for(j=0;j<strlen((ptr+i)->name);j++)
                       {if((ptr+i)->name[j]<97)
                               (ptr+i)->name[j]+=32;
                       }

        for(i=0;i<n;i++)
               for(j=0;j<n-1;j++)
                       if(strcmp((ptr+j)->name,(ptr+j+1)->name)>0)
                               {strcpy(copy,(ptr+j)->name);
                                strcpy((ptr+j)->name,(ptr+j+1)->name);
                                strcpy((ptr+j+1)->name,copy);
                               }
       }







void disp(int n)
       {
        int i;
        for(i=0;i<n;i++)
               (ptr+i)->name[0]-=32;
        printf("\n\nThe sorted list is:\n");
        for(i=1;i<n;i++)
               puts((ptr+i)->name);

       }

Program for various operations with doubly link list

Program for the various operations with a doubly link list. Comment below if you need help

#include<stdio.h>

#include<stdlib.h>

struct node

{

 int data;

 struct node *next,*prev;

}*start=NULL,*newn,*temp,*ptr;

int num,pos,ele;

void insert_beg();

void insert_beg();

void insert_end();

void insert_aftr();

void insert_pos();

void delete_beg();

void delete_end();

void delete_ele();

void display();

main()

{

 int ch,c,ch1,p,ch2,q;

 do

 {

 printf("\n DLL MENU\n1.Ins\n2.Del\n3.Disp");

 printf("\nEnter the number corresponding to your operation:");

 scanf("%d",&ch);

 switch(ch)

 {

 case 1:do

 {

 printf("\nINSERT MENU\n1.beginning\n2.End\n3.After a node\n4.at a

position\n5.Display");

 printf("\nEnter your choice:");

 scanf("%d",&ch1);

 switch(ch1)

 {

 case 1:insert_beg();

 break;

 case 2:insert_end();

 break;

 case 3:insert_aftr();

 break;

 case 4:insert_pos();

 break;

 case 5:display();

 break;

 default:printf("\nWrong Choice\n");

 break;

 }

 printf("\nDo you want to continue-Insert Menu?(1.Yes;2.No)");

 scanf("%d",&p);

 }while(p!=2);

 break;

 case 2:do

 {

 printf("\nDELETE MENU\n1.Front\n2.End\n3.A specific node\n4.Display");

 printf("\nEnter your choice:");

 scanf("%d",&ch2);

 switch(ch2)

 {

 case 1:delete_beg();

 break;

 case 2:delete_end();

 break;

 case 3:delete_ele();

 break;

 case 4:display();

 break;

 default:printf("\nWrong Choice\n");

 break;

 }

 printf("\nDo you want to continue-Delete Menu?(1.Yes;2.No):");

 scanf("%d",&q);

 }while(q!=2);

 break;

 case 3:display();

 break;

 default:printf("\nWrong Choice\n");

 break;

 }

 printf("\nDo you want to continue-Main Menu?(1.Yes;2.No):");

 scanf("%d",&c);

 }while(c!=2);

}

void display()

{

 if(start==NULL)

 printf("\nLink List is empty\n");

 else

 {

 temp=start;

 while(temp->next!=NULL)

 {

 printf("%d<->",temp->data);

 temp=temp->next;

}

printf("%d",temp->data);

 }

}

void insert_beg()

{

 printf("\nEnter the element to be inserted:");

 scanf("%d",&num);

 newn=(struct node*)malloc(sizeof(struct node));

 newn->data=num;

 if(start==NULL)

 {

 newn->next=NULL;

 newn->prev=NULL;

 }

 else

 {

 newn->prev=NULL;

 newn->next=start;

 start->prev=newn;

 }

 start=newn;

}

void insert_end()

{

 printf("\nEnter the element to be inserted:");

 scanf("%d",&num);

 newn=(struct node*)malloc(sizeof(struct node));

 newn->data=num;

 if(start==NULL)

 {

 newn->next=NULL;

 newn->prev=NULL;

 start=newn;

 }

 else

 {

 temp=start;

 while(temp->next!=NULL)

 {

 temp=temp->next;

 }

 temp->next=newn;

 newn->prev=temp;

 newn->next=NULL;

 }

}

void insert_aftr()

{

 int flag=0;

 if(start==NULL)

 printf("\nLink List is empty\n");

 else

 {

 printf("\nEnter the element after which insertion must be done:");

 scanf("%d",&ele);

 printf("\nEnter the element to be inserted:");

 scanf("%d",&num);

 newn=(struct node*)malloc(sizeof(struct node));

 newn->data=num;

 temp=start;

 while(temp!=NULL)

 {

 if(temp->data==ele)

 {

 if(temp->next==NULL)

 insert_end();

 else

{ newn->next=temp->next;

 newn->prev=temp;

 temp->next->prev=newn;

 temp->next=newn;

 }

 flag=1;

 break;

 }

 temp=temp->next;

 }

 if(flag==0)

 printf("\nElement not found\n");

 }

}

void insert_pos()

{

 int k=1,flag=0;

 printf("\nEnter the position for insertion:");

 scanf("%d",&pos);

 if(pos==1)

 insert_beg();

 else

 {

 printf("\nEnter the element to be inserted:");

 scanf("%d",&num);

 newn=(struct node*)malloc(sizeof(struct node));

 newn->data=num;

 temp=start->next;

 while(temp!=NULL)

 {

 k++;

 if(k==pos)

 {

 newn->next=temp;

 temp->prev->next=newn;

 newn->prev=temp->prev;

 temp->prev=newn;

 flag=1;

 break;

 }

 temp=temp->next;

 }

 if(flag==0)

 printf("\nPosition not found\n");

 }

}

void delete_beg()

{

 if(start==NULL)

 printf("\nLL is empty\n");

 else

{

 if(start->next==NULL)

{

free(start);

start=NULL;

}

else

 {

 temp=start;

 start=start->next;

 start->prev=NULL;

 free(temp);

 }

}

}

void delete_end()

{

 if(start==NULL)

 printf("\nLL is empty\n");

 else if(start->next==NULL)

 delete_beg();

 else

 {ptr =temp;

 temp=start;

 while(temp->next!=NULL)

 {

 temp=temp->next;

 }

 temp->prev->next=NULL;

 free(temp);

 }

}

void delete_ele()

{

 int flag=0;

 if(start==NULL)

 printf("\nLL is empty\n");

 else

 {

 printf("\nEnter the element to be deleted:");

 scanf("%d",&num);

 temp=start;

 while(temp!=NULL)

 {

 if(start->data==num)

 {

 delete_beg();

 flag=1;

 temp=start;

 break;

 }

 else if(temp->data==num)

 {

 if(temp->next==NULL)

 {

 delete_end();

 flag=1;

 break;

 }

 else

 {

 ptr=temp;

 temp->prev->next=temp->next;

 temp->next->prev=temp->prev;

 free(ptr);

 flag=1;

 break;

 }

 }

 temp=temp->next;

 }

 if(flag==0)

 printf("\nElement not found\n");

 }

}