Advance ‘c’ and data structure 1. Write a program to print the value and address of the variable using pointer. #include<stdio.h> #include<conio.h> void main() { int x,y; int *ptr; clrscr(); x=10; ptr=&x; y=*ptr; printf("value of x is %d\n",x); printf("%d is stored at address %u\n",x,&x); printf("%d is stored at address %u\n",*&x,&x); printf("%d is stored at address %u\n",*ptr,ptr); printf("%d is stored at address %u\n",ptr,&ptr); printf("%d is stored at address %u\n",y,&y); *ptr=25; printf("\n now x=%d",x); getch(); } 2. Write a program to illustrate the use of pointers in arithmetic operators. #include<stdio.h> #include<conio.h> void main() { int a,b,*p1,*p2,x,y,z; clrscr(); a=12; b=4; p1=&a; p2=&b; S.Y.B.C.A.( Third Semester) N.L.C.P.A.S 1
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Advance ‘c’ and data structure
1. Write a program to print the value and address of the variable using pointer.
#include<stdio.h>#include<conio.h>void main(){
int x,y;int *ptr;clrscr();x=10;ptr=&x;y=*ptr;printf("value of x is %d\n",x);printf("%d is stored at address %u\n",x,&x);printf("%d is stored at address %u\n",*&x,&x);printf("%d is stored at address %u\n",*ptr,ptr);printf("%d is stored at address %u\n",ptr,&ptr);printf("%d is stored at address %u\n",y,&y);*ptr=25;printf("\n now x=%d",x);getch();
}
2. Write a program to illustrate the use of pointers in arithmetic operators.
#include<stdio.h>#include<conio.h>void main(){
int a,b,*p1,*p2,x,y,z;clrscr();a=12;b=4;p1=&a;p2=&b;
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
1
Advance ‘c’ and data structure
x=*p1 * *p2-6;y=4* - *p2/ *p1+10;printf("address of a=%u\n",p1);printf("address of b=%u\n",p2);printf("\n");printf("a=%d,b=%d\n",a,b);printf("x=%d,y=%d\n",x,y);*p2=*p2+3;*p1=*p2-5;z=*p1 * *p2-6;printf("\n a=%d,b=%d",a,b);printf("\n z=%d \n",z);getch();
}
3. Write a program to make function using pointer to exchange the value of two location in the memory.
5. Write a program to create an array of pointer for character which will store name of which will store name of 5 cities.
/* program to create an array of pointer for character which will storename of which will store name of 5 cities */#include<stdio.h>#include<conio.h>#include<string.h>void main(){
int i,j,n[3][3],m[3][3],ans[3][3];clrscr();printf("enter matrix element");for(i=0;i<3;i++){
for(j=0;j<3;j++){
scanf("%d",&n[i][j]);}
}printf(" enter matrix element");for(i=0;i<3;i++){
for(j=0;j<3;j++){
scanf("%d",&m[i][j]);}
}add(n,m,ans);for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%d",n[i][j]);}printf("\n");
}for(i=0;i<3;i++){
for(j=0;j<3;j++)
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
5
Advance ‘c’ and data structure
{printf("%d",m[i][j]);
}printf("\n");
}for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%d",ans[i][j]);}printf("\n");
}getch();
}
void add(int *ar1,int *ar2,int ar3[3][3]){
int i=0,j=0;for(i=0;i<3;i++){
for(j=0;j<3;j++){
ar3[i][j]=*ar1+*ar2;ar1++;ar2++;
}}
}
7. Write a program to create structure emp accept data for three employees containing eno,enm,sal. do sum of salary of all employees using pointer to structure.
printf("\t\t TOWER OF HANOI\n\n");printf("enter the value n=");scanf("%d",&n);hanoi(n,'A','C','B');getch();
}
void hanoi(int n,char a,char c,char b){
if(n==1){
printf("\n move disc %d from peg %c to %c",n,a,c);
return;}hanoi(n-1,a,b,c);printf("\n move disk %d from peg %c to %c",n,a,c);hanoi(n-1,b,c,a);
}
10. Write a program to find factorial by using operation on stack.
#include<stdio.h>
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
11
Advance ‘c’ and data structure
#include<conio.h>#define N 4int top=-1;int s[N];void main(){
int num;int fact(int);void push(int);int pop(void);clrscr();
printf("enter number");scanf("%d",&num);printf("Factorial of %d is %d",num,fact(num));getch();
}
void push(int n){
if(top>=N){
printf("\n stack overflow");printf("\n %d cannot be pushed");getch();exit(0);
}top++;s[top]=n;
}
int pop(void){
if(top==-1){
printf("\n stack underflow");printf("\n number cannot be popped");getch();
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
12
Advance ‘c’ and data structure
exit(0);}return(s[top--]);
}
int fact(int n){
if(n==1){
return 1;}else{
push(n);return(fact(n-1)*pop());
}}
11. Write a program to convert infix to postfix without parenthesis.
#include<stdio.h>#include<conio.h>#include<string.h>#define N 20int top=0;char s[N];
void main(){
int r=0,i,m;int len=0;char temp;char infix[30];void push(char);char pop(void);int prec(char);int rank(char);
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
13
Advance ‘c’ and data structure
clrscr();s[top]='#';printf("\n enter string:");gets(infix);len=strlen(infix);for(i=0;i<len;i++){
while(prec(infix[i])<=prec(s[top])){
temp=pop();putchar(temp);r=r+rank(temp);
if(r<1){
printf("\n Invalid");exit(0);
}}push(infix[i]);
}while(s[top]!='#'){
temp=pop();putchar(temp);r=r+rank(temp);
if(r<1){
printf("\n Invalid");exit(0);
}}if(r==1){
printf("\n Valid");}else
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
14
Advance ‘c’ and data structure
{printf("\n Invalid");
}getch();
}
void push(char c){
if(top>=N){
printf("\n stack overflow");printf("\n %c cannot be pushed",c);getch();exit(0);
}top++;s[top]=c;
}
char pop(void){
if(top==0){
getch();exit(0);
}return(s[top--]);
}
int prec(char c){
if(c=='+'|| c=='-')return (1);
else if(c=='*'|| c=='/')return (2);
else if(c=='#')return (0);
else
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
15
Advance ‘c’ and data structure
return (3);}
int rank(char c){
if(c=='+'|| c=='-')return (-1);
else if(c=='*'|| c=='/')return (-1);
else if(c=='#')return (0);
elsereturn (1);
}
12. Write a program to evaluate postfix expression.
#include<stdio.h>#include<conio.h>#include<string.h>#define N 10int top=0;int s[N];
void main(){
char postfix[50];int i,len=0;int op1,op2,temp=0;void push(int);int pop(void);clrscr();printf("\n enter string:");gets(postfix);
len=strlen(postfix);for(i=0;i<len;i++)
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
16
Advance ‘c’ and data structure
{if(postfix[i]=='+'){
op2=pop();op1=pop();temp=op1+op2;push(temp);
}else if(postfix[i]=='-'){
op2=pop();op1=pop();temp=op1-op2;push(temp);
}else if(postfix[i]=='*'){
op2=pop();op1=pop();temp=op1*op2;push(temp);
}else if(postfix[i]=='/'){
op2=pop();op1=pop();temp=op1/op2;push(temp);
}else{
if(postfix[i]=='0')push (0);
else if(postfix[i]=='1')push (1);
else if(postfix[i]=='2')push (2);
else if(postfix[i]=='3')
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
17
Advance ‘c’ and data structure
push (3);else if(postfix[i]=='4')
push (4);else if(postfix[i]=='5')
push (5);else if(postfix[i]=='6')
push (6);else if(postfix[i]=='7')
push (7);else if(postfix[i]=='8')
push (8);else if(postfix[i]=='9')
push (9);}
}printf("\n result is :%d",pop());getch();
}
void push(int x){
if(top>=N){
printf("\n stack overflow");getch();exit(0);
}top=top+1;s[top]=x;
}
int pop(void){
if(top==0){
printf("stack underflow");getch();exit(0);
}
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
18
Advance ‘c’ and data structure
top=top-1;return(s[top+1]);
}
13. Write a program to convert infix expression to postfix with parenthesis.
#include<stdio.h>#include<conio.h>#include<string.h>#define N 30
int top=0;char s[N];void main(){
int r=0,i,len=0;char temp;char infix[30];void push(char);char pop(void);int inprec(char);int stprec(char);clrscr();s[top]='(';printf("\n enter string:");gets(infix);len=strlen(infix);for(i=0;i<len;i++){
while(inprec(infix[i])<stprec(s[top])){
temp=pop();putchar(temp);r=r+rank(temp);if(r<1){
printf("\n Invalid");
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
19
Advance ‘c’ and data structure
exit(0);}
}if(inprec(infix[i])!=stprec(s[top]))
push(infix[i]);else{
temp=pop();}
}while(s[top]!='('){
temp=pop();putchar(temp);r=r+rank(temp);if(r<1){
printf("\n Invalid");}
}if(top!=0 || r!=1)
printf("\n Invalid");else
printf("\n Valid");getch();
}
void push(char c){
if(top>=N){
printf("\n stack overflow");printf("\n %c cannot be pushed",c);getch();exit(0);
}top++;s[top]=c;
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
20
Advance ‘c’ and data structure
}
char pop(void){
if(top==0){
getch();exit(0);
}return(s[top--]);
}
int inprec(char c){
if(c=='+' || c=='-')return (1);
else if(c=='*' || c=='/')return (3);
else if(c=='^')return (6);
else if(c=='(')return (9);
else if(c==')')return (0);
elsereturn (7);
}
int stprec(char c){
if(c=='+' || c=='-')return (2);
else if(c=='*' || c=='/')return (4);
else if(c=='^')return (5);
else if(c=='(')return (0);
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
21
Advance ‘c’ and data structure
else if(c==')')return (0);
elsereturn (8);
}
int rank(char c){
if(c=='+' || c=='-')return (-1);
else if(c=='*' || c=='/')return (-1);
else if(c=='^')return (-1);
else if(c=='(')return (0);
else if(c==')')return (0);
elsereturn (1);
}
14. Write a program to insert & delete element from simple queue.
#include<stdio.h>#include<conio.h>#define N 4int q[N];int f=-1;int r=-1;
void main(){
int choice,x;void qinsert(int);int qdelete(void);clrscr();
printf("\n enter value for nx:");scanf("%d",&nx);printf("\n enter value for ny:");scanf("%d",&ny);printf("\n enter value for nz:");scanf("%d",&nz);printf("\n enter value for coeff:");scanf("%d",&ncoeff);poly=polyfront(nx,ny,nz,ncoeff,poly);printf("\n press 1 to insert node");scanf("%d",&ch);
printf("\n enter the value for nx:");scanf("%d",&nx);printf("\n enter the value for ny:");scanf("%d",&ny);printf("\n enter the value for nz:");scanf("%d",&nz);printf("\n enter the value for coeff:");scanf("%d",&ncoeff);poly=polyend(nx,ny,nz,ncoeff,poly);printf("\n press 1 to insert a node");scanf("%d",&ch);
int n;printf(" Enter no for splitting:");scanf("%d",&n);
cnode=fnode;while(cnode->no!=n){
pnode=cnode;cnode=cnode->next;
}if(cnode->no==n){
pnode->next=NULL;fnode1=cnode;
}else
printf(" node has not found");}
void merge(){
int num;printf(" enter no after which merge:");scanf("%d",&num);cnode=fnode;while(cnode->no!=num && cnode->next!=NULL)
cnode=cnode->next;if(cnode->no==num){
temp=fnode1;
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
57
Advance ‘c’ and data structure
while(temp->next!=NULL)temp=temp->next;
temp->next=cnode->next;cnode->next=fnode1;
}else
printf("num not found");}28. Write a program using link list to create structure containing emp_no,name,dept_no,sal. create,insert,delete,display and display record whose salary is highest.
int no;char nm[20],dept[15];int sal;struct link *next;
};
int ans;typedef struct link node;node *fnode,*cnode,*nnode,*pnode;void create(node *);void disp();void insert();void del();void main(){
int ch;clrscr();fnode=(node *)malloc(sizeof(node));create(fnode);do
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
58
Advance ‘c’ and data structure
{printf("\n\t Link List\n");printf("\n==============================");printf("\n 1. insert");printf("\n 2. delete");printf("\n 3. display");printf("\n 4. exit");printf("\n==============================");printf("\n enter your choice:");scanf("%d",&ch);switch(ch){
case 1:insert();break;
case 2:del();break;
case 3:disp();break;
case 4:exit(0);break;
}}while(ch!=4);getch();
}
void insert(){
int ch1,k,c=0;nnode=(node *)malloc(sizeof(node));printf("\n enter number to insert:");
scanf("%d",nnode->no);printf("\n enter name:");
scanf("%s",nnode->nm);printf("\n enter salary:");
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
59
Advance ‘c’ and data structure
scanf("%d",&nnode->sal);printf("\n enter dept:");
scanf("%s",nnode->dept);printf("\n 1.at beg");printf("\n 2.at any pos");printf("\n enter choice:");
scanf("%d",&ch1);if(ch1==1){
nnode->next=fnode;fnode=nnode;
}elseif(ch1==2){
cnode=fnode;printf("\n enter pos:");
scanf("%d",&k);while(c<k && cnode->next!=NULL){
pnode=cnode;cnode=cnode->next;c++;
}if(c==k){
pnode->next=nnode;nnode->next=cnode;
}else
printf("\n Wrong pos");}
}
void del(){
int ch1,k,c=0;node *temp;
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
60
Advance ‘c’ and data structure
printf("\n 1. from beg");printf("\n 2. at any pos");printf("\n enter choice:");
int ch;clrscr();fnode=(node *)malloc(sizeof(node));create(fnode);do{
printf("\n\t Link List");printf("\n==========================");printf("\n 1. insert");printf("\n 2. delete");printf("\n 3. display");printf("\n 4. exit");printf("\n==========================");printf("\n Enter your choice");scanf("%d",&ch);switch(ch){
case 1:
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
63
Advance ‘c’ and data structure
insert();break;
case 2:del();break;
case 3:disp();break;
case 4:exit(0);break;
}}while(ch!=4);getch();
}
void insert(){
int ch1,k,c=0;nnode=(node *)malloc(sizeof(node));printf("\n enter number to insert:");
scanf("%d",&nnode->no);printf("\n==================");printf("\n 1. at beg");printf("\n 2. at any pos");printf("\n==================");printf("\n enter your choice:");scanf("%d",&ch1);if(ch1==1){
nnode->next=fnode;fnode=nnode;
}else if(ch1==2){
cnode=fnode;printf("\n enter pos:");scanf("%d",&k);
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
64
Advance ‘c’ and data structure
while(c<k && cnode->next!=NULL){
pnode=cnode;cnode=cnode->next;c++;
}if(c==k){
pnode->next=nnode;nnode->next=cnode;
}else
printf("\n wrong pos");}
}
void del(){
int ch1,k,c=0;node *temp;printf("\n=======================");printf("\n 1. from beg");printf("\n 2. at any pos");printf("\n=======================");printf("\n enter choice:");scanf("%d",&ch1);if(ch1==1){
cnode=fnode;printf("\n List is............");while(cnode->next!=NULL){
printf("||%d||->",cnode->no);cnode=cnode->next;
}printf("||%d||->",cnode->no);
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
66
Advance ‘c’ and data structure
}
30. Write a menu driven program to perform following operations on circular link list.1) Insert node after given number. And if number is notin list insert at end.2) Delete node from given position.3) Display list.4) Exit
#include<stdio.h>#include<conio.h>#include<stdlib.h>struct link{int no;struct link *next;};typedef struct link node;node *fnode,*cnode,*nnode,*pnode;void create(node*);void disp();void insert();void del();void main(){ int ch; clrscr(); fnode=(node*)malloc(sizeof(node)); create(fnode); do
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
67
Advance ‘c’ and data structure
{ printf("\n Link List"); printf("\n ======================"); printf("\n 1.Insert node after given number.if number isnot in list insert at end."); printf("\n 2.Delete node from specified position"); printf("\n 3.Display list"); printf("\n 4.Exit"); printf("\n======================="); printf("\n Enter choice:"); scanf("%d",&ch); switch(ch) { case 1: insert();
break; case 2: del();
break; case 3: disp();
break; } }while(ch !=4); getch();}void create(node *cnode){ printf("Enter No:"); scanf("%d",&cnode->no); if(cnode->no ==99) cnode->next = fnode; else { cnode->next=(node*)malloc(sizeof(node)); create(cnode->next); } }void insert(){ int t,c1=0; nnode=(node*)malloc(sizeof(node)); printf("\n Enter number to insert:"); scanf("%d",&nnode->no);
S.Y.B.C.A.( Third Semester) N.L.C.P.A.S
68
Advance ‘c’ and data structure
printf("\n Enter number after which we have to insert."); scanf("%d",&t); cnode=fnode; while(cnode->next != fnode)