My Profile

I, Das ShrikKrishna J. MCA III IMSCD&R, Ahmednagar.

Monday 15 August 2011

Program of sparse matrix for 3-tuple method using array


/* Program of sparse matrix for 3-tuple method using array*/
#include<stdio.h>
#define srow 50
#define mrow 20
#define mcolumn 20
main()
{
int mat[mrow][mcolumn],sparse[srow][3];
int i,j,nzero=0,mr,mc,sr,s;
printf("Enter number of rows : ");
scanf("%d",&mr);
printf("Enter number of columns : ");
scanf("%d",&mc);
for(i=0;i<mr;i++)
for(j=0;j<mc;j++)
{
printf("Enter element for row %d,column %d : ",i+1,j+1);
scanf("%d",&mat[i][j]);
}
printf("Entered matrix is : \n");
for(i=0;i<mr;i++)
{
for(j=0;j<mc;j++)
{
printf("%6d",mat[i][j]);
if(mat[i][j]!=0)
nzero++;
}
printf("\n");
}
sr=nzero+1;
sparse[0][0]=mr;
sparse[0][1]=mc;
sparse[0][2]=nzero;
s=1;
for(i=0;i<mr;i++)
for(j=0;j<mc;j++)
{
if(mat[i][j]!=0)
{
sparse[s][0]=i+1;
sparse[s][1]=j+1;
sparse[s][2]=mat [i][j];
s++;
}
}
printf("Sparse matrix is :\n");
for(i=0;i<sr;i++)
{
for(j=0;j<3;j++)
printf("%5d",sparse[i][j]);
printf("\n");
}
}/*End of                main()*/

Program of stack using array


/* Program of stack using array*/
#include<stdio.h>
#define MAX 5
int top = -1;
int stack_arr[MAX];
main()
{
int choice;
while(1)
{
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
push()
{
int pushed_item;
if(top == (MAX-1))
printf("Stack Overflow\n");
else
{
printf("Enter the item to be pushed in stack : ");
scanf("%d",&pushed_item);
top=top+1;
stack_arr[top] = pushed_item;
}
}/*End of push()*/
pop()
{
if(top == -1)
printf("Stack Underflow\n");
else
{
printf("Popped element is : %d\n",stack_arr[top]);
top=top-1;
}
}/*End of pop()*/
display()
{
int i;
if(top == -1)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n");
for(i = top; i >=0; i--)
printf("%d\n", stack_arr[i] );
}
}/*End of    display()*/

Program of stack using linked list

 
/* Program of stack using linked list*/
# include<stdio.h>
# include<malloc.h>
struct node
{
int info;
struct node *link;
} *top=NULL;
main()
{
int choice;
while(1)
{ printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ") ;
scanf("%d", &choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
}/*End of main() */
push()
{
struct node *tmp;
int pushed_item;
tmp = (struct node *)malloc(sizeof(struct node));
printf("Input the new value to be pushed on the stack : ");
scanf("%d",&pushed_item);
tmp->info=pushed_item;
tmp->link=top;
top=tmp;
}/*End of push()*/
pop()
{
struct node *tmp;
if(top == NULL)
printf("Stack is empty\n");
else
{ tmp=top;
printf("Popped item is %d\n",tmp->info);
top=top->link;
free(tmp);
}
}/*End of pop()*/
display()
{ struct node *ptr;
ptr=top;
if(top==NULL)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n");
while(ptr!= NULL)
{
printf("%d\n",ptr->info);
ptr = ptr->link;
}/*End of         while */
}/*End of           else*/
}/*End of      display()*/

Addition Of two Polynomial

#include<stdio.h>
struct node {
int coef,pow;
struct node *next;
};
typedef struct node *POLY;
main()
{
POLY p1=NULL,p2=NULL,p3=NULL;
POLY create(POLY,int);
void display (POLY);
POLY add (POLY p1,POLY p2,POLY p3);
int n;
clrscr();
printf("/********* Addition of polynomials *************/\n");
printf("\n How many terms in the first polynomial ?");
scanf("%d",&n);
p1=create(p1,n);
display(p1);
printf("\n How many terms in the second polynomial ?");
scanf("%d",&n);
p2=create(p2,n);
display(p2);
p3=add(p1,p2,p3);
printf("\n the addition is : ");
display(p3);
getch();
return(0);
}
POLY create(POLY p1,int n)
{
POLY temp=p1,newnode;
int i;
printf("\n enter the termsin descending order of power :\n");
for(i=0;i<n;i++)
{
newnode=(POLY) malloc(sizeof(struct node));
newnode->next=NULL;
printf("\n coeff= :");
scanf("%d",&newnode->coef);
printf("\n power= : ");
scanf("%d",&newnode->pow);
if (p1==NULL)
{
temp=p1=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
}
return p1;
}
void display (POLY p1)
{
POLY temp=p1;
while(temp)
{
printf("+%dX^%d ",temp->coef,temp->pow);
temp=temp->next;
}
}
POLY add(POLY p1,POLY p2,POLY p3)
{
POLY t1=p1,t2=p2,t3=p3,newnode;
int i;
while (t1 && t2)
{
newnode=(POLY) malloc(sizeof (struct node));
newnode->coef=NULL;
if(t1->pow>t2->pow)
{
newnode->pow=t1->pow;
newnode->coef=t1->coef;
t1=t1->next;
}
else
if (t1->pow<t2->pow)
{
newnode->pow=t2->pow;
newnode->coef=t2->coef;
t2=t2->next;
}
else
{
newnode->pow=t1->pow;
newnode->coef=t1->coef+t2->coef;
t1=t1->next;
t2=t2->next;
}
/********* link newnode *****/
if (t3==NULL)
{
p3=t3=newnode;
}
else
{
t3->next=newnode;
t3=newnode;
}
}
while(t1)
{
newnode=(POLY) malloc(sizeof (struct node));
newnode->next=NULL;
newnode->pow=t1->pow;
newnode->coef=t1->coef;
t3->next=newnode;
t3=newnode;
t1=t1->next;
}
while(t2)
{
newnode=(POLY) malloc(sizeof(struct node));
newnode->next=NULL;
newnode->coef=t2->coef;
t3->next=newnode;
t3=newnode;
t2=t2->next;
}
return p3;
}
 

Infix to Prefix Conversion

/* Infix to Prefix Conversion */
# include<stdio.h>
# include<conio.h>
# include<string.h>
# include<stdlib.h>
# define MAX 30
int rear=-1,top=-1;
char stk[30],que[30],ch,chout;
int stkempty (int top)
{
if(top==-1)
{
return(1);
}
else
{
return(0);
}
}
int stkfull (int top)
{
if(top==MAX-1)
{
return(1);
}
else
{
return(0);
}
}
int push(char ch)
{
if(stkfull(top))
{
printf("\nstack is full.cannot push into stack");
return(top);
}
else
if(top==-1)
{
top++;
stk[top]='~';
}
top++;
stk[top]=ch;
return(top);
}
int pop()
{
if(stkempty(top))
{
printf("");
}
else
if(stk[top]!='~')
{
chout=stk[top];
top--;
}
return(chout);
}
int qempty (int rear)
{
if(rear==-1)
{
return(1);
}
else
{
return(0);
}
}
int qfull (int rear)
{
if(rear==MAX-1)
{
return(1);
}
else
{
return(0);
}
}
int addq(char ch)
{
if(qfull(rear))
{
printf("\nqueue is full.cannot add in queue");
return(rear);
}
else
{
rear++;
que[rear]=ch;
}
return(rear);
}
int isp(char ch)
{
switch(ch)
{
case '+' :
case '-' : return(1);
case '*' :
case '/' : return(2);
case '^' : return(3);
case '(' : return(0);
case '~' : return(-99);
}
return 0;
}
int icp(char ch)
{
switch(ch)
{
case '+' :
case '-' : return(1);
case '*' :
case '/' : return(2);
case '^' : return(4);
case '(' : return(4);
}
return 0;
}
int display(int len)
{
int i;
for(i=len;i>=0;)
{
if(que[i]=='('||que[i]==')')
{
i--;
}
else
{
printf(" %c",que[i]);
i--;
}
}
return;
}
void prefix()
{
char str[10],ch;
int i,len;
printf("\nEnter the expression:");
scanf("%s",str);
len=strlen(str);
printf("\n");
for(i=len;i>=0;i--)
{
ch=str[i];
if(ch>='a'&&ch<='z')
{
addq(ch);
}
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^'||ch=='('||ch==')')
{
if(ch=='(')
{
while(chout!=')')
{
chout=pop();
addq(chout);
}
}
if(icp(ch)>isp(stk[top]))
{
push(ch);
}
else
{
chout=pop();
addq(chout);
push(ch);
}
}
}
while(stk[top]!='~')
{
chout=pop();
addq(chout);
}
display(len);
}
void main()
{
clrscr();
prefix();
getch();
}

Infix To Postfix Conversion

# include<stdio.h>
# include<conio.h>
# include<string.h>
# include<stdlib.h>
# define MAX 30
int rear=-1,top=-1;
char stk[30],que[30],ch,chout;
int stkempty (int top)
{
if(top==-1)
{
return(1);
}
else
{
return(0);
}
}
int stkfull (int top)
{
if(top==MAX-1)
{
return(1);
}
else
{
return(0);
}
}
int push(char ch)
{
if(stkfull(top))
{
printf("\nstack is full cannot push into stack ");
return(top);
}
else
if(top==-1)
{
top++;
stk[top]='~';
}
top++;
stk[top]=ch;
return(top);
}
int pop()
{
if(stkempty(top))
{
printf("\nstack is empty cannot pop from stack ");
}
else
if(stk[top]!='~')
{
chout=stk[top];
top--;
}
return(chout);
}
int qempty (int rear)
{
if(rear==-1)
{
return(1);
}
else
{
return(0);
}
}
int qfull (int rear)
{
if(rear==MAX-1)
{
return(1);
}
else
{
return(0);
}
}
int addq(char ch)
{
if(qfull(rear))
{
printf("\nqueue is full cannot add in queue ");
return(rear);
}
else
{
rear++;
que[rear]=ch;
}
return(rear);
}
int isp(char ch)
{
switch(ch)
{
case '+' :
case '-' : return(1);
case '*' :
case '/' : return(2);
case '^' : return(3);
case '(' : return(0);
case '~' : return(-99);
}
return 0;
}
int icp(char ch)
{
switch(ch)
{
case '+' :
case '-' : return(1);
case '*' :
case '/' : return(2);
case '^' : return(4);
case '(' : return(4);
}
return 0;
}
 
int display(int len)
{
int i;
printf("\n Postfix conversion is \n");
for(i=0;i<=len;i++)
{
if(que[i]!='('&&que[i]!=')')
printf(" %c",que[i]);
}
return;
}
 
void postfix()
{
char str[10],ch;
int i,len;
printf("\nEnter the expression: ");
scanf("%s",str);
len=strlen(str);
printf("\n");
for(i=0;i<=len;i++)
{
ch=str[i];
if(ch>='a'&&ch<='z')
{
addq(ch);
}
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^'||ch=='('||ch==')')
{
if(ch==')')
{
while(chout!='(')
{
chout=pop();
addq(chout);
}
}
if(icp(ch)>isp(stk[top]))
{
push(ch);
}
else
{
chout=pop();
addq(chout);
push(ch);
}
}
}
while(stk[top]!='~')
{
chout=pop();
addq(chout);
}
display(len);
}
void main()
{
clrscr();
postfix();
getch();
}

Decision Of Graph

/* Graph */
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>
# include <process.h>
# include <alloc.h>
# define MAX 50
static int mat[MAX][MAX],que[MAX],visit[MAX];
int stk[MAX];
int top=-1,rear=-1,front=-1;
int vert;
void push(int ele)
{ if(top==MAX)
{ printf("\nstack is full cannot push into stack"); }
else { top++; stk[top]=ele; }
}
int pop()
{ int ele;
if(top<=-1)
{ printf("\nstack is empty cannot pop from stack"); }
else { ele=stk[top]; top--; }
return(ele);
}
void addq(int ele)
{ if(rear==MAX)
{ printf("\nqueue is full.cannot add in queue"); }
else
{ que[++rear]=ele; }
}
int delq()
{ int ele;
if(front==rear)
{ printf("\nqueue is empty cannot delete from stack");
return(rear);
}
else
{ ele=que[++front]; }
return(ele);
}
void print()
{ int i,j;
printf("\nThe adjacency matrix is::\n");
for(i=0;i<vert;i++)
{ for(j=0;j<vert;j++)
{ printf(" %d",mat[i][j]); }
}
}
 
 
 
 
 
 
void create()
{ int i,j,ans,direct;
printf("\nEnter:: 1: for directed matrix\n\t0:for non-directed:: ");
scanf("%d",&direct);
printf("\nEnter the no. of vertices::");
scanf("%d",&vert);
for(i=0;i<vert;i++)
{ while(1)
{ printf("\n\nVERTEX %d",i);
printf("\nEnter 1 if there is any adjacent vertex for %d:: ",i);
scanf("%d",&ans);
if(ans!=1)
{ break; }
else { printf("\nEnter the adjacent vertex:: ");
scanf("%d",&j);
mat[i][j]=1;
if(direct==0)
{ mat[j][i]=1; }
}
}
}
}
void bfs()
{ int start,ele,j,flag=-1;
printf("\nEnter the vertex from which you want to start:: ");
scanf("%d",&start);
addq(start);
visit[start]=1;
while(front!=rear)
{ ele=delq();
printf("\n%d ",ele);
for(j=0;j<vert;j++)
{ if(mat[ele][j]==1 && visit[j]!=1)
{ addq(j); visit[j]=1; }
}
}
for(j=0;j<vert;j++)
{ if(visit[j]!=1) { flag=j;}
}
if(flag!=-1)
{ printf("\nThe graph is not a connected graph!"); }
}
void main()
{ create();
print();
bfs();
getch();
}

Eightqueen Problem

#include<stdio.h>
#include<math.h>
eightqueen(int,int);
int place(int,int);
int a[9][9],x[9];
int j;
main()
{
clrscr();
eightqueen(1,8);
getch();
return 0;
}
eightqueen(int k,int n)
{
int i;
for(i=1;i<=n;i++)
{
if(place(k,i))
{
x[k]=i;
if(k==n)
{
printf("\n vector X[1---8]\n");
putchar('\n');
for(i=1;i<=n;i++)
printf("%7d",x[i]);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j]=0;
for(i=1;i<=n;i++)
a[i][x[i]]=i;
printf("\n Eightqueen placement is \n");
for(i=1;i<=n;i++)
{
puts("\n");
for(j=1;j<=n;j++)
printf("%8d",a[i][j]);
}
}
else
eightqueen(k+1,n);
}
}
return;
}
int place(int k,int i)
{
for(j=1;j<=k-1;j++)
{
if(x[j]==i)
return 0;
if(abs(x[j]-i)==abs(j-k))
return 0;
}
return 1;
}

Generation of circle using Bresenham's algorithm

/* Generation of circle using Bresenham's algorithm */
#include<stdio.h>
#include<graphics.h>
main()
{
int x,y,r,d,orgx,orgy,gd=DETECT,gm;
clrscr();
printf("\n Enter the radius of circle ");
scanf("%d",&r);
clrscr();
x=0;
y=r;
orgx=orgy=r;
d=1-r;
initgraph(&gd,&gm,"");
while(y>x)
{
if(d<0)
{
x++;
d=d+(2*x)+3;
}
else
{
x++;
y--;
d=d+(2*(x-y))+5;
}
putpixel(orgx+x,orgy+y,RED);
putpixel(orgx-x,orgy+y,RED);
putpixel(orgx+y,orgy+x,RED);
putpixel(orgx+x,orgy-y,RED);
putpixel(orgx-y,orgy+x,RED);
putpixel(orgx+y,orgy-x,RED);
putpixel(orgx-x,orgy-y,RED);
putpixel(orgx-y,orgy-x,RED);
}
getch();
return 0;
}

DFS

#include<stdio.h>
#include<conio.h>
struct stack {
int data[20];
int top;
} s1;
void push(int n)
{
s1.data[++s1.top]=n;
}
int pop()
{
return s1.data[s1.top--];
}
void initstack()
{
s1.top=-1;
}
int emptystack()
{
return(s1.top==-1);
}
void dfs(int m[10][10],int n)
{
int i,v,w;
int visited[20]={0};
initstack();
v=0;
visited[v]=1;
push(v);
printf("%d\t",v+1);
while(1)
{
for(w=0;w<n;w++)
{
if((m[v][w]==1)&&(visited[w]==0))
{
push(w);
printf("%d\t",w+1);
visited[w]=1;
}
}
if(emptystack())
break;
else
v=pop();
}
}
void recdfs(int m[10][10],int n,int v)
{
int w;
static int visited[20]={20};
visited[v]=1;
printf("%d\t",v+1);
for(w=0;w<n;w++)
{
if((m[v][w]==1)&&(visited[w]==0))
{
recdfs(m,n,w);
}
}
}
void main()
{
int m[10][10],i,n,j;
clrscr();
printf("how many vertices");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
m[i][j]=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i!=j)
{
printf(" Is there any edge between vertex %d & %d (1/0)",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
clrscr();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",m[i][j]);
//else
//printf("0");
}
printf("\n");
}
printf("\nRECURSIVE DFS IS");
printf("\n");
recdfs(m,n,0);
printf("\nNON RECURSIV DFS IS");
printf("\n");
dfs(m,n);
getch();
}

Convert Infix To Prefix

#include<stdio.h>
#define MAX 20
struct stack
{int top;
char item[MAX];
char *ex;
};
typedef struct stack STACK;
void initstack(STACK *ps)
{ ps->top=-1;
}
int isempty(STACK *ps)
{ return ps->top==-1;
}
void push(STACK *ps,int n)
{ ps->item[++ps->top]=n;
}
char pop(STACK *ps)
{ return ps->item[ps->top--];
}
char stacktop(STACK *ps)
{ return ps->item[ps->top];
}
//CONVERTING INFIX TO POSTFIX USING PRIORITY METHOD
int priority(char ch)
{ switch(ch)
{ case '(':return 0;
case '+':
case '-':return 1;
case '/':
case '%':
case '*':return 2;
}
}
void postfix(char *in,char *post)
{ int i,j=0;
STACK s1;
char ch;
initstack(&s1);
for(i=0;in[i]!='\0';i++)
{ switch(in[i])
{ case '(': push(&s1,in[i]); break;
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
case 'D':
case 'd': post[j++]=in[i]; break;
case '+':
case '-':
case '/':
case '%':
case '*': if(isempty(&s1) || (priority(in[i])>priority(stacktop(&s1))))
push(&s1,in[i]);
else
{ while(priority(in[i]) <= priority(stacktop(&s1))&&!isempty(&s1))
post[j++]=in[i];
push(&s1,in[i]);
}
break;
case ')': while((ch=pop(&s1))!='(')
post[j++]=ch;
} //switch end
} //for end
while(!isempty(&s1))
post[j++]=pop(&s1);
post[j]='\0';
}
//EVALUATING POSTFIX EXPRESSION
int evaluate(char *post)
{ STACK s1;
int i,vala,valb,valc,vald,op1,op2;
printf("\n ENTER THE VALUES OF a,b,c & d: ");
scanf("%d %d %d %d",&vala,&valb,&valc,&vald);
for(i=0;post[i]!='\0';i++)
{ switch(post[i])
{ case 'a':
case 'A': push(&s1,vala); break;
case 'B':
case 'b': push(&s1,valb);break;
case 'C':
case 'c': push(&s1,valc);break;
case 'D':
case 'd': push(&s1,vald);break;
default: op2=pop(&s1);
op1=pop(&s1);
}
switch(post[i])
{ case '+': push(&s1,op1 + op2); break;
case '-': push(&s1,op1 - op2); break;
case '/': push(&s1,op1 / op2); break;
case '%': push(&s1,op1 % op2); break;
case '*': push(&s1,op1 * op2); break;
}
}
return pop(&s1);
}
void main()
{ char instr[20],poststr[20];
clrscr();
printf("\n ENTER THE INFIX EXPRESSION : ");
gets(instr);
postfix(instr,poststr);
printf("\n POSTFIX EXPRESSION IS : ");
puts(poststr);
printf("\n RESULT = %d",evaluate(poststr));
getch();
}

BFS

#include<stdio.h>
#include<conio.h>
struct q
{
int data[20];
int front,rear;
}q1;
void add(int n)
{
q1.rear++;
q1.data[q1.rear]=n;
}
int del()
{
q1.front++;
return q1.data[q1.front];
}
void initq()
{
q1.front=q1.rear=-1;
}
int emptyq()
{
return (q1.rear==q1.front);
}
void main()
{
int m[10][10],n,i,j;
void bfs(int m[10][10],int n);
clrscr();
printf("\nenter how many vertices");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i!=j)
{
printf("is there is edge between vertex%d & %d(1/0)",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
bfs(m,n);
getch();
}
void bfs(int m[10][10],int n)
{
int i,j,v,w;
int visited[20];
initq();
for(i=0;i<n;i++)
visited[i]=0;
printf("\nthe bread first traversal is:\n");
v=0;
visited[v]=1;
add(v);
while(!emptyq())
{
v=del();
printf("v%d",v+1);
 
 
 
 
 
 
 
 
 
for(w=0;w<n;w++)
{
if((m[v][w]==1)&&(visited[w]==0))
{
add(w);
visited[w]=1;
}
}
}
}
 

Adjuncy Matrix

#include<stdio.h>
struct node
{
int vertex;
struct node * next;
}*v[10];
void createmat(int m[10][10],int n)
{
int i,j;
char ans;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
m[i][j]=0;
if(i!=j)
{
printf("is there any edge between vertex %d and %d(1,0):",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
}
void isempty(int m[10][10],int n)
{
int i,j;
printf("\nthe adjacency matrix is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("\t%d",m[i][j]);
printf("\n");
}
}
void createlist(int m[10][10],int n)
{
int i,j;
struct node *temp,*newnode;
for(i=0;i<n;i++)
{
v[i]=NULL;
for(j=0;j<n;j++)
{
if(m[i][j]==1)
{
newnode=(struct node*)malloc(sizeof(struct node));
newnode->next=NULL;
newnode->vertex=j+1;
if(v[i]==NULL)
v[i]=temp=newnode;
else
{
temp->next=newnode;
temp=newnode;
}
}
}
}
}
void displist(int n)
{
struct node *temp;
int i;
printf("\n adjacency list is \n");
for(i=0;i<n;i++)
{
printf("\n v%d->",i+1);
temp=v[i];
while(temp)
{
printf("v%d->",temp->vertex);
temp=temp->next;
}
printf("NULL");
}
}
void main()
{
int m[10][10],n;
clrscr();
printf("\n enter the no of vertex:");
scanf("%d",&n);
createmat(m,n);
isempty(m,n);
createlist(m,n);
displist(n);
getch();
}

Sort Using Quick Sort

Write a Program in C to Accept 5 Numbers from the User and Sort Using Quick Sort
#include <stdio.h>
#include <conio.h>
int split(int lo,int up) ;
void quicksort(int lo, int up) ;
int arr[10]={90,80,70,60,50,40,30,20,10,0};
void main( )
{
int i ;
clrscr( ) ;
printf("Original Array\n");
for(i=0;i<=9;i++)
{
printf("%d\t",arr[i]);
}
quicksort(0,9);
printf ( "\nArray after using Quick Sort\n") ;
for(i=0;i<=9;i++)
{
printf("%d\t",arr[i]) ;
}
getch( ) ;
}
void quicksort(int lo,int up)
{
int i;
if(up>lo) //check whether upper is greater than lower
{
i=split(lo,up);
quicksort(lo,i-1);
quicksort(i+1,up);
}
}
int split(int lo,int up)
{
int i,p,q,t;
p=lo+1; //lower limit
q=up; //upper limit
i=arr[lo];
while(q>=p) //check index p crosses q
{
while(arr[p]<i)
{
p++;
}
while(arr[q]>i)
{
q--;
}
if(q>p)
{
t= arr[p];
arr[p]=arr[q];
arr[q]=t;
}
}
t=arr[lo] ;
arr[lo]=arr[q] ;
arr[q]=t ;
return q;
}

Sort Using Merge Sort

Write a Program in C to Accept 5 Numbers from the User and Sort Using Merge Sort
#include <stdio.h>
#include <conio.h>
void main( )
{
int num1[3]={2,9,11};
int num2[3]={1,3,17};
int ans[6] ;
int i,j,k,l,temp ;
clrscr( ) ;
j=0,k=0;
printf("Orignal Numbers\n");
printf("num1[]=");
for(i=0;i<=2;i++)
{
printf("%d ",num1[i]);
}
printf("\nnum2[]=");
for(i=0;i<=2;i++)
{
printf("%d ",num2[i]);
}
printf("\n\n\n");
for(i=1;i<=6;i++)
{
printf("Comparing num1[%d] and num2[%d]\t: ",j+1,k+1);
if(num1[j]<=num2[k])
{
ans[i]=num1[j] ;
j++;
}
else
{
ans[i]=num2[k] ;
k++;
}
for(l=1;l<=i;l++)
{
printf("%d\t",ans[l]);
}
printf("\n\n");
if(j==3||k==3)
break;
}
i++;
if(j==2)
{
ans[i]=num1[j];
}
if(k==2)
{
ans[i]=num2[k];
}
printf("%d\n\n",ans[i]);
printf("Numbers after using Merge Sort Method\n");
 
for(i=1;i<=6;i++)
{
printf("%d\n",ans[i]);
}
getch( ) ;
}

Sort Using Selection Sort

Write a Program in C to Accept 5 Numbers from the User and Sort Using Selection Sort
#include<stdio.h>
#include<conio.h>
void main()
{
int swap,i,j,num[10],k;
clrscr();
printf("Enter 5 numbers\n\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num[i]); //Accepting 5 numbers
}
printf("\n\n Original Nos. \t\t\t");
for(k=0;k<=4;k++)
{
printf("%d\t",num[k]); //printing the numbers
}
printf("\n\n");
for(i=0;i<=3;i++)
{
printf("Round %d \n",i+1);
for(j=i+1;j<=4;j++)
{
printf("Comapring (%d) and (%d)",i+1,j+1);
if(num[i]>num[j])
{
swap=num[i];
num[i]=num[j];
num[j]=swap;
}
printf("\t\t");
for(k=0;k<=4;k++)
{
printf("%d\t",num[k]); //printing the numbers
}
printf("\n");
getch();
}
printf("\n");
}
printf("\n\nNumbers after using Selection Sort Method\n");
for(i=0;i<=4;i++)
{
printf("%d\n",num[i]);
}
getch();
}

Sort Using Insertion Sort

Write a Program in C to Accept 5 Numbers from the User and Sort Using Insertion Sort
 
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,swap,num[10];
clrscr();
printf("Enter 5 numbers\n\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num[i]);
}
printf("\n\nOriginal Nos. \t\t\t");
for(k=0;k<=4;k++)
{
printf("%d\t",num[k]);
}
printf("\n\n");
for(i=1;i<=4;i++)
{
printf("Round %d \n",i);
for(j=0;j<i;j++)
{
printf("Comapring (%d) and (%d)",i+1,j+1);
if(num[j]>num[i])
{
swap=num[j] ;
num[j]=num[i] ;
for(k=i;k>j;k--)
{
num[k]=num[k-1];
}
num[k+1]=swap;
}
printf("\t\t");
for(k=0;k<=4;k++)
{
printf("%d\t",num[k]); //printing the numbers
}
printf("\n");
getch();
}
printf("\n");
}
printf("\n\nNumbers after using Insertion Sort Method\n");
for(i=0;i<=4;i++ )
{
printf("%d\n",num[i]);
}
getch();
}

Sort Using Bubble Sort

Write a Program in C to Accept 5 Numbers from the User and Sort Using Bubble Sort
#include<stdio.h>
#include<conio.h>
void main()
{
int swap,i,j,num[10],k;
clrscr();
printf("Enter 5 numbers\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num[i]); //accepting 5 numbers
}
printf("\n\n Original Nos. \t\t\t");
for(k=0;k<=4;k++)
{
printf("%d\t",num[k]); //printing the numbers
}
printf("\n\n");
for(i=0;i<=3;i++)
{
printf("Round %d \n",i+1);
for(j=0;j<=3-i;j++)
{
printf("Comapring (%d) and (%d)",j+1,j+2);
if(num[j]>num[j+1])
{
swap=num[j];
 
num[j]=num[j+1]; //swapping if is greater
num[j+1]=swap;
}
printf("\t\t");
for(k=0;k<=4;k++)
{
printf("%d\t",num[k]); //printing the numbers
}
printf("\n");
getch();
}
printf("\n");
}
printf("\n\nNumbers after using Bubble Sort Method\n");
for(i=0;i<=4;i++)
{
printf("%d\n",num[i]);
}
getch();
}

Perform Operations on Graph

Write a Program in C to Perform Operations on Graph
#include<stdio.h>
#include<conio.h>
void create();
void display();
void iedge(int t,int h);
void inode();
void dedge(int t,int h);
void dnode(int d);
int adj[10][10];
int n;
void main()
{
int node,tail,head,action;
char choice='y';
clrscr();
create();
display();
while(1)
{
printf("\n\n1) Insert a node\n");
printf("2) Insert an edge\n");
printf("3) Delete a node\n");
printf("4) Delete an edge\n");
printf("5) Dispaly\n");
printf("6) Exit\n");
printf("Enter your choice : ");
scanf("%d",&action);
switch(action)
{
case 1:
choice='y';
while(choice=='y'||choice=='Y')
{
inode();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
display();
break;
case 2:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter an edge to be inserted : ");
scanf("%d %d",&tail,&head);
iedge(tail,head);
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
display();
break;
case 3:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter a node to be deleted : ");
scanf("%d",&node);
dnode(node);
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
display();
break;
case 4:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter an edge to be deleted : ");
scanf("%d %d",&tail,&head);
dedge(tail,head);
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
display();
break;
case 5:
display();
break;
case 6:
exit();
break;
}
}
}
void create()
{
int i,j,edges,tail,head;
printf("Enter number of nodes : ");
scanf("%d",&n);
edges=n*(n-1);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
adj[i][j]=0;
}
}
printf("\nEnter -1 -1 to exit \n\n");
for(i=1;i<=edges;i++)
{
printf("Edge %d : ",i);
scanf("%d %d",&tail,&head);
if(tail==-1&&head==-1)
break;
if(tail>n||head>n||tail<=0||head<=0)
{
printf("Invalid edge!\n");
i--;
}
else
adj[tail][head]=1;
}
}
void display()
{
int i,j;
printf("\n");
for(i=1;i<=n;i++)
{
printf("\t%d",i);
}
printf("\n---------------------------------\n");
for(i=1;i<=n;i++)
{
printf("%d |",i);
for(j=1;j<=n;j++)
{
printf("\t%d",adj[i][j]);
}
printf(" | \n");
}
getch();
}
void inode() //insert new node by increasing size of 2d array
{
int i;
n++;
printf("\nNode %d is inserted successfully\n",n);
for(i=1;i<=n;i++)
{
adj[i][n]=0;
adj[n][i]=0;
}
}
void dnode(int d) //delete the node from graph
{
int i,j;
if(n==0)
{
printf("Graph is empty\n");
return;
}
if(d>n)
{
printf("Invalid Node\n");
return;
}
for(i=d;i<=n-1;i++)
{
for(j=1;j<=n;j++)
{
adj[i][j]=adj[i+1][j]; //move rows upward
adj[j][i]=adj[j][i+1]; //move columns left
}
}
n--;
}
void iedge(int t,int h) //insert or add link between 2 nodes
{
if(t>n||h>n||t<=0||h<=0)
{
printf("Invalid edge!\n");
return;
}
else
{
adj[t][h]=1;
}
}
void dedge(int t,int h) //delete or remove link between 2 nodes
{
if(t>n||h>n||t<=0||h<=0||adj[t][h]==0)
{
printf("Invalid edge or doesnot exist\n");
return;
}
else
{
adj[t][h]=0;
}
}

Create an Adjacency Matrix

Write a Program in C to Create an Adjacency Matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int edges,i,j,tail,head,gtype,adj[10][10],n;
clrscr();
printf("Enter number of nodes : ");
scanf("%d",&n);
printf("1) Directed \n2) Undirected \nChoice : ");
scanf("%d",&gtype);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
adj[i][j]=0; //Initialize the array of n*n size with 0
}
}
if(gtype==2)
{
edges=n*(n-1)/2; //maximum no. of edges in undirected
}
else
{
edges=n*(n-1); //maximum no. of edges in directed
}
printf("\nEnter -1 -1 to exit\n\n");
for(i=1;i<=edges;i++)
{
printf("Edge %d : ",i);
scanf("%d %d",&tail,&head);
if(tail==-1||head==-1)
break;
if(tail>n||head>n||tail<=0||head<=0)
{
printf("Invalid edge!\n");
i--;
}
else
{
adj[tail][head]=1;
if(gtype==2) //undirected
{
adj[head][tail]=1;
}
}
}
printf("\nAdjacency Matrix \n\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\t%d",adj[i][j]);
}
printf("\n");
}
getch();
}

Create a Binary Tree, Traverse Using Inorder, Preorder & Postorder

Write a Program in C to Create a Binary Tree, Traverse Using Inorder, Preorder & Postorder
 
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *right, *left;
}*root,*p,*q;
struct node *make(int y)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=y;
newnode->right=newnode->left=NULL;
return(newnode);
}
void left(struct node *r,int x)
{
if(r->left!=NULL)
printf("\n Invalid !");
else
r->left=make(x);
}
void right(struct node *r,int x)
{
if(r->right!=NULL)
printf("\n Invalid !");
else
r->right=make(x);
}
void inorder(struct node *r)
{
if(r!=NULL)
{
inorder(r->left);
printf("\t %d",r->data);
inorder(r->right);
}
}
void preorder(struct node *r)
{
if(r!=NULL)
{
printf("\t %d",r->data);
preorder(r->left);
preorder(r->right);
}
}
void postorder(struct node *r)
{
if(r!=NULL)
{
postorder(r->left);
postorder(r->right);
printf("\t %d",r->data);
}
}
void create()
{
int no;
char choice='y';
printf("\nEnter the root:");
scanf("%d",&no);
root=make(no);
p=root;
while(choice=='y'||choice=='Y')
{
printf("\nEnter number:");
scanf("%d",&no);
if(no==-1)
break;
p=root;
q=root;
while(no!=p->data && q!=NULL)
{
p=q;
if(no<p->data)
q=p->left;
else
q=p->right;
}
if(no<p->data)
{
printf("Left branch of %d is %d\n",p->data,no);
left(p,no);
}
else
{
right(p,no);
printf("Right Branch of %d is %d\n",p->data,no);
}
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
}
void main()
{
int no,action;
clrscr();
while(1)
{
printf("\n\n1) Create\n");
printf("2) Inorder Traversal\n");
printf("3) Preorder Traversal\n");
printf("4) Postorder Traversal\n");
printf("5) Exit\n");
printf("Enter choice : ");
scanf("%d",&action);
switch(action)
{
case 1:
create();
break;
case 2:
printf("\n\nInorder Traversal is \n\t\t\t");
inorder(root);
break;
case 3:
printf("\n\nPreorder Traversal is \n\t\t\t");
preorder(root);
break;
case 4:
printf("\n\nPostorder Traversal is \n\t\t\t");
postorder(root);
break;
case 5:
exit();
break;
}
getch();
}
}

Perform "Insert" and "Delete" on Priority Queue

Write a Program in C to Perform "Insert" and "Delete" on Priority Queue
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void insert(int num,int prio);
void display();
void del();
struct node
{
int priority;
int data;
struct node *addr_next;
}*front = NULL,*tmp,*q,*ptr;
void main()
{
char choice='y';
int action,n,prio;
clrscr();
while(1)
{
printf("1) Insert\n");
printf("2) Delete\n");
printf("3) Display\n");
printf("4) Exit\n");
printf("Enter your choice : ");
scanf("%d",&action);
switch(action)
{
case 1:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter a number and Priority : ");
scanf("%d %d",&n,&prio);
insert(n,prio);
printf("\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
break;
case 2:
choice='y';
while(choice=='y'||choice=='Y')
{
del();
printf("\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
break;
case 3:
display();
break;
case 4:
exit();
break;
}
}
}
void insert(int num,int prio)
{
tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=num;
tmp->priority=prio;
if(front==NULL||prio<front->priority) // check queue is empty or item to be added has priority more than first one
{
tmp->addr_next=front;
front=tmp;
}
else
{
q=front;
while(q->addr_next!=NULL && q->addr_next->priority<=prio)
{
q=q->addr_next;
}
tmp->addr_next = q->addr_next;
q->addr_next = tmp;
}
}
void display()
{
ptr = front;
if(front == NULL)
{
printf("Queue is empty\n");
}
else
{
printf("Data \t Priority\n");
while(ptr != NULL)
{
printf("%d \t %d\n",ptr->data,ptr->priority);
ptr=ptr->addr_next;
}
}
}
void del()
{
if(front == NULL)
{
printf("Queue is empty\n");
}
else
{
tmp=front;
front = front->addr_next;
free(tmp);
}
}

Create a Double Linked List

Write a Program in C to Create a Double Linked List by Accepting Numbers from User and Display it
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void create(int num);
void display();
struct node
{
int data;
struct node *addr_prev;
struct node *addr_next;
}*start,*q,*tmp;
void main()
{
int n;
char choice='y';
start=NULL;
clrscr();
printf("\t\t\tDouble Linked List\n\n");
printf("\t\t| Previous Node | DATA | Next Node |\n\n");
while(choice=='y'||choice=='Y')
{
printf("\nEnter a number : ");
scanf("%d",&n);
create(n);
printf("\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
}
void create(int num)
{
tmp=malloc(sizeof(struct node));
tmp->data=num;
tmp->addr_next=NULL;
if(start==NULL) //check if 1st element
{
tmp->addr_prev=NULL;//assign null for previous address as it the first element
start->addr_prev=tmp;
start=tmp; //point start at tmp
}
else //not 1st element
{
q=start; //point q at start
while(q->addr_next!=NULL) //go to end of linked list
{
q=q->addr_next;
}
q->addr_next=tmp; //store address
tmp->addr_prev=q;
}
}
void display()
{
if(start == NULL) //linked list is empty
{
printf("List is empty\n");
}
else
{
q=start; //point q at start
while(q!=NULL) //go to end of linked list
{
printf("| %d | %d | %d | --> ",q->addr_prev,q->data,q->addr_next);
q=q->addr_next;
}
printf("\n\n");
}
}

Create a Circular Linked List

Write a Program in C to Create a Circular Linked List by Accepting Numbers from User and Display it
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void create(int num);
void display();
struct node
{
int data;
struct node *addr_next;
}*end,*q,*tmp;
void main()
{
int n;
char choice='y';
end=NULL;
clrscr();
printf("\t\t\tCreating a Circular Linked List\n");
while(choice=='y'||choice=='Y')
{
printf("\nEnter a number : ");
scanf("%d",&n);
create(n);
printf("\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
}
void create(int num)
{
tmp=malloc(sizeof(struct node));
tmp->data=num;
if(end==NULL) //check if last
{
end=tmp; //point start at tmp
tmp->addr_next=end;
}
else //not 1st element
{
tmp->addr_next=end->addr_next;
end->addr_next=tmp;
end=tmp;
}
}
void display()
{
if(end==NULL) //linked list is empty
{
printf("List is empty\n");
}
else
{
q=end->addr_next;
while(q!=end) //go to end of linked list
{
printf("| %d | %d | --> ", q->data,q->addr_next);
q=q->addr_next;
}
printf("| %d | %d | ",end->data,end->addr_next);
printf("\n\n");
}
}

Delete a node from the Specified Position of the link list


Write a Program in C to Delete a node from the Specified Position of the list
#include <stdio.h>
#include<conio.h>
#include <malloc.h>
void create(int num);
void display();
void del_end(int end);
int count();
struct node
{
int data;
struct node *addr_next;
}*start,*q,*tmp;
void main()
{
int end,action,n,m,position,i;
char choice='y';
start=NULL;
clrscr();
while(1)
{
printf("1) Create Linked List\n");
printf("2) Delete at End\n");
printf("3) Display\n");
printf("4) Exit\n");
printf("Enter your choice : ");
scanf("%d",&action);
switch(action)
{
case 1:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter a number : ");
scanf("%d",&n);
create(n);
printf("\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
break;
case 2:
choice='y';
while(choice=='y'||choice=='Y')
{
if(start==NULL)
{
printf("List is empty\n");
continue;
}
printf("\n");
end=count();
del_end(end);
printf("\n");
display();
if(end==1)
{
printf("Cannot delete as only one element exists in the list\n");
}
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
break;
case 3:
display();
break;
case 4:
exit();
}
}
}
void create(int num)
{
tmp=malloc(sizeof(struct node));
tmp->data=num;
tmp->addr_next=NULL;
if(start==NULL) //check if 1st element
{
start=tmp; //point start at tmp
}
else //not 1st element
{
q=start; //point q at start
while(q->addr_next!=NULL) //go to end of linked list
{
q=q->addr_next;
}
q->addr_next=tmp; //store address
}
}
int count()
{
struct node *q=start;
int cnt=0;
while(q!=NULL)
{
q=q->addr_next;
cnt++;
}
return cnt;
}
void del_end(int end)
{
int i,cnt=1;
q=start;
for(i=0;i<end-2;i++)
{
cnt++;
q=q->addr_next;
}
tmp=q->addr_next;
free(tmp);
printf("Last element is deleted\n");
q->addr_next=NULL;
}
void display()
{
if(start == NULL) //linked list is empty
{
printf("List is empty\n");
}
else
{
q=start; //point q at start
while(q!=NULL) //go to end of linked list
{
printf("| %d | %d | --> ", q->data,q->addr_next);
q=q->addr_next;
}
printf("\n\n");
}
}

Delete a node from the Specified Position of the list

Write a Program in C to Delete a node from the Specified Position of the list
#include <stdio.h>
#include<conio.h>
#include <malloc.h>
void create(int num);
void display();
void del_middle(int mid);
struct node
{
int data;
struct node *addr_next;
}*start,*q,*tmp;
void main()
{
int action,n,mid,position,i;
char choice='y';
start=NULL;
clrscr();
while(1)
{
printf("1) Create Linked List\n");
printf("2) Delete at Specified Position\n");
printf("3) Display\n");
printf("4) Exit\n");
printf("Enter your choice : ");
scanf("%d",&action);
switch(action)
{
case 1:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter a number : ");
scanf("%d",&n);
create(n);
printf("\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
break;
case 2:
choice='y';
while(choice=='y'||choice=='Y')
{
if(start==NULL)
{
printf("List is empty\n");
continue;
}
printf("\nEnter the position : ");
scanf("%d",&mid);
if(mid==1)
{
printf("\nCannot delete element at 1st position\n\n");
}
else
{
printf("\n");
del_middle(mid);
printf("\n");
display();
}
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
break;
case 3:
display();
break;
case 4:
exit();
}
}
}
void create(int num)
{
tmp=malloc(sizeof(struct node));
tmp->data=num;
tmp->addr_next=NULL;
if(start==NULL) //check if 1st element
{
start=tmp; //point start at tmp
}
else //not 1st element
{
q=start; //point q at start
while(q->addr_next!=NULL) //go to end of linked list
{
q=q->addr_next;
}
q->addr_next=tmp; //store address
}
}
void del_middle(int mid)
{
int i;
q=start;
for(i=0;i<mid-2;i++)
{
q=q->addr_next;
}
tmp=q->addr_next;
q->addr_next=tmp->addr_next;
free(tmp);
printf("Element at position (%d) is deleted\n",mid);
q=q->addr_next;
}
void display()
{
if(start == NULL) //linked list is empty
{
printf("List is empty\n");
}
else
{
q=start; //point q at start
while(q!=NULL) //go to end of linked list
{
printf("| %d | %d | --> ", q->data,q->addr_next);
q=q->addr_next;
}
printf("\n\n");
}
}

Delete the First node of the list

Write a Program in C to Delete the First node of the list
# include <stdio.h>
#include<conio.h>
# include <malloc.h>
void create(int num);
void display();
void del_begin();
struct node
{
int data;
struct node *addr_next;
}*start,*q,*tmp;
void main()
{
int action,n,m,position,i;
char choice='y';
start=NULL;
clrscr();
while(1)
{
printf("1) Create Linked List\n");
printf("2) Delete at Beginning\n");
printf("3) Display\n");
printf("4) Exit\n");
printf("Enter your choice : ");
scanf("%d",&action);
switch(action)
{
case 1:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter a number : ");
scanf("%d",&n);
create(n);
printf("\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
break;
case 2:
choice='y';
while(choice=='y'||choice=='Y')
{
if(start==NULL)
{
printf("List is empty\n");
continue;
}
printf("\n");
del_begin();
printf("\n\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
break;
case 3:
display();
break;
case 4:
exit();
}
}
}
void create(int num)
{
tmp=malloc(sizeof(struct node));
tmp->data=num;
tmp->addr_next=NULL;
if(start==NULL) //check if 1st element
{
start=tmp; //point start at tmp
}
else //not 1st element
{
q=start; //point q at start
while(q->addr_next!=NULL) //go to end of linked list
{
q=q->addr_next;
}
q->addr_next=tmp; //store address
}
}
void del_begin()
{
tmp=start;
start=start->addr_next; //First element deleted
free(tmp);
printf("First element is deleted");
return;
}
void display()
{
if(start == NULL) //linked list is empty
{
printf("List is empty\n");
return;
}
else
{
q=start; //point q at start
while(q!=NULL) //go to end of linked list
{
printf("| %d | %d | --> ", q->data,q->addr_next);
q=q->addr_next;
}
printf("\n\n");
}
}

Insert a node at the End of the list

Write a Program in C to Insert a node at the End of the list
# include <stdio.h>
#include<conio.h>
# include <malloc.h>
void create(int num);
void display();
int count();
void insert_end(int num,int end);
struct node
{
int data;
struct node *addr_next;
}*start,*q,*tmp;
void main()
{
int action,n,end,i;
char choice='y';
start=NULL;
clrscr();
while(1)
{
printf("1) Create Linked List\n");
printf("2) Insert at End\n");
printf("3) Display\n");
printf("4) Exit\n");
printf("Enter your choice : ");
scanf("%d",&action);
switch(action)
{
case 1:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter a number : ");
scanf("%d",&n);
create(n);
printf("\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
break;
case 2:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter a number : ");
scanf("%d",&n);
end=count();
insert_end(n,end);
printf("\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
break;
case 3:
display();
break;
case 4:
exit();
}
}
}
void create(int num)
{
tmp=malloc(sizeof(struct node));
tmp->data=num;
tmp->addr_next=NULL;
if(start==NULL) //check if 1st element
{
start=tmp; //point start at tmp
}
else //not 1st element
{
q=start; //point q at start
while(q->addr_next!=NULL) //go to end of linked list
{
q=q->addr_next;
}
q->addr_next=tmp; //store address
}
}
int count()
{
struct node *q=start;
int cnt=0;
while(q!=NULL)
{
q=q->addr_next;
cnt++;
}
return cnt;
}
void insert_end(int num,int end)
{
int i;
q=start;
for(i=0;i<end-1;i++)
{
q=q->addr_next;
if(q==NULL)
{
printf("Invalid Position");
return;
}
}
tmp=malloc(sizeof(struct node) );
tmp->addr_next=q->addr_next;
tmp->data=num;
q->addr_next=tmp;
}
void display()
{
if(start == NULL) //linked list is empty
{
printf("List is empty\n");
}
else
{
q=start; //point q at start
while(q!=NULL) //go to end of linked list
{
printf("| %d | %d | --> ", q->data,q->addr_next);
q=q->addr_next;
}
printf("\n\n");
}
}

Insert a node at the Specified Position of the list

Write a Program in C to Insert a node at the Specified Position of the list
# include <stdio.h>
#include<conio.h>
# include <malloc.h>
void create(int num);
void display();
void insert_middle(int num,int mid);
struct node
{
int data;
struct node *addr_next;
}*start,*q,*tmp;
void main()
{
int action,n,middle,i;
char choice='y';
start=NULL;
clrscr();
while(1)
{
 
printf("1) Create Linked List\n");
printf("2) Insert at Specified Position\n");
printf("3) Display\n");
printf("4) Exit\n");
printf("Enter your choice : ");
scanf("%d",&action);
switch(action)
{
case 1:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter a number : ");
scanf("%d",&n);
create(n);
printf("\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
break;
case 2:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter a number : ");
scanf("%d",&n);
printf("Enter the position : ");
scanf("%d",&middle);
insert_middle(n,middle);
printf("\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
break;
case 3:
display();
break;
case 4:
exit();
}
}
}
void create(int num)
{
tmp=malloc(sizeof(struct node));
tmp->data=num;
tmp->addr_next=NULL;
if(start==NULL) //check if 1st element
{
start=tmp; //point start at tmp
}
else //not 1st element
{
q=start; //point q at start
while(q->addr_next!=NULL) //go to end of linked list
{
q=q->addr_next;
}
q->addr_next=tmp; //store address
}
}
void insert_middle(int num,int mid)
{
int i;
q=start;
for(i=0;i<mid-2;i++)
{
q=q->addr_next;
if(q==NULL)
{
printf("Invalid Position");
return;
}
}
tmp=malloc(sizeof(struct node) );
tmp->addr_next=q->addr_next;
tmp->data=num;
q->addr_next=tmp;
}
 
void display()
{
if(start == NULL) //linked list is empty
{
printf("List is empty\n");
}
else
{
q=start; //point q at start
while(q!=NULL) //go to end of linked list
{
printf("| %d | %d | --> ", q->data,q->addr_next);
q=q->addr_next;
}
printf("\n\n");
}
}

Insert a node at the beginning of the list

Write a Program in C to Insert a node at the beginning of the list
# include <stdio.h>
#include<conio.h>
# include <malloc.h>
void create(int num);
void display();
void insert_begin();
struct node
{
int data;
struct node *addr_next;
}*start,*q,*tmp;
void main()
{
int action,n,i;
char choice='y';
start=NULL;
clrscr();
while(1)
{
printf("1) Create Linked List\n");
printf("2) Insert at Beginning\n");
printf("3) Display\n");
printf("4) Exit\n");
printf("Enter your choice : ");
scanf("%d",&action);
switch(action)
{
case 1:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter a number : ");
scanf("%d",&n);
create(n);
printf("\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
break;
case 2:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter a number : ");
scanf("%d",&n);
insert_begin(n);
printf("\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
break;
case 3:
display();
break;
case 4:
exit();
}
}
}
void create(int num)
{
tmp=malloc(sizeof(struct node));
tmp->data=num;
tmp->addr_next=NULL;
if(start==NULL) //check if 1st element
{
start=tmp; //point start at tmp
}
else //not 1st element
{
q=start; //point q at start
while(q->addr_next!=NULL) //go to end of linked list
{
q=q->addr_next;
}
q->addr_next=tmp; //store address
}
}
void insert_begin(int num)
{
tmp=malloc(sizeof(struct node));
tmp->data=num;
tmp->addr_next=start;
start=tmp;
}
void display()
{
if(start == NULL) //linked list is empty
{
printf("List is empty\n");
}
else
{
q=start; //point q at start
while(q!=NULL) //go to end of linked list
{
printf("| %d | %d | --> ", q->data,q->addr_next);
q=q->addr_next;
}
printf("\n\n");
}
}

Create a Linked List by Accepting Numbers from User

Write a Program in C to Create a Linked List by Accepting Numbers from User and Display it
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void create(int num);
void display();
struct node
{
int data;
struct node *addr_next;
}*start,*q,*tmp;
void main()
{
int n;
char choice='y';
start=NULL;
clrscr();
while(choice=='y'||choice=='Y')
{
printf("\nEnter a number : ");
scanf("%d",&n);
create(n);
printf("\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
}
void create(int num)
{
tmp=malloc(sizeof(struct node));
tmp->data=num;
tmp->addr_next=NULL;
if(start==NULL) //check if 1st element
{
start=tmp; //point start at tmp
}
else //not 1st element
{
q=start; //point q at start
while(q->addr_next!=NULL) //go to end of linked list
{
q=q->addr_next;
}
q->addr_next=tmp; //store address
}
}
void display()
{
if(start == NULL) //linked list is empty
{
printf("List is empty\n");
}
else
{
q=start; //point q at start
while(q!=NULL) //go to end of linked list
{
printf("| %d | %d | --> ", q->data,q->addr_next);
q=q->addr_next;
}
printf("\n\n");
}
}

Implement Structure with pointers

Write a Program in C to Implement Structure with pointers
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name[50];
int age;
};
void main()
{
struct student s1,s2;
struct student *ps1,*ps2;
printf("Enter Roll Number\n");
scanf("%d",&s1.roll);
printf("\nEnter name\n");
scanf("%s",&s1.name);
printf("\nEnter age\n");
scanf("%d",&s1.age);
ps1=&s1; //ps1 will point to s1 of type structure "student"
printf("\n\nEnter Roll Number\n");
scanf("%d",&s2.roll);
printf("\nEnter name\n");
scanf("%s",&s2.name);
printf("\nEnter age\n");
scanf("%d",&s2.age);
ps2=&s2; //ps1 will point to s2 of type structure "student"
printf("\n\nRoll Number \t Name \t\t Age\n\n");
printf(" %d \t \t %s \t %d \n",ps1->roll,ps1->name,ps1->age);
printf(" %d \t \t %s \t %d \n",ps2->roll,ps2->name,ps2->age);
getch();
}

Implement the of Nested Structure

Write a Program in C to Implement the Concept of Nested Structure
#include<stdio.h>
#include<conio.h>
struct student_academic
{
int maths;
int science;
int english;
int total;
};
struct student_personal
{
int roll;
char name[50];
int age;
struct student_academic as; //Nested
};
 
 
void main()
{
struct student_academic as;
struct student_personal ps;
printf("Enter Roll Number\n");
scanf("%d",&ps.roll);
printf("\nEnter name\n");
scanf("%s",&ps.name);
printf("\nEnter age\n");
scanf("%d",&ps.age);
printf("\nEnter marks for Maths\n");
scanf("%d",&ps.as.maths); //nested
printf("\nEnter marks for Science\n");
scanf("%d",&ps.as.science); //nested
printf("\nEnter marks for English\n");
scanf("%d",&ps.as.english); //nested
ps.as.total=(ps.as.maths)+(ps.as.science)+(ps.as.english);
printf("\n\nRoll Number \t Name \t\t Age\n\n");
printf(" %d \t \t %s \t %d \n",ps.roll,ps.name,ps.age);
printf("\n\nMarks of Student %s\n\n",ps.name);
printf("Maths : %d\n",ps.as.maths);
printf("Science : %d\n",ps.as.science);
printf("English : %d\n",ps.as.english);
printf(" ----\n");
printf("Total : %d\n",ps.as.total);
getch();
}

Implement Multiple Structures

Write a Program in C to Implement Multiple Structures
#include<stdio.h>
#include<conio.h>
struct student_personal
{
int roll;
char name[50];
int age;
};
struct student_academic
{
int maths;
int science;
int english;
int total;
};
void main()
{
struct student_personal ps;
struct student_academic as;
printf("Enter Roll Number\n");
scanf("%d",&ps.roll);
printf("\nEnter name\n");
scanf("%s",&ps.name);
printf("\nEnter age\n");
scanf("%d",&ps.age);
printf("\nEnter marks for Maths\n");
scanf("%d",&as.maths);
printf("\nEnter marks for Science\n");
scanf("%d",&as.science);
printf("\nEnter marks for English\n");
scanf("%d",&as.english);
as.total=(as.maths)+(as.science)+(as.english);
printf("\n\nRoll Number \t Name \t\t Age\n\n");
printf(" %d \t \t %s \t %d \n",ps.roll,ps.name,ps.age);
printf("\n\nMarks of Student %s\n\n",ps.name);
printf("Maths : %d\n",as.maths);
printf("Science : %d\n",as.science);
printf("English : %d\n",as.english);
printf(" ----\n");
printf("Total : %d\n",as.total);
getch();
}

Implement Structure with Functions(2nd Meth)

Write a Program in C to implement Structure with Functions using 2nd Method
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name[50];
};
void fun1(int r,char *n); //Function Prototype
void main()
{
struct student s1,s2;
printf("Enter Roll Number for Student 1\n");
scanf("%d",&s1.roll);
printf("\nEnter Name for Student 1\n");
scanf("%s",&s1.name);
printf("\n\nEnter Roll Number for Student 2\n");
scanf("%d",&s2.roll);
printf("\nEnter Name for Student 2\n");
scanf("%s",&s2.name);
printf("\n\nRoll Number \t Names \n\n");
fun1(s1.roll,s1.name);
fun1(s2.roll,s2.name);
getch();
}
void fun1(int r,char *n)
{
printf(" %d \t %s \n",r,n);
}

Implement Structure with Functions

Write a Program in C to implement Structure with Functions using 1st Method
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name[50];
};
void fun1(struct student s); //Function Prototype
void main()
{
struct student s1,s2;
printf("Enter Roll Number for Student 1\n");
scanf("%d",&s1.roll);
printf("\nEnter Name for Student 1\n");
scanf("%s",&s1.name);
printf("\n\nEnter Roll Number for Student 2\n");
scanf("%d",&s2.roll);
printf("\nEnter Name for Student 2\n");
scanf("%s",&s2.name);
printf("\n\nRoll Number \t Names \n\n");
fun1(s1); //Function call for s1
fun1(s2); //Function call for s2
getch();
}
 
void fun1(struct student s)
{
printf(" %d \t %s \n",s.roll,s.name);
}

Implement Structure with Array

Write a Program in C to implement Structure with Array
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name;
int maths;
};
void main()
{
struct student s[4];
int i;
clrscr();
printf("Enter Roll Number, First Character of Name and Marks\n");
for(i=0;i<=3;i++)
{
printf("\nInformation for student %d\n",i+1);
scanf("%d %c %d",&s[i].roll,&s[i].name,&s[i].maths);
}
printf("\n\nRoll Number \t Names \t\t Marks\n\n");
for(i=0;i<=3;i++)
{
printf(" %d \t %c \t\t %d\n",s[i].roll,s[i].name,s[i].maths);
}
getch();
}

Print the Data and its Corresponding Memory Addresses

Write a Program in C to Accept Roll Number, Name and Marks from User for Two Students and Print the Data and its Corresponding Memory Addresses. Use Structures
 
#include<stdio.h>
#include<conio.h>
struct student //Structure
{
int roll;
char name;
float maths;
};
void main()
{
struct student s1,s2; //Elements of Structure
double stud_avg;
clrscr();
printf("Enter information for 1st student\n");
printf("\nRoll Number\n");
scanf("%d",&s1.roll);
printf("\nFirst character of name\n");
scanf(" %c",&s1.name);
printf("\nMarks for Maths\n");
scanf("%f",&s1.maths);
printf("\n\nEnter information for 2nd student\n");
printf("\nRoll Number\n");
scanf("%d",&s2.roll);
printf("\nFirst character of name\n");
scanf(" %c",&s2.name);
printf("\nMarks for Maths\n");
scanf("%f",&s2.maths);
printf("\n\nInformation for 1st Student\n\n");
printf("Roll : %d at %u\n",s1.roll,&s1.roll);
printf("Name : %c at %u\n",s1.name,&s1.name);
printf("Marks : %.2f at %u\n",s1.maths,&s1.maths);
printf("\n\nInformation for 2nd Student\n\n");
printf("Roll : %d at %u\n",s2.roll,&s2.roll);
printf("Name : %c at %u\n",s2.name,&s2.name);
printf("Marks : %.2f at %u\n",s2.maths,&s2.maths);
stud_avg=(s1.maths+s2.maths)/2;
printf("\n\nAverage : %.2lf at %u \n",stud_avg,&stud_avg);
getch();
}

Implement the Concept of Structure

Write a Program in C to Implement the Concept of Structure
#include<stdio.h>
#include<conio.h>
struct student //Declaring a Structure of type "student"
{
int roll;
char name;
float maths;
};
void main()
{
struct student s1,s2; //Declaring Variable of type "student"

double stud_avg;
clrscr();
printf("Enter information for 1st student\n");
printf("\nRoll Number\n");
scanf("%d",&s1.roll);
printf("\nFirst character of name\n");
scanf(" %c",&s1.name);
printf("\nMarks for Maths\n");
scanf("%f",&s1.maths);
printf("\n\nEnter information for 2nd student\n");
printf("\nRoll Number\n");
scanf("%d",&s2.roll);
printf("\nFirst character of name\n");
scanf(" %c",&s2.name);
printf("\nMarks for Maths\n");
scanf("%f",&s2.maths);
printf("\n\nRoll Number \t Names \t\t Marks\n\n");
printf(" %d \t %c \t\t %.2f\n",s1.roll,s1.name,s1.maths);
printf(" %d \t %c \t\t %.2f\n",s2.roll,s2.name,s2.maths);
stud_avg=(s1.maths+s2.maths)/2;
printf("\n\nAverage of both students is %.3lf\n",stud_avg);
getch();
}

Search an Element using Binary Search (Recursive)

Write a Program in C to Search an Element in the Array using Binary Search (Recursive)
#include<stdio.h>
#include<conio.h>
int a[10],flag=0;
int binary(int num,int start, int mid, int end)
{
if(start<=end)
{
if(a[mid]==num)
{
flag=1;
mid++;
return mid;
}
else
{
if(num>a[mid])
{
start=mid+1;
}
else
{
end=mid-1;
}
mid=(start+end)/2;
binary(num,start,mid,end);
}
}
mid++;
return mid;
}
void main()
{
int i,start,end,mid,num,ans;
clrscr();
printf("Enter 5 Numbers\n");
for(i=0;i<=4;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched : ");
scanf("%d",&num);
start=0;
end=4;
mid=(start+end)/2;
ans=binary(num,start,mid,end);
ans++;
if(flag==1)
{
printf("Number (%d) found at position (%d)",num,ans);
}
else
{
printf("Didnot find the number (%d)",num);
}
getch();
}

Search an Element Binary Search (Iterative)

Write a Program in C to Search an Element in the Array using Binary Search (Iterative)
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],start,end,mid,i,num,flag=0;
clrscr();
printf("Enter 5 Numbers\n");
for(i=0;i<=4;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched : ");
scanf("%d",&num);
start=0;
end=4;
mid=(start+end)/2;
while(start<=end)
{
if(a[mid]==num)
{
printf("Number (%d) found at position (%d)\n",num,mid+1);
flag=1;
break;
}
if(num>a[mid])
{
start=mid+1;
}
else
{
end=mid-1;
}
mid=(start+end)/2;
}
if(flag==0)
{
printf("\nDidnot find the number (%d)\n",num);
}
getch();
}

Search an Element using Linear Search (Recursive)

Write a Program in C to Search an Element in the Array using Linear Search (Recursive)
#include<stdio.h>
#include<conio.h>
int a[5];
int linear(int num,int position) //function definition
{
if(num==a[position])
{
return position;
}
else
{
position++;
return linear(num,position);
}
}
void main()
{
int i,n,ans;
clrscr();
printf("Enter 5 numbers\n");
for(i=0;i<=4;i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter the number to be searched\n");
scanf("%d",&n);
ans=linear(n,0); //function call
if(ans<=4)
{
printf("Number (%d) found at position (%d)",n,ans+1);
}
else
{
printf("Didnot find the number (%d)",n);
}
getch();
}

Search an Element using Linear Search (Iteration)

Write a Program in C to Search an Element in the Array using Linear Search (Iteration)
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,num,flag=0;
clrscr();
printf("Enter 5 numbers\n");
for(i=0;i<=4;i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter the number to be searched\n");
scanf("%d",&num);
for(i=0;i<=4;i++)
{
if(a[i]==num)
{
flag=1;
printf("Number (%d) found at position (%d)\n",num,i+1);
break;
}
}
if(flag==0)
{
printf("\nDidnot find the number (%d)\n",num);
}
getch();
}

Function Definition without Function Prototype

//Function Definition without Function Prototype
#include<stdio.h>
#include<conio.h>
void fun1() //Function Definition
{
printf("Function is being executed\n");
}
void main()
{
clrscr();
printf("Before executing function\n");
fun1(); //Function Call
printf("After executing function\n");
getch();
}

Addition on Two 2*2 Matrix

//Write a Program in C to Perform Addition on Two 2*2 Matrix and Print it
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m1[2][2],m2[2][2],m_add[2][2];
clrscr();
printf("Enter four numbers for 1st 2*2 matrix\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&m1[i][j]); //accepting 4 numbers
}
}
printf("\nEnter four numbers for 2nd 2*2 matrix\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&m2[i][j]); //accepting 4 numbers
}
}
printf("\nPrinting 1st Matrix\n");
for(i=0;i<=1;i++)
{
printf("[ ");
for(j=0;j<=1;j++)
{
printf("%d ",m1[i][j]);
}
printf("]\n");
}
printf("\nPrinting 2nd matrix\n");
for(i=0;i<=1;i++)
{
printf("[ ");
for(j=0;j<=1;j++)
{
printf("%d ",m2[i][j]);
}
printf("]\n");
}
//Adding Two Matrix
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
m_add[i][j] = m1[i][j] + m2[i][j];
}
}
printf("\n\nAddition of two Matrices \n\n");
for(i=0;i<=1;i++)
{
printf("[ ");
for(j=0;j<=1;j++)
{
printf("%d ",m_add[i][j]);
}
printf("]\n");
}
getch();
}

Swap Values of Two Arrays

Write a Program in C to Swap Values of Two Arrays
#include<stdio.h>
#include<conio.h>
void main()
{
int swap,i,num1[2],num2[2];
clrscr();
printf("Enter two numbers for 1st array\n");
for(i=0;i<=1;i++) //Continue this loop for 2 values i.e. from 0 to 1
{
scanf("%d",&num1[i]);
}
printf("Enter two numbers for 2nd array\n");
for(i=0;i<=1;i++)
{
scanf("%d",&num2[i]);
}
printf("\nBefore swapping \nnum1 = %d, %d \nnum2 = %d,%d\n\n",num1[0],num1[1],num2[0],num2[1]);
//Swapping contents of num1 and num2
for(i=0;i<=1;i++)
{
swap=num1[i]; //Assigning value of num1 to temporary variable
num1[i]=num2[i]; //Assigning value of num2 to num1
num2[i]=swap; //Assigning value of temporary variable to num2
}
printf("After swapping \nnum1 = %d, %d \nnum2 = %d,%d\n\n",num1[0],num1[1],num2[0],num2[1]);
getch();
}

Implement calloc and realloc function

Write a Program in C to Implement "calloc" and "realloc" function
#include<stdio.h>
#include<conio.h>
void main()
{
int le,i,*pa,new_le;
clrscr();
printf("Enter Total Numbers\n");
scanf("%d",&le);
pa=(int*)calloc(le*sizeof(int));
printf("\nEnter %d numbers\n",le);
for(i=0;i<=le-1;i++)
{
scanf("%d",(pa+i));
}
printf("\nPrinting Numbers\n");
for(i=0;i<=le-1;i++)
{
printf("%d at %u\n",*(pa+i),(pa+i));
}
new_le=12;
pa=(int*)realloc(pa,new_le);
printf("\n\nAfter reallocating\n\n");
for(i=0;i<=new_le-1;i++)
{
printf("%d at %u\n",*(pa+i),(pa+i));
}
getch();
}

Implement "free" function

Write a Program in C to Implement "free" function
 
#include<stdio.h>
#include<conio.h>
void main()
{
int le,i,*pa;
clrscr();
printf("Enter Total Numbers\n");
scanf("%d",&le);
pa=(int*)malloc(le*sizeof(int));
printf("\nEnter %d numbers\n",le);
for(i=0;i<=le-1;i++)
{
scanf("%d",(pa+i));
}
printf("\nPrinting Numbers\n");
for(i=0;i<=le-1;i++)
{
printf("%d\n",*(pa+i));
}
free(pa);
getch();
}

Print Character Using malloc

Program in C to Accept a String from User and Print it Character by Character Using "malloc"
#include<stdio.h>
#include<conio.h>
void main()
{
int le=0,i=0;
char *pc="";
clrscr();
printf("Enter total number of characters\n");
scanf("%d",&le);
pc=(char*)malloc(le*sizeof(char));
printf("\nEnter a string of %d characters\n",le);
scanf("%s",pc);
printf("\nPrinting String Character by Character\n");
for(i=0;i<=le-1;i++)
{
printf("%c\n",*(pc+i));
}
getch();
}

Number Using "malloc" function

Program in C to Accept Numbers from User and Print Number Using "malloc" function
#include<stdio.h>
#include<conio.h>
void main()
{
int le,i,*pa;
clrscr();
printf("Enter Total Numbers\n");
scanf("%d",&le);
pa=(int*)malloc(le*sizeof(int));
printf("\nEnter %d numbers\n",le);
for(i=0;i<=le-1;i++)
{
scanf("%d",(pa+i)); //Accepting Numbers
}
printf("\nPrinting Numbers\n");
for(i=0;i<=le-1;i++)
{
printf("%d\n",*(pa+i)); //Printing Numbers
}
getch();
}

Implement the Concept of Pointers

Program in C to Implement the Concept of Pointers
#include<stdio.h>
#include<conio.h>
void main()
{
int n=10,*pn;
clrscr();
pn=&n;
printf("Content of variable n = %d\n",n);
printf("Address of variable n = %u\n",&n);
printf("Content at address of n = %d\n",*(&n));
printf("Value of Pointer variable pn = %u\n",pn);
printf("Pointer Variable pn = %d\n",*pn);
printf("Address of variable pn = %u\n",&pn);
getch();
}

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls