DS Programs
DS Programs
DS Programs
#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
#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
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;
}
}
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
#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
#include<iostream.h>
#include<conio.h>
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
#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;
}
}
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
#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
#include <iostream.h>
#include <conio.h>
int i,j;
int count = 0;
for (i = start + 1; i <= end; i++) {
if (arr[i] <= pivot)
count++;
}
i = start, j = end;
temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
return pivotIndex;
}
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];
getch();
}
OUTPUT
Enter limit:4
Enter array:77 55 11 44
Array in ascending order:11 44 55 77
Program 9
#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];
smatrix(a,r,c);
getch();
}
OUTPUT
#include<iostream.h>
#include<conio.h>
int i,j;
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";
}
smatrix(a,r,c);
getch();
}
OUTPUT
#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;
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";
}
for(i=0;i<k;i++)
cout<<"\n"<<t[i][0]<<" "<<t[i][1]<<"
"<<t[i][2];
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++ ;
}
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
#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;
getch();
OUTPUT
****MENU****
1)Push
2)Pop
3)View
Enter option===> 2
Unable to perform pop ...stack empty
#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 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;
}
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('(');
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
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
struct node
{
int val;
node *next;
};
node *top=NULL;
node *newNode;
int pop()
{
node *temp=top;
int v;
if(top == NULL)
{
cout<<"\nList empty";
return 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;
}
cout<<"\nPushed=> "<<r;
push(r);
}
else if(str[i] == ' ')
continue;
else
{
cout<<"\nError!!..invalid string literal found,
exiting...";
getch();
exit(0);
}
}
void main()
{
clrscr();
char str[33];
cout<<"\nEnter postfix expression:";
gets(str);
postfixeval(str);
getch();
}
Program 15
#include<iostream.h>
#include<conio.h>
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();
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;
}while(c=='y');
getch();
}
OUTPUT
Select an option=> 1
Enter value:12
Select an option=> 1
Enter value:44
Enter value:55
Select an option=>
3
Elements in queue: 44 55
Select an option=> 2
#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;
}
for(i=front;i<size;i++)
cout<<cqueue[i]<<" ";
}
void main()
{
int o,v;
char c;
clrscr();
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;
break;
}while(c=='y');
getch();
}
OUTPUT
Select an option=> 1
Enter value: 44
Select an option=> 1
Enter value: 55
Select an option=> 1
Enter value: 66
Select an option=> 1
Enter value: 77
Select an option=> 2
Select an option=> 3
Elements in queue: 55 66 77
Program 17
#include<iostream.h>
#include<conio.h>
struct node
{
int val;
node *next;
};
node *head=NULL;
node *newNode;
int length = 0;
else
{
newNode->next = head;
head = newNode;
}
length++;
}
else
{
node *temp = head;
while(temp->next!=NULL)
{ temp = temp->next; }
temp->next = newNode;
}
length++;
}
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--;
}
}
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
#include<iostream.h>
#include<conio.h>
void printlist();
struct node
{
int val;
node *next;
};
node *head=NULL;
node *newNode;
int length = 0;
else
{
newNode->next = head;
head = newNode;
}
length++;
}
else
{
node *temp = head;
while(temp->next!=NULL)
{ temp = temp->next; }
temp->next = newNode;
}
length++;
}
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;
}
}
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
#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;
else
{
rear->next = newNode;
rear = newNode;
}
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);
}
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
#include<iostream.h>
#include<conio.h>
struct node
{
int val;
node *next;
};
node *head=NULL;
node *newNode;
int length = 0;
else
{
node *temp = head;
while(temp->next!=NULL)
{ temp = temp->next; }
temp->next = newNode;
}
length++;
}
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;
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');
#include<iostream.h>
#include<conio.h>
struct node
{
int val;
node *next;
};
node *top=NULL;
node *newNode;
int length = 0;
else
{
newNode->next = top;
top = newNode;
}
length++;
}
int pop()
{
node *temp=top;
int v;
if(top == NULL)
{
cout<<"\nList empty";
return NULL;
}
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;
break;
}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
#include<iostream.h>
#include<conio.h>
struct node
{
int val;
node *next;
};
node *front=NULL;
node *rear=NULL;
node *newNode;
int length = 0;
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;
break;
}while(c=='y');
getch();
}
OUTPUT
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
Select an option=> 3
Elements in queue: 22
Total length=0
Program 23
#include<iostream.h>
#include<conio.h>
struct node
{
int val;
node *next;
};
node *head=NULL;
node *rear=NULL;
node *newNode;
int length = 0;
else
{
newNode->next = head;
head = newNode;
}
length++;
}
void insertEnd(int item)
{
newNode = new node;
newNode ->val = item;
newNode->next = NULL;
else
{
rear->next = newNode;
newNode->next = head;
rear = newNode;
length++;
}
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
{
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
#include<iostream.h>
#include<conio.h>
struct node
{
int val;
node *next;
node *prev;
};
node *head=NULL;
node *newNode;
int length = 0;
else
{
newNode->next = head;
head = newNode;
}
length++;
}
else
{
node *temp = head;
while(temp->next!=NULL)
{ temp = temp->next; }
newNode->prev = temp;
temp->next = newNode;
length++;
}
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
{
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
#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;
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)
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--;
}
}
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