Top Banner
Index S.N o. Name of Program Pg. No Remarks 1. WAP for one dimensional (1D) array. 1-1 2. WAP for two dimensional (2D) array. 2-2 3. WAP for linear search. 3-5 4. WAP for binary search. 6-7 5. WAP for insertion. 8-9 6. WAP for deletion. 10-11 7. WAP for sorting. 12-13 8. WAP which can perform all the operations of array. 14-18 9. WAP to demonstrate the use of stack (linear array) in converting arithmetic expression from infix notation to postfix notation. 19-24 10. WAP to demonstrate various operations on linear queue using linear array. 25-27 11. WAP to demonstrate various operations on circular queue using linear array. 28-32 12. WAP to sort array of integers using bubble sort. 33-34 13. WAP to sort array of integers using insertion sort. 35-36 14. WAP to sort array of integers using merge sort. 37-39 15. WAP to sort array of integers using quick sort. 40-41 16. WAP to traverse a linked list. 42-43 17. WAP to insert an element in a linked list. 44-47 18. WAP to delete an element in a linked list. 48-51 19. WAP to search an element in a linked list. 52-54
70
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

Index

S.No. Name of Program Pg. No Remarks

1. WAP for one dimensional (1D) array. 1-1

2. WAP for two dimensional (2D) array. 2-2

3. WAP for linear search. 3-5

4. WAP for binary search. 6-7

5. WAP for insertion. 8-9

6. WAP for deletion. 10-11

7. WAP for sorting. 12-13

8. WAP which can perform all the operations of array. 14-18

9.WAP to demonstrate the use of stack (linear array) in converting arithmetic expression from infix notation to postfix notation.

19-24

10. WAP to demonstrate various operations on linear queue using linear array. 25-27

11. WAP to demonstrate various operations on circular queue using linear array. 28-32

12. WAP to sort array of integers using bubble sort. 33-34

13. WAP to sort array of integers using insertion sort. 35-36

14. WAP to sort array of integers using merge sort. 37-39

15. WAP to sort array of integers using quick sort. 40-41

16. WAP to traverse a linked list. 42-43

17. WAP to insert an element in a linked list. 44-47

18. WAP to delete an element in a linked list. 48-51

19. WAP to search an element in a linked list. 52-54

20. WAP of shell sort. 55-57

Page 2: Data Structure

12

DSPM Lab BTCS-306

1. WAP for one dimensional (1D) array.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int a[10],n,i;

cout<<"Enter the no.elements in array"<<endl;

cin>>n;

cout<<"Enter diffrent array's element\n";

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

{

cin>>a[i];

}

cout<<"\nArray elements are:-\n";

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

cout<<a[i];

getch();

}

Page 3: Data Structure

12

DSPM Lab BTCS-306

2. WAP for two dimensional(2D) array.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int a[10][10],n,m,i,j;

cout<<"\nEnter the no. of rows and columns of matrix:\n ";

cin>>m>>n;

cout<<"\nEnter the array element\n";

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

{

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

{

cin>>a[i][j];

}

}

cout<<"\nEntered matrix is";

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

{

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

{

cout<<"\t"<<a[i][j];

}

cout<<"\n";

Page 4: Data Structure

12

DSPM Lab BTCS-306

}

getch();

}

Page 5: Data Structure

12

DSPM Lab BTCS-306

3. WAP for linear search.

#include<iostream.h>

#include<conio.h>

int Lsearch(int[],int,int);

void main()

{

int ar[50],item,n,index;

cout<<"Enter the array";

cin>>n;

cout<<"\nEnter array element\n";

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

{

cin>>ar[i];

}

cout<<"\nEnter element to be searched for..";

cin>>item;

index=Lsearch(ar,n,item);

if(index==-1)

cout<<"given element could not found";

else

cout<<"Element found at index: "<<index<<"position: "<<index+1<<endl;

}

int Lsearch(int ar[],int size,int item)

{

for(int i=0;i<size;i++)

Page 6: Data Structure

12

DSPM Lab BTCS-306

{

if(ar[i]==item)

return i;

}

return-1;

}

Page 7: Data Structure

12

DSPM Lab BTCS-306

4. WAP for binary search.

#include<iostream.h>

#include<conio.h>

int Bsearch(int[],int,int);

void main()

{

int ar[50],item,n,index;

cout<<"enter the size of array";

cin>>n;

cout<<"\nEnter the array's element(must be sorted in asc order)\n";

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

cin>>ar[i];

cout<<"\nEnter element to be search for...";

cin>>item;

index=Bsearch(ar,n,item);

if(index==-1)

cout<<"\ngiven element could not be found\n";

else

cout<<"\nElement found at index:"<<index<<",position:"<<index+1<<endl;

}

int Bsearch(int ar[],int size,int item)

{

int beg,last,mid;

beg=0; last=size-1;

while(beg<=last)

Page 8: Data Structure

12

DSPM Lab BTCS-306

{

mid=(beg+last)/2;

if(item==ar[mid]) return mid;

else if(item>ar[mid]) beg=mid+1;

else last=mid-1;

}

return-1;

}

Page 9: Data Structure

12

DSPM Lab BTCS-306

5. WAP for insertion.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int a[20],n,k,i,item;

void insert(int a[],int n,int k,int item);

cout<<"Enter the no.of element";

cin>>n;

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

{

cout<<"Enter the element a["<<i<<"]=";

cin>>a[i];

}

cout<<"Enter item to be inserted";

cin>>item;

cout<<"Enter index position for the new item";

cin>>k;

insert(a,k,n,item);

cout<<"Enter element after insertion\n";

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

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

getch();

}

Page 10: Data Structure

12

DSPM Lab BTCS-306

void insert(int a[],int n,int k,int item)

{

int i=n-1;

while(i>=k)

{

a[i+1]=a[i];

i=i-1;

}

a[k]=item;

n=n+1;

}

Page 11: Data Structure

12

DSPM Lab BTCS-306

6. WAP for deletion.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int a[20],n,k,item;

void delete1(int a[],int&n,int k);

cout<<"Enter no. of element";

cin>>n;

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

{

cout<<"Enter element a["<<i<<"]=";

cin>>a[i];

}

cout<<"Enter the postion of element we want to delete";

cin>>k;

delete1(a,n,k-1);

cout<<"\nElement after deletion\n";

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

cout<<a[i];

getch();

}

void delete1(int a[],int&n,int i)

{

Page 12: Data Structure

12

DSPM Lab BTCS-306

for(int k;i<=n-2;i++)

a[i]=a[i+1];

n=n-1;

}

Page 13: Data Structure

12

DSPM Lab BTCS-306

7. WAP for sorting.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int a[20],n,i;

void sort(int a[],int n);

cout<<"\nEnterno.of element\n";

cin>>n;

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

{

cout<<"Enter element a["<<i<<"]=";

cin>>a[i];

}

sort(a,n);

cout<<"sorted element..";

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

{

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

}

getch();

}

void sort(int a[],int n)

{

Page 14: Data Structure

12

DSPM Lab BTCS-306

int temp;

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

{

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

{

if(a[j]>a[j+1])

{

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}

}

Page 15: Data Structure

12

DSPM Lab BTCS-306

8. WAP which can perform all the operations of array.

#include<iostream.h>

#include<conio.h>

void main()

{clrscr();

int a[20], n, i, x;

charcont;

void Search(int[],int);

void Insert(int[],int);

void Delete(int[],int);

void Sort(int[],int);

cout<<"Enter the limit-";

cin>>n;

cout<<"Enter the elements-";

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

cin>>a[i];

CHOICE:

clrscr();

cout<<"\nArray:-";

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

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

cout<<"\nEnter your choice\n";

cout<<"1.Search \n2.Insert \n3.Delete \n4.Sort \n";

cin>>x;

switch(x)

Page 16: Data Structure

12

DSPM Lab BTCS-306

{case 1: Search(a,n);

break;

case 2: Insert(a,n);

n++;

break;

case 3: Delete(a,n);

n--;

break;

case 4: Sort(a,n);

break;

default:cout<<"\nEnter correct choice\n";

goto CHOICE;

}

cout<<"\nDo you want to continue..??";

cout<<"\nIf yes press Y/y other press any key-";

cin>>cont;

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

goto CHOICE;

getch();

}

void Search(int a[], int n)

{int i, ser, flag=0;

cout<<"Enter the number to be searched-";

cin>>ser;

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

Page 17: Data Structure

12

DSPM Lab BTCS-306

{if(a[i]==ser)

{cout<<ser<<" found at position "<<i+1<<endl;

flag=1;

}

}

if(flag==0)

cout<<ser<<"not found";

}

void Insert(int a[], int n)

{intpos, item, i;

cout<<"Enter the postion on which you want to insert element-";

cin>>pos;

pos--;

cout<<"Enter the element to be inserted-";

cin>>item;

cout<<"Array before inserting new element:-\n";

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

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

for(i=n;i>=pos;i--)

a[i+1]=a[i];

a[pos]=item;

n++;

cout<<"\nArray after inserting new element:-\n";

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

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

Page 18: Data Structure

12

DSPM Lab BTCS-306

}

void Delete(int a[], int n)

{intpos,i;

cout<<"Enter the position you want to delete-";

cin>>pos;

pos--;

cout<<"Array before deleting element:-\n";

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

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

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

a[i]=a[i+1];

n--;

cout<<"\nArray after deleting element:-\n";

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

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

}

void Sort(int a[], int n)

{int temp, i, j;

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

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

{if(a[j]>a[j+1])

{temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

Page 19: Data Structure

12

DSPM Lab BTCS-306

}

}

cout<<"\nArray sorted in assending order:-\n";

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

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

}

Page 20: Data Structure

12

DSPM Lab BTCS-306

9. WAP to demonstrate the use of stack (linear array) in converting arithmetic expression from infix notation to postfix notation.

#include<iostream.h>

#include<string.h>

#include<stdlib.h>

#include<ctype.h>

#include<conio.h>

class expression

{

private:

char infix[100];

char stack[200];

int top;

int r;

char postfix[100];

public:

void convert();

intinput_p(char);

intstack_p(char);

int rank(char);

};

int expression::input_p(char c)

{

if(c=='+' || c=='-')

Page 21: Data Structure

12

DSPM Lab BTCS-306

return 1;

else if(c=='*' || c=='/')

return 3;

else if(c=='^')

return 6;

else if(isalpha(c)!=0)

return 7;

else if(c=='(')

return 9;

else if(c==')')

return 0;

else

{

cout<<"Invalid expression ::input error\n";

exit(0);

}

}

int expression::stack_p(char c)

{

if(c=='+' || c=='-')

return 2;

else if(c=='*' || c=='/')

return 4;

else if(c=='^')

Page 22: Data Structure

12

DSPM Lab BTCS-306

return 5;

else if(isalpha(c)!=0)

return 8;

else if(c=='(')

return 0;

else

{

cout<<"Invalid expression ::stack error\n";

exit(0);

}

}

int expression::rank(char c)

{

if(c=='+' || c=='-')

return -1;

else if(c=='*' || c=='/')

return -1;

else if(c=='^')

return -1;

else if(isalpha(c)!=0)

return 1;

else

{

cout<<"Invalid expression ::in rank\n";

Page 23: Data Structure

12

DSPM Lab BTCS-306

exit(0);

}

}

void expression::convert()

{

cout<<"Enter an infix expression ::\n";

cin>>infix;

int l=strlen(infix);

infix[l]=')';

infix[l+1]=' ';

//Convertion starts

top=1;

stack[top]='(';

r=0;

int x=-1;

int i=0;

char next=infix[i];

while(next!=' ')

{

Page 24: Data Structure

12

DSPM Lab BTCS-306

//Pop all the elements to outputin stack which have higher precedence

while(input_p(next) <stack_p(stack[top]) )

{

if(top<1)

{

cout<<"invalid expression ::stack error\n";

exit(0);

}

postfix[++x]=stack[top];

top--;

r=r+rank(postfix[x]);

if(r<1)

{

cout<<"Invalid expression ::r<1\n";

exit(0);

}

}

if(input_p( next ) != stack_p( stack[top]))

stack[++top]=next;

else

top--;

Page 25: Data Structure

12

DSPM Lab BTCS-306

i++;

next=infix[i];

}

postfix[++x]=' ';

if(r!=1 || top!=0)

{

cout<<"Invalid expression ::error in rank or stack\n";

exit(0);

}

cout<<"\n\nThe corresponding postfix expression is ::\n";

cout<<postfix<<endl;

}

void main()

{

clrscr();

expressionobj;

obj.convert();

Page 26: Data Structure

12

DSPM Lab BTCS-306

getch();

}

10.WAP to demonstrate various operations on linear queue using linear array.

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

class queue

{

int queue1[5];

intrear,front;

public:

queue()

{

rear=-1;

front=-1;

}

void insert(int x)

{

if(rear > 4)

Page 27: Data Structure

12

DSPM Lab BTCS-306

{

cout<<"queue over flow";

front=rear=-1;

return;

}

queue1[++rear]=x;

cout<<"inserted" <<x;

}

voiddelet()

{

if(front==rear)

{

cout<<"queue under flow";

return;

}

cout<<"deleted" <<queue1[++front];

}

void display()

{

if(rear==front)

{

cout<<" queue empty";

return;

}

for(int i=front+1;i<=rear;i++)

Page 28: Data Structure

12

DSPM Lab BTCS-306

cout<<queue1[i]<<" ";

}

};

void main()

{

clrscr();

intch;

queuequ;

while(1)

{

cout<<"\n1.insert 2.delet 3.display 4.exit\nEnterur choice";

cin>>ch;

switch(ch)

{

case 1: cout<<"enter the element";

cin>>ch;

qu.insert(ch);

break;

case 2: qu.delet(); break;

case 3: qu.display();break;

case 4: exit(0);

}

}

Page 29: Data Structure

12

DSPM Lab BTCS-306

getch;

}

11.WAP to demonstrate various operations on circular queue using linear array.

#include<iostream.h>

#include<conio.h>

constint MAX = 5;

Page 30: Data Structure

12

DSPM Lab BTCS-306

classcqueue

{

int a[MAX],front,rear;

public :

cqueue()

{

front=rear=-1;

}

void insert(int );

int deletion();

void display();

};

voidcqueue :: insert(intval)

{

if((front==0 && rear==MAX-1) || (rear+1==front))

cout<<" Circular Queue is Full!";

else

{

if(rear==MAX-1)

rear=0;

else

rear++;

a[rear]=val;

}

Page 31: Data Structure

12

DSPM Lab BTCS-306

if(front==-1)

front=0;

}

intcqueue :: deletion()

{

int k;

if(front==-1)

cout<<"Circular Queue is Empty!";

else

{

k=a[front];

if(front==rear)

front=rear=-1;

else

{

if(front==MAX-1)

front=0;

else

front++;

}

}

return k;

}

voidcqueue :: display()

{

Page 32: Data Structure

12

DSPM Lab BTCS-306

int i;

if(front==-1)

cout<<"Circular Queue is Empty!";

else

{

if(rear < front)

{

for(i=front;i<=MAX-1;i++)

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

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

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

}

else

{

for(i=front;i<=rear;i++)

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

cout<<endl;

}

}

}

void main()

{

cqueue c1;

Page 33: Data Structure

12

DSPM Lab BTCS-306

intch,val;

char op;

do {

clrscr();

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

cout<<"1.Insertion \n2.Deletion\n3.Display\n";

cout<<"\nEnter Your Choice:";

cin>>ch;

switch(ch)

{

case 1 : cout<<"Enter Element to Insert :";

cin>>val;

c1.insert(val);

cout<<"Inserted Element :"<<val<<endl;

break;

case 2 : val=c1.deletion();

cout<<"Deleted Element :"<<val<<endl;

break;

case 3 : c1.display();

break;

}

cout<<"Do you want to continue<Y/N>:";

cin>>op;

}

Page 34: Data Structure

12

DSPM Lab BTCS-306

while(op=='Y' || op=='y');

getch();

}

12.WAP to sort array of integers using bubble sort.

#include<iostream.h>

Page 35: Data Structure

12

DSPM Lab BTCS-306

#include<conio.h>

void main(){

clrscr();

inti,k,j,n,t,a[20];

cout<<"Enter number of elements:";

cin>>n;

cout<<"Enter unsorted elements\n";

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

cin>>a[i];

}

cout<<"Elements after sorting:";

for(k=0;k<n-1;k++){

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

if(a[j]>a[j+1]){

int t=a[j];

a[j]=a[j+1];

a[j+1]=t;

}

}

}

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

cout<<" "<<a[j]<<" ";

}

getch();

}

Page 36: Data Structure

12

DSPM Lab BTCS-306

Page 37: Data Structure

12

DSPM Lab BTCS-306

13.WAP to sort array of integers using insertion sort.

#include<iostream.h>

#include<conio.h>

void main(){

clrscr();

inti,k,j,n,t,a[20];

cout<<"Enter number of elements:";

cin>>n;

cout<<"Enter unsorted elements:\n";

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

cin>>a[i];

}

for(k=1;k<n;k++){

t=a[k];

j=k-1;

while((j>=0)&&(t<a[j]))

{

a[j+1]=a[j];

j--;

}

a[j+1]=t;

}

cout<<"Elements after sorting\n";

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

cout<<a[j]<<" ";

Page 38: Data Structure

12

DSPM Lab BTCS-306

}

getch();

}

Page 39: Data Structure

12

DSPM Lab BTCS-306

14.WAP to sort array of integers using merge sort.

#include<iostream.h>

#include<conio.h>

voidmergesort(int *,int,int);

void merge(int *,int,int,int);

int a[20],i,n,b[20];

void main()

{

clrscr();

cout<<"Enter no of elements:";

cin>> n;

cout<<"Enter elements:";

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

cin>> a[i];

mergesort(a,0,n-1);

cout<<"\nNumbers after sort:";

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

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

getch();

}

voidmergesort(int a[],inti,int j)

{

Page 40: Data Structure

12

DSPM Lab BTCS-306

int mid;

if(i<j)

{

mid=(i+j)/2;

mergesort(a,i,mid);

mergesort(a,mid+1,j);

merge(a,i,mid,j);

}

}

void merge(int a[],intlow,int mid ,int high)

{

inth,i,j,k;

h=low;

i=low;

j=mid+1;

while(h<=mid && j<=high)

{

if(a[h]<=a[j])

b[i]=a[h++];

else

b[i]=a[j++];

i++;

}

if( h > mid)

Page 41: Data Structure

12

DSPM Lab BTCS-306

for(k=j;k<=high;k++)

b[i++]=a[k];

else

for(k=h;k<=mid;k++)

b[i++]=a[k];

cout<<"\n";

for(k=low;k<=high;k++)

{ a[k]=b[k];

cout<< a[k] <<" ";

}

}

Page 42: Data Structure

12

DSPM Lab BTCS-306

15.WAP to sort array of integers using quick sort.

#include<iostream.h>

#include<conio.h>

int a[10],l,u,i,j;

void quick(int *,int,int);

void main()

{

clrscr();

cout<<"Enter 10 elements:\n";

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

cin>> a[i];

l=0;

u=9;

quick(a,l,u);

cout<<"\nSorted elements:";

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

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

getch();

}

void quick(int a[],intl,int u)

{

intp,temp;

if(l<u)

Page 43: Data Structure

12

DSPM Lab BTCS-306

{

p=a[l];

i=l;

j=u;

while(i<j)

{

while(a[i] <= p && i<j )

i++;

while(a[j]>p && i<=j )

j--;

if(i<=j)

{

temp=a[i];

a[i]=a[j];

a[j]=temp;}

}

temp=a[j];

a[j]=a[l];

a[l]=temp;

cout<<"\n";

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

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

quick(a,l,j-1);

quick(a,j+1,u);

}}

Page 44: Data Structure

12

DSPM Lab BTCS-306

16.WAP to traverse a linked list.

#include<iostream.h>

#include<conio.h>

class node

{ public:

int data;

node *next;

};

void main()

{

node *start,*p;

charch='y';

int j=0;

clrscr();

start='\0';

do

{ if (start=='\0')

{ start= new node;

p=start;

}

else

{ p->next = new node;

p=p->next;

}

Page 45: Data Structure

12

DSPM Lab BTCS-306

cout<<"\n Enter data: ";

cin>>p->data;

j++;

cout<<"\n Enter again (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();

}

Page 46: Data Structure

12

DSPM Lab BTCS-306

17.WAP to insert an element in a linked list.

#include<iostream.h>

#include<conio.h>

class node

{

public:

char data;

node *next;

};

void main()

{

node *start, *p, *current;

charch;

int j=0;

clrscr();

start='\0';

do

{

if(start=='\0')

{

start=new node;

p=start;

}

else

Page 47: Data Structure

12

DSPM Lab BTCS-306

{

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

cout<<"\nData for nodes";

p=start;

while(p!='\0')

{

cout<<endl<<p->data;

p=p->next;

}

cout<<endl<<"No. of nodes"<<j;

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

intloc;

int count=1;

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

cin>>loc;

Page 48: Data Structure

12

DSPM Lab BTCS-306

if(loc<=j)

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

getch();

}

Page 49: Data Structure

12

DSPM Lab BTCS-306

Page 50: Data Structure

12

DSPM Lab BTCS-306

18.WAP to delete an element in a linked list.

#include<iostream.h>

#include<conio.h>

struct node

{ int data;

node *next;

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

intfindloc(int);

intdeleteitem();

void main()

{ charch;

int j=0;

clrscr();

start='\0';

do

{ if(start=='\0')

{ start=new node;

p=start;

}

else

{ p->next=new node;

p=p->next;

}

cout<<"Enter data:";

Page 51: Data Structure

12

DSPM Lab BTCS-306

cin>>p->data;

j++;

cout<<"Enter again (y/n)?";

cin>>ch;

}while(ch=='Y'||ch=='y');

p->next='\0';

cout<<"Data for nodes:";

p=start;

while(p!='\0')

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

p=p->next;

}

cout<<endl<<"No. of nodes:"<<j;

cout<<"\nEnter the item to be deleted:";

int i;

cin>>i;

findloc(i);

deleteitem();

cout<<"Data for nodes after deletion:";

p=start;

while(p!='\0')

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

p=p->next; }

getch();

}

Page 52: Data Structure

12

DSPM Lab BTCS-306

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

Page 53: Data Structure

12

DSPM Lab BTCS-306

}

intdeleteitem()

{ if(loc==0)

{cout<<"\nNode not found!";

return(0); }

else if(locp==0)

{ start=start->next; }

else

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

deleteloc;

}

Page 54: Data Structure

12

DSPM Lab BTCS-306

19.WAP to search an element in a linked list.

#include<iostream.h>

#include<conio.h>

#include<process.h>

class node

{

public:

int data ;

node *next;

};

void main()

{ node *start,*p,*beg,*end,*loc;

charch;

int j=0,item;

clrscr();

start='\0';

do

{

if(start=='\0')

{ start = new node;

p= start;

}

else

{ p->next=new node;

Page 55: Data Structure

12

DSPM Lab BTCS-306

p=p->next;

}

cout<<"Enter data:";

cin>>p->data;

j++;

cout<<"Continue to enter data(y/n)? ";

cin>>ch;

}while(ch=='y'||ch=='Y');

p->next='\0';

cout<<"data for nodes:";

p=start;

while(p!=0)

{

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

p=p->next;

}

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

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

cin>>item;

p=start;

while(p!=0)

{ if (item==p->data)

{ loc=p;

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

getch();

Page 56: Data Structure

12

DSPM Lab BTCS-306

exit(0);

}

else

{ p=p->next; }

}

loc='\0';

cout<<"\nSearch Unsuccessful";

getch();

}

Page 57: Data Structure

12

DSPM Lab BTCS-306

Page 58: Data Structure

12

DSPM Lab BTCS-306

20.WAP of shell sort.

#include<iostream.h>

#include<conio.h>

void read(int a[10],int n)

{

cout<<"Enter elements:\n";

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

cin>>a[i];

}

void display(int a[10],int n)

{

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

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

}

voidshellsort(int a[10],int n)

{

int gap=n/2;

do

{

int swap;

do

{

swap=0;

Page 59: Data Structure

12

DSPM Lab BTCS-306

for(int i=0;i<n-gap;i++)

if(a[i]>a[i+gap])

{

int t=a[i];

a[i]=a[i+gap];

a[i+gap]=t;

swap=1;

}

}

while(swap);

}

while(gap=gap/2);

}

void main()

{

int a[10];

int n;

clrscr();

cout<<"Enter number of elements:";

cin>>n;

read(a,n);

cout<<"Before sorting\n";

display(a,n);

shellsort(a,n);

cout<<"\nAfter sorting\n";

Page 60: Data Structure

12

DSPM Lab BTCS-306

display(a,n);

getch();

}