Social Icons

Saturday, 4 May 2013

Reversing a linked list inserted from begining


#include<iostream>
#include<conio.h>
using namespace std;
struct link
{
 int data;
 struct link *next;
 struct link *pre;
};
struct link *newn,*loc, *start,*ptr,*end;
void binsert();
void einsert();
void minsert();
void sinsert();
void reverse();
void display();
int main()
{char ch;
int op;
for(int i=0;i<5;i++)
{

 binsert();
 display();
}
cout<<"\n reverse of the linked list : ";
   reverse();
 
getch();
}


void display()
{
ptr=start;
while(ptr!=NULL)
{
cout<<" "<<ptr->data;
cout<<"-->";
ptr=ptr->next;
}
}



void binsert()
{
int item;
cout<<"\n Enter item to insert in begining :";
cin>>item;
newn = new struct link[1];
newn->data=item;
newn->next=NULL;
newn->pre=NULL;
//tr=start;
loc=end;
if(end==NULL)
{
start=newn;
end=newn;
}
else
{
while(loc->pre!=NULL)
{
loc=loc->pre;
}
 // newn->next=start;
loc->pre=newn;
  newn->next=loc;
  start=newn;




}

}

 void reverse()
 {
  loc=start;
  while(loc!=NULL)
  {
  cout<<" "<<loc->data;
cout<<"-->";
loc=loc->next;
 
  }
 }











Friday, 3 May 2013

Time Using Strftime Function


#include<unistd.h>
#include<time.h>
#include<stdio.h>
int main()
{
time_t t1,t2;
struct tm *tm_ptr;
int m,p,u;
char *n;
char buf[256];
time(&t1);

tm_ptr=localtime(&t1);

strftime(buf,256,"%A:%d:%B:%I:%S:%p",tm_ptr);
printf("\n time is %s",buf);

return 0;
}

Time Using Ctime Function


#include<unistd.h>
#include<time.h>
#include<stdio.h>
int main()
{
time_t t1,t2;
struct tm *tm_ptr;
int m,p,u;
char *n;
time(&t1);

n=ctime(&t1);


printf("\n time is %s",n);
return 0;
}



Time Using Localtime Function


#include<unistd.h>
#include<time.h>
#include<stdio.h>
int main()
{
time_t t1,t2;
struct tm *tm_ptr;
int m,p,u;
char *n;
time(&t1);

tm_ptr=localtime(&t1);


printf("\n year %2d  : mon %2d    hour  %2d, Minute %2d , Second %2d",tm_ptr->tm_year+1900,tm_ptr->tm_mon+1,tm_ptr->tm_hour,tm_ptr->tm_min,tm_$
return 0;
}

Time Function Using C program


#include<unistd.h>
#include<time.h>
#include<stdio.h>
int main()
{
time_t t1,t2;
int m,p,u;
char *n;
time(&t1);

m=t1;
printf("\n time is %d",m);

printf("\n enter your name :");
scanf("%s",n);

time(&t2);

p=t2;
printf("\n time is %d",p);

u=p-m;
printf("\n time taken in writing name is : %d",u);


return 0;
}

Thursday, 2 May 2013

Thread cancelletion Using Deferred Cancel Type


#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
void  *func1(void *arg);
char m[]="hi how r u";

int main()
{
int r,x;
void *p;
pthread_t a;
r=pthread_create(&a,NULL,func1,(void *)m);
if(r!=0)
{

perror("thread creation failed : ");
exit(EXIT_FAILURE);
}
else
sleep(3);
x=pthread_cancel(a);
if(x==-1)
{
perror("thrAed cancel failed ");


}

else
sleep(3);
x=pthread_cancel(a);
if(x==-1)
{
perror("thrAed cancel failed ");


}
else
{
printf("\n thread cancelled");

r=pthread_join(a,&p);
if(r!=0)
{
perror("thread joining failed :");
exit(EXIT_FAILURE);
}
printf("\n thread joined with value %s ", (char *)p);
printf("\n messafe is %s",m);
exit(EXIT_SUCCESS);
}
}



Wednesday, 1 May 2013

Machine Information Using Library Function getuid,getgid,gethostname


#include<stdio.h>
#include<sys/types.h>
#include<sys/utsname.h>
#include<unistd.h>
#include<stdlib.h>
#include<pwd.h>
int main()
{
uid_t uid;
gid_t gid;
char s[100];
struct passwd *pw;
struct utsname uts;

uid=getuid();
gid=getgid();

printf("\n userid : %d  , groupid : %d ",uid,gid);
printf("\n login name : %s ",getlogin());

  pw=getpwuid(uid);
printf("\n uid : %d , gid :  %d , name : %s",pw->pw_uid,pw->pw_gid,pw->pw_name);

gethostname(s,255);
printf("\n host name : %s",s);

uname(&uts);

printf("\n machine name :  %s , \n  sysname :  %s ,\n  version : %s ,\n Nodename : %s ,\n  ",uts.machine,uts.sysname,uts.version,uts.nodename);
return 0;
}

Sum And Product Using Getopt Function


#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(int argc , char *argv[])
{

int n,i,sum=0, pr=1;
int a,b;
while((n=getopt(argc,argv,":if:::l::r"))!=-1)
{
 switch(n)
 {
 case 'i' :
 case 'r' : printf("\n option %c",n);
              break;
 case 'f'  :// printf("\n filename %s",optarg);
                a=optind;
               for(;optind<a+3;optind++)
                 {
                    i=atoi(argv[optind]);
                    sum=sum+i;
                  }
                    printf(" \n sum of the two no :  %d ",sum);
                 break;
 case 'l'  :     b=optind;
                 for(;optind<b+2;optind++)
                 {
                    i=atoi(argv[optind]);
                    pr=pr*i;
                  }
                 printf(" \n Product  of the two no :  %d ",pr);
                 break;

 case '?'  : printf("\n unknown option %c",optopt);
               break;
 case ':'  : printf("\n argument needed : ");
              break;
 }

 }

           for(;optind<argc;optind++)
    {
    printf(" \n arguments : %s ",argv[optind]);
    }


return 0;
}


             
             

Copying Files content Using Read & write system call


#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int main()
{
int nread;
char buf[150];
int  m,n,o;
m=open("kaha", O_RDONLY,S_IRUSR|S_IWUSR);
if(m==-1)
{
printf("\n Error Occured : ");
}

n=open("tu.txt",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
if(n==-1)
                {

printf("\n Error Occured : ");

}

lseek(m,2,SEEK_SET);
lseek(n,5,SEEK_SET);

while(nread=read(m,buf,sizeof(buf)))
{
  write(n,buf,10);
}



return 0;
}

Reading Files Of a Directory


#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<dirent.h>
struct dirent *p;
int main()
{
DIR *q;
int  m;
q=opendir("1");
if(q==NULL)
{
printf("\n Error Occured : ");

}

else
{
printf("\n  File Name  |||||||     Inode No.\n");
printf("......................................");
while(p=readdir(q))
{

printf("\n %s",p->d_name);
m=p->d_ino;
printf("\t\t\t %d",m);
}
}
closedir(q);
return 0;
}


1 12 123 1234 12345 Pattern Using Shell Programme


#!/bin/sh
c=6;
m=1;
for((i=1;i<=5;i++))
do
for((k=1;k<=c;k++))
do
  echo -n  " "
done
for((j=1;j<=i;j++))
do
echo -n "$j"
echo -n " "
done
c=$(($c-1))
echo -e " "
done


exit 0


Loop Patterns Using Shell Commands


#!/bin/sh
c=5;
for((i=1;i<=5;i++))
do
for((k=1;k<=c;k++))
do
  echo -n  " "
done
for((j=1;j<=i;j++))
do
echo -n "*"
echo -n " "
done
c=$(($c-1))
echo -e " "
done


exit 0

Output :

Geometric Progression Using shell


#!/bin/sh

echo -n "Enter First No. of G.P :  "
read  a
echo -n "Enter Common Ratio of G.p :"
read r
echo -n  "how many no. you want to print : "
read  n
echo " .............G.p is : ........   "

while [ $n -gt 0 ]
do
echo -n "$a "
a=$(($a*$r))
n=$(($n-1))
done

exit 0

Cheking Number is Palindrome


#!/bin/sh

echo -n "Enter any no to find Reverse :  "
read  n
p=$n
rev=0
while [ $n -gt 0 ]
do
z=$(($n%10))
n=$(($n/10))
rev=$(($(($rev*10)) + z))
done
echo " .............reverse of the no. is : ........   : $rev"

if [ $rev -eq $p ]
then
echo "............Number is Palindrome.......... "
else
 echo  "........Number  is Not Palindrome ............."
fi
exit 0
output : 

Reverse Of a No.


#!/bin/sh

echo -n "Enter any no to find Reverse :  "
read  n
p=$n
rev=0
while [ $n -gt 0 ]
do
z=$(($n%10))
n=$(($n/10))
rev=$(($(($rev*10)) + z))
done
echo " .............reverse of the no. is : ........   : $rev"
exit 0
Output :



Factorial Of a No. Using Shell Program



#!/bin/sh
fact=1;
echo -n "Enter any no to find factorial :  "
read  n
for((i=1;i<=n;i++))
do
((fact=$fact * $i))

done

echo " .............factorial of the no. is : ........   : $fact"
exit 0


Shell Programming [If- Else]


#!/bin/sh
echo -n "Enter how many times you want to enter  :  "
read d
for((i=0;i<d;i++))
do
echo -n "Enter your age :  "
read n
if [ $n -gt 20 ] && [ $n -lt 50 ]
then
echo "...........you are middle aged men...................... "
elif [ $n -gt 5 ] && [ $n -lt 20 ]
then
echo "................you r a boy................"
else
 echo ".............you are a old man ............. "
fi
done
exit 0

Output:

Excuting Command in C program using "system" function


#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("\n Showing Details of A file using System command : \n");


system("more first.txt");
return 0;


}



 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management