#include"stdio.h"
#include"conio.h"
void tower(int, char *,char *,char *,int *);
void main()
{
char *t1="source",*t2="auxiliary",*t3="destination";
int disk,step=0;
clrscr();
printf("Enter no. of disks:");
scanf("%d",&disk);
tower(disk,t1,t3,t2,&step);
getch();
}
void tower(int disk,char *t1,char *t3,char *t2,int *step)
{
if(disk==1) {
printf("\nstep %d:move %s to %s\n",*step+1,t1,t3);
(*step)++; }
else{
tower(disk-1,t1,t2,t3,step);
printf("\nstep %d:move %s to %s\n",*step+1,t1,t3);
(*step)++;
tower(disk-1,t2,t3,t1,step);
}
}
Hi, this is the blog for programs in different programming languages designd by me. the all programs are original and not copied from any where.
Wednesday, March 24, 2010
ds - infix (paranthesized) to prefix conversion
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"string.h"
#define MAX 50
struct stack
{
int top;
char data[MAX];
};
typedef struct stack stack;
void push(stack *,char);
char pop(stack *);
char *reverse(char *);
int prec_stack(char);
int prec(char);
void infix_prefix_par(char *,char *);
void main()
{
char i[20],p[20];
clrscr();
printf("Enter a infix string>");
gets(i);
infix_prefix_par(strcat(reverse(i),"("),p);
strcpy(p,reverse(p));
p[strlen(p)]='\0';
printf("\nPrefix is : %s",p);
getch();
exit(0);
}
void infix_prefix_par(char *i,char *p)
{
stack st;
char next,t,*in=i;
st.top=-1;
push(&st,')');
next=*in;
while(next!='\0')
{
while(prec(next) {
t=pop(&st);
*p=t;
p++;
}
if(prec(next)!=prec_stack(st.data[st.top]))
push(&st,next);
else
pop(&st);
in++;
next=*in;
}
while(st.top>=0)
{ t=pop(&st);
*p=t;
p++;
}
p='\0';
}
void push(stack *st,char data)
{
if(st->top==MAX-1)
{
printf("\nList is Already FULL");
return;
}
st->data[++st->top]=data;
}
char pop(stack *st)
{
char res;
if(st->top==-1)
{
printf("\nList is Empty");
return NULL;
}
res=st->data[st->top];
st->top--;
return res;
}
int prec(char c)
{
if(c=='#' || c=='(')
return 0;
else if(c=='*' || c=='/')
return (4);
else if(c=='+' || c=='-')
return (2);
else if(c=='^')
return (5);
else if(c==')')
return(9);
else
return(8);
}
int prec_stack(char c)
{
if(c=='*' || c=='/')
return (3);
else if(c=='+' || c=='-')
return (1);
else if(c=='^')
return (6);
else if(c==')')
return(0);
else
return(7);
}
char *reverse(char *i)
{
char *t;
int l,j,c=0;
l=strlen(i);
for(j=l-1;j>=0;j--)
t[c++]=i[j];
t[c]='\0';
return t;
}
#include"conio.h"
#include"alloc.h"
#include"string.h"
#define MAX 50
struct stack
{
int top;
char data[MAX];
};
typedef struct stack stack;
void push(stack *,char);
char pop(stack *);
char *reverse(char *);
int prec_stack(char);
int prec(char);
void infix_prefix_par(char *,char *);
void main()
{
char i[20],p[20];
clrscr();
printf("Enter a infix string>");
gets(i);
infix_prefix_par(strcat(reverse(i),"("),p);
strcpy(p,reverse(p));
p[strlen(p)]='\0';
printf("\nPrefix is : %s",p);
getch();
exit(0);
}
void infix_prefix_par(char *i,char *p)
{
stack st;
char next,t,*in=i;
st.top=-1;
push(&st,')');
next=*in;
while(next!='\0')
{
while(prec(next)
t=pop(&st);
*p=t;
p++;
}
if(prec(next)!=prec_stack(st.data[st.top]))
push(&st,next);
else
pop(&st);
in++;
next=*in;
}
while(st.top>=0)
{ t=pop(&st);
*p=t;
p++;
}
p='\0';
}
void push(stack *st,char data)
{
if(st->top==MAX-1)
{
printf("\nList is Already FULL");
return;
}
st->data[++st->top]=data;
}
char pop(stack *st)
{
char res;
if(st->top==-1)
{
printf("\nList is Empty");
return NULL;
}
res=st->data[st->top];
st->top--;
return res;
}
int prec(char c)
{
if(c=='#' || c=='(')
return 0;
else if(c=='*' || c=='/')
return (4);
else if(c=='+' || c=='-')
return (2);
else if(c=='^')
return (5);
else if(c==')')
return(9);
else
return(8);
}
int prec_stack(char c)
{
if(c=='*' || c=='/')
return (3);
else if(c=='+' || c=='-')
return (1);
else if(c=='^')
return (6);
else if(c==')')
return(0);
else
return(7);
}
char *reverse(char *i)
{
char *t;
int l,j,c=0;
l=strlen(i);
for(j=l-1;j>=0;j--)
t[c++]=i[j];
t[c]='\0';
return t;
}
ds - infix (paranthesized) to postfix conversion
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#define MAX 50
struct stack
{
int top;
char data[MAX];
};
typedef struct stack stack;
void push(stack *,char);
char pop(stack *);
int prec_stack(char c);
int prec(char c);
void infix_postfix_par(char *i,char *p);
void main()
{
char i[20],p[20];
clrscr();
printf("Enter a infix string>");
gets(i);
strcat(i,")");
infix_postfix_par(i,p);
printf("\nPostfix is : %s",p);
getch();
}
void infix_postfix_par(char *i,char *p)
{
stack st;
char next,t,*in=i;
st.top=-1;
push(&st,'(');
next=*in;
while(next!='\0')
{
while(prec(next) {
t=pop(&st);
*p=t;
p++;
}
if(prec(next)!=prec_stack(st.data[st.top]))
push(&st,next);
else
pop(&st);
in++;
next=*in;
}
while(st.top>=0)
{
t=pop(&st);
*p=t;
p++;
}
}
void push(stack *st,char data)
{
if(st->top==MAX-1)
{
printf("\nList is Already FULL");
return;
}
st->data[++st->top]=data;
}
char pop(stack *st)
{
char res;
if(st->top==-1)
{
printf("\nList is Empty");
return NULL;
}
res=st->data[st->top];
st->top--;
return res;
}
int prec(char c)
{
if(c=='#' || c==')')
return 0;
else if(c=='*' || c=='/')
return (3);
else if(c=='+' || c=='-')
return (1);
else if(c=='^')
return (6);
else if(c=='(')
return(9);
else
return(7);
}
int prec_stack(char c)
{
if(c=='(')
return 0;
else if(c=='*' || c=='/')
return (4);
else if(c=='+' || c=='-')
return (2);
else if(c=='^')
return (5);
else if(c==')')
return(0);
else
return(8);
}
#include"conio.h"
#include"alloc.h"
#define MAX 50
struct stack
{
int top;
char data[MAX];
};
typedef struct stack stack;
void push(stack *,char);
char pop(stack *);
int prec_stack(char c);
int prec(char c);
void infix_postfix_par(char *i,char *p);
void main()
{
char i[20],p[20];
clrscr();
printf("Enter a infix string>");
gets(i);
strcat(i,")");
infix_postfix_par(i,p);
printf("\nPostfix is : %s",p);
getch();
}
void infix_postfix_par(char *i,char *p)
{
stack st;
char next,t,*in=i;
st.top=-1;
push(&st,'(');
next=*in;
while(next!='\0')
{
while(prec(next)
t=pop(&st);
*p=t;
p++;
}
if(prec(next)!=prec_stack(st.data[st.top]))
push(&st,next);
else
pop(&st);
in++;
next=*in;
}
while(st.top>=0)
{
t=pop(&st);
*p=t;
p++;
}
}
void push(stack *st,char data)
{
if(st->top==MAX-1)
{
printf("\nList is Already FULL");
return;
}
st->data[++st->top]=data;
}
char pop(stack *st)
{
char res;
if(st->top==-1)
{
printf("\nList is Empty");
return NULL;
}
res=st->data[st->top];
st->top--;
return res;
}
int prec(char c)
{
if(c=='#' || c==')')
return 0;
else if(c=='*' || c=='/')
return (3);
else if(c=='+' || c=='-')
return (1);
else if(c=='^')
return (6);
else if(c=='(')
return(9);
else
return(7);
}
int prec_stack(char c)
{
if(c=='(')
return 0;
else if(c=='*' || c=='/')
return (4);
else if(c=='+' || c=='-')
return (2);
else if(c=='^')
return (5);
else if(c==')')
return(0);
else
return(8);
}
Saturday, March 13, 2010
ds - add a node at last in doubly linked list.
//////add node at last in doubly linked list/////
/////////////////////////////////////////////////
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
typedef struct node
{
int data;
struct node *pre,*next;
}node;
void insert(node **,int);
void display(node *);
void main()
{
node *last=NULL;
clrscr();
insert(&last,15);
insert(&last,25);
insert(&last,35);
printf("list is \n ");
display(last);
getch();
}
void insert(node **last,int d)
{
node *q;
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL) {
printf("memory overflow");
exit(0); }
if(*last==NULL) {
newnode->next=NULL;
newnode->pre=*last;
*last=newnode; }
else {
q=*last;
while(q->next!=NULL) { q=q->next; }
newnode->pre=q;
newnode->next=NULL;
q->next=newnode;
q=newnode; }
newnode->data=d;
}
void display(node *last)
{
while(last!=NULL)
{
printf(" %d",last->data);
last=last->next;
}
}
/////////////////////////////////////////////////
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
typedef struct node
{
int data;
struct node *pre,*next;
}node;
void insert(node **,int);
void display(node *);
void main()
{
node *last=NULL;
clrscr();
insert(&last,15);
insert(&last,25);
insert(&last,35);
printf("list is \n ");
display(last);
getch();
}
void insert(node **last,int d)
{
node *q;
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL) {
printf("memory overflow");
exit(0); }
if(*last==NULL) {
newnode->next=NULL;
newnode->pre=*last;
*last=newnode; }
else {
q=*last;
while(q->next!=NULL) { q=q->next; }
newnode->pre=q;
newnode->next=NULL;
q->next=newnode;
q=newnode; }
newnode->data=d;
}
void display(node *last)
{
while(last!=NULL)
{
printf(" %d",last->data);
last=last->next;
}
}
ds - insert and delete at front in doubly linked list
//add and delete at front in doubly linked list//
/////////////////////////////////////////////////
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
typedef struct node
{
int data;
struct node *next,*pre;
}node;
void insert(node **,int);
void display(node *);
void del(node **);
void main()
{
node *first=NULL;
clrscr();
insert(&first,15);
insert(&first,25);
insert(&first,35);
printf("list is.\n");
display(first);
del(&first);
display(first);
getch();
}
void insert(node **first,int d)
{
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL) {
printf("memory overflow");
exit(0); }
if((*first)==NULL) {
newnode->next=NULL;
newnode->pre=*first;
*first=newnode; }
else {
newnode->next=*first;
(*first)->pre=newnode;
newnode->pre=*first;
*first=newnode; }
newnode->data=d;
}
void display(node *first)
{
while(first!=NULL)
{
printf(" %d ",first->data);
first=first->next;
}
}
void del(node **first)
{
node *q;
if(*first==NULL){
printf("memory underflow");
exit(0); }
q=*first;
*first=q->next;
if(q->next==NULL) {
*first=NULL; }
else {
(*first)->pre=*first;
(*first)=q->next; }
free(q);
printf("\nafter deletion");
}
/////////////////////////////////////////////////
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
typedef struct node
{
int data;
struct node *next,*pre;
}node;
void insert(node **,int);
void display(node *);
void del(node **);
void main()
{
node *first=NULL;
clrscr();
insert(&first,15);
insert(&first,25);
insert(&first,35);
printf("list is.\n");
display(first);
del(&first);
display(first);
getch();
}
void insert(node **first,int d)
{
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL) {
printf("memory overflow");
exit(0); }
if((*first)==NULL) {
newnode->next=NULL;
newnode->pre=*first;
*first=newnode; }
else {
newnode->next=*first;
(*first)->pre=newnode;
newnode->pre=*first;
*first=newnode; }
newnode->data=d;
}
void display(node *first)
{
while(first!=NULL)
{
printf(" %d ",first->data);
first=first->next;
}
}
void del(node **first)
{
node *q;
if(*first==NULL){
printf("memory underflow");
exit(0); }
q=*first;
*first=q->next;
if(q->next==NULL) {
*first=NULL; }
else {
(*first)->pre=*first;
(*first)=q->next; }
free(q);
printf("\nafter deletion");
}
ds - insertion and deletion before specified position in singly linked list
//singly linked list add & deletion before specified position/////
/////////////////////////////////////////////////////////////////
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
typedef struct node
{
int data;
int pos;
struct node *next;
}node;
void insertbeg(node **,int);
void insert(node **,int,int);
void del(node **,int);
void display(node *);
void main()
{
node *first=NULL;
clrscr();
printf("list is: \n");
insertbeg(&first,30);
insertbeg(&first,25);
insertbeg(&first,10);
display(first);
insert(&first,20,3);
display(first);
del(&first,3);
display(first);
getch();
}
void insertbeg(node **first,int d)
{
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL)
{
printf("memory overflow\n");
exit(0);
}
else
{
newnode->data=d;
newnode->next=*first;
*first=newnode;}
}
void display(node *first)
{
while(first!=NULL)
{
printf("%d ",first->data);
first=first->next;
}
}
void insert(node **first,int d,int pos)
{
node *p;
int i=0;
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL||pos<1)
{printf("\nmemory overflow / invalid position");
exit(0);
}
printf("\nafter insert middle\n");
if(pos==1) {
newnode->data=d;
newnode->next=*first;
*first=newnode ;}
else
{
p=*first;
while(i p=p->next;
if(p==NULL){
printf("\nless value than position \n");
exit(0);
}i++; }
newnode->data=d;
newnode->next=p->next;
p->next=newnode;
}
}
void del(node **first,int pos)
{
node *p,*q;
int i;
if(*first==NULL||pos<=1)
{printf("\nempty list / invalid position");
exit(0); }
else
{
p=*first;
q=NULL;
i=0;
while(i q=p;
p=p->next;
if(p==NULL) {
printf("\nmore position than elements\n");
exit(0);}
i++; }
if(q==NULL)
*first=p->next;
else
q->next=p->next;
free(p);
}
printf("\nafter deletion\n");
}
/////////////////////////////////////////////////////////////////
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
typedef struct node
{
int data;
int pos;
struct node *next;
}node;
void insertbeg(node **,int);
void insert(node **,int,int);
void del(node **,int);
void display(node *);
void main()
{
node *first=NULL;
clrscr();
printf("list is: \n");
insertbeg(&first,30);
insertbeg(&first,25);
insertbeg(&first,10);
display(first);
insert(&first,20,3);
display(first);
del(&first,3);
display(first);
getch();
}
void insertbeg(node **first,int d)
{
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL)
{
printf("memory overflow\n");
exit(0);
}
else
{
newnode->data=d;
newnode->next=*first;
*first=newnode;}
}
void display(node *first)
{
while(first!=NULL)
{
printf("%d ",first->data);
first=first->next;
}
}
void insert(node **first,int d,int pos)
{
node *p;
int i=0;
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL||pos<1)
{printf("\nmemory overflow / invalid position");
exit(0);
}
printf("\nafter insert middle\n");
if(pos==1) {
newnode->data=d;
newnode->next=*first;
*first=newnode ;}
else
{
p=*first;
while(i
if(p==NULL){
printf("\nless value than position \n");
exit(0);
}i++; }
newnode->data=d;
newnode->next=p->next;
p->next=newnode;
}
}
void del(node **first,int pos)
{
node *p,*q;
int i;
if(*first==NULL||pos<=1)
{printf("\nempty list / invalid position");
exit(0); }
else
{
p=*first;
q=NULL;
i=0;
while(i
p=p->next;
if(p==NULL) {
printf("\nmore position than elements\n");
exit(0);}
i++; }
if(q==NULL)
*first=p->next;
else
q->next=p->next;
free(p);
}
printf("\nafter deletion\n");
}
ds - insertion and deletion after specified position in singly linked list.
//singly linked list add & deletion after specified position/////
/////////////////////////////////////////////////////////////////
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
typedef struct node
{
int data;
int pos;
struct node *next;
}node;
void insertbeg(node **,int);
void insert(node **,int,int);
void del(node **,int);
void display(node *);
void main()
{
node *first=NULL;
clrscr();
printf("list is: \n");
insertbeg(&first,30);
insertbeg(&first,25);
insertbeg(&first,10);
display(first);
insert(&first,20,2);
display(first);
del(&first,2);
display(first);
getch();
}
void insertbeg(node **first,int d)
{
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL)
{
printf("memory overflow\n");
exit(0);
}
else
{
newnode->data=d;
newnode->next=*first;
*first=newnode;}
}
void display(node *first)
{
while(first!=NULL)
{
printf("%d ",first->data);
first=first->next;
}
}
void insert(node **first,int d,int pos)
{
node *p;
int i=1;
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL||pos<1)
{printf("memory overflow / invalid position");
exit(0);
}
else
{
p=*first;
while(i p=p->next;
if(p==NULL){
printf("less value\n");
exit(0);
}i++; }
newnode->data=d;
newnode->next=p->next;
p->next=newnode;
}
printf("\nafter insert middle\n");
}
void del(node **first,int pos)
{
node *p,*q;
int i;
if(*first==NULL||pos<1)
{printf("empty list / invalid position");
exit(0); }
else
{
p=*first;
q=NULL;
i=1;
while(i q=p;
p=p->next;
if(p==NULL) {
printf("less position\n");
exit(0);}
i++; }
if(q==NULL)
*first=p->next;
else
q->next=p->next;
free(p);
}
printf("\nafter deletion\n");
}
/////////////////////////////////////////////////////////////////
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
typedef struct node
{
int data;
int pos;
struct node *next;
}node;
void insertbeg(node **,int);
void insert(node **,int,int);
void del(node **,int);
void display(node *);
void main()
{
node *first=NULL;
clrscr();
printf("list is: \n");
insertbeg(&first,30);
insertbeg(&first,25);
insertbeg(&first,10);
display(first);
insert(&first,20,2);
display(first);
del(&first,2);
display(first);
getch();
}
void insertbeg(node **first,int d)
{
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL)
{
printf("memory overflow\n");
exit(0);
}
else
{
newnode->data=d;
newnode->next=*first;
*first=newnode;}
}
void display(node *first)
{
while(first!=NULL)
{
printf("%d ",first->data);
first=first->next;
}
}
void insert(node **first,int d,int pos)
{
node *p;
int i=1;
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL||pos<1)
{printf("memory overflow / invalid position");
exit(0);
}
else
{
p=*first;
while(i
if(p==NULL){
printf("less value\n");
exit(0);
}i++; }
newnode->data=d;
newnode->next=p->next;
p->next=newnode;
}
printf("\nafter insert middle\n");
}
void del(node **first,int pos)
{
node *p,*q;
int i;
if(*first==NULL||pos<1)
{printf("empty list / invalid position");
exit(0); }
else
{
p=*first;
q=NULL;
i=1;
while(i
p=p->next;
if(p==NULL) {
printf("less position\n");
exit(0);}
i++; }
if(q==NULL)
*first=p->next;
else
q->next=p->next;
free(p);
}
printf("\nafter deletion\n");
}
Monday, March 8, 2010
ds-insert at last, delete at last in single linked list
//insert at last, delete at last in single linked list./////
////////////////////////////////////////////////////////////
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
typedef struct node
{
int data;
struct node *next;
}node;
void insert(node **,int);
void del(node **);
void play(node *);
void main()
{
struct node *first=NULL;
clrscr();
insert(&first,10);
insert(&first,20);
insert(&first,30);
insert(&first,40);
play(first);
del(&first);
printf("\nafter deletion\n");
play(first);
getch();
}
void insert(node **first,int d)
{
node *p;
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL)
{
printf("memory overflow");
exit(0);
}
newnode->data=d;
newnode->next=NULL;
if(*first==NULL)
*first=newnode;
else{
p=*first;
while((p->next)!=NULL)
p=p->next;
p->next=newnode;
}
}
void play(node *p)
{
if(p==NULL)
printf("list is empty");
while(p!=NULL)
{
printf(" %d ",p->data);
p=p->next;
}
}
void del(node **first)
{
node *t,*p;
//node *t=(node *)malloc(sizeof(node));
if(*first==NULL) {
printf("empty list");
exit(0); }
else
{
p=*first;
//t=NULL;
while(p->next!=NULL)
{t=p;
p=p->next;
}
if(t==NULL)
(*first)->next=p->next;
else
t->next=p->next;
free(p);
}
}
////////////////////////////////////////////////////////////
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
typedef struct node
{
int data;
struct node *next;
}node;
void insert(node **,int);
void del(node **);
void play(node *);
void main()
{
struct node *first=NULL;
clrscr();
insert(&first,10);
insert(&first,20);
insert(&first,30);
insert(&first,40);
play(first);
del(&first);
printf("\nafter deletion\n");
play(first);
getch();
}
void insert(node **first,int d)
{
node *p;
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL)
{
printf("memory overflow");
exit(0);
}
newnode->data=d;
newnode->next=NULL;
if(*first==NULL)
*first=newnode;
else{
p=*first;
while((p->next)!=NULL)
p=p->next;
p->next=newnode;
}
}
void play(node *p)
{
if(p==NULL)
printf("list is empty");
while(p!=NULL)
{
printf(" %d ",p->data);
p=p->next;
}
}
void del(node **first)
{
node *t,*p;
//node *t=(node *)malloc(sizeof(node));
if(*first==NULL) {
printf("empty list");
exit(0); }
else
{
p=*first;
//t=NULL;
while(p->next!=NULL)
{t=p;
p=p->next;
}
if(t==NULL)
(*first)->next=p->next;
else
t->next=p->next;
free(p);
}
}
ds-insertion and deletion in singly linked list at beginning
//insert at begining, delete at begining in singly linked list.
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
typedef struct node
{
int data;
struct node *next;
}node;
void display(node *);
void del(node **);
void insert(node **,int);
void main()
{
node *first=NULL;
clrscr();
insert(&first,15);
insert(&first,18);
display(first);
del(&first);
printf("\n");
display(first);
getch();
}
void insert(node **first,int d)
{
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL) {
printf("memory overflow");
exit(0); }
else
{
newnode->data=d;
newnode->next=*first;
*first=newnode;
}
}
void display(node *first)
{
while(first!=NULL)
{
printf(" %d ",first->data);
first=first->next;
}
}
void del(node **first)
{
node *t;
if(first==NULL){
printf("empty list");
exit(0); }
else
{
printf("\nafter deletion.....");
t=*first;
*first=t->next;
free(t);
}
}
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
typedef struct node
{
int data;
struct node *next;
}node;
void display(node *);
void del(node **);
void insert(node **,int);
void main()
{
node *first=NULL;
clrscr();
insert(&first,15);
insert(&first,18);
display(first);
del(&first);
printf("\n");
display(first);
getch();
}
void insert(node **first,int d)
{
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL) {
printf("memory overflow");
exit(0); }
else
{
newnode->data=d;
newnode->next=*first;
*first=newnode;
}
}
void display(node *first)
{
while(first!=NULL)
{
printf(" %d ",first->data);
first=first->next;
}
}
void del(node **first)
{
node *t;
if(first==NULL){
printf("empty list");
exit(0); }
else
{
printf("\nafter deletion.....");
t=*first;
*first=t->next;
free(t);
}
}
Friday, March 5, 2010
about my posts
hi everyone,
i m again here. i want to consider here that
1>all c++ programs i.e. here are run on dev c++ compiler succesfully.
2>in data structure section there are some linked list programs in c language.
there will be some good algo too in a few days. stack, queue and trees will also
be here.
3>your comments are welcome.they will be entertain.
thanks.bye
i m again here. i want to consider here that
1>all c++ programs i.e. here are run on dev c++ compiler succesfully.
2>in data structure section there are some linked list programs in c language.
there will be some good algo too in a few days. stack, queue and trees will also
be here.
3>your comments are welcome.they will be entertain.
thanks.bye
Thursday, March 4, 2010
c++ - friend function for operator overloading
#include
using namespace std;
class number
{
int n1,n2;
public:
number(int x=0,int y=0)
{
n1=x;
n2=y;
}
void show()
{
cout n1 & n2
}
friend number operator +(number,number);
};
number operator +(number n,number m)
{
number t;
t.n1=n.n1+m.n1;
t.n2=n.n2+m.n2;
return t;
}
int main()
{
number a(2,3),b(5,6),c;
a.show();
b.show();
c=a+b;
c.show();
system("pause");
}
using namespace std;
class number
{
int n1,n2;
public:
number(int x=0,int y=0)
{
n1=x;
n2=y;
}
void show()
{
cout n1 & n2
}
friend number operator +(number,number);
};
number operator +(number n,number m)
{
number t;
t.n1=n.n1+m.n1;
t.n2=n.n2+m.n2;
return t;
}
int main()
{
number a(2,3),b(5,6),c;
a.show();
b.show();
c=a+b;
c.show();
system("pause");
}
c++ - operator overloading addition
#include
using namespace std;
class number
{
int n1,n2;
public: number(int x=0,int y=0)
{
n1=x;
n2=y;
}
void show()
{
cout n1 & n2
}
number operator +(number);
};
number number:: operator +(number n)
{
number t;
t.n1=n1+n.n1;
t.n2=n2+n.n2;
return t;
}
int main()
{
number a(2,3),b(5,6),c;
a.show();
b.show();
c=a+b;
c.show();
system("pause");
}
using namespace std;
class number
{
int n1,n2;
public: number(int x=0,int y=0)
{
n1=x;
n2=y;
}
void show()
{
cout n1 & n2
}
number operator +(number);
};
number number:: operator +(number n)
{
number t;
t.n1=n1+n.n1;
t.n2=n2+n.n2;
return t;
}
int main()
{
number a(2,3),b(5,6),c;
a.show();
b.show();
c=a+b;
c.show();
system("pause");
}
ds-operation on multi link list
//creating multi link list
//insert and delete operation is done based on ascending order
#include
#include
#include
typedef struct node
{
char name[20];
int age;
struct node *link1;
struct node *link2;
}node;
void ins_beg(node **,node **);
void insert(node **,node **);
void del_name(node **,node **);
void display_name(node *);
void display_age(node *);
void main()
{
node *head1=NULL,*head2=NULL;
int cmd;
clrscr();
while(cmd!=0)
{
printf("\nEnter Command-Insert(1),Display(Name Sorted)(2),Display(Age Sorted)(3),Quit(0)\n>");
scanf("%d",&cmd);
if(cmd==1)
insert(&head1,&head2);
else if(cmd==2)
display_name(head1);
else if(cmd==3)
display_age(head2);
else if(cmd==4)
del_name(&head1,&head2);
else
printf("\nInvalid Command!");
}
getch();
}
void insert(node **head1,node **head2)
{
node *newnode=(node *)malloc(sizeof(node));
node *p=NULL,*p1=NULL,*q=NULL,*q1=NULL;
if(newnode==NULL)
{
printf("\nMemory Overflow!");
return;
}
printf("\nEnter Name and Age: ");
scanf("%s%d",newnode->name,&newnode->age);
if(*head1==NULL && *head2==NULL)
{
newnode->link1=NULL;
newnode->link2=NULL;
*head1=*head2=newnode;
}
else
{ p=*head1;q=*head2;
while((p!=NULL) && strcmp(newnode->name,p->name)>0)
{
p1=p;
p=p->link1;
}
while((q!=NULL) && (newnode->age>q->age))
{
q1=q;
q=q->link2;
}
if(p1==NULL)
*head1=newnode;
else
p1->link1=newnode;
newnode->link1=p;
if(q1==NULL)
*head2=newnode;
else
q1->link2=newnode;
newnode->link2=q;
}
}
void ins_beg(node **head1,node **head2)
{
char ch;
do
{
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL)
{
printf("\nMemory Overflow!");
return;
}
printf("\nEnter Name and Age : ");
scanf("%s%d",newnode->name,&newnode->age);
newnode->link1=*head1;
newnode->link2=*head2;
*head1=*head2=newnode;
fflush(stdin);
printf("\nDo You Want to continue(Y/N)>");
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
}
void display_name(node *head1)
{
printf("\nDisplaying result in sorted order of name:");
while(head1!=NULL)
{ printf("\nName=%s\tAge=%d",head1->name,head1->age);
head1=head1->link1;
}
}
void display_age(node *head2)
{
printf("\nDisplaying result in sorted order of age:");
while(head2!=NULL)
{ printf("\nName=%s\tAge=%d",head2->name,head2->age);
head2=head2->link2;
}
}
void del_name(node **head1,node **head2)
{
node *p=NULL,*q=NULL;
if(*head1==NULL)
{
printf("\nList is Empty!");
return;
}
p=*head1;
q=*head2;
if(p->link1==NULL && q->link2==NULL)
{
*head1=*head2=NULL;
return;
}
while(q->link2!=p)
{
q=q->link2;
}
if(q->link2==p)
q->link2=NULL;
else
q->link2=q->link2->link2;
(*head1)=p->link1;
free(p);
}
//insert and delete operation is done based on ascending order
#include
#include
#include
typedef struct node
{
char name[20];
int age;
struct node *link1;
struct node *link2;
}node;
void ins_beg(node **,node **);
void insert(node **,node **);
void del_name(node **,node **);
void display_name(node *);
void display_age(node *);
void main()
{
node *head1=NULL,*head2=NULL;
int cmd;
clrscr();
while(cmd!=0)
{
printf("\nEnter Command-Insert(1),Display(Name Sorted)(2),Display(Age Sorted)(3),Quit(0)\n>");
scanf("%d",&cmd);
if(cmd==1)
insert(&head1,&head2);
else if(cmd==2)
display_name(head1);
else if(cmd==3)
display_age(head2);
else if(cmd==4)
del_name(&head1,&head2);
else
printf("\nInvalid Command!");
}
getch();
}
void insert(node **head1,node **head2)
{
node *newnode=(node *)malloc(sizeof(node));
node *p=NULL,*p1=NULL,*q=NULL,*q1=NULL;
if(newnode==NULL)
{
printf("\nMemory Overflow!");
return;
}
printf("\nEnter Name and Age: ");
scanf("%s%d",newnode->name,&newnode->age);
if(*head1==NULL && *head2==NULL)
{
newnode->link1=NULL;
newnode->link2=NULL;
*head1=*head2=newnode;
}
else
{ p=*head1;q=*head2;
while((p!=NULL) && strcmp(newnode->name,p->name)>0)
{
p1=p;
p=p->link1;
}
while((q!=NULL) && (newnode->age>q->age))
{
q1=q;
q=q->link2;
}
if(p1==NULL)
*head1=newnode;
else
p1->link1=newnode;
newnode->link1=p;
if(q1==NULL)
*head2=newnode;
else
q1->link2=newnode;
newnode->link2=q;
}
}
void ins_beg(node **head1,node **head2)
{
char ch;
do
{
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL)
{
printf("\nMemory Overflow!");
return;
}
printf("\nEnter Name and Age : ");
scanf("%s%d",newnode->name,&newnode->age);
newnode->link1=*head1;
newnode->link2=*head2;
*head1=*head2=newnode;
fflush(stdin);
printf("\nDo You Want to continue(Y/N)>");
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
}
void display_name(node *head1)
{
printf("\nDisplaying result in sorted order of name:");
while(head1!=NULL)
{ printf("\nName=%s\tAge=%d",head1->name,head1->age);
head1=head1->link1;
}
}
void display_age(node *head2)
{
printf("\nDisplaying result in sorted order of age:");
while(head2!=NULL)
{ printf("\nName=%s\tAge=%d",head2->name,head2->age);
head2=head2->link2;
}
}
void del_name(node **head1,node **head2)
{
node *p=NULL,*q=NULL;
if(*head1==NULL)
{
printf("\nList is Empty!");
return;
}
p=*head1;
q=*head2;
if(p->link1==NULL && q->link2==NULL)
{
*head1=*head2=NULL;
return;
}
while(q->link2!=p)
{
q=q->link2;
}
if(q->link2==p)
q->link2=NULL;
else
q->link2=q->link2->link2;
(*head1)=p->link1;
free(p);
}
c++ - operator overloading (-) for date difference
//using operator overloading find difference between two date. enter a latest date first.
#include "iostream"
using namespace std;
class date
{
int day;
int month;
int year;
public:
void input()
{
int d,m,y;
cout "enter date(dd mm yyyy)"; cin>>d>>m>>y;
day=d;
month=m;
year=y;
}
void show()
{cout date month & year
}
date operator -(date);
};
date date::operator -(date di)
{
date t;
t.year=year-di.year;
t.month=month-di.month;
t.day=day-di.day;
if((t.year)<0)
cout "first date should be greater" endl; if((t.month)<0){
t.year=t.year-1;
t.month=t.month+12;}
if(t.day<0){
t.month=t.month-1;
t.day=t.day+30;}
return t;
}
int main()
{
date d1;
d1.input();
date d2;
d2.input();
date d3;
d3=d1-d2;
cout "difference of date is :" endl;
d3.show();
system("pause");
return 0;
}
#include "iostream"
using namespace std;
class date
{
int day;
int month;
int year;
public:
void input()
{
int d,m,y;
cout "enter date(dd mm yyyy)"; cin>>d>>m>>y;
day=d;
month=m;
year=y;
}
void show()
{cout date month & year
}
date operator -(date);
};
date date::operator -(date di)
{
date t;
t.year=year-di.year;
t.month=month-di.month;
t.day=day-di.day;
if((t.year)<0)
cout "first date should be greater" endl; if((t.month)<0){
t.year=t.year-1;
t.month=t.month+12;}
if(t.day<0){
t.month=t.month-1;
t.day=t.day+30;}
return t;
}
int main()
{
date d1;
d1.input();
date d2;
d2.input();
date d3;
d3=d1-d2;
cout "difference of date is :" endl;
d3.show();
system("pause");
return 0;
}
Wednesday, March 3, 2010
first experience of blogging
This blog is for all computer learners. my goal to make this blog for c++ beginners and data structure students. i will try my best to update this on daily basis, so plz visit this . and say that you like this or not. and how can i make this better. thanx.
ds-addition of polynomial
//addition of two polynomial
#include "stdio.h"
#include "conio.h"
#include "alloc.h"
#include "stdlib.h"
typedef struct node
{
int px,py,pz,co;
struct node *next;
}node;
void insert(node **,int,int,int,int);
void display(node *);
void copy(node **,node **);
void addpoly(node *,node *,node **);
void main()
{
node *p1=NULL,*p2=NULL,*p3=NULL;
clrscr();
insert(&p1,1,1,1,4);
insert(&p1,1,1,2,1);
insert(&p1,1,2,1,2);
insert(&p1,2,1,1,3);
display(p1);
insert(&p2,1,1,1,3);
insert(&p2,1,1,2,5);
insert(&p2,1,2,1,2);
display(p2);
addpoly(p1,p2,&p3);
printf("---------------------------------------------\n");
display(p3);
getch();
}
void insert(node **p,int x,int y,int z,int c)
{
node *newnode=(node *)malloc(sizeof(node));
newnode->px=x;
newnode->py=y;
newnode->pz=z;
newnode->co=c;
if(*p==NULL){
newnode->next=NULL;
*p=newnode; }
else {
newnode->next=*p;
*p=newnode; }
}
void display(node *p)
{
while(p!=NULL) {
printf("%dX^%dY^%dZ^%d + ",p->co,p->px,p->py,p->pz);
p=p->next; }
printf("\n");
}
void addpoly(node *p1,node *p2,node **p3)
{
int a1,a2,b1,b2,c1,c2,d1,d2;
while((p1!=NULL)&&(p2!=NULL)){
a1=p1->px;a2=p2->px;
b1=p1->py;b2=p2->py;
c1=p1->pz;c2=p2->pz;
d1=p1->co;d2=p2->co;
if((a1==a2)&&(b1==b2)&&(c1==c2))
{ if((d1+d2)!=0)
insert(p3,a1,b1,c1,d1+d2);
p1=p1->next;
p2=p2->next;
}
else if((a1>a2)(a1==a2)&&(b1>b2)(a1==a2)&&(b1==b2)&&(c1>c2))
{insert(p3,a1,b1,c1,d1);
p1=p1->next; }
else {
insert(p3,a2,b2,c2,d2);
p2=p2->next; } }
if(p1!=NULL)
copy(&p1,p3);
else{
if(p2!=NULL){
copy(&p2,p3);
*p3=(*p3)->next;
while(p3!=NULL) {
(*p3)->co=-((*p3)->co);
*p3=p2;}}
}
}
void copy(node **first,node **begin)
{
node *t,*p;
t=*first;
while(t!=NULL){
node *newnode=(node *)malloc(sizeof(node));
newnode->px=t->px;
newnode->next=NULL;
if(*begin==NULL){
*begin=newnode; }
else {
p=*begin;
while((p->next)!=NULL)
p=p->next;
p->next=newnode;
}
t=t->next; }
}
#include
#include
#include
#include
typedef struct node
{
int px,py,pz,co;
struct node *next;
}node;
void insert(node **,int,int,int,int);
void display(node *);
void copy(node **,node **);
void addpoly(node *,node *,node **);
void main()
{
node *p1=NULL,*p2=NULL,*p3=NULL;
clrscr();
insert(&p1,1,1,1,4);
insert(&p1,1,1,2,1);
insert(&p1,1,2,1,2);
insert(&p1,2,1,1,3);
display(p1);
insert(&p2,1,1,1,3);
insert(&p2,1,1,2,5);
insert(&p2,1,2,1,2);
display(p2);
addpoly(p1,p2,&p3);
printf("---------------------------------------------\n");
display(p3);
getch();
}
void insert(node **p,int x,int y,int z,int c)
{
node *newnode=(node *)malloc(sizeof(node));
newnode->px=x;
newnode->py=y;
newnode->pz=z;
newnode->co=c;
if(*p==NULL){
newnode->next=NULL;
*p=newnode; }
else {
newnode->next=*p;
*p=newnode; }
}
void display(node *p)
{
while(p!=NULL) {
printf("%dX^%dY^%dZ^%d + ",p->co,p->px,p->py,p->pz);
p=p->next; }
printf("\n");
}
void addpoly(node *p1,node *p2,node **p3)
{
int a1,a2,b1,b2,c1,c2,d1,d2;
while((p1!=NULL)&&(p2!=NULL)){
a1=p1->px;a2=p2->px;
b1=p1->py;b2=p2->py;
c1=p1->pz;c2=p2->pz;
d1=p1->co;d2=p2->co;
if((a1==a2)&&(b1==b2)&&(c1==c2))
{ if((d1+d2)!=0)
insert(p3,a1,b1,c1,d1+d2);
p1=p1->next;
p2=p2->next;
}
else if((a1>a2)(a1==a2)&&(b1>b2)(a1==a2)&&(b1==b2)&&(c1>c2))
{insert(p3,a1,b1,c1,d1);
p1=p1->next; }
else {
insert(p3,a2,b2,c2,d2);
p2=p2->next; } }
if(p1!=NULL)
copy(&p1,p3);
else{
if(p2!=NULL){
copy(&p2,p3);
*p3=(*p3)->next;
while(p3!=NULL) {
(*p3)->co=-((*p3)->co);
*p3=p2;}}
}
}
void copy(node **first,node **begin)
{
node *t,*p;
t=*first;
while(t!=NULL){
node *newnode=(node *)malloc(sizeof(node));
newnode->px=t->px;
newnode->next=NULL;
if(*begin==NULL){
*begin=newnode; }
else {
p=*begin;
while((p->next)!=NULL)
p=p->next;
p->next=newnode;
}
t=t->next; }
}
Subscribe to:
Posts (Atom)