Professional Documents
Culture Documents
2011
Class11 Getting started with c++
#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,sum=0; cout<<"upto how many numbers:"; cin>>n; for(int i=1;i<=n;i++) sum+=i; cout<<"\nSum upto "<<n<<" is "<<sum; }
Q.2.write a program to Find the smallest and largest odd no. #include<iostream.h>
Labhesh Khetawat Page 1
Q.3.write a program to calculate student's % accepting marks in 3 subs #include<iostream.h> #include<conio.h> void main() { clrscr(); float sub1,sub2,sub3,marks,perc; cout<<"\nEnter marks obtained in three subjects out of 100:"; cin>>sub1>>sub2>>sub3; marks=sub1+sub2+sub3; perc=marks/3; cout<<"\nThe percentage of the student is:"<<perc<<"%"; } otuput: Enter marks obtained in three subjects out of 100:86 89 95 the percantage of the student is:90%
Labhesh Khetawat
Page 3
Q.5.write a program to calculate temperature in celsius to fahrenheit #include<iostream.h> #include<conio.h> void main() { clrscr(); float f,c; cout<<"Enter temperature in celsius:"; cin>>c; f=((9*c)/5)+32; cout<<"\nTemperature in fahrenheit is:"<<f; } output: enter the temperature in celsius:33 temperature in farehrenheit is :91.400002
Data handling
Labhesh Khetawat Page 5
Q.1.Program to enter a character and print its ASCII code and ASCII code of next character. #include<iostream.h> #include<conio.h> void main() { clrscr(); char ch; cout<<"enter the character:"; cin>>ch; int num=ch; cout<<"the ascii code for"<<ch<<" is"<<num<<"\n"; cout<<"adding 1 to the character code:\n"; ch=ch+1; num=ch; cout<<"the ascii code for "<<ch<<"is"<<num; }
Output: Enter the character : k The ASCII code for 'k' is 107 Adding 1 to the character code : The ACSII code for 'l' is 108
Labhesh Khetawat Page 6
Q.2.Program to print cube of a number. #include<iostream.h> #include<conio.h> float cube(float); int main() { clrscr(); float num; cout<<"\nEnter a number:"; cin>>num; cout<<"\nThe cube of"<<num<<"is"<<cube(num); return 0; } float cube(float a) { return a*a*a; } INPUT: Enter a number:5
Q.3.Program to print area of a square. #include<iostream.h> #include<conio.h> void main() { clrscr(); float side,area; cout<<"enter the side of the square:"; cin>>side; cout<<"the area of the square is:"<<side*side<<"square-units"<<endl; }
Output: Enter the side of the square : 5 The area of the square is : 25 square units
Q.4.Program to print area and perimeter of a rectangle. #include<iostream.h> #include<conio.h> void main() { int area,perimeter,length,breadth; cout<<"enter the length and breadth of the the rectangle:";
Labhesh Khetawat Page 8
Output: Enter the lenght and breadth of the rectangle: 3 5 The area of the rectangle is : 15 The perimeter of rectangle is : 16
Q.5.Program to enter a character and print next fout characters. #include<iostream.h> #include<conio.h> void main() { clrscr(); char ch; cout<<"\nEnter the character between A and V:"; cin>>ch; int num=ch; cout<<"\nNext four character are:\n"; cout<<(char)(ch+1)<<"\n"
Labhesh Khetawat Page 9
Q.6.Program to enter a number and print no to the power #include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int n; cout<<"\nEnter the value of n:";
Labhesh Khetawat
2,3,4 and 5.
Page 10
OUTPUT: the square of n:4 the cube of n:8 the value of n^4:16 the value of n^5:32
Operators and expression Q.1.Program to calculate salary of a person on the basis of his experience and age using conditional operator. #include<iostream.h> #include<conio.h> void main()
Labhesh Khetawat Page 11
Output: Is the person experinced ? : 1 Enter the age of the person : 92 The salary of the person is : 6000
Q.2.Progarm to calculate no of teams and left overs from given no of players. #include<iostream.h> #include<conio.h> void main() { clrscr(); int no_of_players,no_of_teams,left_overs; cout<<"\nEnter the no of players:";
Labhesh Khetawat Page 12
Output: Enter the no. of players : 11 There will be 2 teams with 1 left overs.
Q.3.Program to raise x to power n. #include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int x,n; cout<<"\nEnter the base and exponent:"; cin>>x>>n; cout<<"\n"<<x<<" to the power "<<n<<" is "<<pow(x,n); }
Labhesh Khetawat Page 13
Q.4.Program to print square if no is even otherwise print cube. #include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int n; cout<<"\nEnter the number:"; cin>>n; if(n%2==0) cout<<"\nThe number is even and its square is "<<pow(n,2); else cout<<"\nThe number is odd and its cube is "<<pow(n,3); }
Output: Enter the no. : 3 The no. is odd and its cube is 27
Labhesh Khetawat Page 14
Q.5.Program to print square root if number is odd and positive otherwise print number power 5. #include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int n; float y; cout<<"\nEnter the number:"; cin>>n; if((n%2!=0)&&(x>0)) y=sqrt(n); else y=pow(n,5); cout<<y; }
Labhesh Khetawat
Page 15
Flow of control
#include<iostream.h> #include<conio.h> void main() { int i,j; clrscr(); cout<<"desired pattern is\n\n"; for(i=1;i<=5;i++) { for(j=1;j<=5-i;j++) cout<<" "; for(j=1;j<=i;j++) cout<<"* "; cout<<"\n"; } getch(); }
Labhesh Khetawat Page 17
#include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { clrscr(); int n,i,j; cout<<"\nenter the number <=11 for generating the pattern"; cin>>n; cout<<"\ndesired pattern is\n"; for(i=1;i<=n;i++) {
Labhesh Khetawat Page 18
OUTPUT:
Labhesh Khetawat Page 19
Q.3:Draw a flow chart and write a C++ program to generate first n fibonacci series?
#include<iostream.h> #include<conio.h> void main() { int a,b,next,n,count; clrscr(); cout<<"How many Fibonacci terms required >=2 and <=24?\n"; cin>>n; a=0; b=1; cout<<"\nfirst "<<n<<" fibonacci terms are:\n\n"; cout<<a<<' '<<b; count=2; while(count<n)
Labhesh Khetawat Page 20
#include<iostream.h> #include<conio.h> #include<math.h> void main() { float a,b,c,disc,xr1,xr2,img1,img2; clrscr(); cout<<"enter the coefficients\n"; cin>>a>>b>>c; cout<<"\na="<<a<<"b= "<<b<<"c= "<<c<<"\n"; if(a==0.0) { if(b==0.0) cout<<"\nequation is degenerate\n"; else { xr1=c/b;
Labhesh Khetawat Page 23
a=1 b=-5 c=6 real and distinct roots first root=3 second root=2
Q.1. write a program that checks whether the given character is alphanumeric or a digit #include<iostream.h> #include<conio.h> #include<ctype.h> #include<stdlib.h> void main() { clrscr(); char ch; int a; cout<<"\n enter the character:\n"; cin>>ch; a=ch;
Labhesh Khetawat Page 26
Q.2.write a program that checks whether the given character is an alphabet or not.if it is an alphabet,whether its lowercase character or uppercase character? #include<iostream.h> #include<conio.h> #include<ctype.h> void main()
Labhesh Khetawat Page 27
Q.3.write a program that reads two string and appends the #include<iostream.h>
Labhesh Khetawat
Page 28
Q.4.write a program that reads two strind and copies the smaller string into the bigger string #include<iostream.h> #include<conio.h> #include<ctype.h> #include<string.h>
Labhesh Khetawat Page 29
Q.5.write a program that converts lower case letters in uppercase & vice versa. #include<iostream.h> #include<conio.h> #include<ctype.h> #include<string.h> void main() {
Labhesh Khetawat Page 31
for(int i=0;str[i]!='\0';i++) { if(isupper(str[i])) str[i]=tolower(str[i]); else if (islower(str[i])) str[i]=toupper(str[i]); } int x=strlen(str); cout.write(str,x); } output: enter a string: dream it do it DREAM IT DO IT
User defined functions Q.1.write program to print cube of a given number using a function.
Labhesh Khetawat Page 32
#include<iostream.h> #include<conio.h> int main() { clrscr(); float cube(float);float x,y; cout<<"enter number whose cube is to be calculated:"; cin>>x; y=cube(x); cout<<"the cube of "<<x<<" is "<<y; getch (); return 0; } float cube(float a) { float n; n=a*a*a; return n; }
Q.2.write a program to illustrate call by value methid of function invoking. #include<iostream.h> #include<conio.h> void main() { clrscr(); int change(int); int orig=10; cout<<"\nthe original value is:"<<orig; cout<<"\nthe return of function change() is"<<change(orig); cout<<"\nthe value after function change() is over "<<orig; getch (); } int change(int a) { a=20; return a ; }
output: The orignal value is :10 the return of function change() is 20 the value after function change() is over 10
Labhesh Khetawat Page 34
Q.3. write a program to swap values using call by value method. #include<iostream.h> #include<conio.h> int main() { clrscr(); void swap(int,int); int a,b; cout<<"\nenter the value of a:"; cin>>a; cout<<"\nenter the value of b:"; cin>>b; cout<<"\noriginal value are:"; cout<<"a="<<a<<",b="<<b; swap(a,b); cout<<"\nthe value after swap() are:"; cout<<"a="<<a<<",b="<<b; getch (); return 0; } void swap(int x,int y) { int temp;
Labhesh Khetawat Page 35
output: enter the value of a:3 enter the value of b:5 original value are:a=3,b=5 swapped values are:a=5,b=3 the value after swap() are:a=3,b=5
Q.4.write a program to convert distance in feet or inches using a call by reference method. #include<iostream.h> #include<conio.h> #include<process.h> void main() { clrscr(); void convert(float &,char&,char); float distance; char choice,type='f';
Labhesh Khetawat Page 36
output: enter the distance in feet:6 you want distance in feet or inches?(f/i):f distance=6f enter the distance in feet:6 you want distance in feet or inches?(f/i):i distance=72i
Q.5.write a program to check weather a given character is contained in a string or not and find its position. #include<iostream.h>< #include<conio.h>
Labhesh Khetawat Page 38
Q.1.Program that reads two matrices A and B of order m*n and computes C=A+B where C is the third matrix of order m*n.
#include<iostream.h> #include<conio.h> void main() { clrscr (); int i,m,n,j; cout<<"\nenter the values for m & n:"; cin>>m>>n; int a[5][5],b[5][5],c[5][5]; cout<<"\nenter the elements of matrix a:";
Labhesh Khetawat Page 40
OUTPUT:
Labhesh Khetawat Page 41
enter the values for m & n:2 enter the elements of matrix a:1 2 3 4
enter the values for m & n:2 enter the elements of matrix b:1 2 3 4
#include<iostream.h> #include<conio.h>
Labhesh Khetawat Page 42
Output: Enter the number of rows and coloumns of matrix: 3 3 Input matrix: 1 2 3 4 5 6 7 8 9
Matrix: 1 2 3 4 5 6 7 8 9
Transpose: 1 4 7 2 5 8
Labhesh Khetawat Page 44
Q.3.Write a c++ program to find sum of the elements of rows and column of matrix.
#include<iostream.h> #include<conio.h> void main () { clrscr (); int a[10][10],m,n,i,j,r[10],c[10]; cout<<"\nenter the no. of rows and column of a matrix:"; cin>>m>>n; cout<<"\nenter the matrix:"; for(i=0;i<m;i++) { for(j=0;j<n;j++) cin>>a[i][j]; } cout<<"\n\tmatrix:"; for(i=0;i<m;i++) { cout<<"\n"; for(j=0;j<n;j++)
Labhesh Khetawat Page 45
OUTPUT:
Labhesh Khetawat
Page 46
Matrix: 1 2 3 4
The sum of coloumn1 is : 4 The sum of coloumn2 is : 6 The sum of row1 is : 3 The sum of row2 is : 7
Q.4.Write a c++ program that reads two matrix A[]m*n and B[]m*n and compare them for equality.
OUTPUT:
Enter the rows and coloumns of two matrices: 2 2 Input the first matrix: 1 2 3 4 Input the second matrix: 1 2 3 4
Structures Q.1.Program to read values in nested structure. #include<iostream.h> #include<conio.h> #include<stdio.h> struct addr { int houseno ; char area[6]; char city[6]; char state[6]; };
Labhesh Khetawat Page 51
Q.2.Program to add two lenghts given in feet and inches. #include<iostream.h> #include<conio.h> struct distance { int feet; int inches; };
Labhesh Khetawat Page 53
Q:3.Write a c++ program by using structures to print all the months in a year with days.
#include<iostream.h> #include<conio.h> #include<stdio.h> struct year { char month[12]; int days; }; year table[12]={{"january",31},
Labhesh Khetawat Page 55
OUTPUT: january,31
Labhesh Khetawat Page 56
MATRICES
//Q:1.write the c++ program to find the largest & smallest elements in a vector.
#include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); char str[80]; int i; cout<<"\nenter any string:"; gets(str); for(i=0;str[i]!='\0';++i); cout<<"\nlength of the string is:"<<i; getch(); }
OUTPUT: Enter the string: labhesh The length of the string is:7
Labhesh Khetawat
Page 59
//Q:3.Program to delete a substring from a string. #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); char str[30]; int i,p,n,len; cout<<"\nenter the string:"; gets(str); for(len=0;str[len]!='\0';++len); cout<<"\nenter the starting position of substring to be deleted:"; cin>>p; if(p>len) { cout<<"\nenetr bthe number of characters of substring to be deleted:"; cin>>n; if(n+p>=len) n=len-p+1; for(i=p-1;str[i]!='\0';++i) str[i]=str[i+n]; cout<<"\nstring after deletion of sub string is:"<<str; getch();
Labhesh Khetawat Page 60
OUTPUT: Enter the string: labhesh Enter the positon of the string to be deleted: 4 The string is: labesh
#include<iostream.h> #include<conio.h> #include<string.h> int main() { clrscr(); char str1[25],str2[25],str3[50]; int i, k,x1; cout<<"\nenter the first string:\n"; cin.getline(str1,25); cout<<"\nenter the second string:\n"; cin.getline(str2,25);
Labhesh Khetawat Page 61
OUTPUT: Enter the first string: tanishk Enter the second string: ankit The concated string is: tanishk
OUTPUT: Enter the string:labhesh Enter the starting position of the substring:1 Number of characters in the string:7 Num7
Labhesh Khetawat Page 64
CLASS 12
Q:1. Program to illustrate the use of object arrays by storing details of 10 items in an array of object.
OUTPUT: enter itemno and price for item1 1 50 enter itemno and price for item2 2 100 enter itemno and price for item3 3 150
item 1
Labhesh Khetawat Page 67
#include<iostream.h> #include<conio.h> class subject{ int days; int subjectno; public: subject(int d=123,int sn=101); void printsub(void) { cout<<"subjec no:"<<subjectno; cout<<"\n"<<"days:"<<days<<"\n"; } };
Labhesh Khetawat Page 68
Q:2.program to illustrate working of default arguments.calculate interest amount making use of default arguments.
#include<iostream.h> #include<conio.h> void amount(float princ,int time=2,float rate=0.08); void amount(float princ,int time,float rate) { cout<<"\npricipal amount:rs"<<princ; cout<<"\t time:"<<time<<"years"; cout<<"\t rate:"<<rate; cout<<"\n interested amount:"<<(princ*rate*time)<<endl; }
Labhesh Khetawat Page 71
OUTPUT: case 1: principal amount:RS 2000 interest amount: 320 case 2: principal amount:RS 2500 interest amount: 600 case 3: principal amount:RS 2300 interest amount: 759 case 4: principal amount:RS 2500
Labhesh Khetawat
time:2years
rate:0.08
time:3years
rate:0.08
time:3years
rate:0.11
time:0years
rate:0.08
Page 72
Inheritance
#include<iostream.h> #include<conio.h> class base { public: int a; }; class d1:virtual public base { public: int b; }; class d2:virtual public bases { public: int c; };
Labhesh Khetawat Page 73
OUTPUT: 35 50 75 160
Labhesh Khetawat
Page 74
#include<iostream.h> #include<conio.h> class base1 { protected: int a; public: base1(int x) { a=x; cout<<"constructing base1\n"; } ~base1() { cout<<"destructing base 1\n"; } }; class base2 { protected: int b; public:
Labhesh Khetawat Page 75
OUTPUT: Constructing base 2 constructing base 1 constructing derived 1.15 2.14 3.16
Labhesh Khetawat
Page 77
Q.1. Program to write and read a structure using write() and read() function using a binary file.
#include<fstream.h> #include<conio.h> #include<string.h> struct customer{ char name[51]; float balance; }; void main() { clrscr(); customer savac; strcpy(savac.name,"Akshat Khemka"); savac.balance=9876543.21; ofstream fout; fout.open("saving",ios::out|ios::binary); if(!fout) { cout<<"\n File cannot be opened\n"; } fout.write((char*)&savac,sizeof(savac));
Labhesh Khetawat Page 78
OUTPUT: file cannot be opened labhesh khetawat has the balance amount of RS 987545
Q.2. Program to create a single file and then display its contents.
#include<fstream.h> #include<conio.h> void main() { clrscr(); ofstream fout("student"); char name[30],ch; float marks=0.0; for(int i=0;i<5;i++)
Labhesh Khetawat Page 79
OUTPUT: student: 1 name:labhesh marks:76 student: 2 name:akshat marks:89 student: 3 name:raj marks:65 student: 4 name:g.one marks:75 student: 5 name:ra.one marks:99
Name:raone marks:98
Q.3. A file named marks.dat already stores some students details like rollno and marks. Write a program that reads more such details and append them to this file. Make sure that previous contents of the the file are not lost.
Labhesh Khetawat Page 81
#include<fstream.h> #include<conio.h> #include<stdlib.h> void main() { clrscr(); ofstream fout; fout.open("marks.dat",ios::app); char ans='y'; int rollno; float marks; if(ans=='y'||ans=='Y') { cout<<"\nEnter Rollno.:"; cin>>rollno; cout<<"\nEnter marks:"; cin>>marks; fout<<rollno<<"\n"<<marks<<"\n"; cout<<"\nWant to enter more records?(y/n)"; cin>>ans; } else exit(0);
Labhesh Khetawat Page 82
OUTPUT:
#include<fstream.h> #include<conio.h> void main() { clrscr(); char ch; ifstream fin; fin.open("marks.dat",ios::in); if(!fin) { cout<<"\nCannot open file!!!\n";
Labhesh Khetawat Page 83
OUTPUT: 1 56 43 345 5 65
Pointers
Q.1:Program to read the 2-D array alongwith rowsum and column sum and display its alogorithm?
Labhesh Khetawat
Page 86
Labhesh Khetawat
Page 88
enter the elements along with rowsum and columnsum is: 1113 1113 1113 333
Q.2:Exchange the positions of the string stored in array using array of pointers?
OUTPUT:
Q.3:Program to search for a given character inside a string and to print the string from the point of match?
#include<iostream.h> #include<conio.h> char *match(char ,char*); void main() { clrscr(); char string[80],*cp,ch; cout<<"enter a string\n"; cin.getline(string,80); cout<<"enter a character to be searched for\n"; cin>>ch; cp=NULL; cp=match(ch,string); if(*cp) { cout<<"\n"; for(;(*cp!='\0');cp++) cout<<*cp;
Labhesh Khetawat Page 91
OUTPUT:
#include<iostream.h>
Labhesh Khetawat Page 92
#include<iostream.h> #include<conio.h> #include<string.h> void main() { clrscr(); char *games[]={"football","cricket","hockey","baseball","golf"}; char *temp; int len=0; len=strlen(games[0]); cout<<"\nstring1 is"; cout.write(games[0],len); cout<<"\nstring4 is"; cout.write(games[3],len); temp=games[0]; games[3]=temp; len=strlen(games[0]); cout<<"\nstring1 is"; cout.write(games[0],len);
Labhesh Khetawat Page 94
OUTPUT:
#include<iostream.h> #include<conio.h> #include<string.h> class strn { char *a, flag; int i,j,k; public: void read(); void ch_pal(); strn()
Labhesh Khetawat Page 95
OUTPUT: enter the string labhesh the entered string is not a palindrome
#include<iostream.h> #include<conio.h>
Labhesh Khetawat Page 97
// This function returns the location of key in the list // a-1 is returned if the value is not found
OUTPUT: enter the item you are searching for:10 the item was found at the idex location 2 Q.2. Write a C++ program to find the location of an element in an array using search process consedering the element in ascending order using binary search method.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int series[100]; int i,n,pos,x; cout<<"Enter the size of the list\n"; cin>>n; for(i=0;i<n;i++)
Labhesh Khetawat Page 99
OUTPUT: enter the size of the list:3 enter the element 1:5 enter the element 2:7 enter the element 3:9
Q.3. Write a C++ program to sort the element of an array in ascending order using selection sort.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int list[30]; int small,pos; int temp,i,j,n; cout<<"\n Enter the size of the list:"; cin>>n;
Labhesh Khetawat Page 101
enter the data elements of list1:3 enter the data elements of list2:9 enter the data elements of list3:7
the sorted list is as follows: data element 3 data element 7 data element 9 Q.4. Write a C++ program to sort the element in descending order using bubble sort.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int list[]={2,13,6,9,8,1,10}; int i=0; int n,j,temp;
Labhesh Khetawat Page 103
#include<iostream.h> #include<conio.h> void main () { clrscr(); int reg[50]; int i,loc,x,n,back; cout<<"\nenter the size of list......"; cin>>n; for(i=0;i<n;i++) { cout<<"\nenter the data element>";
Labhesh Khetawat Page 105
OUTPUT: enter the size of list...4 enter the data element:1 enter the data element:2
Labhesh Khetawat Page 106
Q.6.Suppose A,B,C are arraysof integers of size M,N and M+N numbers in array A and B appear in ascending order.Give in neccesary declaration for array A,B and C in C++.Write a program in C++ to produce third array C by merging arrays A and B in ascending order.
respectively.The
#include<iostream.h> #include<conio.h> void main () { int par1[20],par2[20],final[40]; int m,n; int ptr1,ptr2,ptr3; int i; cout<<"\n Enter the Size of lists 1 & 2:"; cin>>n>>m; cout<<"\n Enter the list 1:"; for(i=0;i<n;i++) cin>>par1[i];
Labhesh Khetawat Page 107
#include<iostream.h> #include<conio.h> void main() { clrscr (); int list[30]; int i,j,k,n; int temp; cout<<"\n Enter the size of the List:"; cin>>n;
Labhesh Khetawat Page 109
OUTPUT:
Labhesh Khetawat Page 110
position.
#include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); int reg[50]; int i,n,x,loc,back; cout<<"\nEnter Size Of The List>"; cin>>n; for(i=0;i<n;i++) { cout<<"\nEnter the element of the array>"; cin>>reg[i]; }
Labhesh Khetawat Page 111
OUTPUT: enter the size of the list:3 enter the element of array:1 enter the element of array:2 enter the element of array:3 enter the value to be inserted:4
Labhesh Khetawat Page 112
Q:1. Write a program to create and traverse a linked list.The linked list contains data of integer type.
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> struct node { int data; node *link; };
Labhesh Khetawat Page 113
Output: Enter how many nodes to create in the linked list: 2 Enter the data value of node-> 1 Enter the data value of node1-> 2 Enter the data value of node2-> 3
Q:2.Write a program to create a linked and searched a particular data value of integer data type
Labhesh Khetawat
Page 115
Output: Enter how many nodes to create in the linked list: 2 Enter the data value of node1-> 2 Enter the data value of node2-> 7
the linked list value are: 2 7 data value to be searched->2 search is suceesfull
Labhesh Khetawat
Page 118
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> struct node { int data; node *link; }; node *addfirst(node*first,int value); node *addbetween(node*first,int value,int val); node *addlast(node*first,int value); void traverse(node *first); void main() { clrscr(); int i,n, choice,val,value; node *first,*temp,*last; cout<<"enter how many nodes to created in link list->"; cin>>n; first=new node; cout<<"\nenter the element of node 1->";
Labhesh Khetawat Page 119
main menu: 1. Inserting at first 2. Inserting in between 3. Inserting at last 4. traversing at last 5. exit enter your choice-> 1 Enter data value to be inserted->3
Q:4.Write a program to create a linked list and delete elements according to users choice.
Labhesh Khetawat
Page 124
case 2:cout<<"\n\t Enter the value of node which is to be deleted:"; cin>>val; first=delbetween(first,value,val); if(value!=-1) cout<<"\n\t The deleted value is:"<<value<<"\n\t"; break;
case 5:exit(0);
Labhesh Khetawat Page 127
node*delfirst(node*first,int&value) { node *temp; temp=first; if(first==NULL) { cout<<"\n list empty"; value=-1; } else
Labhesh Khetawat Page 128
output: enter how many nodes in the linked list:2 enter the data value of node 1:2 enter the data value of node 2:3 main menu: 1.for deleting at first 2.for deleting in between 3.for deleting at last 4.traversing the list 5.for exit enter tour choice:1 the deleted data is:2
Labhesh Khetawat Page 131
Q:5.Program to perform all basic operation on stack the stack contain data of integer type?
#include<iostream.h> #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<ctype.h> # define max 100 int stack[max]; int top; void push(int stack[],int val,int &top); int pop(int stack[],int &top); void show_stack(int stack[],int top); void main() { int choice,val; char opt='y'; top=-1; clrscr(); do { clrscr(); cout<<"\n main menu"
Labhesh Khetawat Page 132
output: main menu: 1.addition 2.deletion 3.traverse 4.exit enter your chice:1
Labhesh Khetawat Page 136
Q.6.Program to display the basic operations of add stack,delete stack.The stack contains data of integer type.
#include<iostream.h> #include<conio.h> #include<ctype.h> #include<process.h> struct node{ int data; node *link; }; node *push(node *top,int val); node *pop(node *top,int &val); void show(node *top); void main() { clrscr(); node *top; int ch,val;
Labhesh Khetawat Page 137
Q:7.Program to perform all basic operation of add queue, delete queue and show queue?
#include<iostream.h> #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<ctype.h> # define max 100 int queue[max]; int top,front,rear; void add_q(int queue[],int val,int &rear); int del_q(int queue[],int &front,int rear); void show_q(int queue[],int front,int rear); void main() { int choice,val; char opt='y'; rear=-1; front=-1; top=-1; clrscr(); do
Labhesh Khetawat Page 142
int del_q(int queue[],int &front,int rear) { int value; if(front==rear) { cout<<"\n \t Queue empty!!!!"; value=-1; } else { front=front+1; value=queue[front]; } return(value); }
output:
Q:8. Program to perform the basic operation of add queue,delete queue,using linked list the queue contains data of integer type?
#include<ctype.h> #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<stdio.h> struct node { int data; node*link; }; node*add_q(node*rear,int val); node*del_q(node*front,int &val); void show_q(node*front);
Labhesh Khetawat Page 147
void show_q(node *front) { node *temp; temp=front; clrscr(); cout<<"\nthe values are:"; while(temp!=NULL) { cout<<"\n"<<temp->data; temp=temp->link; } getch(); }
output:
Labhesh Khetawat Page 151
Labhesh Khetawat
Page 152
Labhesh Khetawat
Page 153