Data Software Programs
Data Software Programs
GIAL
3
#include<iostream.h>
#include<conio.h>
class array
{
public:int a[5],n;
void create();
void traverse();
void insert();
void remove();
};
void array:: create()
{
void array::traverse()
{
cout<<a[i]<<" ";
}
[Type text]
4
void array::insert()
{
int pos,item;
cout<<"Enter the position to insert : ";
cin>>pos;
if (pos>n+1 || pos<0)
{
cout<<"\nInsertion is not possible";
}
cout<<"Enter the item to insert : ";
cin>>item;
for(int i=n;i>=pos;i--)
{
a[i]=a[i-1];
}
a[i]=item;
n=n+1;
traverse();
}
void array :: remove()
{
int loc;
cout<<"\nEnter the location to delete: ";
cin>>loc;
if(loc>n||loc<0)
{
for(int i=loc-1;i<n;i++)
[Type text]
{
a[i]=a[i+1];
}
n=n-1;
traverse();
}
void main()
{
clrscr();
int c;
array a1;
do
{
break;
case 2: a1.traverse();
break;
case 3: a1.insert();
break;
case 4: a1.remove();
break;
case 5: return;
default:
cout<<"\nInvalid Choice";
break;
}
}while(c!=6);
getch();
}
OUTPUT
8
#include<iostream.h>
#include<conio.h>
class linear
{
public:int a[20],n,item;
void read();
void search();
};
void linear::read()
{
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
void linear::search()
{
int flag=0,loc;
{
if(a[i]==item)
{
9
loc=i;
flag=1;
break;
}
}
if(flag==1)
void main()
{
clrscr();
linear l1;
l1.read();
l1.search();
getch();
}
10
OUTPUT
11
#include<conio.h>
class binary
int a[10],n;
void search();
};
void binary::read()
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
cout<<a[i]<<" ";
void binary::search()
12
int item,first,mid,last;
cin>>item;
first=0; last=n-1;
mid=(first+last)/2;
while(first<=last)
if(item==a[mid])
cout<<"Search is Successful\n";
break;
else if(item>a[mid])
first=mid+1;
else
last=mid-1;
mid=(first+last)/2;
}
13
if(first>last)
void main()
clrscr();
binary b1;
b1.read();
b1.search();
getch();
}
14
OUTPUT
15
#include<iostream.h>
#include<conio.h>
class sel
{
int a[10],n;
public:
void read();
void sort();
};
void sel::read()
{
for(int i=0;i<n;i++)
cin>>a[i];
}
void sel::sort()
{
int min,loc,i,j,t;
for(i=0;i<n;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
{
if(a[j]<min)
{
16
min=a[j];
loc=j;
}
}
if(loc!=i)
{
t=a[i];
a[i]=a[loc];
a[loc]=t;
}
}
cout<<"Sorted array is:\n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
}
void main()
{
sel s1;
clrscr();
s1.read();
s1.sort();
getch();
}
17
OUTPUT
18
#include<iostream.h>
#include<conio.h>
class insort
int a[10],n;
public:
void read();
void sort();
};
void insort::read()
{
cout<<"Enter the no. of elements,n :";
cin>>n;
cout<<"Enter the elements:\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
void insort::sort()
{
int t,m,i,j;
for(m=1;m<n;m++)
{
t=a[m];
j=m-1;
while(t<a[j]&&(j>=0))
19
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=t;
}
}
}
void main()
{
clrscr();
insort i1;
i1.read();
i1.sort();
getch()
}
20
OUTPUT
21
#include<iostream.h>
#include<conio.h>
class quicksort
int a[10],n;
public:
void read();
void sort();
void qsort(int[],int,int);
int partition(int [],int,int);
void display();
};
void quicksort::read()
{
cout<<"Enter number of elements,n : ";
cin>>n;
cout<<"Enter the elements:\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
void quicksort::sort()
{
qsort(a,0,n-1);
}
if(l<u)
{
int j=partition(a,l,u);
qsort(a,l,j-1);
qsort(a,j+1,u);
}
}
int quicksort::partition(int a[],int l,int u)
{
int pivot=a[l];
int i=l;
int j=u+1;
do
{
do
{
i++;
}
while(a[i]<pivot&&i<=u);
do
{
j--;
}while(a[j]>pivot);
if(i<j)
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
23
}while(i<j);
a[l]=a[j];
a[j]=pivot;
return j;
}
void quicksort::display()
{
cout<<"The Sorted array is : \n";
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}
void main()
{
clrscr();
quicksort q1;
q1.read();
q1.sort();
q1.display();
getch();
}
24
OUTPUT
25
#include<conio.h>
class poly
public:int a[10],coef,d;
void read();
void display();
};
void poly::read()
cin>>d;
for(int i=0;i<=d;i++)
cin>>coef;
a[i]=coef;
void poly::display()
for(int i=d;i>0;i--)
if(a[i]!=0)
26
cout<<a[i]<<"x^"<<i<<" + ";
cout<<a[0]<<"x^"<<i;
void main()
poly p1;
clrscr();
p1.read();
p1.display();
getch();
}
27
OUTPUT
28
int a[20],n;
float coef;
public:
void read();
poly add(poly);
void display();
};
void poly :: read()
{
int i=0;
cout<<"\nEnter the highest degree of the polynomial : ";
cin>>n;
for(i=0;i<=n;i++)
{
cout<<"\nEnter the coeff of x^"<<i<<" : ";
cin>>coef;
a[i]=coef;
}
}
poly poly::add(poly p2)
{
int x,i;
poly p3;
29
if(n>p2.n)
{
x=n+1;
}
else
{
x=p2.n+1;
}
for(i=n+1;i<=x;i++)
{
a[i]=0;
}
for(i=p2.n+1;i<=x;i++)
{
p2.a[i]=0;
}
for(i=0;i<=x;i++)
{
p3.a[i]=a[i]+p2.a[i];
}
p3.n=x;
return p3;
}
void poly :: display()
{
int i;
for(i=n;i>0;i--)
{
if(a[i]!=0)
30
cout<<a[i]<<"x^"<<i<<" + ";
}
cout<<a[0]<<"x^"<<i;
}
void main()
{
poly p1,p2,p3;
clrscr();
p3=p1.add(p2);
p3.display();
getch();
}
31
OUTPUT
32
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
}
void sparse::sparse_matrix()
{
int x=1,count=0;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
if(a[i][j]!=0)
{
b[x][0]=i;
b[x][1]=j;
b[x][2]=a[i][j];
x++;
count++;
}
}
}
b[0][0]=row;
b[0][1]=col;
b[0][2]=count;
cout<<"The Sparse Matrix is:\n";
for(int m=0;m<=count;n++)
{
for(int n=0;n<=2;n++)
{
34
cout<<b[m][n]<<"\t";
}
cout<<endl;
}
}
void main()
{
clrscr();
sparse s1;
s1.read();
s1.display();
s1.sparse_matrix();
getch();
}
35
OUTPUT
36
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
}
void sparse::sparse_matrix()
{
int x=1,count=0;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
if(a[i][j]!=0)
{
b[x][0]=i;
b[x][1]=j;
b[x][2]=a[i][j];
x++;
count++;
}
}
}
b[0][0]=row;
b[0][1]=col;
b[0][2]=count;
cout<<"The Sparse Matrix is:\n";
for(int m=0;m<=count;m++)
{
for(int n=0;n<=2;n++)
{
38
cout<<b[m][n]<<"\t";
}
cout<<endl;
}
}
void sparse::transpose()
{
cout<<"Transpose of the matrix is :\n";
for(int i=0;i<col;i++)
{
for(int j=0;j<row;j++)
{
cout<<a[j][i]<<"\t";
}
cout<<"\n";
}
}
void sparse::sparse_transpose()
{
int x=1,count=0;
for(int i=0;i<col;i++)
{
for(int j=0;j<row;j++)
{
if(a[j][i]!=0)
{
b[x][0]=i;
b[x][1]=j;
b[x][2]=a[j][i];
x++;
count++;
39
}
}
}
b[0][0]=row;
b[0][1]=col;
b[0][2]=count;
cout<<"The Transpose of Sparse Matrix is:\n";
for(int m=0;m<=count;m++)
{
for(int n=0;n<=2;n++)
{
cout<<b[m][n]<<"\t";
}
cout<<endl;
}
}
void main()
{
clrscr();
sparse s1;
s1.read();
s1.display();
s1.sparse_matrix();
s1.transpose();
s1.sparse_transpose();
getch();
}
40
OUTPUT
41
};
void sparse::read()
{
for(int j=0;j<c1;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter the size of rows and columns of Second matrix: \n";
cin>>r2>>c2;
for( i=0;i<r2;i++)
{
42
for(int j=0;j<c2;j++)
{
cin>>b[i][j];
}
}
}
void sparse::display()
{
for(int j=0;j<c1;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
{
cout<<b[i][j]<<"\t";
}
cout<<"\n";
}
}
void sparse::s_matrix1()
{
43
int x=1,cnt1=0;
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
if(a[i][j]!=0)
{
c[x][0]=i;
c[x][1]=j;
c[x][2]=a[i][j];
x++;
cnt1++;
}
}
}
c[0][0]=r1;
c[0][1]=c1;
c[0][2]=cnt1;
cout<<"Sparse Matrix 1 is:\n";
for(int m=0;m<=cnt1;m++)
{
for(int n=0;n<=2;n++)
{
cout<<c[m][n]<<" ";
}
cout<<endl;
}
}
void sparse::s_matrix2()
44
{
int y=1,cnt2=0;
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
if(b[i][j]!=0)
{
d[y][0]=i;
d[y][1]=j;
d[y][2]=b[i][j];
y++;
cnt2++;
}
}
}
d[0][0]=r2;
d[0][1]=c2;
d[0][2]=cnt2;
cout<<"Sparse Matrix 2 is:\n";
for(int m=0;m<=cnt2;m++)
{
for(int n=0;n<=2;n++)
{
cout<<d[m][n]<<" ";
}
cout<<endl;
}
}
45
void sparse::s_add()
{
if(r1!=r2||c1!=c2)
{
else
{
e[0][0]=r1;
e[0][1]=c2;
int p=1,q=1,s=1;
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
if(c[p][0]==i&&c[p][1]==j&&d[q][0]==i&&d[q][1]==j)
{
e[s][0]=c[p][0];
e[s][1]=c[p][1];
e[s][2]=c[p][2]+d[q][2];
p++;
q++;
s++;
}
else if(c[p][0]==i&&c[p][1]==j)
{
e[s][0]=c[p][0];
e[s][1]=c[p][1];
e[s][2]=c[p][2];
46
s++;
p++;
}
else if(d[q][0]==i&&d[q][1]==j)
{
e[s][0]=d[q][0];
e[s][1]=d[q][1];
e[s][2]=d[q][2];
s++;
q++;
}
}
}
e[0][2]=s-1;
{
for(int n=0;n<=2;n++)
{
cout<<e[m][n]<<" ";
}
cout<<endl;
}
}
}
void main()
{
clrscr();
sparse s1;
47
s1.read();
s1.display();
s1.s_matrix1();
s1.s_matrix2();
s1.s_add();
getch();
}
48
OUTPUT
PROGRAM 12 : IMPLEMENTATION OF STACK USING ARRAYS
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 20
class Stack
{
int s[MAXSIZE];
int top;
public:
Stack()
{
top=-1;
}
void push();
int pop();
void traverse();
};
void Stack::push()
{
if(top==MAXSIZE-1)
{
cout<<"Stack is Full - OVERFLOW\n";
}
else
{
int item;
cout<<"Enter the element to be inserted : ";
cin>>item;
top=top+1;
s[top]=item;
}
}
int Stack::pop()
{
int item;
if(top== -1)
{
cout<<"Stack is Empty - UNDERFLOW\n";
}
else
{
cout<<"Deleted item";
item=s[top];
top=top-1;
}
return (item);
}
void Stack::traverse()
{
if (top!=-1)
{
cout<<"Stack elements are : \n";
for(int i=top;i>=0;i--)
{
cout<<s[i]<<" ";
}
}
else
cout<<"The Stack is Empty!\n";
}
void main()
{
int ch;
clrscr();
Stack s1;
cout<<"1. Push\n";
cout<<"2. Pop\n";
cout<<"3. Traverse\n";
cout<<"4. Exit\n";
do
{
cout<<"\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
s1.push();
break;
case 2:
s1.pop();
break;
case 3:
s1.traverse();
break;
case 4:
break;
default:
cout<<"Invalid choice";
}
}while(ch!=4);
getch();
}
OUTPUT
PROGRAM 13 : INFIX TO POSTFIX CONVERSION
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
# define MAX 30
class infpos
char stack[MAX];
int top;
char infix[MAX],postfix[MAX];
public:
infpos()
top=-1;
void push(char);
char pop();
void postfix_coversion();
};
if(top>MAX-1)
{
cout<<"Stack is full";
return;
top=top+1;
stack[top]=s;
char s;
if(top==-1)
cout<<"Stack is Empty";
s= stack[top];
top=top-1;
return(s);
switch(s)
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default :
return 0;
char s,temp;
int p=0,i=0,l;
cin>>infix;
l=strlen(infix);
while(i<l)
s=infix[i];
switch(s)
{
case '(' :
push(s);
break;
case ')' :
temp=pop();
postfix[p]=temp;
p++;
temp=pop();
break;
case '+' :
case '-' :
case '*' :
case '/' :
case '^' :
while(precedence(stack[top])>=precedence(s))
temp=pop();
postfix[p]=temp;
p++;
push(s);
break;
default:
postfix[p++]=s;
break;
i++;
while(top>=0)
postfix[p++]=pop();
postfix[p]='\0';
puts(postfix);
return;
void main()
clrscr();
infpos p1;
p1.postfix_coversion();
getch();
}
OUTPUT
PROGRAM 14 : POSTFIX EVALUATION
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<string.h>
# define MAX 30
class pos_val
char stack[MAX];
int top;
char postfix[MAX];
public:
pos_val()
top=-1;
void push(char);
char pop();
void find();
};
if(top>MAX-1)
{
cout<<"Stack is Full! - Stack Overflow";
top=top+1;
stack[top]=s;
char s;
if(top==-1)
s=stack[top];
top=top-1;
return(s);
char c;
int i=0,l,a,b,result,value;
cin>>postfix;
l=strlen(postfix);
while(i<l)
{
c=postfix[i];
switch(c)
case '+' :
b=pop();
a=pop();
push(a+b);
break;
case '-':
b=pop();
a=pop();
push(a-b);
break;
case '*' :
b=pop();
a=pop();
push(a*b);
break;
case '/' :
b=pop();
a=pop();
push(a/b);
break;
case '^' :
b=pop();
a=pop();
push(pow(a,b));
break;
default :
cin>>value;
push(value);
break;
i++;
result=pop();
void main()
clrscr();
pos_val p1;
p1.find();
getch();
}
OUTPUT
PROGRAM 15 : LINEAR QUEUE USING ARRAYS
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 5
class queue
{
private:
int front,rear,Q[MAXSIZE];
public:
queue()
{
front=-1;
rear=-1;
}
void insert();
void dele();
void traverse();
};
void queue::insert()
int item;
if(rear<MAXSIZE)
{
cout<<"Enter the item to insert:";
cin>>item;
if(front==-1&rear==-1)
{
front=0;
rear=0;
}
else
rear=rear+1;
Q[rear]=item;
}
void queue::dele()
{
int item;
if(front==-1)
{
cout<<"\nQueueis empty-Underflow"<<endl;
else
item=Q[front];
cout<<item<<"\nDeleted item"<<endl;
front=front+1;
}
if(front>rear)
{
front=-1;
rear=-1;
}
}
void queue::traverse()
{
int i;
if(front==-1)
cout<<"\n\tQueue is empty"<<endl;
else
{
cout<<"The elements are:\n";
for(i=front;i<=rear;i++)
cout<<"\t"<<Q[i]<<" \t ";
}
}
void main()
queue q;
int ch;
clrscr();
do
{
cout<<"\n*********MENU**********";
cout<<"\n1.Insertion"<<endl;
cout<<"2.Deletion"<<endl;
cout<<"3.Traverse"<<endl;
cout<<"4.exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:
q.insert();
break;
case 2:
q.dele();
break;
case 3:
q.traverse();
break;
case 4:
break;
default:
cout<<"Invalid choice!";
}
}while(ch!=4);
getch();
}
OUTPUT Commented [M1]:
PROGRAM 16 : CIRCULAR QUEUE
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 5
class queue
private:
int front,rear,Q[MAXSIZE];
public:
queue()
front=-1;
rear=-1;
void insert();
void dele();
void traverse();
};
void queue::insert()
int item;
if(rear<MAXSIZE)
if(front==-1&rear==-1)
front=0;
rear=0;
else
rear=(rear+1)%MAXSIZE;
Q[rear]=item;
void queue::dele()
int item;
if(front==-1)
cout<<"\nQueueis empty-Underflow"<<endl;
else
item=Q[front];
cout<<item<<"\nDeleted item"<<endl;
front=(front+1)%MAXSIZE;
if(front>rear)
front=-1;
rear=-1;
void queue::traverse()
int i;
if(front==-1)
cout<<"\n\tQueue is empty"<<endl;
else
for(i=front;i<=rear;i++)
cout<<"\t"<<Q[i]<<" \t ";
void main()
queue q;
int ch;
clrscr();
do
cout<<"\n*********MENU**********";
cout<<"\n1.Insertion"<<endl;
cout<<"2.Deletion"<<endl;
cout<<"3.Traverse"<<endl;
cout<<"4.exit"<<endl;
cin>>ch;
switch(ch)
case 1:
q.insert();
break;
case 2:
q.dele();
break;
case 3:
q.traverse();
break;
case 4:
break;
default:
cout<<"Invalid choice!";
}while(ch!=4);
getch();
}
OUTPUT
PROGRAM 17 : SINGLY LINKED LIST
#include<iostream.h>
#include<conio.h>
struct Node
{
int num;
Node*ptr;
};
class linkedlist
{
Node*Newnode,*start,*temp;
public: linkedlist()
{
start=NULL;
}
void insert();
void insbeg();
void insend();
void inspecific();
void delfirst();
void delend();
void delspecific();
void display();
};
void linkedlist::insert()
{
char ch='y';
start=NULL;
while(ch=='y')
{
Newnode=new Node;
cout<<"Enter a num : ";
cin>>Newnode->num;
if(start==NULL)
{
start=Newnode;
temp=Newnode;
}
else
{
temp->ptr=Newnode;
temp=Newnode;
}
cout<<"Do you want to continue (y or n)-";
cin>>ch;
}
temp->ptr=NULL;
}
void linkedlist::insbeg()
{
Node*begnode;
begnode=new Node;
cout<<"Enter the number to be inserted at the beginning : ";
cin>>begnode->num;
if(start==NULL)
{
begnode->ptr=NULL;
}
else
{
begnode->ptr=start;
}
start=begnode;
}
void linkedlist::insend()
{
Node*endnode,*loc;
endnode=new Node;
cout<<"\nEnter the number to be inserted at the end : ";
cin>>endnode->num;
endnode->ptr=NULL;
if(start==NULL)
{
start=endnode;
}
else
{
loc=start;
while(loc->ptr!=NULL)
{
loc=loc->ptr;
}
loc->ptr=endnode;
}
}
void linkedlist::inspecific()
{
Node*newnode,*tem;
int pos;
cout<<"Enter the position at which element has to be inserted : ";
cin>>pos;
tem=start;
for(int i=1;i<pos-1;i++)
{
tem=tem->ptr;
}
cout<<"Enter the item to be inserted at pos "<<pos<<" :";
cin>>newnode->num;
newnode->ptr=tem->ptr;
tem->ptr=newnode;
}
void linkedlist::delfirst()
{
Node*dnode;
if(start==NULL)
{
cout<<"No Item in Linkedlist";
}
else
{
cout<<"\nDeleted First Node";
dnode=start;
start=start->ptr;
delete dnode;
}
display();
}
void linkedlist::delend()
{
Node*dnode,*loc;
if(start==NULL)
{
cout<<"No Item in Linked List";
}
else if(start->ptr==NULL)
{
dnode=start;
start=NULL;
delete dnode;
}
else
{
loc=start;
dnode=start->ptr;
while(dnode->ptr!=NULL)
{
loc=dnode;
dnode=dnode->ptr;
}
loc->ptr=NULL;
delete dnode;
}
cout<<"After Deletion at end";
display();
}
void linkedlist::delspecific()
{
Node*dnode,*loc;
int item;
cout<<"Enter the item you want to delete :";
cin>>item;
dnode=start;
if(start==NULL)
{
cout<<"List is Empty";
}
else
{
loc=dnode;
if(dnode->num==item)
{
start=start->ptr;
delete dnode;
}
while(dnode!=NULL)
{
if(dnode->num==item)
{
loc->ptr=dnode->ptr;
delete dnode;
break;
}
loc=dnode;
dnode=dnode->ptr;
}
}
cout<<"After deleting "<<item;
display();
}
void linkedlist::display()
{
temp=start;
cout<<"\nLinked list is : ";
while(temp!=NULL)
{
cout<<temp->num<<" ";
temp=temp->ptr;
}
}
void main()
{
clrscr();
int n;
linkedlist l1;
cout<<" MENU ";
cout<<"\n1.Creation";
cout<<"\n2.Insertion at Beginning";
cout<<"\n3.Insertion at end ";
cout<<"\n4.Insertion at specific location";
cout<<"\n5.Deleting First Node";
cout<<"\n6.Deleting Last Node";
cout<<"\n7.Deleting specific Node";
cout<<"\n8.Display Linked List ";
cout<<"\n9.Exit\n ";
do
{
cout<<"\nEnter your choice ";
cin>>n;
switch(n)
{
case 1:
l1.insert();
break;
case 2:
l1.insbeg();
break;
case 3:
l1.insend();
break;
case 4:
l1.inspecific();
break;
case 5:
l1.delfirst();
break;
case 6:
l1.delend();
break;
case 7:
l1.delspecific();
break;
case 8:
l1.display();
break;
case 9:break;
default:
cout<<"\nInvalid choice";
break;
}
}while(n!=9);
getch();
}
OUTPUT
PROGRAM 18 : SINGLY LINKED LIST - SORTING
#include<iostream.h>
#include<conio.h>
struct Node
{
int num;
Node*ptr;
};
class sortsll
{
Node*Newnode,*start,*temp;
public:sortsll()
{
start=NULL;
}
void sort();
void display();
};
void sortsll::sort()
{
char ch='y';
while(ch=='y')
{
Newnode=new Node;
cout<<"Enter a num : ";
cin>>Newnode->num;
if(start==NULL)
{
start=Newnode;
temp=Newnode;
}
else
{
temp->ptr=Newnode;
temp=Newnode;
}
cout<<"Do you want to continue (y or n)-";
cin>>ch;
}
temp->ptr=NULL;
temp=start;
cout<<"\nSingly linkedlist \nBefore sorting is :\n";
while(temp!=NULL)
{
cout<<temp->num<<" ";
temp=temp->ptr;
}
Node *t1,*t2;
int t;
for(t1=start;t1!=NULL;t1=t1->ptr)
{
for(t2=t1->ptr;t2!=NULL;t2=t2->ptr)
{
if(t1->num>t2->num)
{
t=t1->num;
t1->num=t2->num;
t2->num=t;
}
}
}
}
void sortsll::display()
{
temp=start;
cout<<"\nAfter sorting is : \n";
while(temp!=NULL)
{
cout<<temp->num<<" ";
temp=temp->ptr;
}
}
void main()
{
clrscr();
sortsll s1;
s1.sort();
s1.display();
getch();
}
OUTPUT
PROGRAM 19 : SINGLY LINKED LIST – CONCATENATION
#include<iostream.h>
#include<conio.h>
struct Node
{
int num;
Node*ptr;
};
class sllcon
{
Node*Newnode,*start,*start2,*temp;
public:
void concat();
void display();
};
void sllcon::concat()
{
char ch='y';
start=NULL;
while(ch=='y')
{
Newnode=new Node;
cout<<"Enter a num : ";
cin>>Newnode->num;
if(start==NULL)
{
start=Newnode;
temp=Newnode;
}
else
{
temp->ptr=Newnode;
temp=Newnode;
}
cout<<"Do you want to continue (y or n)-";
cin>>ch;
}
temp->ptr=NULL;
temp=start;
cout<<"\nFirst Singly linkedlist : ";
while(temp!=NULL)
{
cout<<temp->num<<" ";
temp=temp->ptr;
}
Node*start2;
char c='y';
start2=NULL;
while(c=='y')
{
Newnode=new Node;
cout<<"\nEnter a num : ";
cin>>Newnode->num;
if(start2==NULL)
{
start2=Newnode;
temp=Newnode;
}
else
{
temp->ptr=Newnode;
temp=Newnode;
}
cout<<"Do you want to continue (y or n)-";
cin>>c;
}
temp->ptr=NULL;
temp=start2;
cout<<"\nSecond Singly linkedlist : ";
while(temp!=NULL)
{
cout<<temp->num<<" ";
temp=temp->ptr;
}
temp=start;
while(temp->ptr!=NULL)
{
temp=temp->ptr;
}
temp->ptr=start2;
}
void sllcon::display()
{
temp=start;
cout<<"\n\nConcatenated Linked list is : ";
while(temp!=NULL)
{
cout<<temp->num<<" ";
temp=temp->ptr;
}
}
void main()
{
clrscr();
sllcon s1;
s1.concat();
s1.display();
getch();
}
OUTPUT
PROGRAM 20 : SINGLY LINKED LIST – SWAPPING ANY TWO
NODES
#include<iostream.h>
#include<conio.h>
struct Node
{
int num;
Node *ptr;
};
class SLLSWAP
{
Node *nnode,*start,*temp;
public:
void swap();
void display();
};
void SLLSWAP::swap()
{
char ch='y';
start=NULL;
clrscr();
while(ch=='y')
{
nnode=new Node;
cout<<"\nEnter a number :";
cin>>nnode->num;
if(start==NULL)
{
start=nnode;
temp=nnode;
}
else
{
temp->ptr=nnode;
temp=nnode;
}
cout<<"\n Do you want to continue?('y'or'n')"<<endl;
cin>>ch;
}
temp->ptr=NULL;
temp=start;
cout<<"\nLinked list is : ";
while(temp!=NULL)
{
cout<<temp->num<<" ";
temp=temp->ptr;
}
Node *temp1,*temp2;
temp1=start;
temp2=(start->ptr)->ptr;
start=start->ptr;
start->ptr=temp1;
temp1->ptr=temp2;
cout<<"\nAfter swapping the list is : ";
temp=start;
while(temp!=NULL)
{
cout<<temp->num<<" ";
temp=temp->ptr;
}
}
void main()
{
SLLSWAP s1;
s1.swap();
getch();
}
OUTPUT
PROGRAM 21 : CIRCULAR LINKED LIST
#include<iostream.h>
#include<conio.h>
struct Node
{
int num;
Node*ptr;
};
class circularll
{
public:Node*Newnode,*start,*last,*temp;
circularll()
{
start=NULL;
}
void Create();
void Insbeg();
void Insend();
void Delbeg();
void Delend();
void Display();
};
void circularll::Create()
{
char ch='y';
start=NULL;
while(ch=='y')
{
Newnode=new Node;
cout<<"Enter a number:";
cin>>Newnode->num;
if(start==NULL)
{
start=Newnode;
temp=Newnode;
}
else
{
last->ptr=Newnode;
temp=Newnode;
}
last=temp;
cout<<"Do you want to continue?('y'or'n')-";
cin>>ch;
}
}
void circularll::Insbeg()
{
Node*newnode;
newnode=new Node;
cout<<"Enter number to be inserted at the beginning:";
cin>>newnode->num;
if(start==NULL)
{
start=newnode;
last=newnode;
last->ptr=start;
}
else
{
newnode->ptr=start;
start=newnode;
last->ptr=start;
}
}
void circularll::Insend()
{
Node *newnode;
newnode=new Node;
cout<<"\nEnter number to be inserted at the end:";
cin>>newnode->num;
if(start==NULL)
{
start=newnode;
last=newnode;
last->ptr=start;
}
else
{
last->ptr=newnode;
newnode->ptr=start;
last=newnode;
}
}
void circularll::Delbeg()
{
Node *dnode;
if(start==NULL)
{
cout<<"No item in the Linked List";
}
else if(start->ptr==start)
{
cout<<"List is empty";
dnode=start;
start=NULL;
delete dnode;
}
else
{
cout<<"Deleted First Node";
dnode=start;
start=dnode->ptr;
last->ptr=start;
delete dnode;
Display();
}
}
void circularll::Delend()
{
Node *dnode;
if(start==NULL)
{
cout<<"No item in the Linked list ";
}
else if(start->ptr==start)
{
cout<<"List is empty";
dnode=start;
start=NULL;
delete dnode;
}
else
{
cout<<"Deleted Last Node";
temp=start;
while((temp->ptr)->ptr!=start)
{
temp=temp->ptr;
}
dnode=temp->ptr;
temp->ptr=start;
delete dnode;
Display();
}
}
void circularll::Display()
{
if(start==NULL)
{
cout<<"No item in the Linked list ";
}
else
{
temp=start;
cout<<"\nCircular Linked List is: ";
do
{
cout<<temp->num<<" ";
temp=temp->ptr;
}while(temp!=start);
}
}
void main()
{
circularll c1;
clrscr();
int n;
cout<<"1.Creation"<<endl;
cout<<"2.Insertion at the Beginning"<<endl;
cout<<"3.Insertion at the End"<<endl;
cout<<"4.Deletion at the Beginning"<<endl;
cout<<"5.Deletion at the end"<<endl;
cout<<"6.Display"<<endl;
cout<<"7.Exit"<<endl;
do
{
cout<<"\nEnter your choice : ";
cin>>n;
switch(n)
{
case 1:
c1.Create();
break;
case 2:
c1.Insbeg();
break;
case 3:
c1.Insend();
break;
case 4:
c1.Delbeg();
break;
case 5:
c1.Delend();
break;
case 6 :
c1.Display();
break;
case 7: return;
default:
cout<<"\nInvalid choice";
break;
}
}while(n!=8);
getch();
}
OUTPUT
PROGRAM 22 : STACK IMPLEMENTATION USING LINKED LIST
#include<iostream.h>
#include<conio.h>
struct Node
{
int num;
Node *ptr;
};
class stack
{
Node*top,*item;
public:
void push();
void pop();
void display();
stack()
{
top=NULL;
}
};
void stack::push()
{
item=new Node;
cout<<"Enter item ";
cin>>item->num;
item->ptr=top;
top=item;
}
void stack::pop()
{
item=top;
top=top->ptr;
cout<<"Deleted item is:"<<item->num;
delete item;
if(top==NULL)
{
cout<<" \n Stack Underflow";
}
}
void stack::display()
{
if(top==NULL)
{
cout<<"Stack is Empty";
}
else
{
item=top;
cout<<"Elements are :";
while(item!=NULL)
{
cout<<item->num<<" ";
item=item->ptr;
}
}
}
void main()
{
clrscr();
stack l1;
int n;
cout<<" MENU ";
cout<<"\n1.Push";
cout<<"\n2.Pop";
cout<<"\n3.Display";
cout<<"\n4.Exit";
do
{
cout<<"\nEnter your choice: ";
cin>>n;
switch(n)
{
case 1:
l1.push();
break;
case 2:
l1.pop();
break;
case 3:
l1.display();
break;
case 4:
return;
default:
cout<<"\n Invalid Choice\n";
}
}while(n!=5);
getch();
}
OUTPUT
PROGRAM 23 : IMPLEMENTATION OF QUEUE USING LINKED LIST
#include<iostream.h>
#include<conio.h>
struct Node
{
int num;
Node *ptr;
};
class queue
{
Node *front,*rear,*item;
public:
void insertion();
void deletion();
void display();
queue()
{
front=NULL;
rear=NULL;
}
};
void queue::insertion()
{
item=new Node;
cout<<"Enter the number:";
cin>>item->num;
item->ptr=NULL;
if(front==NULL)
{
front=item;
rear=item;
}
else
{
rear->ptr=item;
rear=item;
}
}
void queue::deletion()
{
if(front==NULL)
{
cout<<"\n Queue UnderFlow" ;
}
else
{
item=front;
cout<<"\n Deleted item is :"<<item->num<<"\n";
front=front->ptr;
delete item;
}
}
void queue::display()
{
if(front==NULL)
{
cout<<"Queue is Empty";
}
item=front;
cout<<"Elements are :";
while(item!=NULL)
{
cout<<item->num<<" ";
item=item->ptr;
}
}
void main()
{
clrscr();
queue ob1;
int n;
cout<<"\n1.Insertion";
cout<<"\n2.Deletion";
cout<<"\n3.Display";
cout<<"\n4.Exist";
do
{
cout<<"\nEnter Your Choice:";
cin>>n;
switch(n)
{
case 1:
ob1.insertion();
break;
case 2:
ob1.deletion();
break;
case 3:
ob1.display();
break;
case 4:
return;
default:
cout<<"\n Invalid Choice!\n";
break;
}
}while(n!=5);
getch();
}
OUTPUT
PROGRAM 24 : DOUBLY LINKED LIST
#include<iostream.h>
#include<conio.h>
struct NODE
{
NODE *prev;
int num;
NODE *next;
};
class DLL
{
NODE *start,*Newnode,*last,*temp;
public: DLL()
{
start=NULL;
}
void CreateLL();
void InsBeg();
void InsEnd();
void DelBeg();
void DelEnd();
void Display();
};
void DLL::CreateLL()
{
char ch='y';
while(ch=='y')
{
Newnode=new NODE;
cout<<"Enter a number : ";
cin>>Newnode->num;
Newnode->next=NULL;
if(start == NULL)
{
Newnode->prev=NULL;
start=Newnode;
last=Newnode;
}
else
{
last->next=Newnode;
Newnode->prev=last;
last=Newnode;
}
cout<<"Do you want to Continue('y' or 'n')-";
cin>>ch;
}
last->next=NULL;
}
void DLL::InsBeg()
{
Newnode=new NODE;
cout<<"Enter number to be inserted at the beginning : ";
cin>>Newnode->num;
if(start == NULL)
{
start=Newnode;
Newnode->prev=NULL;
Newnode->next=NULL;
last=Newnode;
}
else
{
Newnode->prev=NULL;
Newnode->next=start;
start->prev=Newnode;
start=Newnode;
}
}
void DLL::InsEnd()
{
Newnode=new NODE;
cout<<"Enter number to be inserted at the end : ";
cin>>Newnode->num;
if(start == NULL)
{
start=Newnode;
Newnode->prev=NULL;
Newnode->next=NULL;
}
else
{
last->next=Newnode;
Newnode->prev=last;
Newnode->next=NULL;
last=Newnode;
}
}
void DLL::DelBeg()
{
NODE *dnode;
if(start == NULL)
{
cout<<"\nThe List is empty\n";
}
else if(start->next==NULL)
{
dnode=start;
start=NULL;
last=NULL;
delete dnode;
}
else
{
dnode=start;
start=start->next;
start->prev= NULL;
delete dnode;
}
cout<<"Deleted First Node\n";
Display();
}
void DLL::DelEnd()
{
NODE *dnode;
if(start == NULL)
{
cout<<"\nThe list is empty\n";
}
else if(start->next==NULL)
{
dnode=start;
start=NULL;
last=NULL;
delete dnode;
}
else
{
dnode=last;
last=last->prev;
last->next=NULL;
delete dnode;
}
cout<<"Deleted Last Node\n";
Display();
}
void DLL::Display()
{
if(start==NULL)
{
cout<<"No item in the List";
}
else
{
temp=start;
cout<<"\nForward Elements are: ";
while(temp!=NULL)
{
cout<<temp->num<<" ";
temp=temp->next;
}
temp=last;
cout<<"\nBackward Elements are: ";
while(temp!=NULL)
{
cout<<temp->num<<" ";
temp=temp->prev;
}
}
}
void main()
{
DLL d1;
clrscr();
int c;
cout<<"1. Creation"<<endl;
cout<<"2. Insertion at the Beginning"<<endl;
cout<<"3. Insertion at the End"<<endl;
cout<<"4. Deletion at the Begining"<<endl;
cout<<"5. Deletion at the End"<<endl;
cout<<"6. Display"<<endl;
cout<<"7. Exit"<<endl;
do
{
cout<<"\nEnter your choice: ";
cin>>c;
switch(c)
{
case 1:
d1.CreateLL();
break;
case 2:
d1.InsBeg();
break;
case 3:
d1.InsEnd();
break;
case 4:
d1.DelBeg();
break;
case 5:
d1.DelEnd();
break;
case 6:
d1.Display();
break;
case 7:
return;
default:
cout<<"Invalid Choice"<<endl;
}
}while(c!=8);
getch();
}
OUTPUT
PROGRAM 25 : CIRCULAR DOUBLY LINKED LIST
#include<iostream.h>
#include<conio.h>
struct Node
{
Node *prev;
int num;
Node *next;
};
class CDLL
{
Node *start,*Newnode,*last,*temp;
public: CDLL()
{
start=NULL;
}
void CreateDLL();
void InsBeg();
void InsEnd();
void DelBeg();
void DelEnd();
void Display();
};
void CDLL::CreateDLL()
{
char ch='y';
while(ch=='y')
{
Newnode=new Node;
cout<<"\nEnter a number : ";
cin>>Newnode->num;
if(start == NULL)
{
start=Newnode;
last=Newnode;
Newnode->prev=last;
Newnode->next=start;
}
else
{
last->next=Newnode;
Newnode->prev=last;
Newnode->next=start;
start->prev=Newnode;
last=Newnode;
}
cout<<"Do you want to Continue?('y'or'n')-";
cin>>ch;
}
}
void CDLL::InsBeg()
{
Newnode=new Node;
cout<<"\nEnter a number to be inserted at the beginning : ";
cin>>Newnode->num;
if(start==NULL)
{
start=Newnode;
last=Newnode;
Newnode->prev=last;
Newnode->next=start;
}
else
{
Newnode->prev=last;
Newnode->next=start;
start->prev=Newnode;
start=Newnode;
last->next=start;
}
}
void CDLL::InsEnd()
{
Newnode=new Node;
cout<<"\nEnter a number to be inserted at the end : ";
cin>>Newnode->num;
if(start == NULL)
{
start=Newnode;
last=Newnode;
Newnode->prev=last;
Newnode->next=start;
}
else
{
last->next=Newnode;
Newnode->prev=last;
Newnode->next=start;
last=Newnode;
start->prev=Newnode;
}
}
void CDLL::DelBeg()
{
Node *dnode;
if(start == NULL)
{
cout<<"\n The List is Empty\n";
}
else if(start->next==start)
{
dnode=start;
start=NULL;
last=NULL;
delete dnode;
}
else
{
dnode=start;
start=start->next;
start->prev= last;
last->next=start;
delete dnode;
}
cout<<"Deleted First Node";
Display();
}
void CDLL::DelEnd()
{
Node *dnode;
if(start == NULL)
{
cout<<"\nThe list is Empty\n";
}
else if(start->next==start)
{
dnode=start;
start=NULL;
last=NULL;
delete dnode;
}
else
{
dnode=last;
last=last->prev;
last->next=start;
start->prev=last;
delete dnode;
}
cout<<"Deleted Last Node";
Display();
}
void CDLL::Display()
{
if(start==NULL)
{
cout<<"\nNo item in the list";
}
else
{
temp=start;
cout<<"\nForward Elements are : ";
do
{
cout<<temp->num<<" ";
temp=temp->next;
}while(temp!=start);
temp=last;
cout<<"\nBackward Elements are : ";
do
{
cout<<temp->num<<" ";
temp=temp->prev;
}while(temp!=last);
}
}
void main()
{
CDLL d1;
clrscr();
int c;
cout<<"1. Creation"<<endl;
cout<<"2. Insertion at the Beginning"<<endl;
cout<<"3. Insertion at the End"<<endl;
cout<<"4. Deletion at the Begining"<<endl;
cout<<"5. Deletion-End"<<endl;
cout<<"6. Display"<<endl;
cout<<"7. Exit"<<endl;
do
{
cout<<"\nEnter choice: ";
cin>>c;
switch(c)
{
case 1:
d1.CreateDLL();
break;
case 2:
d1.InsBeg();
break;
case 3:
d1.InsEnd();
break;
case 4:
d1.DelBeg();
break;
case 5:
d1.DelEnd();
break;
case 6:
d1.Display();
break;
case 7:
return;
default:
cout<<"Invalid Choice"<<endl;
}
}while(c!=7);
getch();
}
OUTPUT