DS Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 99

Program 1

Q) To insert an element into an array

#include<iostream.h>
#include<conio.h> int i;
class insert
{
int size,a[33],pos,ele;public:
void oper();
};
void insert::oper()
{
cout<<"\nEnter size:";
cin>>size; cout<<"\nEnter
array:";for(i=0;i<size;i++)
cin>>a[i];
cout<<"\nEnter insert element and position:";cin>>ele>>pos;
int t,p=pos-1,s=size;if(p>=0
&& p<=size)
{
while(s>p)
{
a[s]=a[s-1];s--;
}
a[p]=ele;
cout<<"\nNew array:\n";
for(i=0;i<size+1;i++)
{
cout<<a[i]<<" ";
}
}
else
cout<<"\nInvalid range";

void main()
{
clrscr(); insert o1;
o1.oper();
getch();
}

OUTPUT

Enter size:4
Enter array:11 22 44 55
Enter insert element and position:33 3
New array:
11 22 33 44 55
Program 2

Q) To delete an element from an array

#include<iostream.h>
#include<conio.h>
class Del
{
int arr[50],size,i,pos,del;
public:
void arrange();
};
void Del::arrange()
{
cout<<"\n enter the array size:";
cin>>size;
cout<<"\n enter the elements:";
for(i=0;i<size;i++)
{
cin>>arr[i];
}
cout<<"\n enter the element to delete:";
cin>>del;
for(i=0;i<size;i++)
{
if(arr[i]==del)
{
pos=i;
break;
}

}
if(i==size)
cout<<"\nElement not found";
else
{
for(i=pos;i<size;i++)
arr[i]=arr[i+1];

size--;
cout<<"\nNew array:";
for(i=0;i<size;i++)
cout<<"\n"<<arr[i];
}

void main()
{
Del d;
clrscr();
d.arrange();
getch();

OUTPUT

enter the array size:4


enter the elements:11 22 33 44
enter the element to delete:22
New array:
11
33
44
Program 3

Q) To add polynomials

#include<iostream.h>
#include<conio.h>

int i,j;

class polynomial
{
int a[36],degree;
public:
void enter();
void print();
void add(polynomial,polynomial);
};

void polynomial::enter()
{
cout<<"\n\nEnter degree: ";
cin>>degree;
for(i=0;i<degree;i++)
{
cout<<"\nEnter term with power "<<i<<" :";
cin>>a[i];

}
}

void polynomial::print()
{
i=0;
cout<<"\n"<<a[i]<<"x^"<<i;
for(i=1;i<degree;i++)
{
cout<<" + "<<a[i]<<"x^"<<i;
}
}

void polynomial::add(polynomial p1,polynomial p2)


{

if(p1.degree>p2.degree)
{
degree=p1.degree;
for(i=0;i<p1.degree;i++)
a[i]=p1.a[i];

for(i=0;i<p2.degree;i++)
a[i]+=p2.a[i] ;
}
else
{
degree=p2.degree;
for(i=0;i<p2.degree;i++)
a[i]=p2.a[i];

for(i=0;i<p1.degree;i++)
a[i]+=p1.a[i] ;
}
}

void main()
{ clrscr();
polynomial p1,p2,p3;
cout<<"\nFirst polynomial:";
p1.enter();
p1.print();
cout<<"\n\nSecond polynomial:";
p2.enter();
p2.print();
cout<<"\n\nSum of polynomials is :";
p3.add(p1,p2);
p3.print();
getch();
}
OUTPUT

First polynomial:

Enter degree: 3
Enter term with power 0 :8
Enter term with power 1 :7
Enter term with power 2 :6
8x^0 + 7x^1 + 6x^2

Second polynomial:

Enter degree: 3
Enter term with power 0 :5
Enter term with power 1 :4
Enter term with power 2 :2
5x^0 + 4x^1 + 2x^2

Sum of polynomials is :
13x^0 + 11x^1 + 8x^2
Program 4

Q)To implement Linear search

#include<iostream.h>
#include<conio.h>

int i;

class linear
{
int a[22],l,ele;
public:
void search();
};
void linear::search()
{
cout<<"\nEnter limit:";
cin>>l;
cout<<"\nEnter array elements:";
for(i=0;i<l;i++)
cin>>a[i];
cout<<"\nEnter search element:";
cin>>ele;
for(i=0;i<l;i++)
if(a[i]==ele)
break;

if(i==l)
cout<<"\nElement not found";
else
cout<<"\nElement found in "<<i+1<<" position";
}

void main()
{
clrscr();
linear o1;
o1.search();
getch();
}

OUTPUT

Enter limit:5
Enter array elements:44 55 99 88 66
Enter search element:88
Element found in 4 position
Program 5

Q)To implement Binary Search

#include<iostream.h>
#include<conio.h>

int i,j; class

binary
{
int a[12],end;public:
void enter();
void search();
};

void binary::enter()
{
cout<<"\nEnter search limit:";cin>>end;
cout<<"\nEnter array in ascending order:";for(i=0;i<end;i++)
cin>>a[i];

void binary::search()
{
int mid,beg=0,flag=0;int
ele;
cout<<"\nEnter search element:";cin>>ele;
while(beg<=end)
{
mid=(beg+end)/2;

if(a[mid]==ele)
{
cout<<"\nElement found in position "<<mid+1;flag=1;
break;
}
else if(a[mid]<ele)
beg=mid+1;

else end=mid-
1;
}

if(flag==0)
cout<<"\nElement not found";

void main()
{
clrscr();
binary B;
B.enter();
B.search();
getch();
}

OUTPUT

Enter search limit:4


Enter array in ascending order:44 55 66 77
Enter search element:66
Element found in position 3
Program 6

Q) To implement selection sort

#include<iostream.h>
#include<conio.h>

int i,j;
class selection
{
int a[12],end;public:
void enter();
void sort();
};

void selection::enter()
{
cout<<"\nEnter limit:";
cin>>end;
cout<<"\nEnter array:";for(i=0;i<end;i++)
cin>>a[i];

void selection::sort()
{
for(i=0;i<end;i++) for(j=i+1;j<end;j++)
{
if(a[i]>a[j])
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}

cout<<"\nArray in ascending order:";

for(i=0;i<end;i++)
cout<<" "<<a[i];
}

void main()
{
clrscr();
selection s;
s.enter();
s.sort();
getch();
}

OUTPUT

Enter limit:5
Enter array:11 44 66 22 77
Array in ascending order: 11 22 44 66 77
Program 7

Q) To implement insertion sort

#include<iostream.h>
#include<conio.h>

int i,j;
class insertion
{
int a[12],end;
public:
void enter();
void sort();
};

void insertion::enter()
{
cout<<"\nEnter limit:";
cin>>end;
cout<<"\nEnter array:";
for(i=0;i<end;i++)
cin>>a[i];

void insertion::sort()
{
int t;
for(i=0;i<end;i++)
{ t=a[i];
j=i-1;
for(;t<a[j]&&j>=0;j--)
{
a[j+1]=a[j] ;
}
a[j+1]=t;
}
cout<<"\nArray in ascending order:";

for(i=0;i<end;i++)
cout<<" "<<a[i];
}

void main()
{
clrscr();
insertion I;
I.enter();
I.sort();
getch();
}

OUTPUT

Enter limit:4
Enter array:45 66 12 36
Array in ascending order: 12 36 45 66
Program 8

Q) Program to implement quick sort

#include <iostream.h>
#include <conio.h>

int i,j;

int partition(int arr[], int start, int end)


{

int pivot = arr[start];

int count = 0;
for (i = start + 1; i <= end; i++) {
if (arr[i] <= pivot)
count++;
}

int pivotIndex = start + count;


int temp = arr[pivotIndex];
arr[pivotIndex] = arr[start] ;
arr[start] = temp ;

i = start, j = end;

while (i < pivotIndex && j > pivotIndex) {

while (arr[i] <= pivot) {


i++;
}
while (arr[j] > pivot) {
j--;
}

if (i < pivotIndex && j > pivotIndex){

temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}

return pivotIndex;
}

void quickSort(int arr[], int start, int end)


{

if (start >= end)


return;

int p = partition(arr, start, end);

quickSort(arr, start, p - 1);

quickSort(arr, p + 1, end);
}

void main()
{
clrscr();
int a[33],n;

cout<<"\nEnter limit:";
cin>>n;

cout<<"\nEnter array:";
for(i=0;i<n;i++)
cin>>a[i];

cout<<"\nArray in ascending order:";


quickSort(a, 0, n - 1);

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


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

getch();
}

OUTPUT

Enter limit:4
Enter array:77 55 11 44
Array in ascending order:11 44 55 77
Program 9

Q) Program to find the triple representation of a sparse matrix

#include<iostream.h>
#include<conio.h>
int i,j;
void smatrix(int m[33][33],int r,int c) //*Here
{
int row[15],col[15],val[15],k=0;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
if(m[i][j]!=0)
{
row[k]=i+1;
col[k]=j+1;
val[k]=m[i][j];
k++;
}
cout<<"\nTriplet representation of sparse matrix
is:\n";
cout<<"\nROW COL VAL";
cout<<"\n "<<r<<" "<<c<<" "<<k;
for(i=0;i<k;i++)
cout<<"\n"<<row[i]<<" "<<col[i]<<" "<<val[i];
}

void main()
{
clrscr();
int a[33][33],r,c; //Use same size '33' in function
also
cout<<"\nEnter row and column size:";
cin>>r>>c;
cout<<"\nEnter 2D sparse matrix values:";
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>a[i][j];

cout<<"\nThe Matrix: \n";


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<a[i][j]<<" ";
cout<<"\n";
}

smatrix(a,r,c);
getch();
}

OUTPUT

Enter row and column size:4 4


Enter 2D sparse matrix values:
0 0 3 0
0 0 0 8
1 0 3 0
0 0 7 0
The Matrix:
0 0 3 0
0 0 0 8
1 0 3 0
0 0 7 0

Triplet representation of sparse matrix is:

ROW COL VAL


4 4 5
0 3 3
1 4 8
2 1 1
2 3 3
3 3 7
Program 10

Q) To find transpose of a sparse matrix

#include<iostream.h>
#include<conio.h>

int i,j;

void swap(int &x,int &y)


{
int t = x;
x = y;
y = t;
}

void smatrix(int m[33][33],int r,int c) //*Here


{
int row[15],col[15],val[15],k=0;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
if(m[i][j]!=0)
{
row[k]=i+1;
col[k]=j+1;
val[k]=m[i][j];
k++;
}
cout<<"\nTriplet representation of sparse matrix
is:\n";
cout<<"\nROW COL VAL";
cout<<"\n"<<r<<" "<<c<<" "<<k<<" ==>Total";
for(i=0;i<k;i++)
cout<<"\n"<<row[i]<<" "<<col[i]<<"
"<<val[i];
cout<<"\n\nTriplet representation of transpose of
sparse matrix:\n";
for(i=0;i<k;i++)
{
swap(row[i],col[i]);
}

for(i=0;i<k;i++)
for(j=i+1;j<k;j++)
{
if(row[i]>row[j])
{
swap(row[i],row[j]);
swap(col[i],col[j]);
swap(val[i],val[j]);
}
}
cout<<"\nROW COL VAL";
cout<<"\n"<<c<<" "<<r<<" "<<k<<" ==>Total";
for(i=0;i<k;i++)
cout<<"\n"<<row[i]<<" "<<col[i]<<"
"<<val[i];

void main()
{
clrscr();
int a[33][33],r,c; //Use same size '33' in function
also
cout<<"\nEnter row and column size:";
cin>>r>>c;
cout<<"\nEnter 2D sparse matrix values:";
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>a[i][j];
clrscr();
cout<<"\nSparse matrix:\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<a[i][j]<<" ";
cout<<"\n";
}

cout<<"\nTranspose of sparse matrix:\n";


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<a[j][i]<<" ";
cout<<"\n";
}

smatrix(a,r,c);
getch();
}

OUTPUT

Enter row and column size:4 4


Enter 2D sparse matrix values:0 0 3 0
0 0 0 8
1 0 3 0
0 0 7 0
Sparse matrix:
0 0 3 0
0 0 0 8
1 0 3 0
0 0 7 0

Transpose of sparse matrix:


0 0 1 0
0 0 0 0
3 0 3 7
0 8 0 0
Triplet representation of sparse matrix is:

ROW COL VAL


4 4 5 ==>Total
0 3 3
1 4 8
2 1 1
2 3 3
3 3 7

Triplet representation of transpose of sparse matrix:

ROW COL VAL


4 4 5 ==>Total
0 2 1
2 0 3
2 2 3
2 3 7
3 1 8
Program 11

Q)To add two sparse matrices using triplet representation

#include<iostream.h>
#include<conio.h>

int i,j;

class smatrix
{
int m[33][33],t[33][3],r,c,k;
public:
smatrix(){ k = 0 ; }
void enter();
void print();
void add(smatrix,smatrix);
};

void smatrix::enter()
{
cout<<"\nEnter row and column values:";
cin>>r>>c;

cout<<"\nEnter matrix values:\n";

for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>m[i][j];

void smatrix::print()
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<m[i][j]<<" ";
cout<<"\n";
}

cout<<"\nIt's Triplex representation:";


for(i=0;i<r;i++)
for(j=0;j<c;j++)
if(m[i][j]!=0)
{
t[k][0]=i+1;
t[k][1]=j+1;
t[k][2]=m[i][j];
k++;
}

cout<<"\nROW COL VAL";


cout<<"\n"<<r<<" "<<c<<" "<<k<<" ==>Total";

for(i=0;i<k;i++)
cout<<"\n"<<t[i][0]<<" "<<t[i][1]<<"
"<<t[i][2];

void smatrix::add(smatrix m1, smatrix m2)


{
if(m1.r == m2.r && m1.c == m2.c)
{
cout<<"\nSum of matrices:\n";
for(i=0;i<m1.r;i++)
{
for(j=0;j<m2.c;j++)
cout<<m1.m[i][j] + m2.m[i][j]<<" ";
cout<<"\n";
}

i=0;j=0;
while(i<m1.k && j<m2.k)
{
if((m1.t[i][0] > m2.t[j][0]) ||
(m1.t[i][0]==m2.t[j][0])&&(m1.t[i][1] > m2.t[j][1]))
{
t[k][0]=m2.t[j][0];
t[k][1]=m2.t[j][1];
t[k][2]=m2.t[j][2];
j++;k++;
}
else if((m1.t[i][0] < m2.t[j][0]) ||
(m1.t[i][0]==m2.t[j][0])&&(m1.t[i][1] < m2.t[j][1]))
{
t[k][0]=m1.t[i][0];
t[k][1]=m1.t[i][1];
t[k][2]=m1.t[i][2];
i++; k++ ;
}
else
{
if((m1.t[i][2] + m2.t[j][2])!=0)
{
t[k][0]=m1.t[i][0];
t[k][1]=m1.t[i][1];
t[k][2]=m1.t[i][2] + m2.t[j][2];
k++;
}
i++; j++;
}
}
while( i < m1.k )
{
t[k][0]=m1.t[i][0];
t[k][1]=m1.t[i][1];
t[k][2]=m1.t[i][2];
i++; k++ ;
}

while( j < m2.k )


{
t[k][0]=m2.t[j][0];
t[k][1]=m2.t[j][1];
t[k][2]=m2.t[j][2];
j++; k++ ;
}

r = m1.r;
c= m1.c;
cout<<"\nROW COL VAL";
cout<<"\n"<<r<<" "<<c<<" "<<k<<"
==>Total";

for(i=0;i<k;i++)
cout<<"\n"<<t[i][0]<<" "<<t[i][1]<<"
"<<t[i][2];
}
else
cout<<"\nSorry, Matrix addition not possible due
to mismatching orders";
}
void main()
{
clrscr();
smatrix m1,m2,sum;
m1.enter();
cout<<"\nMatrix1\n";
m1.print();
cout<<"\n\nPress enter to input Matrix2 ==>";
getch();
clrscr();
m2.enter();
cout<<"\nMatrix2\n";
m2.print();
cout<<"\n\nPress enter to display sum of matrixes
=>";
getch();
clrscr();
sum.add(m1,m2);
getch();
}

OUTPUT

Enter row and column values:4 4


Enter matrix values:
0 0 3 0
0 0 8 0
1 0 3 0
0 0 7 0
Matrix1
0 0 3 0
0 0 8 0
1 0 3 0
0 0 7 0

It's Triplex representation:


ROW COL VAL
4 4 5 ==>Total
1 3 3
2 3 8
3 1 1
3 3 3
4 3 7

Press enter to input Matrix2 ==>


Enter row and column values: 4 4

Enter matrix values:


0 0 2 0
0 0 0 4
0 2 0 6
0 0 3 0
Matrix2
0 0 2 0
0 0 0 4
0 2 0 6
0 0 3 0

It's Triplex representation:


ROW COL VAL
4 4 5 ==>Total
1 3 2
2 4 4
3 2 2
3 4 6
4 3 3

Press enter to display sum of matrixes=>


Sum of matrices:
0 0 5 0
0 0 8 4
1 2 3 6
0 0 10 0

ROW COL VAL


4 4 8==>Total
1 3 5
2 3 8
2 4 4
3 1 1
3 2 2
3 3 3
3 4 6
4 3 10
Program 12

Q) To implement linear stack

#include<iostream.h>
#include<conio.h>
int ele;

class stack
{
int arr[3],top,max;
public:
stack(){ top = -1;
max = 3; }

void print();

void push(){
if( top == max-1 )
cout<<"\nStack full";
else
{
cout<<"\nEnter element: ";
cin>>ele;
top++;
arr[top]=ele;
cout<<"\nAfter push:";
print();
}
}
void pop()
{
if( top == -1 )
cout<<"\nUnable to perform pop ...stack empty";
else
{
top--;
cout<<"\nAfter pop:";
print();
}

}s;

void stack::print()
{
if( top == -1 )
cout<<"\n( Empty )";
else
{
cout<<"\nElements in stack :\n";
for(int i=top;i>=0;i--)
cout<<arr[i]<<" ";
}
}
void main()
{
char c;
int o;
do
{
clrscr();
cout<<"\n****MENU****";
cout<<"\n1)Push";
cout<<"\n2)Pop";
cout<<"\n3)View";
cout<<"\nEnter option===> ";
cin>>o;
switch(o)
{
case 1: s.push();
break;

case 2: s.pop();
break;

case 3: s.print();
break;

default: cout<<"\nInvalid option";


}
cout<<"\n\nDo you wish to continue (y/n):";
cin>>c;
}while(c=='y'||c=='Y');

getch();

OUTPUT

****MENU****
1)Push
2)Pop
3)View
Enter option===> 2
Unable to perform pop ...stack empty

Do you wish to continue (y/n):y


****MENU****
1)Push
2)Pop
3)View
Enter option===> 1
Enter element: 22
After push:
Elements in stack :
22

Do you wish to continue (y/n):y


****MENU****
1)Push
2)Pop
3)View
Enter option===> 1
Enter element: 11
After push:
Elements in stack :
11 22

Do you wish to continue (y/n):y


****MENU****
1)Push
2)Pop
3)View
Enter option===> 2
After pop:
Elements in stack :
22
Program 13

Q)To convert infix to postfix

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

class stack
{
int str[75],top,max;
public:
stack(){ top = -1;
max = 75; }

// void print();

void push(char ele)


{
top++;
str[top]=ele;
}

void pop()
{
top--;

void infixToPostfix(char*) ;
}S;
int prec(char c) {
if(c == '^')
return 3;
else if(c == '/' || c=='*')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}

void stack::infixToPostfix(char s[]) {

char result[33];
int j = 0;
int l = strlen(s);
for(int i = 0; i < l ; i++) {
char c = s[i];

if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9'))
{
result[j] = c;
j++;
}
else if(c == '(')
push('(');

else if(c == ')') {


while(str[top] != '(')
{
result[j]= str[top];
j++;
pop();
}
pop();
}
else {
while(top!=-1 && prec(s[i]) <= prec(str[top])) {
result[j]= str[top];
j++;
pop();
}
push(c);
}
}

while(top!=-1) {
result[j] = str[top];
j++;
pop();
}

for(i=0;i<j;i++)
cout<<result[i];
}

void main()
{
clrscr();
char exp[33];
cout<<"\nEnter infix expression: ";
gets(exp);
cout<<"\nIt's postfix expression: ";
S.infixToPostfix(exp);
getch();
}
Program 14

Q)To evaluate Postfix expression

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

struct node
{
int val;
node *next;
};

node *top=NULL;
node *newNode;

void push(int item)


{
newNode = new node;
newNode ->val = item;
newNode ->next = NULL;

if( top == NULL)


{
top = newNode;
}
else
{
newNode->next = top;
top = newNode;
}

int pop()
{
node *temp=top;
int v;

if( top != NULL)


v = top->val;

if(top == NULL)
{
cout<<"\nList empty";
return NULL;
}

else if(top->next == NULL)


{
top = NULL;

}
else{
top = top->next;
}
delete temp;
return v;
}
int charToint(char c)
{
if(c == '0')
return 0;
if(c == '1')
return 1;
if(c == '2')
return 2;
if(c == '3')
return 3;
if(c == '4')
return 4;
if(c == '5')
return 5;
if(c == '6')
return 6;
if(c == '7')
return 7;
if(c == '8')
return 8;
if(c == '9')
return 9;
}

void postfixeval(char str[])


{
for(int num=0,i=0;str[i]!='\0';i++)
{
if((str[i]>='0' && str[i]<='9'))
{
while(1)
{
num = charToint(str[i]) + num * 10;
i++;
if(str[i]==' ')
{
cout<<"\nPushed=> "<<num;
push(num);
num=0;
break;
}
}

else if(str[i] == '+' || str[i] == '-' || str[i]


== '/' || str[i] == '*')
{
int v2 = pop();
cout<<"\nPopped=> "<<v2;
int r,v1 = pop();
cout<<"\nPopped=> "<<v1;

if( str[i] == '+') r=v1+v2;


if( str[i] == '-') r=v1-v2;
if( str[i] == '*') r=v1*v2;
if( str[i] == '/') r=v1/v2;

cout<<"\nPushed=> "<<r;
push(r);
}
else if(str[i] == ' ')
continue;

else
{
cout<<"\nError!!..invalid string literal found,
exiting...";
getch();
exit(0);
}
}

cout<<"\nTh final result is : "<<pop();


}

void main()
{
clrscr();
char str[33];
cout<<"\nEnter postfix expression:";
gets(str);
postfixeval(str);
getch();
}
Program 15

Q)To implement linear queue

#include<iostream.h>
#include<conio.h>

int queue[33],size,front = -1,rear = -1;

void Insert()
{
int data;

if(rear == size-1)
{
cout<<"\nQueue full";
return;
}
else if(rear == -1)
{
cout<<"\nEnter value:";
cin>>data;
front ++;
rear ++;
queue[rear] = data;
}
else
{
cout<<"\nEnter value:";
cin>>data;
rear++;
queue[rear] = data;
}
}

int Delete()
{
int v = queue[front];

if(front == -1)
{
cout<<"\nQueue empty";
return NULL;
}
else if(front == rear)
{
front = -1;
rear = -1;
return v;
}
else
{
front ++ ;
return v;
}
}

void print()
{
if(rear == -1)
{
cout<<"\nQueue empty";
return;
}
for(int i=front;i<=rear;i++)
cout<<queue[i]<<" ";
}

void main()
{
int o,v;
char c;
clrscr();

cout<<"\nEnter the limit for queue:";


cin>>size;

do{
clrscr();
cout<<"#### QUEUE ####";
cout<<"\n1)Add";
cout<<"\n2)Remove";
cout<<"\n3)View";
cout<<"\n\nSelect an option=> ";
cin>>o;
switch(o)
{
case 1:
Insert();

break;

case 2: v=Delete();
if(v)
cout<<"\n "<<v<<" has been removed from
queue";

break;

case 3: cout<<"\nElements in queue: ";


print();
break;

default: cout<<"\nInvalid option!";


}

cout<<"\n\nPress 'y' to continue: ";


cin>>c;

}while(c=='y');

getch();
}

OUTPUT

Enter the limit for queue:3


#### QUEUE ####
1)Add
2)Remove
3)View

Select an option=> 1

Enter value:12

Press 'y' to continue: y


#### QUEUE ####
1)Add
2)Remove
3)View

Select an option=> 1

Enter value:44

Enter value:55

Press 'y' to continue: y


#### QUEUE ####
1)Add
2)Remove
3)View

Select an option=>
3

Elements in queue: 44 55

Press 'y' to continue: y


#### QUEUE ####
1)Add
2)Remove
3)View

Select an option=> 2

44 has been removed fromqueue


Program 16

Q) To implement circular queue

#include<iostream.h>
#include<conio.h>

int size,cqueue[33],front=-1,rear=-1;

void cqinsert()
{
int v;
if(front == (rear+1)%size || (rear == size-1 &&
front == 0))
{
cout<<"\nCircular queue is full";
return;
}
else
{
cout<<"\nEnter value: ";
cin>>v;

if(front == -1)
{
front++;
rear++;
cqueue[rear] = v ;
}
else
{
rear = (rear+1)%size;
cqueue[rear] = v;

}
}
}

int cqdelete()
{
int v = cqueue[front];

if(front == -1)
{
cout<<"\nQueue empty";
return NULL;
}

if(front == rear)
{
front = -1;
rear = -1;
}

else
front = (front+1)%size;

return v;
}

void print()
{
if(rear == -1)
{
cout<<"\nCircular queue empty";
return;
}

if( rear >= front )


{
for(int i=front;i<=rear;i++)
cout<<cqueue[i]<<" ";
}
else
{
for(int i=0;i<=rear;i++)
cout<<cqueue[i]<<" ";

for(i=front;i<size;i++)
cout<<cqueue[i]<<" ";
}

void main()
{
int o,v;
char c;
clrscr();

cout<<"\nEnter the limit for circular queue:";


cin>>size;

do{
clrscr();
cout<<"#### CIRCULAR QUEUE ####";
cout<<"\n1)Add";
cout<<"\n2)Remove";
cout<<"\n3)View";
cout<<"\n\nSelect an option=> ";
cin>>o;
switch(o)
{
case 1:
cqinsert();

break;

case 2: v=cqdelete();
if(v)
cout<<"\n "<<v<<" has been removed from
queue";

break;

case 3: cout<<"\nElements in queue: ";


print();

break;

default: cout<<"\nInvalid option!";


}

cout<<"\n\nPress 'y' to continue: ";


cin>>c;

}while(c=='y');

getch();
}

OUTPUT

Enter the limit for circular queue:4


#### CIRCULAR QUEUE ####
1)Add
2)Remove
3)View

Select an option=> 1
Enter value: 44

Press 'y' to continue: y


#### CIRCULAR QUEUE ####
1)Add
2)Remove
3)View

Select an option=> 1

Enter value: 55

Press 'y' to continue: y


#### CIRCULAR QUEUE ####
1)Add
2)Remove
3)View

Select an option=> 1

Enter value: 66

Press 'y' to continue: y


#### CIRCULAR QUEUE ####
1)Add
2)Remove
3)View

Select an option=> 1

Enter value: 77

Press 'y' to continue: y


#### CIRCULAR QUEUE ####
1)Add
2)Remove
3)View

Select an option=> 2

44 has been removed fromqueue

Press 'y' to continue: y


#### CIRCULAR QUEUE ####
1)Add
2)Remove
3)View

Select an option=> 3

Elements in queue: 55 66 77
Program 17

Q)To implement singly linked list

#include<iostream.h>
#include<conio.h>

struct node
{
int val;
node *next;
};

node *head=NULL;
node *newNode;
int length = 0;

void insertBeg(int item)


{
newNode = new node;
newNode->val = item;
newNode->next = NULL;

if( head == NULL)


{
head = newNode;
}

else
{
newNode->next = head;
head = newNode;
}
length++;
}

void insertEnd(int item)


{
newNode = new node;
newNode ->val = item;
newNode->next = NULL;

if( head == NULL)


{
head = newNode;
}

else
{
node *temp = head;
while(temp->next!=NULL)
{ temp = temp->next; }

temp->next = newNode;
}

length++;
}

void insertInBtw(int item,int pos)


{
if(pos==1)
insertBeg(item);

else if( pos == length)


insertEnd(item);
else if(pos>length)
return;

else
{
node *p1 = head;
node *p2 = head->next;
newNode = new node;
newNode->val = item;
newNode->next = NULL;

for(int i=1;i<pos-1;i++)
{
p1 = p2;
p2 = p2->next;

}
p1->next = newNode;
newNode->next = p2;
length++;
}
}

void deleteStart()
{
if(head == NULL)
{
cout<<"\nList empty";
return;
}
else{
node *temp = head;
head = head->next;
delete temp;
length--;
}
}

void deleteEnd()
{
if(head == NULL)
{
cout<<"\nList empty";
return;
}
else{
node *p1=head;
node *p2=head->next;

while( p2->next!=NULL )
{
p1 = p2;
p2 = p2->next;
}
p1->next = NULL;
delete p2;
length--;
}
}

void deleteInBtw(int pos)


{
if(pos==1)
deleteStart();

else if( pos == length)


deleteEnd();

else
{
node *p1=head;
node *p2=head->next;

for(int i=1;i<pos-1;i++)
{
p1 = p2;
p2 = p2->next;
}

p1->next = p2->next;
delete p2;
length--;
}
}

void printlist()
{
node *temp = head;
while(temp->next != NULL )
{
cout<<temp->val<<" ";
temp = temp->next;

cout<<temp->val;
cout<<"\nTotal length="<<length;
}

void main()
{
clrscr();

insertBeg(1);
insertBeg(2);
insertBeg(3);
insertEnd(4);
insertEnd(5);
insertBeg(0);
insertEnd(11);
insertBeg(17);
insertInBtw(22,2);
cout<<"\nInsertion: ";
printlist(); //17 0 22 3 2 1 4 5 11
cout<<"\n\nDeletion: ";
deleteInBtw(3);
cout<<"\n";
printlist();
cout<<"\n";
deleteStart();
deleteEnd();
deleteStart();
deleteEnd();
printlist();
getch();
}

Output
Insertion: 17 22 0 3 2 1 4 5 11
Total length=9

Deletion:
17 22 3 2 1 4 5 11
Total length=8
3 2 1 4
Total length=4
Program 18

Q)To arrange nodes of a list in ascending order

#include<iostream.h>
#include<conio.h>

void printlist();

struct node
{
int val;
node *next;
};

node *head=NULL;
node *newNode;
int length = 0;

void insertBeg(int item)


{
newNode = new node;
newNode->val = item;
newNode->next = NULL;

if( head == NULL)


{
head = newNode;
}

else
{
newNode->next = head;
head = newNode;
}
length++;
}

void insertEnd(int item)


{
newNode = new node;
newNode ->val = item;
newNode->next = NULL;

if( head == NULL)


{
head = newNode;
}

else
{
node *temp = head;
while(temp->next!=NULL)
{ temp = temp->next; }

temp->next = newNode;
}

length++;
}

void insertInBtw(int item,int pos)


{
if(pos==1)
insertBeg(item);

else
{
node *p1 = head;
node *p2 = head->next;
newNode = new node;
newNode->val = item;
newNode->next = NULL;

for(int i=1;i<pos-1;i++)
{
p1 = p2;
p2 = p2->next;

}
p1->next = newNode;
newNode->next = p2;
length++;
}
}

void EnterAndArrange()
{
int num,v,j;
node *temp;
cout<<"\nHow many node do you want to create: ";
cin>>num;
cout<<"\nEnter node values:";
for(int i =0 ; i < num ; i++)
{
cin>>v;
if(i == 0)
{
insertBeg(v) ;
continue;
}
for( temp = head ,j = 0; temp != NULL ; temp =
temp->next , j++ )
{
if( temp->val > v)
{
insertInBtw(v,j+1);
break;
}
}

if(temp == NULL && j==i)


{
insertEnd(v);
}
}

void printlist()
{
node *temp = head;
while(temp->next != NULL )
{
cout<<temp->val<<" ";
temp = temp->next;

cout<<temp->val;
cout<<"\nTotal length="<<length;
}

void main()
{
clrscr();
EnterAndArrange();
cout<<"\nYour nodes in ascending order:";
printlist();
getch();
}

output
How many node do you want to create: 5
Enter node values:8 7 6 4 3
Your nodes in ascending order:3 4 6 7 8
Total length=5
Program 19

Q)To concatenate two linked list

#include<iostream.h>
#include<conio.h>

struct node
{
int val;
node *next;
};

class Llist
{
node *head;
node *rear;
node *newNode;
int length ;

public:

Llist()
{
head = NULL;
rear = NULL;
}

void insert(int);
void ccat(Llist,Llist);
void printlist();
}l1,l2,l3;

void Llist::insert(int item)


{
newNode = new node;
newNode ->val = item;
newNode->next = NULL;

if( head == NULL)


{
head = newNode;
rear = newNode;
}

else
{
rear->next = newNode;
rear = newNode;
}

length++;
}

void Llist::ccat(Llist l1,Llist l2)


{
head = l1.head;
rear = l1.rear;
rear->next = l2.head;
rear = l2.rear;
length = l1.length + l2.length;
}

void Llist::printlist()
{
node *temp = head;
while(temp->next != NULL )
{
cout<<temp->val<<" ";
temp = temp->next;

cout<<temp->val<<endl;
cout<<"\nTotal length="<<length<<endl;
}

void main()
{
clrscr();
int i,num,v;
cout<<"\nHow many node do you want to create for
list1: ";
cin>>num;
cout<<"\nEnter node values:";
for( i =0 ; i < num ; i++)
{
cin>>v;
l1.insert(v);
}

cout<<"\nHow many node do you want to create for


list2: ";
cin>>num;
cout<<"\nEnter node values:";
for( i =0 ; i < num ; i++)
{
cin>>v;
l2.insert(v);
}
cout<<"\nList 1: ";
l1.printlist();
cout<<"\nList 2: ";
l2.printlist();
cout<<"\nConcatenated list: ";
l3.ccat(l1,l2);
l3.printlist();
getch();
}

output
How many node do you want to create for list1: 3
Enter node values:1 2 3
How many node do you want to create for list2: 4
Enter node values:1 2 3 4
List 1: 1 2 3

Total length=3

List 2: 1 2 3 4

Total length=4

Concatenated list: 1 2 3 1 2 3 4

Total length=7
Program 20

Q)To interchange the nodes of a linked list

#include<iostream.h>
#include<conio.h>

struct node
{
int val;
node *next;
};

node *head=NULL;
node *newNode;
int length = 0;

void insert(int item)


{
newNode = new node;
newNode ->val = item;
newNode->next = NULL;

if( head == NULL)


{
head = newNode;
}

else
{
node *temp = head;
while(temp->next!=NULL)
{ temp = temp->next; }

temp->next = newNode;
}

length++;
}

int swap(int pos1,int pos2)


{
pos1--;
pos2--;

if(pos1==pos2 || pos1<0 || pos2<0 || pos1>length


|| pos2>length)
{
cout<<"\nRange error!,Swap failed";
return 0;
}

else
{

int i;
node *p1,*p2,*P1,*P2;
for(i=0;i<=pos1;i++)
{
if(i==0)
{
p1=NULL;
p2=head;
}
else
{
p1 = p2;
p2 = p2->next;
}
}

for(i=0;i<=pos2;i++)
{
if(i==0)
{
P1=NULL;
P2=head;
}
else
{
P1 = P2;
P2 = P2->next;
}
}

if(p1 != NULL)
p1->next = P2;
else
head = P2;

if(P1 != NULL)
P1->next = p2;
else
head = p2;

node *temp = P2->next;


P2->next = p2->next;
p2->next = temp;
return 1;
}
}
void printlist()
{
node *temp = head;
while(temp->next != NULL )
{
cout<<temp->val<<" ";
temp = temp->next;

cout<<temp->val;
cout<<"\nTotal length= "<<length;
}

void main()
{
char c;
int v;
do{
clrscr();
cout<<"\nEnter node value: ";
cin>>v;
insert(v);
cout<<"\n\nDo you want to enter more
elements(y/n): ";
cin>>c;
}while(c=='y');

cout<<"\nThe elements you entered: ";


printlist();
cout<<"\n\nEnter swap positions : ";
int a,b,r;
cin>>a>>b;
r = swap(a,b);
if(r)
{
cout<<"\nList elements after swap: ";
printlist();
}
getch();
}
Output:
Enter node value: 4
Do you want to enter more elements(y/n): y
Enter node value: 5
Do you want to enter more elements(y/n): y
Enter node value: 6
Do you want to enter more elements(y/n): n
The elements you entered: 4 5 6
Total length= 3

Enter swap positions : 1


3
List elements after swap: 6 5 4
Total length= 3
Program 21

Q)To implement linked stack

#include<iostream.h>
#include<conio.h>

struct node
{
int val;
node *next;
};

node *top=NULL;
node *newNode;
int length = 0;

void push(int item)


{
newNode = new node;
newNode ->val = item;
newNode ->next = NULL;

if( top == NULL)


{
top = newNode;
}

else
{
newNode->next = top;
top = newNode;
}
length++;
}

int pop()
{
node *temp=top;
int v;

if(top == NULL)
{
cout<<"\nList empty";
return NULL;
}

else if(top->next == NULL)


{
top = NULL;
}
else{
top = top->next;
}
v = temp->val;
delete temp;
length--;
return v;
}

void printlist()
{
node *temp = top;
if(top == NULL)
{
cout<<"\nEmpty list";
return;
}
while(temp->next != NULL )
{
cout<<temp->val<<" ";
temp = temp->next;

cout<<temp->val;
cout<<"\nTotal length="<<length;
}

void main()
{
int o,v;
char c;
do{
clrscr();
cout<<"#### STACK ####";
cout<<"\n1)Push";
cout<<"\n2)Pop";
cout<<"\n3)View";
cout<<"\n\nSelect an option=> ";
cin>>o;
switch(o)
{
case 1: cout<<"\nEnter value:";
cin>>v;
push(v);
break;

case 2: v=pop();
if(v)
cout<<"\n "<<v<<" has been popped from
stack";

break;

case 3: cout<<"\nElements in stack: ";


printlist();

break;

default: cout<<"\nInvalid option!";


}

cout<<"\n\nPress 'y' to continue: ";


cin>>c;

}while(c=='y');

getch();
}
output
#### STACK ####
1)Push
2)Pop
3)View

Select an option=> 1
Enter value:2
Press 'y' to continue: y
#### STACK ####
1)Push
2)Pop
3)View

Select an option=> 1
Enter value:3
Press 'y' to continue: y
#### STACK ####
1)Push
2)Pop
3)View

Select an option=> 3
Elements in stack: 3 2
Total length=2

Press 'y' to continue:


Program 22

Q)To implement linked queue

#include<iostream.h>
#include<conio.h>

struct node
{
int val;
node *next;
};

node *front=NULL;
node *rear=NULL;
node *newNode;
int length = 0;

void add(int item)


{
newNode = new node;
newNode ->val = item;
newNode ->next = NULL;
if( front == NULL)
{
front = newNode;
rear = newNode;
}
else
{
rear->next = newNode;
rear = newNode ;
}

length++;
}

int remove()
{
int v;
if(front == NULL)
{
cout<<"\nEmpty queue";
return NULL;
}
else{
node *temp = front;
v = temp->val;
front= front->next;
delete temp;
length--;
return v;
}
}

void printlist()
{
node *temp = front;
if(front == NULL)
{
cout<<"\n( Empty )";
return;
}
while(temp!=NULL)
{
cout<<temp->val<<" ";
temp = temp->next;

cout<<"\nTotal length="<<length;
}

void main()
{
int o,v;
char c;
do{
clrscr();
cout<<"#### QUEUE ####";
cout<<"\n1)Add";
cout<<"\n2)Remove";
cout<<"\n3)View";
cout<<"\n\nSelect an option=> ";
cin>>o;
switch(o)
{
case 1: cout<<"\nEnter value:";
cin>>v;
add(v);

break;

case 2: v=remove();
if(v)
cout<<"\n "<<v<<" has been removed from
queue";

break;

case 3: cout<<"\nElements in queue: ";


printlist();

break;

default: cout<<"\nInvalid option!";


}

cout<<"\n\nPress 'y' to continue: ";


cin>>c;

}while(c=='y');

getch();
}
OUTPUT

#### QUEUE ####


1)Add
2)Remove
3)View

Select an option=> 1
Enter value:12
Press 'y' to continue: y
#### QUEUE ####
1)Add
2)Remove
3)View

Select an option=> 1
Enter value:22
Press 'y' to continue: y
#### QUEUE ####
1)Add
2)Remove
3)View

Select an option=> 2
12 has been removed from queue

Press 'y' to continue: y


#### QUEUE ####
1)Add
2)Remove
3)View

Select an option=> 3
Elements in queue: 22
Total length=0
Program 23

Q)To implement circular linked list

#include<iostream.h>
#include<conio.h>

struct node
{
int val;
node *next;

};

node *head=NULL;
node *rear=NULL;
node *newNode;
int length = 0;

void insertBeg(int item)


{
newNode = new node;
newNode->val = item;
newNode->next = NULL;

if( head == NULL)


{
head = newNode;
rear = newNode;
}

else
{
newNode->next = head;
head = newNode;
}
length++;
}
void insertEnd(int item)
{
newNode = new node;
newNode ->val = item;
newNode->next = NULL;

if( head == NULL)


{
head = newNode;
rear = newNode;
}

else
{
rear->next = newNode;
newNode->next = head;
rear = newNode;

length++;
}

void insertInBtw(int item,int pos)


{
if(pos==1)
insertBeg(item);

else if( pos == length)


insertEnd(item);

else if(pos>length)
return;

else
{
node *p1 = head;
node *p2 = head->next;
newNode = new node;
newNode->val = item;
newNode->next = NULL;

for(int i=1;i<pos-1;i++)
{
p1 = p2;
p2 = p2->next;

p1->next = newNode;
newNode->next = p2;
length++;
}
}

void deleteStart()
{
if(head == NULL)
{
cout<<"\nList empty";
return;
}
else{
node *temp = head;
head = head->next;
delete temp;
length--;
}
}

void deleteEnd()
{
if(head == NULL)
{
cout<<"\nList empty";
return;
}
else{
node *temp = head;
while(temp->next!=rear)
{
temp = temp->next;
}
temp -> next = head;
node *t2 = rear;
rear = temp;
delete t2;
length--;
}
}
void deleteInBtw(int pos)
{
if(pos==1)
deleteStart();

else if( pos == length)


deleteEnd();

else
{
node *p1=head;
node *p2=head->next;
node *temp;
for(int i=1;i<pos-1;i++)
{
p1 = p2;
p2 = p2->next;
}
temp = p2->next;
p1->next = temp;
delete p2;
length--;
}
}

void printlist()
{
node *temp = head;
while(temp != rear )
{
cout<<temp->val<<" ";
temp = temp->next;

cout<<rear->val;
cout<<"\nTotal length="<<length;
}

void main()
{
clrscr();

insertBeg(1);
insertBeg(2);
insertBeg(3);
insertEnd(4);
insertEnd(5);
insertBeg(0);
insertEnd(11);
insertBeg(17);
insertInBtw(22,2);
cout<<"\nInsertion: ";
printlist(); //17 22 0 3 2 1 4 5 11
cout<<"\n\nDeletion of 3rd element: ";
deleteInBtw(3);
cout<<"\n";
printlist();
cout<<"\n\nAfter random Deletions: ";
deleteStart();
deleteEnd();
deleteStart();
deleteEnd();
printlist();
getch();
}

OUTPUT

Insertion: 17 22 0 3 2 1 4 5 11
Total length=8

Deletion of 3rd element:


17 22 3 2 1 4 5 11
Total length=7

After random Deletions: 3 2 1 4


Total length=3
Program 24

Q)To implement doubly linked list

#include<iostream.h>
#include<conio.h>

struct node
{
int val;
node *next;
node *prev;
};

node *head=NULL;
node *newNode;
int length = 0;

void insertBeg(int item)


{
newNode = new node;
newNode->val = item;
newNode->next = NULL;
newNode->prev = NULL;

if( head == NULL)


{
head = newNode;
}

else
{
newNode->next = head;
head = newNode;
}
length++;
}

void insertEnd(int item)


{
newNode = new node;
newNode ->val = item;
newNode->next = NULL;
newNode->prev = NULL;

if( head == NULL)


{
head = newNode;
}

else
{
node *temp = head;
while(temp->next!=NULL)
{ temp = temp->next; }

newNode->prev = temp;
temp->next = newNode;

length++;
}

void insertInBtw(int item,int pos)


{
if(pos==1)
insertBeg(item);

else if( pos == length)


insertEnd(item);

else if(pos>length)
return;

else
{
node *p1 = head;
node *p2 = head->next;
newNode = new node;
newNode->val = item;
newNode->next = NULL;
newNode->prev = NULL;

for(int i=1;i<pos-1;i++)
{
p1 = p2;
p2 = p2->next;

}
newNode->prev = p1;
p1->next = newNode;
newNode->next = p2;
p2->prev = newNode;
length++;
}
}

void deleteStart()
{
if(head == NULL)
{
cout<<"\nList empty";
return;
}
else{
node *temp = head;
head = head->next;
head->prev = NULL;
delete temp;
length--;
}
}

void deleteEnd()
{
if(head == NULL)
{
cout<<"\nList empty";
return;
}
else{
node *p1=head;
node *p2=head->next;

while( p2->next!=NULL )
{
p1 = p2;
p2 = p2->next;
}
p1->next = NULL;
delete p2;
length--;
}
}
void deleteInBtw(int pos)
{
if(pos==1)
deleteStart();

else if( pos == length)


deleteEnd();

else
{
node *p1=head;
node *p2=head->next;
node *temp;
for(int i=1;i<pos-1;i++)
{
p1 = p2;
p2 = p2->next;
}
temp = p2->next;
p1->next = temp;
temp->prev = p1;
delete p2;
length--;
}
}

void printlist()
{
node *temp = head;
while(temp->next != NULL )
{
cout<<temp->val<<" ";
temp = temp->next;
}

cout<<temp->val;
cout<<"\nTotal length="<<length;
}

void main()
{
clrscr();

insertBeg(1);
insertBeg(2);
insertBeg(3);
insertEnd(4);
insertEnd(5);
insertBeg(0);
insertEnd(11);
insertBeg(17);
insertInBtw(22,2);
cout<<"\nInsertion: ";
printlist(); //17 22 0 3 2 1 4 5 11
cout<<"\n\nDeletion of 3rd element: ";
deleteInBtw(3);
cout<<"\n";
printlist();
cout<<"\n\nAfter random Deletions: ";
deleteStart();
deleteEnd();
deleteStart();
deleteEnd();
printlist();
getch();
}
output

Insertion: 17 22 0 3 2 1 4 5 11
Total length=9

Deletion of 3rd element:


17 22 3 2 1 4 5 11
Total length=8

After random Deletions: 3 2 1 4


Total length=4
Program 25

Q)To implement circular doubly linked list

#include<iostream.h>
#include<conio.h>

struct node
{
int val;
node *next;
node *prev;
};

node *head=NULL;
node *rear=NULL;
node *newNode;
int length = 0;

void insertBeg(int item)


{
newNode = new node;
newNode->val = item;
newNode->next = NULL;
newNode->prev = NULL;

if( head == NULL)


{
head = newNode;
rear = newNode;
}
else
{
newNode->next = head;
newNode->prev = rear;
head = newNode;
}
length++;
}

void insertEnd(int item)


{
newNode = new node;
newNode ->val = item;
newNode->next = NULL;
newNode->prev = NULL;

if( head == NULL)


{
head = newNode;
rear = newNode;
}

else
{
rear->next = newNode;
newNode->prev = rear;
newNode->next = head;
head->prev = newNode;
rear = newNode;

length++;
}
void insertInBtw(int item,int pos)
{
if(pos==1)
insertBeg(item);

else if( pos == length)


insertEnd(item);

else if(pos>length)
return;

else
{
node *p1 = head;
node *p2 = head->next;
newNode = new node;
newNode->val = item;
newNode->next = NULL;
newNode->prev = NULL;

for(int i=1;i<pos-1;i++)
{
p1 = p2;
p2 = p2->next;

}
newNode->prev = p1;
p1->next = newNode;
newNode->next = p2;
p2->prev = newNode;
length++;
}
}
void deleteStart()
{
if(head == NULL)
{
cout<<"\nList empty";
return;
}
else{
node *temp = head;
head = head->next;
head->prev = rear;
delete temp;
length--;
}
}

void deleteEnd()
{
if(head == NULL)
{
cout<<"\nList empty";
return;
}
else{
node *temp = rear;
rear = rear->prev;
rear->next = head;
head->prev = rear;
delete temp;
length--;
}
}

void deleteInBtw(int pos)


{
if(pos==1)
deleteStart();

else if( pos == length)


deleteEnd();

else
{
node *p1=head;
node *p2=head->next;
node *temp;
for(int i=1;i<pos-1;i++)
{
p1 = p2;
p2 = p2->next;
}
temp = p2->next;
p1->next = temp;
temp->prev = p1;
delete p2;
length--;
}
}

void printlist()
{
node *temp = head;
while(temp != rear )
{
cout<<temp->val<<" ";
temp = temp->next;

}
cout<<rear->val;
cout<<"\nTotal length="<<length;
}

void main()
{
clrscr();

insertBeg(1);
insertBeg(2);
insertBeg(3);
insertEnd(4);
insertEnd(5);
insertBeg(0);
insertEnd(11);
insertBeg(17);
insertInBtw(22,2);
cout<<"\nInsertion: ";
printlist(); //17 22 0 3 2 1 4 5 11
cout<<"\n\nDeletion of 3rd element: ";
deleteInBtw(3);
cout<<"\n";
printlist();
cout<<"\n\nAfter random Deletions: ";
deleteStart();
deleteEnd();
deleteStart();
deleteEnd();
printlist();
getch();
}

OUTPUT
Insertion: 17 22 0 3 2 1 4 5 11
Total length=9
Deletion of 3rd element:
17 22 3 2 1 4 5 11
Total length=8

After random Deletions: 3 2 1 4


Total length=4

You might also like