Unit II - Programs
List using arrays
#include<iostream>
#define max 10
using namespace std;
class Listarr
{
private:
int a[max],count;
public:
Listarr()
{
count=0;
}
void create()
{
int i;
cout<<"How many elements?\n";
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];count++;
}
}
void printList()
{
cout<<"\nElements in the array list\n";
for(int i=0;i<count;i++)
{
cout<<a[i];cout<<"\n";
}
}
void find(int x)
{
int ele;
cout<<”enter search element”;
cin>>ele;
for(int i=0;i<count-1;i++)
{
if(a[i]==ele)
{
cout<<”element found”;break;
}
else
cout<<”element not found”;
}
}
void insert(int x,int pos)
{
int i;
for(i=count-1;i>pos;i--)
{
a[i]=a[i-1];
}
a[pos]=x;count++;
}
void deleteele(int x)
{int i,pos;
for(int i=0;i<n;i++)
{
if(a[i]==x)
{
pos= i;break;
}
for(int j=pos;j<=count-1;j++)
{
a[j]=a[j+1];
}
count--;
}
}
Singly Linked List
#include<iostream>
#define max 10
using namespace std;
int n;
class LinkedList
{
private:
struct node
{
int id;
struct node *next;
}*head,*p,*tmp;
public:
LinkedList()
{
head=p=NULL;
void create()
{
cout<<"Enter how many nodes to be created?\n";
cin>>n;
cout<<"Enter the elements\n";
for(int i=0;i<n;i++)
{
tmp= new node;
cin>>tmp->id;
tmp->next=NULL;
if(head==NULL)
{
p=head=tmp;
}
else
{
p->next=tmp;
p=p->next;
}
}
}
void printList()
{
p=head;
cout<<"Elements in the List are\n";
while(p!=NULL)
{
cout<<p->id;cout<<"\n";
p=p->next;
}
}
void find(int x)
{
p=head;
while(p!=NULL && p->id!=x)
{
p=p->next;
}
if(p!=NULL)
cout<<p->id<<"\n";
else
cout<<"Element is not int the list\n";
void insert()
{
int pos;
p=head;
cout<<"Enter element\n";
tmp= new node;
cin>>tmp->id;
tmp->next=NULL;
cout<<"Enter position to be inserted\n";
cin>>pos;
if(pos==0)
{
tmp->next=head;
head=head->next;
}
else
{
tmp->next=p->next;
p->next=tmp;
}
}
void delete()
{
int pos;
cout<<"Enter the node to be deleted";
cin>>pos;
p=head;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
if(pos==0)
{
tmp=head;
head=tmp->next;
delete(tmp);
}
else
{
tmp=p->next;
p->next=tmp->next;
delete(tmp);
}
}
};
int main()
{
int ch,x;
LinkedList L;
L.create();
L.printList();
cout<<"Enter the element to be searched for\n";
cin>>x;
L.find(x);
L.insert();
L.delete();
}
Polynomial Addition using Singly Linked List
#include<iostream>
#define max 10
using namespace std;
int n;
class PolyLinkedList
{
private:
struct node
{
int coeff;
int exp;
struct node *next;
}*head,*p;
public:
LinkedList()
{
head->coeff=head->exp=-999;
head->next=NULL;
}
void create();
void printList();
void add(LinkedList,LinkedList);
};
void LinkedList::create()
{
struct node *tmp=NULL;
p=head;
cout<<"Enter how many nodes to be created?\n";
cin>>n;
for(int i=0;i<n;i++)
{
tmp= new node;
cout<<"Enter Coefficient and Exponential\n";
cin>>tmp->coeff;
cin>>tmp->exp;
tmp->next=NULL;
if(head->next==NULL)
{head->next=tmp;
p=tmp;
}
else
{
p->next=tmp;
p=p->next;
}
}
}
void LinkedList::printList()
{
p=head->next;
cout<<"Elements in the List are\n";
while(p!=NULL)
{
cout<<p->coeff;cout<<"x^";
cout<<p->exp;
cout<<"+";
p=p->next;
}
void LinkedList::add(LinkedList L1,LinkedList L2)
{
struct node *tmp,*p1,*p2,*p3;
p1=L1.head;
p2=L2.head;
p3=head;
while(p1!=NULL&&p2!=NULL)
{
tmp=new node;
if(p1->exp > p2->exp)
{
tmp->coeff=p1->coeff;
tmp->exp=p1->exp;
p3->next=tmp;
p3=p3->next;
p1=p1->next;
}
else if(p2->exp > p1->exp)
{
tmp->coeff=p2->coeff;
tmp->exp=p2->exp;
p3->next=tmp;
p3=p3->next;
p2=p2->next;
else
{
tmp->coeff=p1->coeff+p2->coeff;
tmp->exp=p1->exp;
p3->next=tmp;
p3=p3->next;
p1=p1->next;
p2=p2->next;
}
}
while(p1!=NULL)
{
tmp=new node();
tmp->coeff=p1->coeff;
tmp->exp=p1->exp;
p3->next=tmp;
p3=p3->next;
p1=p1->next;
}
while(p2!=NULL)
{
tmp=new node();
tmp->coeff=p2->coeff;
tmp->exp=p2->exp;
p3->next=tmp;
p3=p3->next;
p2=p2->next;
}
int main()
{
int ch,x;
LinkedList L1,L2,L3;
cout<<"\nEnter the first equation elements\n";
L1.create();
cout<<"\nEnter the second equation elements\n";
L2.create();
cout<<"Equations are\n";
L1.printList();
cout<<"\n";
L2.printList();
cout<<"\n";
L3.add(L1,L2);
L3.printList();
}
Doubly Linked List
#include<stdio.h>
#include<stdlib.h>
class DLL
{
private:
struct node
{
int data;
struct node *next;
struct node *prev;
}*tmp,*p,*head;
public:
DLL()
{
p=head=NULL;
}
void create(int n)
{
p=head;
for(int i=0;i<n;i++)
{
tmp=new node;
cin>>tmp->data;
tmp->next=NULL;
tmp->prev=NULL;
if(head==NULL)
{
head=p=tmp;
}
else
{
p->next=tmp;
p=p->next;
}
}
}
void insert()
{p=head;
int pos,ele;
cin>>pos;cin>>ele
tmp=new node;
tmp->data=ele;
tmp->next=tmp->prev=NULL;
if(pos==0)
{
tmp->next=head;
head->prev=tmp;
head=tmp;
}
else
{
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
if(p->next!=NULL)
{
tmp->next=p->next;
tmp->prev=p;
tmp->next->prev=tmp;
p->next=tmp;
}
else//last node
{
tmp->next=p->next;
tmp->prev=p;
p->next=tmp;
}
}
void delet()
{
p=head;
cin>>pos;
if(pos==0)
{
tmp=head;
head=head->next
head->prev=NULL;
delete(tmp);
}
else
{
for(int i=0;i<p;i++)
{
p=p->next;
}
if(p->next!=NULL)
{
tmp=p->next;
p->next=tmp->next;
tmp->next->prev=p;
delete(tmp);
}
else
{
tmp=p->next;
p->next=NULL;
delete(tmp);
}
}
void display()
{
p=head;
while(p!=NULL)
{
cout<<p->data);
p=p->next;
}
}