Top Banner
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
74

DATA STRUCTURE

Feb 07, 2023

Download

Documents

Bhavin Vayla
Welcome message from author
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
Page 1: DATA STRUCTURE

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

Page 2: DATA STRUCTURE

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.

#include<stdio.h>#include<conio.h>void exchange(int *,int *);void main(){

int x,y;clrscr();x=100;y=200;printf("Before exchange x=%d,y=%d\n",x,y);exchange(&x,&y);printf("After exchange x=%d,y=%d\n",x,y);getch();

}void exchange(int *a,int *b){

int t;t=*a;*a=*b;*b=t;

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

2

Page 3: DATA STRUCTURE

Advance ‘c’ and data structure

}

4. Write a program to illustrate the use of structure pointer.

/* program to illustrate the use of structure pointer */#include<stdio.h>#include<conio.h>struct invent{

char *name[20];int number;float price;

};void main(){

struct invent product[3],*ptr;clrscr();printf("INPUT\n\n");for(ptr=product;ptr<product+3;ptr++){

scanf("%s %d %f",ptr->name,&ptr->number,&ptr->price);

}printf("OUTPUT\n\n");ptr=product;while(ptr<product+3){

printf("%-20s %5d %102f\n",ptr->name,ptr->number,ptr->price);

ptr++;}getch();

}

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

3

Page 4: DATA STRUCTURE

Advance ‘c’ and data structure

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(){

char *cn[5]={"surat","bharuch","navsari","vapi","baroda"};

int i,ans=0,l=0;clrscr();for(i=0;i<5;i++){

printf("\n %s",cn[i]);}printf("\n\n %s",cn[2]);for(i=0;i<5;i++){

printf("\n %c",cn[i][3]);}for(i=0;i<5;i++){

while(cn[i][l++]!='\0');{

ans=ans+l -1;l=0;

}}printf("\n\n size=%d",ans);getch();

}

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

4

Page 5: DATA STRUCTURE

Advance ‘c’ and data structure

6. Write a function using pointer to add two dimension matrix to the calling function.

#include<stdio.h>#include<conio.h>void add(int *,int *,int[3][3]);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

Page 6: DATA STRUCTURE

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.

#include<stdio.h>

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

6

Page 7: DATA STRUCTURE

Advance ‘c’ and data structure

#include<conio.h>#include<string.h>struct emp{

int eno,sal;char nm[25];

};void main(){

struct emp e[3],*ep=e;int i;float ans=0;clrscr();for(i=0;i<3;i++){

printf("Eno:");scanf("%d",&ep->eno);printf("sal:");scanf("%d",&ep->sal);ans=ans+ep->sal;fflush(stdin);printf("Ename:");gets(ep->nm);ep++;

}printf("\n Total salary:%f",ans);getch();

}

8. Write a program to perform various operations like push,pop,peep and change on stack.

#include<stdio.h>#include<conio.h>#define n 4int s[n];int top=-1;void main()

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

7

Page 8: DATA STRUCTURE

Advance ‘c’ and data structure

{int choice,x,p,i;void push(int);int pop(void);int peep(int);void change(int,int);void display();clrscr();do{

printf("\n 1. push operation");printf("\n 2. pop operation");printf("\n 3. peep");printf("\n 4. change");printf("\n 5. display");printf("\n 6. exit");printf("\n enter your choice:");scanf("%d",&choice);switch(choice){

case 1:printf("\n enter the value for

push:");scanf("%d",&x);push(x);break;

case 2:p=pop();printf("\n %d is popped",p);break;

case 3:printf("\n enter position of

element:");scanf("%d",&i);printf("\n %d is peeped",peep(i));break;

case 4:

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

8

Page 9: DATA STRUCTURE

Advance ‘c’ and data structure

printf("\n enter position of element for change:");

scanf("%d",&i);printf("\n enter value for change:");scanf("%d",&x);change(x,i);break;

case 5:display();break;

case 6:exit(0);

}}while(choice!=6);getch();

}

void push(int x){

if(top>=n){

printf("\n stack overflow");printf("\n %d can not be pushed",x);getch();exit(0);

}top++;s[top]=x;printf("\n pushed number is %d",x);

}

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

9

Page 10: DATA STRUCTURE

Advance ‘c’ and data structure

exit(0);}top--;return(s[top+1]);

}

int peep(int i){

if(top-i+1<=-1){

printf("\n stack underflow");getch();exit(0);

}return(s[top-i+1]);

}

void change(int x,int i){

if(top-i+1<=-1){

printf("\n stack underflow");getch();exit(0);

}s[top-i+1]=x;printf("\n changed element is %d",x);

}

void display(){

int i;if(top==-1)

printf("stack empty");else

printf("\n stack contents:\n");for(i=top;i>=0;i--)

printf("||%d||\n",s[i]);

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

10

Page 11: DATA STRUCTURE

Advance ‘c’ and data structure

}

9. Write a program to solve Tower of Hanoi using recursion.

#include<stdio.h>#include<conio.h>void hanoi(int,char,char,char);void main(){

int n;clrscr();

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

Page 12: DATA STRUCTURE

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

Page 13: DATA STRUCTURE

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

Page 14: DATA STRUCTURE

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

Page 15: DATA STRUCTURE

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

Page 16: DATA STRUCTURE

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

Page 17: DATA STRUCTURE

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

Page 18: DATA STRUCTURE

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

Page 19: DATA STRUCTURE

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

Page 20: DATA STRUCTURE

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

Page 21: DATA STRUCTURE

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

Page 22: DATA STRUCTURE

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();

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

22

Page 23: DATA STRUCTURE

Advance ‘c’ and data structure

do{

printf("\n 1. queue insert");printf("\n 2. queue delete");printf("\n 3. exit");

printf("\n enter your choice:");scanf("%d",&choice);switch(choice){

case 1:printf("\n enter x:");scanf("%d",&x);qinsert(x);break;

case 2:printf("\n %d is deleted",qdelete());break;

case 3:exit(0);

}}while(choice!=3);getch();

}

void qinsert(int x){

if(r>=N){

printf("\n overflow");getch();exit(0);

}r++;q[r]=x;

if(f==-1)

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

23

Page 24: DATA STRUCTURE

Advance ‘c’ and data structure

f++;}

int qdelete(void){

int x;if(f==-1){

printf("\n underflow");getch();exit(0);

}x=q[f];if(f==r){

f=-1;r=-1;

}else

f++;return (x);

}

15. Write a program to insert & delete elements from circular queue.

#include<stdio.h>#include<conio.h>#define N 4int q[N];int f=-1;int r=-1;

void main(){

int choice,x;int cqdelete(void);

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

24

Page 25: DATA STRUCTURE

Advance ‘c’ and data structure

void cqinsert(int);clrscr();

do{

printf("\n 1. circular queue insert");printf("\n 2. cirdular queue delete");printf("\n 3. exit");

printf("\n enter your choice:");scanf("%d",&choice);switch(choice){

case 1:printf("\n enter x:");scanf("%d",&x);cqinsert(x);break;

case 2:printf("\n %d is

deleted",cqdelete());break;

case 3:exit(0);

}}while(choice!=3);getch();

}

int cqdelete(void){

int x;if(f==-1){

printf("\n underflow");getch();exit(0);

}

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

25

Page 26: DATA STRUCTURE

Advance ‘c’ and data structure

x=q[f];

if(f==r){

f=-1;r=-1;

}else if(f==N)

f=0;else

f++;return (x);

}

void cqinsert(int x){

if(r==N)r=0;

elser++;

if(f==r){

printf("\n overflow");getch();exit(0);

}q[r]=x;

if(f==-1)++f;

}

16.Write a program to insert & delete element from input-restricted d-queue.

#include<stdio.h>#include<conio.h>#define N 4

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

26

Page 27: DATA STRUCTURE

Advance ‘c’ and data structure

int q[N];int f=-1;int r=-1;

void main(){

int choice;int x;int ldelete(void);int rdelete(void);void insert(int);clrscr();

do{

printf("\n 1. Queue insert");printf("\n 2. queue delete from left");printf("\n 3. queue delete from right");printf("\n 4. exit");

printf("\n enter your choice:");scanf("%d",&choice);switch(choice){

case 1:printf("\n enter x:");scanf("%d",&x);insert(x);break;

case 2:printf("\n %d is deleted",ldelete());break;

case 3:printf("\n %s is deleted",rdelete());break;

case 4:exit(0);

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

27

Page 28: DATA STRUCTURE

Advance ‘c’ and data structure

}}while(choice!=4);getch();

}

void insert(int x){

if(r>=N){

printf("\n overflow");getch();exit(0);

}if(r==-1 && f==-1){

f=0;r=0;

}else

r++;q[r]=x;

}

int ldelete(void){

int x;if(f==-1 && r==-1){

printf("\n underflow");getch();exit(0);

}x=q[f];

if(f==r){

f=-1;r=-1;

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

28

Page 29: DATA STRUCTURE

Advance ‘c’ and data structure

}else

f++;return(x);

}

int rdelete(void){

int x;if(f==-1 && r==-1){

printf("\n underflow");getch();exit(0);

}x=q[r];

if(f==r){

f=-1;r=-1;

}else

r--;return(x);}

17. Write a program to insert & delete elements from output restricted d-queue.

#include<stdio.h>#include<conio.h>#define N 4int q[N];int f=-1;int r=-1;

void main(){

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

29

Page 30: DATA STRUCTURE

Advance ‘c’ and data structure

int choice;int x;void linsert(int);void rinsert(int);int qdelete(void);clrscr();

do{

printf("\n 1. Queue insert from left");printf("\n 2. Queue insert from right");printf("\n 3. Queue delete");printf("\n 4. exit");

printf("\n enter your choice");scanf("%d",&choice);switch(choice){

case 1:printf("\n enter x:");scanf("%d",&x);linsert(x);break;

case 2:printf("\n enter x:");scanf("%d",&x);rinsert(x);break;

case 3:printf("\n %d is deleted",qdelete());break;

case 4:exit(0);

}}while(choice!=4);getch();

}void linsert(int x)

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

30

Page 31: DATA STRUCTURE

Advance ‘c’ and data structure

{if(r==-1)

r=N;if(f==0){

printf("\n overflow");getch();exit(0);

}if(f==-1){

f=N;}else

f=f-1;q[f]=x;

}

void rinsert(int x){

if(r>=N){

printf("\n overflow");getch();exit(0);

}else

r++;q[r]=x;

if(f==-1)++f;

}

int qdelete(void){

int x;if(f==-1 && r==-1)

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

31

Page 32: DATA STRUCTURE

Advance ‘c’ and data structure

{printf("\n underflow");getch();exit(0);

}x=q[f];

if(f==r){

f=-1;r=-1;

}else

f++;return(x);}

18. Write a program to insert & delete element from priority queue.

#include<stdio.h>#include<conio.h>int f[3],r[3],ch,temp,a[3][5],n=5,ptr;

void ins(int);void disp();void del();void main(){

f[0]=-1; f[1]=-1; f[2]=-1;r[0]=-1; r[1]=-1; r[2]=-1;clrscr();do{

printf("\n 1. insert");printf("\n 2. delete");printf("\n 3. display");printf("\n 4. exit");

printf("\n enter your choice");

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

32

Page 33: DATA STRUCTURE

Advance ‘c’ and data structure

scanf("%d",&ch);switch(ch){

case 1:printf("\n enter no:");scanf("%d",&temp);printf("\n enter priority 0 to 2");scanf("%d",&ptr);ins(temp);break;

case 2:del();break;

case 3:disp();break;

case 4:exit(0);break;

default:printf("Wrong Choice");

}}while(ch!=4);getch();

}

void ins(int t){

if(r[ptr]==(n-1))printf("\n overflow");

else{

if(f[ptr]==-1 && r[ptr]==-1){

f[ptr]=0;r[ptr]=0;

}else

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

33

Page 34: DATA STRUCTURE

Advance ‘c’ and data structure

r[ptr]++;a[ptr][r[ptr]]=t;printf("\n value is inserted");

}}

void del(){

int i;if(f[0]==-1 && f[1]==-1 && f[2]==-1)

printf("\n\t Underflow");else if(f[0]!=-1){

i=a[0][f[0]];if(f[0]==r[0]){

f[0]=-1;r[0]=-1;

}else

f[0]++;printf("\n %d is deleted",i);

}else if(f[1]!=-1){

i=a[1][f[1]];if(f[1]==r[1]){

f[1]=-1;r[1]=-1;

}else

f[1]++;printf("\n %d is deleted",i);

}else if(f[2]!=-1){

i=a[2][f[2]];

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

34

Page 35: DATA STRUCTURE

Advance ‘c’ and data structure

if(f[2]==r[2]){

f[2]=-1;r[2]=-1;

}else

f[2]++;printf("\n %d is deleted",i);

}}

void disp(){

int i;printf("\n\t");for(i=f[0];i<=r[0];i++){

if(f[0]!=-1)printf("%d\t",a[0][i]);

}printf("\n\t");for(i=f[1];i<=r[1];i++){

if(f[1]!=-1)printf("%d\t",a[1][i]);

}printf("\n\t");for(i=f[2];i<=r[2];i++){

if(f[2]!=-1)printf("%d\t",a[2][i]);

}}

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

35

Page 36: DATA STRUCTURE

Advance ‘c’ and data structure

19. Write a program to convert infix expression to prefix.

#include<stdio.h>#include<conio.h>#include<string.h>

#define MAX 30

char stack[MAX],r[MAX],s[MAX];int top,ch;char temp;void push(char);char pop();void disp();

void main(){ int i=0,j=0,l=0; top=-1; clrscr(); printf("Enter Infix expre:"); scanf("%s",s); l=strlen(s); i=l-1; while(i >= 0) { if((s[i] >='a' && s[i]<='z')||(s[i] >='A' && s[i]<='Z')) { r[j]=s[i]; j++; } else if(s[i]==')') { push(s[i]); } else if(s[i]=='(') {

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

36

Page 37: DATA STRUCTURE

Advance ‘c’ and data structure

temp = pop(); while(temp !=')') { r[j]=temp; j++; temp=pop(); } }else if(s[i]=='*' || s[i]=='/'){ push(s[i]);}else if(s[i]=='+' || s[i]=='-'){ temp=stack[top]; while(temp == '*' || temp=='/') { r[j]=temp; pop(); j++; temp=stack[top]; } push(s[i]);} i--; } while(top != -1) { r[j]=pop(); j++; } r[j]='\0'; strrev(r); printf("res is::%s",r); getch();}void push(char k){

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

37

Page 38: DATA STRUCTURE

Advance ‘c’ and data structure

if (top==(MAX-1)) printf("\n Stack overflow."); else {

top++; stack[top] = k;

}}char pop(){char i;if(top == -1){ printf("\n Stack underflow."); return '0';}else{ i = stack[top]; top--; return i;}}void disp(){int i;printf("\n stack contents :\n"); for (i=0;i <= top ;i++ ) printf("||%d||\t", stack[i]);}

20.Write a program to insert a node at the end of the singly linked list.

#include<stdio.h>#include<conio.h>#include<stdlib.h>#define NULL 0

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

38

Page 39: DATA STRUCTURE

Advance ‘c’ and data structure

struct link_list{

int num;struct link_list *next;

};typedef struct link_list node;void main(){

node *first,*temp,*prev;int ch;int n;node *insend(node *,int);void print(node *);clrscr();first=NULL;printf("\n press 1 to insert a node");scanf("%d",&ch);while(ch==1){

printf("\n enter value for node:");scanf("%d",&n);first=insend(first,n);printf("If you want to insert node than press

1..........\n");scanf("%d",&ch);

}print(first);getch();

}

node *insend(node *first,int n){

node *temp,*prev;temp=(node *)malloc(sizeof(node));temp->num=n;temp->next=NULL;if((first)==NULL){

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

39

Page 40: DATA STRUCTURE

Advance ‘c’ and data structure

first=temp;}else{

prev=first;while(prev->next!=NULL){

prev=prev->next;}

}prev->next=temp;return (first);

}

void print(node *first){

node *temp;temp=first;while(temp!=NULL){

printf("|%d|->",temp->num);temp=temp->next;

}}

21. Write a program to insert a node in front of singly link list.

#include<stdio.h>#include<conio.h>#define NULL 0struct link_list{

int num;struct link_list *next;

};typedef struct link_list node;void main()

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

40

Page 41: DATA STRUCTURE

Advance ‘c’ and data structure

{node *first,*temp;int ch;int n;node *insert(node *,int);first=NULL;clrscr();printf(" Do you want to insert node->");scanf("%d",&ch);while(ch==1){

printf("enter the value for node:");scanf("%d",&n);first=insert(first,n);printf("do you want to insert node:");scanf("%d",&ch);

}temp=first;while(temp!=NULL){

printf("||%d||->",temp->num);temp=temp->next;

}getch();

}node *insert(node *first,int n){

node *temp;temp=(node *)malloc(sizeof(node));temp->num=n;temp->next=first;return (temp);

}

22. Write a program to insert a node in sorted order in singly link_list.

#include<stdio.h>

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

41

Page 42: DATA STRUCTURE

Advance ‘c’ and data structure

#include<conio.h>#define NULL 0struct link_list{

int num;struct link_list *next;struct link_list *prev;

};typedef struct link_list node;void main(){

node *first,*temp;int ch,n;node *insert(node *,int);first=NULL;clrscr();printf("press 1 to insert the node");scanf("%d",&ch);while(ch==1){

printf("\n enter the value of node:");scanf("%d",&n);first=insert(first,n);printf("press 1 to insert node");scanf("%d",&ch);

}temp=first;while(temp!=NULL){

printf("||%d||->",temp->num);temp=temp->next;

}getch();

}

node *insert(node *first,int n){

node *newnode;

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

42

Page 43: DATA STRUCTURE

Advance ‘c’ and data structure

node *save;newnode=(node *)malloc(sizeof(node));newnode->num=n;if(first==NULL){

newnode->next=NULL;return (newnode);

}if(newnode->num<=first->num){

newnode->next=first;return (newnode);

}save=first;while(save->next!=NULL && save->next->num<=newnode-

>num){

save=save->next;}newnode->next=save->next;save->next=newnode;return (first);

}

23.Write a program to delete a node from singly link_list.

#include<stdio.h>#include<conio.h>#include<stdlib.h>#define NULL 0struct link_list{

int num;struct link_list *next;

};typedef struct link_list node;

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

43

Page 44: DATA STRUCTURE

Advance ‘c’ and data structure

void main(){

node *first,*temp,*prev;int ch;int n;int num;node *insend(node *,int);node *del(int,node *);void print(node *);clrscr();first=NULL;do{

printf("\n 1.insert node");printf("\n 2.delete node");printf("\n 3.display");printf("\n 4. exit");printf("\n enter your choice");scanf("%d",&ch);switch(ch){

case 1:printf("\n enter value for node");scanf("%d",&n);first=insend(first,n);break;

case 2:printf("\n enter value for delete");scanf("%d",&num);first=del(num,first);break;

case 3:print(first);break;

case 4:exit(0);break;

}

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

44

Page 45: DATA STRUCTURE

Advance ‘c’ and data structure

}while(ch!=4);getch();

}

node *insend(node *first,int n){

node *temp,*prev;temp=(node *)malloc(sizeof(node));temp->num=n;temp->next=NULL;if(first==NULL){

first=temp;}else{

prev=first;while(prev->next!=NULL){

prev=prev->next;}

}prev->next=temp;return (first);

}

node *del(int num,node *first){

node *pred;node *temp;if(first==NULL){

printf("\n underflow");}temp=first;while(temp->num!=num && temp->next!=NULL){

pred=temp;

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

45

Page 46: DATA STRUCTURE

Advance ‘c’ and data structure

temp=temp->next;}if(temp->num!=num)

printf("\n node not found");if(num==first->num)

first=first->next;else

pred->next=temp->next;return (first);

}

void print(node *first){

node *temp;temp=first;while(temp!=NULL){

printf("||%d||->",temp->num);temp=temp->next;

}}

24. Write a program to insert and delete a node from doubly linked list.

#include<stdio.h>#include<conio.h>

#define NULL 0

struct link_list{

int num;struct link_list *lptr;struct link_list *rptr;

};

typedef struct link_list node;node *l,*r;

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

46

Page 47: DATA STRUCTURE

Advance ‘c’ and data structure

void main(){

int ch,n,m,num;void insert(int,int);void del(int);void print(node *);

clrscr();do{

printf("\n 1.insert node");printf("\n 2.Delete node");printf("\n 3.Display");printf("\n 4.Exit");

printf("\n Enter choice:");scanf("%d",&ch);

switch(ch) {

case 1:printf("Enter the value for the node:");scanf("%d",&n);printf("Enter the value for M:");scanf("%d",&m);insert(m,n);break;

case 2:printf("\n Enter value for delete:");scanf("%d",&num);del(num);break;

case 3:print(l);break;

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

47

Page 48: DATA STRUCTURE

Advance ‘c’ and data structure

case 4:exit(0);

}}while(ch!=4);

getch();}

void insert(int m,int n) {

node *newnode,*pred,*save;

newnode=(node *)malloc(sizeof(node));newnode->num=n;

if(r==NULL){

newnode->lptr=NULL;newnode->rptr=NULL;l=newnode;r=newnode;return;

}if(m==l->num){

newnode->lptr=NULL;newnode->rptr=l;l->lptr=newnode;l=newnode;return;

}save=l;while(save->rptr != NULL && save ->num !=m){pred=save;save=save->rptr;}if(save->num ==m){

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

48

Page 49: DATA STRUCTURE

Advance ‘c’ and data structure

newnode->lptr=pred;pred->rptr=newnode;newnode->rptr=save;save->lptr=newnode;return;

}else{printf("\n Search value not found");return;}

}

void print(node *l) {

node *temp;temp=l;while(temp != NULL){

printf("|%d|<=>",temp->num);temp=temp->rptr;

} }

void del(int old){

node *save;

if(r==NULL){ printf("\n Underflow"); return;}if(l->num==r->num){

l=NULL;r=NULL;return;

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

49

Page 50: DATA STRUCTURE

Advance ‘c’ and data structure

}else if(old==l->num){l=l->rptr;l->lptr=NULL;return;}else if(old==r->num){

r=r->lptr;r->rptr=NULL;return;

}else{

save=l;while(save->rptr!=NULL && save ->num !=old){ save=save->rptr;}save->lptr->rptr=save->rptr;save->rptr->lptr=save->lptr;return;}

}

25. Write a program to insert a term or node in front of polynomial.

#include<stdio.h>#include<conio.h>#define NULL 0struct link_list{

int power_x;int power_y;int power_z;

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

50

Page 51: DATA STRUCTURE

Advance ‘c’ and data structure

int coeff;struct link_list *next;

};typedef struct link_list node;void main(){

node *poly;int ch,nx,ny,nz,ncoeff;node *polyfront(int,int,int,int,node *);void print(node *);clrscr();poly=NULL;printf("\n press 1 to insert node");scanf("%d",&ch);while(ch==1){

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);

}print(poly);getch();

}

node *polyfront(int nx,int ny,int nz,int ncoeff,node *poly){

node *newnode;newnode=(node *)malloc(sizeof(node));newnode->power_x=nx;

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

51

Page 52: DATA STRUCTURE

Advance ‘c’ and data structure

newnode->power_y=ny;newnode->power_z=nz;newnode->coeff=ncoeff;newnode->next=poly;return (newnode);

}

void print(node *poly){

node *temp;temp=poly;while(temp!=NULL){

if(temp->coeff>0 && temp!=poly){

printf("||%dx||%dy||%dz||%d||->",temp->power_x,temp->power_y,temp->power_z,temp->coeff);

temp=temp->next;}else{

printf("||%dx||%dy||%dz||%d||->",temp->power_x,temp->power_y,temp->power_z,temp->coeff);

temp=temp->next;}}}

26. Write a program to insert term or node at the end of polynomial.

#include<stdio.h>#include<conio.h>#define NULL 0struct link_list{

int power_x;int power_y;int power_z;

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

52

Page 53: DATA STRUCTURE

Advance ‘c’ and data structure

int coeff;struct link_list *next;

};typedef struct link_list node;void main(){

node *poly;int ch,nx,ny,nz,ncoeff;node *polyend(int,int,int,int,node *);void print(node *);clrscr();poly=NULL;printf("\n press 1 to insert node:");scanf("%d",&ch);while(ch==1){

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);

}print (poly);getch();

}

node *polyend(int nx,int ny,int nz,int ncoeff,node *poly){

node *newnode,*save;newnode=(node *)malloc(sizeof(node));newnode->power_x=nx;newnode->power_y=ny;

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

53

Page 54: DATA STRUCTURE

Advance ‘c’ and data structure

newnode->power_z=nz;newnode->coeff=ncoeff;newnode->next=NULL;if(poly==NULL){

return (newnode);}else{

save=poly;while(save->next!=NULL)

save=save->next;save->next=newnode;return (poly);

}}

void print(node *poly){

node *temp;temp=poly;while(temp!=NULL){

if(temp->coeff>0 && temp!=poly){

printf("||%dx||%dy||%dz||%d||->",temp->power_x,temp->power_y,temp->power_z,temp->coeff);

temp=temp->next;}else{

printf("||%dx||%dy||%dz||%d||->",temp->power_x,temp->power_y,temp->power_z,temp->coeff);

temp=temp->next;}

}}

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

54

Page 55: DATA STRUCTURE

Advance ‘c’ and data structure

27. Write a menu driven program to apply spilt & merge onsingle link_list.

#include<stdio.h>#include<conio.h>struct link_list{

int no;struct link_list *next;

};typedef struct link_list node;node *fnode,*fnode1,*cnode,*pnode,*temp;void create(node *);void disp(node *);void split();void merge();void main(){

int ch;char c;clrscr();printf("\n enter nos for the first link_list");fnode=(node *)malloc(sizeof(node));create(fnode);disp(fnode);do{

printf("\n enter 1 for split");printf("\n enter 2 for merge");printf("\n enter your choice:");scanf("%d",&ch);if(ch==1){

split();printf("\n first::");disp(fnode);printf("\n second::");disp(fnode1);

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

55

Page 56: DATA STRUCTURE

Advance ‘c’ and data structure

}else if(ch==2){

printf("\n enter nos for second link_list");

fnode1=(node *)malloc(sizeof(node));create(fnode1);merge();printf("\n merge is ::");disp(fnode);

}else

printf("\n wrong choice");printf("\n do you want to continue y/n");c=getchar();

}while(c!='n');getch();

}

void create(node *cnode){

printf("Enter no:");scanf("%d",&cnode->no);if(cnode->no==99)

cnode->next=NULL;else{

cnode->next=(node *)malloc(sizeof(node));create(cnode->next);

}}

void disp(node *temp){

cnode=temp;while(cnode!=NULL){

if(cnode->no!=-99)

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

56

Page 57: DATA STRUCTURE

Advance ‘c’ and data structure

printf("\t %d",cnode->no);cnode=cnode->next;

}printf("\n");

}

void split(){

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

Page 58: DATA STRUCTURE

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.

#include<stdio.h>#include<conio.h>#include<stdlib.h>struct link{

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

Page 59: DATA STRUCTURE

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

Page 60: DATA STRUCTURE

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

Page 61: DATA STRUCTURE

Advance ‘c’ and data structure

printf("\n 1. from beg");printf("\n 2. at any pos");printf("\n enter choice:");

scanf("%d",&ch1);if(ch1==1){

temp=fnode;fnode=fnode->next;free(temp);

}else if(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=cnode->next;free(cnode);

}else

printf("\n Wrong pos");}

}

void create(node *cnode){

printf(" Enter no:");scanf("%d",&cnode->no);

if(cnode->no==99)cnode->next=NULL;

else

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

61

Page 62: DATA STRUCTURE

Advance ‘c’ and data structure

{printf("\n enter name:");

scanf("%s",cnode->nm);printf("\n Enter salary:");

scanf("%d",&cnode->sal);printf("\n Enter dept:");

scanf("%s",cnode->dept);cnode->next=(node *)malloc(sizeof(node));

create(cnode->next);}

}

void disp(){

node *t;t=(node *)malloc(sizeof(node));cnode=fnode;t->sal=cnode->sal;

printf("\n List is...................\n");while(cnode->next!=NULL){

if(cnode->sal>t->sal){

t->sal=cnode->sal;strcpy(t->nm,cnode->nm);strcpy(t->dept,cnode->dept);t->no=cnode->no;

}printf("\n %d\t %s\t %d\t %s\n",cnode-

>no,cnode->nm,cnode->sal,cnode->dept);cnode=cnode->next;

}printf("\n \t Highest salary is...........");printf("\n %d\t %s\t %d\t %s\n",t->no,t->nm,t-

>sal,t->dept);}

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

62

Page 63: DATA STRUCTURE

Advance ‘c’ and data structure

29 .Write a menu driven program to perform basic operations on singly link_list.

#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{

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

Page 64: DATA STRUCTURE

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

Page 65: DATA STRUCTURE

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){

temp=fnode;fnode=fnode->next;free(temp);

}else if(ch1==2){

cnode=fnode;printf("\n Enter pos:");

scanf("%d",&k);while(c<k && cnode->next!=NULL)

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

65

Page 66: DATA STRUCTURE

Advance ‘c’ and data structure

{pnode=cnode;cnode=cnode->next;c++;

}if(c==k){

pnode->next=cnode->next;free(cnode);

}else

printf("\nWrong pos");}

}

void create(node *cnode){

printf(" Enter no:");scanf("%d",&cnode->no);

if(cnode->no==99)cnode->next=NULL;

else{

cnode->next=(node *)malloc(sizeof(node));create(cnode->next);

}}

void disp(){

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

Page 67: DATA STRUCTURE

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

Page 68: DATA STRUCTURE

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

Page 69: DATA STRUCTURE

Advance ‘c’ and data structure

printf("\n Enter number after which we have to insert."); scanf("%d",&t); cnode=fnode; while(cnode->next != fnode)

{ if(cnode->no==t) {

nnode->next=cnode->next;cnode->next=nnode; c1++;break;

} cnode=cnode->next; }

if(cnode->next ==fnode && c1== 0) {

cnode->next=nnode; nnode->next=NULL;

} printf("Inserted"); }void del(){ int ch1,k,c=0; node *temp; printf("\n 1. From Beg"); printf("\n 2.At any pos"); printf("\n Enter choice"); scanf("%d",&ch1); if(ch1==1) { cnode=fnode; while(cnode->next != fnode) cnode=cnode->next;

temp=fnode; fnode=fnode->next; cnode->next=fnode; free(temp);

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

69

Page 70: DATA STRUCTURE

Advance ‘c’ and data structure

} else if(ch1==2) { cnode=fnode; printf("\n Enter pos:"); scanf("%d",&k); while(c < k && cnode->next != fnode) { pnode=cnode; cnode=cnode->next; c++; } if( c==k) { pnode->next=cnode->next; free(cnode); } else printf("Wrong pos"); }}void disp(){ cnode=fnode; printf("\n List is:::::::::::::::"); while(cnode->next != fnode) { printf("||%d||->",cnode->no); cnode=cnode->next; } printf("||%d||",cnode->no);}

31. Write a program to perform binary search .

#include<stdio.h>#include<conio.h>

void search(int x[],int n);

void main(){

int a[10],i;

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

70

Page 71: DATA STRUCTURE

Advance ‘c’ and data structure

clrscr();for(i=0;i<10;i++){

printf("\n Enter no :");scanf("%d",&a[i]);

}search(a,i);getch();

}

void search(int x[],int n){int i,j,temp,pos,beg=0,end=n-1,mid=(beg+end)/2,s_no;for(i=0;i<n;i++){

for(j=0;j<n-i;i++){

if(x[j]>x[j+1]){ temp=x[j]; x[j]=x[j+1]; x[j+1]=temp;}

}}printf("\nEnter searching no:");scanf("%d",&s_no);

while(beg <=end && x[mid]!=s_no){

if(x[mid]>s_no){

end=mid-1;mid=(beg+end)/2;

}else{

beg=mid+1;

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

71

Page 72: DATA STRUCTURE

Advance ‘c’ and data structure

mid=(beg+end)/2;}

}if(x[mid]==s_no){

pos=mid;printf("position of search no is %d",pos);

}else

printf("\n Number not found");}

32. Write a program to sort an array by bubble sort .

#include<stdio.h>#include<conio.h>#define N 10int k[N];

void main(){

int pass,temp,i,exchs,last;void bubble();clrscr();

printf("\nEnter element:");

for(i=0;i<N;i++){

scanf("\n%d",&k[i]);}bubble();for(i=0;i<N;i++){

printf("%d\n",k[i]);}getch();

}

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

72

Page 73: DATA STRUCTURE

Advance ‘c’ and data structure

void bubble(){

int last,pass,i,exchs,temp;last=N;for(pass=0;pass<N-1;pass++){

exchs=0;

for(i=0;i<last-1;i++){

if(k[i] >k[i+1]){temp=k[i];k[i]=k[i+1];k[i+1]=temp;

exchs=exchs+1;}

}if(exchs == 0)

return;elselast=last-1;

}}

33. Write a program to sort an array by selection sort .

#include<conio.h>#define N 10

void main(){int i,k[N],pass,min_index;int temp;clrscr();printf("\n Enter elements of array\n");

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

73

Page 74: DATA STRUCTURE

Advance ‘c’ and data structure

for(i=0;i<N;i++){scanf("%d",&k[i]);}for(pass=0;pass<N-1;pass++){min_index=pass;for(i=pass+1;i<N;i++){if(k[i]<k[min_index]){min_index=i;}}if(min_index !=pass){temp=k[pass];k[pass]=k[min_index];k[min_index]=temp;}}for(i=0;i<N;i++){printf("\n%d",k[i]);}getch();}

S.Y.B.C.A.( Third Semester) N.L.C.P.A.S

74