data structure and programming methodology

Post on 25-Nov-2014

15 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

DATA STRUCTURES AND PROGRAMMING METHOLOGY FILE CONTAINING ALL THE PROGRAMMES ON THE BASIS OF ALGORITHMS,QUICK SORT,LINEAR SEARCH,BINARY SEARCH ETC.

TRANSCRIPT

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>

void main()

{ clrscr();

int a[5],n,i,sum=0;

cout<<"\nEnter the size of array: ";

cin>>n;

cout<<"\nEnter the elements of array: ";

for(i=0;i<n;i++)

{ cin>>a[i];

sum+=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<iostream.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 beginning: ";

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

for(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 no.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;

cout<<"\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<iostream.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;

while(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++)

{ cout<<a[i]<<" ";

}

cout<<"\n------------------------------------------------------";

n=n-1;

cout<<"\nThe array elements after deleting the last element are \n";

for(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 :

6. WAP to implement linear search in linear array.

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 10

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

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

}

Output :

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 11

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

7. WAP to implement a binary search.

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 12

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

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

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<<"\nSorted array: ";

for(i=1;i<=n;i++)

{ cout<<a[i]<<" "; }

cout<<"\n---------------------------------------";

cout<<"\nEnter the element to be searched \n";

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 13

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

cin>>item;

int beg,end,mid,loc=-1;

beg=1;

end=n;

mid=int(beg+end)/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();

}

Output :

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 14

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

8. WAP to sort given list using bubble sort.

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 15

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

Program : //To sort the elements of linear array using bubble sort

#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<<"\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 sorted array elements are: \n";

for(i=1;i<=n;i++)

{ cout<<a[i]<<" ";

}

getch(); }

Output :

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 16

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

9. WAP to traverse a linked list.

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 17

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

Program : //To create and traverse a linked list

#include<iostream.h>

#include<conio.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= new node;

p=start;

}

else

{ p->next = new node;

p=p->next;

}

cout<<"\n Enter data ";

cin>>p->data;

j++;

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 18

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

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;

}

cout<<"\n Number of nodes = "<<j;

getch();

}

Output :

10. WAP to insert an element in the beginning of linked list.

Program : //To insert an element in the beginning of linked list

#include<iostream.h>

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 19

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

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

p=p->next;

}

cout<<"\nEnter the DATA\t";

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 20

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

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;

cout<<"\nDATA for nodes is";

p=start;

j=0;

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 21

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

while(p!=0)

{

cout<<"\t"<<p->data;

j++;

p=p->next;

}

cout<<endl;

cout<<"\nNumber of nodes = "<<j;

getch();

}

Output :

11. WAP to insert an element at given location in linked list.

Program : //Insertion at any location in linked list

#include<iostream.h>

#include<conio.h>

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 22

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

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

{

p->next=new node;

p=p->next;

}

cout<<"\nEnter data";

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 23

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

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<<endl<<"No. of nodes"<<j;

////////////////////////Insertion of element at any location///////////////

cout<<"\nInsertion of element at any location";

int loc;

int count=1;

cout<<"\nEnter location where you want to enter node";

cin>>loc;

if(loc<=j)

{ current=new node;

cout<<"\nEnter data : ";

cin>>current->data;

p=start;

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 24

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

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!!"; }

getch();

}

Output :

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 25

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

12. WAP to insert an element at end of linked list.

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 26

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

Program : //To insert an element at end of linked list

#include<iostream.h>

#include<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')

{ start = 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<<"\nNumber 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;

while(p->next!='\0')

{ p=p->next; }

p->next=end;

end->next='\0';

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 28

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 :

13. WAP to delete an element from the begining of linked list.

Program : //To delete a node from the beginning of the linked list

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 29

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

#include<iostream.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

{

p->next=new node;

p=p->next;

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 30

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

}

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<<endl<<"No. of nodes"<<j;

///////////////////////////////Delete element from begining////////////////

if(start==0)

{

cout<<"There is no node in the list";

}

else

{

p=start;

start=p->next;

delete p;

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 31

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

cout<<"\nData deleted";

}

/////////////////////////////////Display data///////////////////////////////

cout<<"\nData for nodes after deletion";

p=start;

while(p!='\0')

{

cout<<endl<<p->data;

p=p->next;

}

getch();

}

Output :

14. WAP to delete an element from the end of linked list.

Program : //To delete a node from the end of the linked list

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 32

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

#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')

{ start=new node;

p=start;

}

else

{ p->next=new node;

p=p->next;

}

cout<<"\nEnter data";

cin>>p->data;

j++;

cout<<"\nEnter choice (y/n)?";

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 33

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

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 the 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=start;

while(p->next!=0)

{ save=p;

p=p->next; }

save->next=0;

delete p;

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 34

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

}

/////////////////////////////////Display data///////////////////////////////

cout<<"\nData for nodes after deletion";

p=start;

while(p!='\0')

{ cout<<endl<<p->data;

p=p->next; }

getch();

}

Output :

15. WAP to delete an element from given location in linked list.

Program : //To delete a node from a given location

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 35

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

#include<iostream.h>

#include<conio.h>

struct node

{ int data;

node *next;

}*p,*start,*save,*loc,*locp;

int findloc(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;

j++;

cout<<"\nEnter choice (y/n)?";

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 36

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

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

}

/////////////////////////////////findloc func/////////////////

int findloc(int d)

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 37

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

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

}

/////////////////////////////////////deleteitem func////////////////

int deleteitem()

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 38

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

{ if(loc==0)

{cout<<"\nNode not found";

return(0); }

else if(locp==0)

{ start=start->next; }

else

{ locp->next=loc->next; }

delete loc;

}

Output :

16. WAP to search an element in linked list.

Program : //To search an element in the linked list

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 39

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

#include<iostream.h>

#include<conio.h>

#include<process.h>

class node

{

public:

int data ;

node *next;

};

//////////////////////////////////////////////////////////////////////////////

void 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 node;

p=p->next;

}

cout<<"\nEnter the DATA\t";

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 40

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

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 LINKED LIST////////////////////////////

cout<<"\nEnter the element to searched: ";

cin>>item;

p=start;

while(p!=0)

{ if (item==p->data)

{ loc=p;

cout<<"\nSearch Successful ----------Element found at location: "<<loc;

getch();

exit(0);

}

else

{ p=p->next; }

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 41

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

}

loc='\0';

cout<<"\nSearch Unsuccessful";

getch();

}

Output :

17. WAP to implement push and pop in stack.

Program : //To implement stack using Linear array

#include<iostream.h>

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 42

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

#include<conio.h>

void main()

{ clrscr();

int stack[10],maxstk,n,item,ch,top,i;

char c;

cout<<"\nHow many elements you want to enter: ";

cin>>n;

if(n!=0)

{ cout<<"\nEnter the elements: ";

for(i=0;i<n;i++)

{ cin>>stack[i]; } }

maxstk=9;

top=n-1;

start :

cout<<"\n1. PUSH.";

cout<<"\n2. POP.";

cout<<"\nEnter your choice: ";

cin>>ch;

switch(ch)

{ case 1: if(top==maxstk)

{ cout<<"\nOVERFLOW ! !"; }

else

{ cout<<"\nEnter the value for item";

cin>>item;

top=top+1;

stack[top]=item;

cout<<"\nAfter the push operation: ";

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 43

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

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 continue(Y/N) : ";

cin>>c;

if(c=='y'||c=='Y')

{ goto start; } }

getch(); }

Output :

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 44

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

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 45

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

#include<iostream.h>

#include<conio.h>

void main()

{ clrscr();

int queue[10],front=0,rear=0,n,item,ch;

char choice;

cout<<"\nEnter the size of Queue: "<<endl;

cin>>n;

do

{ cout<<"\n1.Insertion";

cout<<"\n2.Deletion";

cout<<"\n3.Traversing";

cout<<"\nEnter your choice: ";

cin>>ch;

switch(ch)

{ case 1:

if((front==1 && rear==n)||(front==rear+1))

{ cout<<"\nOverflow ! !"; }

else if(front==0)

{ front=1;

rear=1;

cout<<"\nInsert the element: ";

cin>>item;

queue[rear]=item;

}

else if(rear==n)

{ rear=1;

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 46

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

cout<<"\nInsert an element";

cin>>item;

queue[rear]=item;

}

else

{ rear=rear+1;

cout<<"\nInsert an element";

cin>>item;

queue[rear]=item;

}

break;

case 2:

if(front==0)

{ cout<<"\n Underflow!!!"; }

else if(front==rear)

{ front=0;

rear=0;

}

else if(front==n)

{ front=1;

}

else

{ front=front+1;

}

break;

case 3:

if(front<=rear)

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 47

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

{ 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'||choice=='Y');

getch();

}

Output :

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 48

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

19.PROGRAM TO PERFORM I-SORT OPERATION

#include<iostream.h>

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 49

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

#include<conio.h>

void main()

{

clrscr();

int a[10],i,s,temp,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;

for(i=1;i<=s;i++)

{

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 50

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

cout<<a[i]<<" " ;

}

getch();

}

output:

20.PROGRAM TO FIND THE LENGTH OF STRING WITHOUT USING STRING OPERATIONS

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 51

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

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

gets(s);

while(s[i]!='\0')

{

c++;

i++;

}

cout<<"length of string is"<<c;

getch();

}

OUTPUT

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 52

Institute of Engineering and Technology CSE-Y Batch 2009 Usman Ghani

INSTITUTE OF ENGINEERING AND TECHNOLOGY, BHADDAL Page 53

top related