Program to insert element in array
#include<iostream.h>
#include<conio.h>
void main()
{
int a[50],item,n,i,k,j;
clrscr();
cout<<"\n\t\t\t How many elements in array : ";
cin>>n;
cout<<"\n\t\t\t Enter Elements of array : ";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"\n\t\t\t Elements of array ";
for(i=1;i<=n;i++)
{
cout<<" "<<a[i];
}
cout<<"\n\n\t\t\t Enter Item to be inserted : ";
cin>>item;
cout<<"\n\t\t\t Enter the position where is to be inserted : ";
cin>>k;
for(j=n;j>=k;j--)
{
a[j+1]=a[j];
}
a[k]=item;
n++;
cout<<"\n\t\t\t New LIst of Array after insertion :";
for(i=1;i<=n;i++)
{
cout<<" " <<a[i];
}
getch();
}
_______________________________________________________
Program to delete element from array
#include<iostream.h>
#include<conio.h>
void main()
{
int a[50],item,n,i,k,j;
clrscr();
cout<<"\n\n\t\t\t How many elements in array : ";
cin>>n;
cout<<"\n\n\t\t\t Enter Elements of array : ";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"\n\n\t\t\t Elements of array ";
for(i=1;i<=n;i++)
{
cout<<" "<<a[i];
}
cout<<"\n\n\n\t\t\t Enter the position where is to be deleted : ";
cin>>k;
for(j=k;j<=n-1;j++)
{
a[j]=a[j+1];
}
n--;
cout<<"\n\n\t\t\t New LIst of Array after deletion :";
for(i=1;i<=n;i++)
{
cout<<" " <<a[i];
}
getch();
}
__________________________________________________________________________________
Program to perform operation on one way linked list
#include<iostream.h>
#include<conio.h>
class node
{
public :
int d; //data
node *p; // contains the address of the next node
};
//*******************************************************
class linked
{
public :
//***********************************************************
node *start;
linked()
{
start=NULL;
}
void insert_at_beg();
void insert_at_mid();
void insert_at_end();
int remove();
void display();
};
//************************************************************
void linked :: insert_at_end()
{
node *AVAIL;
if(start==NULL)
{
start=new node [1];
start->p=NULL;
cout<<"\n\n\t\t Enter Data to insert : ";
cin>>start->d;
}
else
{
AVAIL=start;
while(AVAIL->p!=NULL)
AVAIL=AVAIL->p;
AVAIL->p= new node [1];
AVAIL=AVAIL->p;
cout<<"\n\n\t\t Enter Data to insert : ";
cin>>AVAIL->d;
AVAIL->p=NULL;
}
}
//****************************************************************
void linked :: insert_at_mid()
{
node *AVAIL,*next;
int x;
AVAIL=start;
cout<<"\n\n\t\t Enter the Neighbour element : ";
cin>>x;
while(AVAIL->d!=x)
AVAIL=AVAIL->p;
next=AVAIL->p;
AVAIL->p=new node [1];
AVAIL=AVAIL->p;
AVAIL->p=next;
cout<<"\n\n\t\t Enter data to insert : ";
cin>>AVAIL->d;
}
//**************************************************************
void linked :: insert_at_beg()
{
node *AVAIL;
AVAIL=new node [1];
AVAIL->p=start;
start=AVAIL;
cout<<"\n\n\t\t Enter element to insert : ";
cin>>AVAIL->d;
}
//***************************************************************
int linked :: remove()
{
node *AVAIL,*prev;
int x;
if(start==NULL)
{
cout<<"\n\n\t\t Underflow -- No element to Delete : ";
return 0;
}
cout<<"\n\n\t\t Enter element to delete : ";
cin>>x;
if(start->d==x && start->p==NULL)
{
delete start;
start=NULL;
cout<<"\n\n\t\t Deletion successfull ";
return 0;
}
if(start->d==x)
{
AVAIL=start->p;
delete start;
start=AVAIL;
cout<<"\n\n\t\t Deletion Successfull ";
return 0;
}
AVAIL=start;
while(AVAIL->d!=x)
{
prev=AVAIL;
AVAIL=AVAIL->p;
}
prev->p=AVAIL->p;
cout<<"\n\n\t\t Deletion Successfull ";
return x;
}
//***********************************************************
void linked :: display()
{
node *AVAIL;
AVAIL=start;
cout<<"\n\n\t\t Linked List elements are : ";
while(AVAIL->p!=NULL)
{
cout<<""<<AVAIL->d<<" ";
AVAIL=AVAIL->p;
}
cout<<AVAIL->d<<endl;
}
//*****************************************************************
int main()
{
clrscr();
int n=0,a;
linked l;
do
{
cout<<"\n\t ************** M E N U **************\n";
cout<<"\n\t\t1.Insert at begining";
cout<<"\n\t\t2.Insert at middle";
cout<<"\n\t\t3.Insert at end";
cout<<"\n\t\t4.Delete ";
cout<<"\n\t\t5.Display elements";
cout<<"\n\t\t6.Exit\n";
cout<<"\n\t\tEnter option : ";
cin>>a;
switch(a)
{
case 1:
clrscr();
l.insert_at_beg();
break;
case 2:
clrscr();
l.insert_at_mid();
break;
case 3:
clrscr();
l.insert_at_end();
break;
case 4:
clrscr();
l.remove();
break;
case 5:
clrscr();
l.display();
break;
case 6:
clrscr();
n=1;
break;
default :
cout<<"\n\n\t\t\t Invalid choice ";
break;
}
}while(n!=1);
return 0;
}
__________________________________________________________________________________
Program to perform operations on double linked list
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
node *prev, *next;
}
*q, *AVAIL, *start=NULL;
//***************************************************************
class lk
{
public:
void create()
{
AVAIL = new node;
AVAIL -> next = NULL;
cout << "\n\n\n\t\t\tEnter Element : " ;
cin >> AVAIL -> data ;
if(start == NULL)
{
start = AVAIL;
AVAIL -> prev = NULL;
}
else
{
q= start;
while(q->next != NULL)
{
q = q->next;
}
q->next = AVAIL;
AVAIL->prev = q;
}
}
//*************************************************************
void display()
{
q=start;
while(q!=NULL)
{ cout<<" ";
cout<<" "<<q->data;
q = q->next;
}
}
//******************************************************************
void insert()
{
cout << "\n\n\t\t\t1.Insertion at Start " ;
cout << "\n\n\t\t\t2.Insertion at Middle " ;
cout << "\n\n\t\t\t3.Insertion at End " ;
int choice;
cout<<"\n\n\t\t\t\t\t";
cin>>choice;
switch(choice)
{
case 1:
clrscr();
AVAIL = new node;
cout<<"\n\n\n\t\t\tEnter Element :";
cin>>AVAIL->data;
start->prev =AVAIL;
AVAIL->next = start;
start = AVAIL;
AVAIL -> prev = NULL;
break;
case 2:
clrscr();
cout<<"\n\n\t\tEnter the data after which you want to add this : ";
int ch;
cin>>ch;
q= start;
while(q->next!=NULL)
{
if(q->data == ch)
{
AVAIL = new node;
cout<<"\n\n\n\t\t\tEnter Element : ";
cin>>AVAIL->data;
q->next->prev = AVAIL;
AVAIL->next = q->next;
AVAIL->prev = q;
q->next = AVAIL;
}
q = q->next;
}
break;
case 3:
clrscr();
AVAIL = new node;
cout<<"\n\n\n\t\t\tEnter Element : ";
cin>> AVAIL->data;
AVAIL->next = NULL;
q = start;
while(q->next != NULL)
{
q= q->next;
}
q->next = AVAIL;
AVAIL->prev = NULL;
}
}
//****************************************************************
void del()
{
clrscr();
cout<<"\n\n\t\tEnter the data you want to delete : ";
int num;
cin>>num;
q = start;
if (start->data == num)
start = start -> next;
else
{
while(q != NULL)
{
if(q->next->data == num)
{
AVAIL = q->next;
q->next = AVAIL->next;
AVAIL->next->prev = q;
delete AVAIL;
}
q = q->next;
}
}
}
};
//***********************************************************
void main()
{
clrscr();
lk a1;
while(1)
{
clrscr();
cout << " \n\n\n\t\t ******* MAIN MENU ******" ;
cout << "\n\n\n\t\t\t1. Adding Elements " ;
cout << "\n\n\t\t\t2. Displaying Elements " ;
cout << "\n\n\t\t\t3. Insertion " ;
cout << "\n\n\t\t\t4. Deletion " ;
cout << "\n\n\t\t\t5. Exit " ;
char ch;
ch=getch();
switch(ch)
{
case '1':
clrscr();
a1.create();
break;
case '2':
clrscr();
cout<<"\n\n\n\t\t\t Elements are : ";
cout<<"\n\n\n\n\n\n\t\t\t ";
a1.display();
getch();
break;
case '3':
clrscr();
a1.insert();
break;
case '4':
clrscr();
a1.del();
break;
case '5':
exit(1);
}
}
}
_________________________________________________________________________________
Program to perform operations on circular linked list
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
node *link;
}
*AVAIL, *q, *last=NULL;
//******************************************************************
class linkedlist
{
public:
//******************************************************************
void add()
{
AVAIL = new node;
cout<<"\n\n\n\t\t\tEnter data : ";
cin>> AVAIL->data;
if(last == NULL)
{
last = AVAIL;
last -> link = last;
cout<<"\n\n\t\t\tNode added.";
q = last;
}
else
{
AVAIL->link = q;
last -> link = AVAIL;
last = AVAIL;
cout<<"\n\n\t\t\tNode added.";
}
}
//*****************************************************************
void display()
{
q = last -> link;
while (q != last)
{
cout<<q->data<<" ";
q = q->link;
}
cout<<q->data<<" ";
}
//*******************************************************************
void insert()
{
clrscr();
cout<<"\n\n\n\n\t\t\t 1. Insertion at start";
cout<<"\n\n\t\t\t 2. Insertion at middle";
cout<<"\n\n\t\t\t 3. Insertion at end";
char ch;
ch = getch();
clrscr();
switch(ch)
{
case '1':
AVAIL = new node;
cout<<"\n\n\t\t\tEnter data : ";
cin>>AVAIL -> data;
q = last -> link;
AVAIL -> link = q;
last -> link = AVAIL;
cout<<"\n\n\t\t\tNode added.";
break;
case '2':
AVAIL = new node;
cout<<"Enter data \n";
cin>>AVAIL -> data;
int ch;
cout<<"Enter data after which you want to put this\n";
cin>>ch;
q = last -> link;
while(q != last)
{
if(q -> data == ch)
{
AVAIL -> link = q -> link;
q -> link = AVAIL;
cout<<"Node added \n";
break;
}
else
{
q = q -> link;
}
}
break;
case '3':
AVAIL = new node;
cout<<"\n\n\n\t\t\tEnter data \n";
cin>>AVAIL -> data;
AVAIL -> link = last -> link;
last -> link = AVAIL;
last = AVAIL;
cout<<"\n\n\t\tNode added ";
break;
}
}
//*********************************************************************
void del()
{
int ch;
cout<<"\n\n\n\t\t Enter data you want to delete ";
cin>>ch;
q = last -> link;
while(q != last)
{
//node *p;
// p = last->link;
//while()
//if()
AVAIL = last -> link;
if(last -> link -> data == ch)
{
// AVAIL = last -> link;
last -> link = AVAIL -> link;
//last = AVAIL -> link;
delete AVAIL;
}
else if(q -> link -> data == ch)
{
AVAIL = q -> link;
q -> link = AVAIL -> link;
cout<<"\n\n\n\t\t\tNode deleted \n"<<AVAIL ->data;
delete AVAIL;
break;
}
q = q -> link;
}
}
//******************************************************************
void rev()
{
node *p1, *p2, *p3;
p1 = last -> link;
p2 = p1 -> link;
p3 = p2 -> link;
q = p1;
p2 -> link =p1;
while (p3 != last)
{
p1 = p2;
p2 = p3;
p3 = p3 -> link;
p2 -> link = p1;
}
p3 -> link = p2;
q -> link = last;
last = q;
cout<<"\nAfter reversing\n";
display();
}
//*******************************************************************
};
//******************************************************************
void main()
{
linkedlist a1;
clrscr();
while(1)
{
clrscr();
cout<<"\n\t\t**********MAIN MENU***********";
cout<<"\n\n\t\t1.Insertion in Circular Linked List";
cout<<"\n\n\t\t2.Diplay data in Circular Linked List";
cout<<"\n\n\t\t3.Insert Node in Circular Linked List";
cout<<"\n\n\t\t4.Delete specified node in Circular Linked List";
cout<<"\n\n\t\t5.Reverse";
cout<<"\n\n\t\t6.Exit";
cout<<"\n\n\n\n\t\t Enter your choice : ";
char ch;
ch=getch();
clrscr();
switch (ch)
{
case '1':
a1.add();
break;
case '2':
a1. display();
getch();
break;
case '3':
a1.insert();
getch();
break;
case '4':
a1.del();
getch();
break;
case '5':
a1.rev();
getch();
break;
case '6':
exit(0);
}
}
getch();
}
thanx
ReplyDelete