You are on page 1of 54

Institute of Engineering and Technology CSE-Y Batch 2009

Usman Ghani
1. WAP to traverse a linear array.
Program : //To traverse a linear array #include<iostream.h> #include<conio.h> vo
id main() { clrscr(); int a[5],n,i,sum=0; cout<<"\nEnter the size of array: "; c
in>>n; cout<<"\nEnter the elements of array: "; for(i=0;i<n;i++) { cin>>a[i]; su
m+=a[i]; } cout<<"\nThe elements of array: "; for(i=0;i<n;i++) { cout<<a[i]<<"\t
"; } cout<<endl; cout<<"\nSum of elements: "<<sum; getch(); }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 1
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 2
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
2. WAP to insert an element in the beginning of linear array.
Program : //To insert an element in the beginning of a linear array #include<ios
tream.h> #include<conio.h> void main() { clrscr(); int a[10],n,i,item,j; cout<<"
\nEnter the number of elements in array: "; cin>>n; cout<<"\nEnter the elements:
"; for(i=0;i<n;i++) { cin>>a[i]; } cout<<"\nEnter item to be inserted in the be
ginning: "; cin>>item; j=n; while(j>=0) { a[j+1]=a[j]; j=j-1; } a[0]=item; n=n+1
; cout<<"\n-----------------------------------"; cout<<"\nArray Elements: \n"; f
or(i=0;i<n;i++) { cout<<a[i]<<"\t"; } getch(); }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 3
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 4
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
3. WAP to insert an element at the end of linear array.
Program : //To insert an element at end in a linear array #include<iostream.h> #
include<conio.h> void main() { clrscr(); int a[100],n,i,item; cout<<"Enter the n
o.of elements in array "; cin>>n; cout<<"\n Enter the elements \n"; for(i=0;i<n;
i++) { cin>>a[i]; } cout<<"The array elements are \n"; for(i=0;i<n;i++) { cout<<
a[i]<<" "; } cout<<"\n---------------------------------------------------"; cout
<<"\nEnter the element to be inserted at end "; cin>>item; n=n+1; a[n-1]=item; c
out<<"\nThe array elements are \n"; for(i=0;i<n;i++) { cout<<a[i]<<" ";} getch()
;
}
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 5
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 6
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
4. WAP to delete an element from the beginning of a linear array.
Program : //To delete an element from the beginning of a linear array #include<i
ostream.h> #include<conio.h> void main() { clrscr();
int a[10],n,i,item,j; cout<<"\nEnter the number of elements in array: "; cin>>n;
cout<<"\nEnter Elements: "; for(i=0;i<n;i++) { cin>>a[i]; } item = a[0]; j=0; w
hile(j<n) { a[j]=a[j+1]; j=j+1; } n=n-1; cout<<"\n------------------------------
---------"; cout<<"\nArray after deleting the first item "<<item<<" : \n"; for(i
=0;i<n;i++) { cout<<a[i]<<"\t"; } getch(); }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 7
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 8
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
5. WAP to detete an element from the end of linear array.
Program : //To delete an element from end in a linear array #include<iostream.h>
#include<conio.h> void main() { clrscr(); int a[100],n,i,item; cout<<"Enter the
no.of elements in array "; cin>>n; cout<<"\n Enter the elements \n"; for(i=0;i<
n;i++) { cin>>a[i]; } cout<<"\nThe array elements are \n"; for(i=0;i<n;i++) { co
ut<<a[i]<<" "; } cout<<"\n------------------------------------------------------
"; n=n-1; cout<<"\nThe array elements after deleting the last element are \n"; f
or(i=0;i<n;i++) { cout<<a[i]<<" ";} getch(); }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 9
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 10
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
6. WAP to implement linear search in linear array.
Program : //To do linear search #include<iostream.h> #include<conio.h> #include<
process.h> void main() { clrscr();
int i,a[5],n; for(i=0;i<5;i++) { cout<<"Enter the value of a["<<i<<"]="; cin>>a[
i]; } cout<<"Enter any number: "; cin>>n; for(i=0;i<5;i++) { if(a[i]==n) { cout<
<"Position is "<<i+1; getch(); exit(0); } } cout<<"Element is not present in the
list"; getch(); }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 11
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 12
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
7. WAP to implement a binary search.
Program : //To search an element in array using binary search #include<iostream.
h> #include<conio.h> void main() { clrscr(); int a[100],n,i,j,item,temp; cout<<"
Enter the no.of elements in array "; cin>>n; cout<<"\n Enter the elements \n"; f
or(i=1;i<=n;i++) { cin>>a[i]; } for(i=1;i<=n-1;i++) { for(j=1;j<=n-i;j++) { if (
a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } cout<<"\nSorted array:
"; for(i=1;i<=n;i++) { cout<<a[i]<<" "; }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 13
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
cout<<"\n---------------------------------------"; cout<<"\nEnter the element to
be searched \n"; cin>>item; int beg,end,mid,loc=-1; beg=1; end=n; mid=int(beg+e
nd)/2; while((beg<=end) && (loc==-1)) { if (a[mid]==item) { loc=mid; } else if (
item<a[mid]) { end=mid-1; } else { beg=mid+1;} mid=int(beg+end)/2; } if (loc==-1
) cout<<"search unsuccessfull"; else cout<<"search successfull"<<"\t location =
"<<loc; getch(); }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 14
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 15
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
8. WAP to sort given list using bubble sort.
Program : //To sort the elements of linear array using bubble sort #include<iost
ream.h> #include<conio.h> void main() { clrscr(); int a[100],n,i,j,item,temp; co
ut<<"Enter the no.of elements in array "; cin>>n; cout<<"\nEnter the elements \n
"; for(i=1;i<=n;i++) { cin>>a[i]; } for(i=1;i<=n-1;i++) { for(j=1;j<=n-i;j++) {
if (a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } cout<<"\nThe sorte
d array elements are: \n"; for(i=1;i<=n;i++) { cout<<a[i]<<" "; } getch(); }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 16
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 17
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
9. WAP to traverse a linked list.
Program : //To create and traverse a linked list #include<iostream.h> #include<c
onio.h> class node { public: int data; node *next; }; void main() { node *start,
*p; char ch='y'; int j=0; clrscr(); start='\0'; do { if (start=='\0') { start= n
ew node; p=start; } else { p->next = new node; p=p->next; } cout<<"\n Enter data
";
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 18
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
cin>>p->data; j++; cout<<"\n Enter your choice (y/n)? "; cin>>ch; } while(ch=='y
' || ch=='Y'); p->next='\0'; cout<<"\n--------------------------------"; cout<<"
\n Data for nodes "; p=start; while(p!=0) { cout<<"\n"<<p->data; p=p->next; } co
ut<<"\n Number of nodes = "<<j; getch(); } Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 19
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
10. WAP to insert an element in the beginning of linked list.
Program : //To insert an element in the beginning of linked list #include<iostre
am.h> #include<conio.h> class node { public: int data ; node *next; }; /////////
///////////////////////////////////////////////////////////////////// void main(
) { node *start,*p,*beg; char ch; int j=0; clrscr(); start='\0'; do { if(start==
'\0') { start = new node; p= start; } else { p->next=new node;
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 20
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
p=p->next; } cout<<"\nEnter the DATA\t"; cin>>p->data; j++; cout<<"\nEnter your
choice(y/n)?\t"; cin>>ch; }while(ch=='y'||ch=='Y'); p->next='\0'; //////////////
/////////////TRAVERSING A LINKED LIST///////////////////////// cout<<"\nDATA for
nodes is"; p=start; while(p!=0) { cout<<"\t"<<p->data; p=p->next; } cout<<endl;
cout<<"\nNumber of nodes is "<<j; getch(); //////////////////////////INSERTION
AT THE BEGINNING//////////////////////// cout<<"\n-----------------INSERTION AT
THE BEGINNING---------"; beg=new node; cout<<"\nEnter DATA for new node "; cin>>
beg->data; beg->next=start; start=beg;
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 21
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
cout<<"\nDATA for nodes is"; p=start; j=0; while(p!=0) { cout<<"\t"<<p->data; j+
+; p=p->next; } cout<<endl; cout<<"\nNumber of nodes = "<<j; getch(); }
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 22
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
11. WAP to insert an element at given location in linked list.
Program : //Insertion at any location in linked list #include<iostream.h> #inclu
de<conio.h> class node { public: char data; node *next; }; /////////////////////
////////////////////////////////////// void main() { node *start, *p, *current;
char ch; int j=0; clrscr(); start='\0'; do { if(start=='\0') { start=new node; p
=start; } else {
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 23
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
p->next=new node; p=p->next; } cout<<"\nEnter data"; cin>>p->data; j++; cout<<"\
nEnter choice y/n?"; cin>>ch; } while(ch=='Y'||ch=='y'); p->next='\0'; /////////
/////////////////////////Display Data///////////////////////////// cout<<"\nData
for nodes"; p=start; while(p!='\0') { cout<<endl<<p->data; p=p->next; } cout<<e
ndl<<"No. of nodes"<<j; ////////////////////////Insertion of element at any loca
tion/////////////// cout<<"\nInsertion of element at any location"; int loc; int
count=1; cout<<"\nEnter location where you want to enter node"; cin>>loc; if(lo
c<=j)
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 24
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
{
current=new node; cout<<"\nEnter data : "; cin>>current->data;
p=start; while(p!='\0' && count<loc-1) { p=p->next; count++; } current->next=p->
next; p->next=current; cout<<"Data in list is:"<<endl; p=start; while(p!='\0') {
cout<<endl<<p->data; p=p->next; } } else { cout<<"\nEnter valid loction!!"; } g
etch(); }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 25
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 26
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
12. WAP to insert an element at end of linked list.
Program : //To insert an element at end of linked list #include<iostream.h> #inc
lude<conio.h> class node { public: int data ; node *next; }; ///////////////////
/////////////////////////////////////////////////////////// void main() { node *
start,*p,*end; char ch; int j=0; clrscr(); start='\0'; do { if(start=='\0') { st
art = new node; p= start; } else { p->next=new node; p=p->next;
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 27
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
} cout<<"\nEnter the DATA\t"; cin>>p->data; j++; cout<<"\nEnter your choice(y/n)
?\t"; cin>>ch; }while(ch=='y'||ch=='Y'); p->next='\0'; /////////////////////////
//TRAVERSING A LINKED LIST/////////////////////// cout<<"\nDATA for nodes is"; p
=start; while(p!=0) { cout<<"\t"<<p->data; p=p->next; } cout<<endl; cout<<"\nNum
ber of nodes is "<<j; /////////////////////////////INSERTION AT THE END/////////
//////////////////// cout<<"\n---------------INSERTION AT THE END---------------
-"; end=new node; cout<<"\nEnter DATA for new node "; cin>>end->data; p=start; w
hile(p->next!='\0') { p=p->next; } p->next=end;
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 28
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
end->next='\0'; cout<<"\nDATA for nodes is"; p=start; j=0; while(p!=0) { cout<<"
\t"<<p->data; j++; p=p->next; } cout<<endl; cout<<"\nNumber of nodes = "<<j; get
ch(); }
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 29
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
13. WAP to delete an element from the begining of linked list.
Program : //To delete a node from the beginning of the linked list #include<iost
ream.h> #include<conio.h> class node { public: char data; node *next; }; ///////
//////////////////////////////////////////////////////////// void main() { node
*start, *p; char ch; int j=0; clrscr(); start='\0'; do { if(start=='\0') { start
=new node; p=start; } else {
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 30
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
p->next=new node; p=p->next; } cout<<"\nEnter data"; cin>>p->data; j++; cout<<"\
nEnter choice (y/n)?"; cin>>ch; }while(ch=='Y'||ch=='y'); p->next='\0'; ////////
/////////////////////////Display data/////////////////////////////// cout<<"\nDa
ta for nodes"; p=start; while(p!='\0') { cout<<endl<<p->data; p=p->next; } cout<
<endl<<"No. of nodes"<<j; ///////////////////////////////Delete element from beg
ining//////////////// if(start==0) { cout<<"There is no node in the list"; } els
e { p=start;
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 31
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
start=p->next; delete p; cout<<"\nData deleted"; } /////////////////////////////
////Display data/////////////////////////////// cout<<"\nData for nodes after de
letion"; p=start; while(p!='\0') { cout<<endl<<p->data; p=p->next; } getch(); }
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 32
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
14. WAP to delete an element from the end of linked list.
Program : //To delete a node from the end of the linked list #include<iostream.h
> #include<conio.h> class node { public: char data; node *next; }; /////////////
////////////////////////////////////////////////////// void main() { node *start
, *p ,*save; char ch; int j=0; clrscr(); start='\0'; do { if(start=='\0') { star
t=new node; p=start; } else { p->next=new node; p=p->next; } cout<<"\nEnter data
"; cin>>p->data;
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 33
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
j++; cout<<"\nEnter choice (y/n)?"; cin>>ch; }while(ch=='Y'||ch=='y'); p->next='
\0'; /////////////////////////////////Display data//////////////////////////////
/ cout<<"\nData for nodes"; p=start; while(p!='\0') { cout<<endl<<p->data; p=p->
next; } cout<<endl<<"No. of nodes"<<j; ////////////////////Delete a node from th
e end of the linked list//////////// if (start==0) { cout<<"/nList is empty"; }
else if (start->next==0) { delete start; start=0; } else { p=start->next; save=s
tart; while(p->next!=0) { save=p; p=p->next; }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 34
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
save->next=0; delete p; } /////////////////////////////////Display data/////////
////////////////////// cout<<"\nData for nodes after deletion"; p=start; while(p
!='\0') { cout<<endl<<p->data; p=p->next; } getch(); }
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 35
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
15. WAP to delete an element from given location in linked list.
Program : //To delete a node from a given location #include<iostream.h> #include
<conio.h> struct node { int data; node *next; }*p,*start,*save,*loc,*locp; int f
indloc(int); int deleteitem(); /////////////////////////////////////////////////
////////////////// void main() { char ch; int j=0; clrscr(); start='\0'; do { if
(start=='\0') { start=new node; p=start; } else { p->next=new node; p=p->next; }
cout<<"\nEnter data"; cin>>p->data;
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 36
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
j++; cout<<"\nEnter choice (y/n)?"; cin>>ch; }while(ch=='Y'||ch=='y'); p->next='
\0'; /////////////////////////////////Display data//////////////////////////////
/ cout<<"\nData for nodes"; p=start; while(p!='\0') { cout<<endl<<p->data; p=p->
next; } cout<<endl<<"No. of nodes"<<j; /////////////////////////////Delete the g
iven item////////////////////// cout<<"\nEnter the item to be deleted "; int i;
cin>>i; findloc(i); deleteitem(); /////////////////////////////////Display data/
////////////////////////////// cout<<"\nData for nodes after deletion"; p=start;
while(p!='\0') { cout<<endl<<p->data; p=p->next; getch(); }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 37
}
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
/////////////////////////////////findloc func///////////////// int findloc(int d
) { if (start==0) { loc=0; locp=0; return(0); } if (start->data==d) { loc=start;
locp=0; return(0); } save=start; p=start->next; while (p!=0) { if (p->data==d)
{loc=p; locp=save; return(0); } save=p; p=p->next; } loc=0; return(0); }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 38
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
/////////////////////////////////////deleteitem func//////////////// int deletei
tem() { if(loc==0) {cout<<"\nNode not found"; return(0); } else if(locp==0) { st
art=start->next; } else { locp->next=loc->next; } delete loc; }
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 39
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
16. WAP to search an element in linked list.
Program : //To search an element in the linked list #include<iostream.h> #includ
e<conio.h> #include<process.h> class node { public: int data ; node *next; }; //
//////////////////////////////////////////////////////////////////////////// voi
d main() { node *start,*p,*beg,*end,*loc; char ch; int j=0,item; clrscr(); start
='\0'; do { if(start=='\0') { start = new node; p= start; } else { p->next=new n
ode;
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 40
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
p=p->next; } cout<<"\nEnter the DATA\t"; cin>>p->data; j++; cout<<"\nTo continue
enter your choice(y/n)? "; cin>>ch; }while(ch=='y'||ch=='Y'); p->next='\0'; ///
////////////////////////TRAVERSING A LINKED LIST/////////////////////////// cout
<<"\nDATA for nodes is"; p=start; while(p!=0) { cout<<"\t"<<p->data; p=p->next;
} cout<<"\nNumber of nodes is "<<j; ///////////////////////////SEARCHING A LINKE
D LIST//////////////////////////// cout<<"\nEnter the element to searched: "; ci
n>>item; p=start; while(p!=0) { if (item==p->data) { loc=p; cout<<"\nSearch Succ
essful ----------Element found at location: "<<loc; getch(); exit(0);
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 41
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
} else { p=p->next; } } loc='\0'; cout<<"\nSearch Unsuccessful"; getch(); }
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 42
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
17. WAP to implement push and pop in stack.
Program : //To implement stack using Linear array #include<iostream.h> #include<
conio.h> void main() { clrscr(); int stack[10],maxstk,n,item,ch,top,i; char c; c
out<<"\nHow many elements you want to enter: "; cin>>n; if(n!=0) { cout<<"\nEnte
r the elements: "; for(i=0;i<n;i++) { cin>>stack[i]; } } maxstk=9; top=n-1; star
t : cout<<"\n1. PUSH."; cout<<"\n2. POP."; cout<<"\nEnter your choice: "; cin>>c
h; switch(ch) { case 1: if(top==maxstk) { cout<<"\nOVERFLOW ! !"; } else { cout<
<"\nEnter the value for item";
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 43
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
cin>>item; top=top+1; stack[top]=item; cout<<"\nAfter the push operation: "; for
(i=0;i<=top;i++) { cout<<stack[i]<<"\t"; } } cout<<"\nDo you want to continue(Y/
N) : "; cin>>c; if(c=='y'||c=='Y') { goto start; } break; case 2: if(top==-1) {
cout<<"\nUNDERFLOW ! !"; } else { cout<<"\nAfter the pop operation: "; top=top-1
; for(i=0;i<=top;i++) { cout<<stack[i]<<"\t"; } } cout<<"\nDo you want to contin
ue(Y/N) : "; cin>>c; if(c=='y'||c=='Y') { goto start; } } getch(); }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 44
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 45
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
18. WAP for insertion, deletion and traversing a queue.
Program : //To insert, delete and traverse a queue #include<iostream.h> #include
<conio.h> void main() { clrscr(); int queue[10],front=0,rear=0,n,item,ch; char c
hoice; cout<<"\nEnter the size of Queue: "<<endl; cin>>n; do { cout<<"\n1.Insert
ion"; cout<<"\n2.Deletion"; cout<<"\n3.Traversing"; cout<<"\nEnter your choice:
"; cin>>ch; switch(ch) { case 1: if((front==1 && rear==n)||(front==rear+1)) { co
ut<<"\nOverflow ! !"; } else if(front==0) { front=1; rear=1; cout<<"\nInsert the
element: "; cin>>item; queue[rear]=item; }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 46
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
else if(rear==n) { rear=1; cout<<"\nInsert an element"; cin>>item; queue[rear]=i
tem; } else { rear=rear+1; cout<<"\nInsert an element"; cin>>item; queue[rear]=i
tem; } break; case 2: if(front==0) { cout<<"\n Underflow!!!"; } else if(front==r
ear) { front=0; rear=0; } else if(front==n) { front=1; } else { front=front+1; }
break;
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 47
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
case 3: if(front<=rear) { for(int i=front;i<=rear;i++) { cout<<queue[i]<<" "; }
} if(front>rear) { for(int i=front;i<=n;i++) { cout<<queue[i]<<" "; } for(i=1;i<
=rear;i++) { cout<<queue[i]<<endl; } } break; default: cout<<"\nWrong Choice"; }
cout<<"\nDo you wish to continue(Y/N): "; cin>>choice; }while(choice=='y'||choi
ce=='Y'); getch(); }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 48
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
Output :
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 49
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
19.PROGRAM TO PERFORM I-SORT OPERATION
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[10],i,s,tem
p,ptr; cout<<"enter no. of elements in list"<<endl; cin>>s; cout<<"enter "<<s<<"
elements"<<endl; for(i=1;i<=s;i++) { cin>>a[i]; } a[0]=-10; for(i=2;i<=s;i++) {
temp=a[i]; ptr=i-1; while(temp<a[ptr]) { a[ptr+1]=a[ptr]; ptr=ptr-1; } a[ptr+1]
= temp; } cout<<"sorted list is " <<endl;
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 50
Institute of Engineering and Technology CSE-Y Batch 2009 for(i=1;i<=s;i++) { cou
t<<a[i]<<" " ; } getch(); }
Usman Ghani
output:
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 51
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
20.PROGRAM TO FIND THE LENGTH OF STRING WITHOUT USING STRING OPERATIONS
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void
main() { clrscr(); char s[20]; int c=0,i=0; cout<<"enter the string"<<endl; get
s(s); while(s[i]!='\0') { c++; i++; }
cout<<"length of string is"<<c; getch(); }
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 52
Institute of Engineering and Technology CSE-Y Batch 2009
Usman Ghani
OUTPUT
INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL
Page 53