You are on page 1of 90

ALL OPERATION - CIRCULAR QUEUE

 #include
using namespace std;
class cQueue
{
int a[5];
int front,rear,maxsize;
public:
cQueue()
{
maxsize=5;
front=rear=-1
}
int isEmpty()
{
if(front==-1)
return 1;
else
return 0;
}
int isFull()
{
if((front==rear+1) ||(front==0 && rear=maxsize-1))
{
return 1;
}
else
{
return 0;
}
}
void enQueue(int item)
{
if(isFull())
{
cout<<"The queue is in overflow condition";
}
else
{
if(front==-1)
{
front=0;
}
rear=(rear+1)%maxsize;
a[rear]=item;
cout<<"item enqueue in the list is:"<<item;
}
}
int deQueue()
{
int item;
if(isEmpty())
{
cout<<"Queue is empty\n";
}
else
{
item=a[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front=(front+1)%maxsize;
}
cout<<"The item dequeue from list is:"<<item;
}
}
};
int main()
{
cQueue obj;
int ch,item;
while(1)
{
cout<<"1 for enqueue\n";
cout<<"2 for dequeue\n";
cout<<"3 for display\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter item:";
cin>>item;
obj.enQueue(item);
break;
case 2:
item=obj.deQueue();
break;
case 3:
obj

}
}
return 0;
}

ALL OPERATION - DOUBLY LINKED LIST


#include
using namespace std;
struct node
{
int info;
struct node *next,*prev;
};
class operation
{
struct node *head;
public:
operation()
{
head=NULL;
}
struct node* createNode(int item)
{
struct node *temp=new struct node;
if(temp!=NULL)
{
temp->info=item;
temp->prev=temp->next=NULL;
}
return temp;
}
void insertAtBeg(int item)
{
struct node *newnode=createNode(item);
if(newnode==NULL)
{
cout<<"Overflow condition\n";
}
else
{
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next=head;
head->prev=newnode;
head=newnode;
}
cout<<"Item inserted\n";
travesing();
}
}
void travesing()
{
struct node *temp=head;
if(temp==NULL)
{
cout<<"underflow";
}
else
{
while(temp!=NULL)
{
if(temp->prev==NULL)
{
cout<<"\\0<-";
}
else
{
coutinfo<<"<-";
}
cout<info<<"- style="box-sizing: border-box;">";
if(temp->next==NULL)
{
cout<<"\\0\n";
}
else
{
coutinfo<<endl;
}

temp=temp->next;
}
}
}
void insertAtEnd(int item)
{
struct node *newnode=createNode(item);
if(newnode==NULL)
{
cout<<"overflow\n";
}
else
{
if(head==NULL)
{
head=newnode;
}
else
{
struct node *temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
newnode->prev=temp;
}
travesing();
}
}

};
int main()
{
operation obj;
int item,ch;
while(1)
{
cout<<"\n1 for insert at begning\n";
cout<<"2 for insert at ending\n";
cout<<"3 for Travesing\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter item:";
cin>>item;
obj.insertAtBeg(item);
break;
case 2:
cout<<"Enter item:";
cin>>item;
obj.insertAtEnd(item);
break;
case 3:
obj.travesing();
break;
}
}

return 0;
}</endl;
</info<<"->
ALL OPERATION - LINKED LIST

 #include
using namespace std;
struct node
{
int info;
struct node *link;
};
class LinkedOperation
{
struct node *head;
public:
LinkedOperation()
{
head=NULL;
}
struct node* createNode(int item)
{
struct node *temp=new struct node;
if(temp!=NULL)
{
temp->info=item;
temp->link=NULL;
}
return temp;
}
void insertNodeAtBeg(int item)
{
struct node *newnode=createNode(item);
if(newnode==NULL)
{
cout<<"Memory is in overflow\n";
}
else
{
struct node *temp=head;
head=newnode;
head->link=temp;
cout<<"Item inserted\n";
}
}
void insertAtEnd(int item)
{
struct node *newnode=createNode(item);
if(head==NULL)
{
head=newnode;
}
else
{
struct node *ptr=head;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
ptr->link=newnode;
cout<<"Item inserted at end\n";
}
}
void traverse()
{
if(head==NULL)
{
cout<<"List is empty";
}
else
{
struct node *ptr=head;
while(ptr!=NULL)
{
cout<info<<"->";
ptr=ptr->link;
}
cout<<"NULL\n";
}
}
void searchItem(int item)
{
struct node *ptr=head;
int loc=0,t=1;
while(ptr!=NULL)
{
if(item==ptr->info)
{
t=0;
break;
}
else
{
loc++;
}
ptr=ptr->link;
}
if(t==1)
{
cout<<"item not found";
}
else
{
cout<<"item found at position "<<loc+1;
}
}
void insertAtMiddle(int preItem,int item)
{
struct node *temp=head;
struct node *newnode=createNode(item);
struct node *preNode,*postNode;
preNode=postNode=NULL;
while(temp!=NULL)
{
if(temp->info==preItem)
{
preNode=temp;
postNode=temp->link;
break;
}
temp=temp->link;
}
if(preNode==NULL)
{
cout<<"Invalid preitem";
}
else
{
preNode->link=newnode;
newnode->link=postNode;
cout<<"The item inserted\n";
cout<<"The list after insertion:\n";
traverse();
}
}
void deletePos(int pos)
{
if(head==NULL)
{
cout<<"Linked list is in underflow condition";
}
else if(pos==1)
{
head=head->link;
traverse();
}
else
{
int cnt=countNode();
if(pos>cnt)
{
cout<<"Invalid position";
}
else
{
struct node *temp=head,*prev=NULL;
int i;
for(i=1;i<pos;i++)
{
prev=temp;
temp=temp->link;
}
prev->link=temp->link;
cout<<"Item deleted\n";
cout<<"The list after deletion\n";
traverse();
}

}
}
int countNode()
{
struct node *temp=head;
int cnt=0;
while(temp!=NULL)
{
cnt++;
temp=temp->link;
}
return cnt;
}
};
int main()
{
LinkedOperation obj;
int ch,item,preItem,pos;
while(1)
{
cout<<"1 for insertion at beginning\n";
cout<<"2 for insertion at ending\n";
cout<<"3 for insert after a give item\n";
cout<<"4 for delete item\n";
cout<<"5 for travesing\n";
cout<<"6 for searching\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter item to be inserted:";
cin>>item;
obj.insertNodeAtBeg(item);
break;
case 2:
cout<<"Enter item to be inserted:";
cin>>item;
obj.insertAtEnd(item);
break;
case 3:
cout<<"Enter preceding item:";
cin>>preItem;
cout<<"Enter item:";
cin>>item;
obj.insertAtMiddle(preItem,item);
break;
case 4:
cout<<"Enter position to be deleted:";
cin>>pos;
obj.deletePos(pos);
break;
case 5:
cout<<"The list is as given below:\n";
obj.traverse();
break;
case 6:
cout<<"Enter item to be search:";
cin>>item;
obj.searchItem(item);
break;
}
}
return 0;
}</pos;i++)
</loc+1;
ALL OPERATION - LINKED QUEUE

 #include <iostream>
using namespace std;
struct node
{
char item;
struct node *link;
};
class LinkedQueue
{
struct node *head;
public:
LinkedQueue()
{
head=NULL;
}
struct node* createNode(char item)
{
struct node *temp=new struct node;
if(temp!=NULL)
{
temp->item=item;
temp->link=NULL;
}
return temp;
}
void enqueue(char item)
{
struct node *newnode=createNode(item);
if(newnode==NULL)
{
cout<<"Overflow condition";
}
else
{
if(head==NULL)
{
head=newnode;
}
else
{
struct node *ptr=head;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
ptr->link=newnode;
}
}
}
void deQueue()
{
if(head==NULL)
{
cout<<"underflow condition";
}
else
{
cout<<"The item dequeue from list is="<<head->item<<endl;
head=head->link;
}
}
void display()
{
if(head==NULL)
{
cout<<"list is empty";
}
else
{
struct node *ptr=head;
while(ptr!=NULL)
{
cout<<ptr->item<<endl;
ptr=ptr->link;
}
}
}

};
int main()
{
int ch;
char item;
LinkedQueue obj;
while(1)
{
cout<<"1 for enqueue";
cout<<"2 for dequeue";
cout<<"3 for display";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter item:";
cin>>item;
obj.enqueue(item);
break;
case 2:
obj.deQueue();
break;
case 3:
obj.display();
break;
case 4:
return 0;
}
}
return 0;
}

ALL OPERATION - QUEUE USING ARRAY


REPRESENTATION

 #include <iostream>
using namespace std;
class QueueArray
{
int a[100];
int maxsize,rear,front;
public:
QueueArray()
{
maxsize=100;
rear=-1;
front=-1;
}
void enQueue(int item)
{
if(rear==maxsize-1)
{
cout<<"The Queue is in overflow condition";
}
else if(rear==-1)
{
rear=0;
front=0;
}
else
{
rear++;
}
a[rear]=item;
cout<<"item added in queue";
}
void deQueue()
{
int item;
if(front==-1 || front>rear)
{
cout<<"Underflow";
return;
}
else if(front==rear)
{
item=a[front];
front=rear=-1;
}
else
{
item=a[front];
front++;
}
cout<<"The item dequeue from list is:"<<item<<endl;
}
void display()
{
cout<<"Front="<<front<<endl;
cout<<"Rear="<<rear<<endl<<endl;
if(rear==-1 || front>rear)
{
cout<<"Queue is in underflow condition";
}
else
{
for(int i=front;i<=rear;i++)
{
cout<<a[i]<<endl;
}
}
}
};
int main()
{
QueueArray obj;
int ch,item;
while(1)
{
cout<<"1 for enqueue\n";
cout<<"2 for dequeue\n";
cout<<"3 for display\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter item:";
cin>>item;
obj.enQueue(item);
break;
case 2:
obj.deQueue();
break;
case 3:
obj.display();
break;
case 4:
return 0;
}
}
return 0;
}

ALL OPERATION - STACK LINKED


REPRESENTATION

#include
using namespace std;
struct node
{
int info;
struct node *link;
};
class LinkedStack
{
struct node *top;
public:
LinkedStack()
{
top=NULL;
}
struct node* createNode(int item)
{
struct node *temp=new struct node;
if(temp!=NULL)
{
temp->info=item;
temp->link=NULL;
}
return temp;
}
void push(int item)
{
struct node *newnode=createNode(item);
if(newnode==NULL)
{
cout<<"Overflow";
}
else
{
if(top==NULL)
{
top=newnode;
}
else
{
struct node *temp=top;
top=newnode;
top->link=temp;
}
cout<<"Item push into the stack;";
}
}
int pop()
{
if(top==NULL)
{
cout<<"Undeflow condition";
return -1;
}
else
{
int item=top->info;
top=top->link;
return item;
}
}
int peek()
{
if(top==NULL)
{
cout<<"Undeflow condition";
return -1;
}
else
{
int item=top->info;
return item;
}
}

};
int main()
{
LinkedStack obj;
int ch,item;
while(1)
{
cout<<"1 for push\n";
cout<<"2 for pop\n";
cout<<"3 for peek\n";
cout<<"4 for exit\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter item to be push:";
cin>>item;
obj.push(item);
break;
case 2:
item=obj.pop();
if(item!=-1)
cout<<"Item poped from stack is="<<item;
break;
case 3:
item=obj.peek();
if(item!=-1)
cout<<"Item poped from stack is="<<item;
break;
case 4:
return 0;
}
}
return 0;
}
 </item;
</item;

ALL OPERATION - STACK USING ARRAY


REPRESENTATION

 #include
using namespace std;
class stackOperation
{
int st[100];
int maxsize;
int top;
public:
stackOperation()
{
maxsize=100;
top=-1;
}
int isFull()
{
if(top==maxsize-1)
{
return 1;
}
else
{
return 0 ;
}
}
int isEmpty()
{
if(top==-1)
{
return 1;
}
else
{
return 0;
}
}
void push(int item)
{
if(isFull())
{
cout<<"Stack is in overflow condition";
}
else
{
st[++top]=item;
cout<<"item push into the stack";
}
}
int pop()
{
if(isEmpty())
{
return NULL;
}
else
{
return st[top--];
}
}
int peek()
{
if(isEmpty())
{
return NULL;
}
else
{
return st[top];
}
}
};
int main()
{
int ch,item;
stackOperation obj;
while(1)
{
cout<<"1 for push\n";
cout<<"2 for pop\n";
cout<<"3 for peek\n";
cout<<"4 for exit\n";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter item to be push:";
cin>>item;
obj.push(item);
break;
case 2:
item=obj.pop();
if(item==NULL)
{
cout<<"Stack is in underflow condition\n";
}
else
{
cout<<"The item pop from stack is="<<item<<endl;
}
break;
case 3:
item=obj.peek();
if(item==NULL)
{
cout<<"Stack is in underflow condition\n";
}
else
{
cout<<"The item at the top of stack is="<<item<<endl;
}
break;
case 4:
return 0;
default:
cout<<"Wrong choice";
}
}
return 0;
}</item<<endl;
</item<<endl;

APPLICATION OF STACK - EVALUATION OF


POSTFIX EXPRESSION
#include
#include
#include
using namespace std;
class stack
{
int a[100];
int maxsize,top;
public:
stack()
{
maxsize=100;
top=-1;
}
void push(int item)
{
if(top >= maxsize -1)
{
printf("stack over flow");
return;
}
else
{
top = top + 1 ;
a[top]= item;
}
}
int pop()
{
int item;
if(top <0)
{
printf("stack under flow");
}
else
{
item = a[top];
top = top - 1;
return item;
}
}
void EvalPostfix(char postfix[])
{
int i ;
char ch;
int val;
int A, B ;
for (i = 0 ; postfix[i] != ')'; i++)
{
ch = postfix[i];
if (isdigit(ch))
{
push(ch - '0');//The value of '0' is 48 in ascii table
}
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
{
A = pop();
B = pop();
switch (ch)
{
case '*':
val = B * A;
break;
case '/':
val = B / A;
break;
case '+':
val = B + A;
break;
case '-':
val = B - A;
break;
}
push(val);
}
}
printf( " \n Result of expression evaluation : %d \n", pop()) ;
}
};
int main()
{
int i ;
stack obj;
char postfix[100];
printf("ASSUMPTION: There are only four operators(*, /, +, -) in an
expression and operand is single digit only.\n");
printf( " \nEnter postfix expression,\npress right parenthesis ')'
for end expression : ");
for (i=0;i<=99;i++)
{
scanf("%c", &postfix[i]);
if ( postfix[i] == ')' )
{
break;
}
}
obj.EvalPostfix(postfix);
return 0;
}

APPLICATION OF STACK - EVALUATION OF


PREFIX EXPRESSION

 #include
#include
#include
#include
using namespace std;
class stack
{
int a[100];
int maxsize,top;
public:
stack()
{
maxsize=100;
top=-1;
}
void push(int item)
{
if(top >= maxsize -1)
{
printf("stack over flow");
return;
}
else
{
top = top + 1 ;
a[top]= item;
}
}
int pop()
{
int item;
if(top <0)
{
printf("stack under flow");
}
else
{
item = a[top];
top = top - 1;
return item;
}
}
void EvalPrefix(char prefix[])
{
int i ;
char ch;
int val;
int A, B ;

for (i = strlen(prefix)-2 ; i>=0; i--)


{
ch = prefix[i];
if (isdigit(ch))
{
push(ch - '0');//The value of '0' is 48 in ascii table
}
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
{
A = pop();
B = pop();
switch (ch)
{
case '*':
val = B * A;
break;
case '/':
val = B / A;
break;
case '+':
val = B + A;
break;
case '-':
val = B - A;
break;
}
push(val);
}
}
printf( " \n Result of expression evaluation : %d \n", pop()) ;
}
};
int main()
{
int i ;
stack obj;
char prefix[100];
printf("ASSUMPTION: There are only four operators(*, /, +, -) in
an expression and operand is single digit only.\n");
printf( " \nEnter postfix expression,\npress right parenthesis ')'
for end expression : ");
for (i=0;i<=99;i++)
{
scanf("%c", &prefix[i]);
if ( prefix[i] == ')' )
{
break;
}
}
obj.EvalPrefix(prefix);
return 0;
}

APPLICATION OF STACK - INFIX TO POSTFIX


CONVERSION

#include
#include
#include
using namespace std;
class stack
{
char a[100];
int top,maxsize;
public:
stack()
{
maxsize=100;
top=-1;
}
void push(char item)
{
if(top==maxsize-1)
{
cout<<"Stack is in overflow condition";
}
else
{
top++;
a[top]=item;
}
}
char pop()
{
char item=a[top];
top--;
return item;
}
int is_operator(char symbol)
{
if(symbol == '^' || symbol == '*' || symbol == '/' ||
symbol == '+' || symbol =='-')
{
return 1;
}
else
{
return 0;
}
}
int precedence(char symbol)
{
if(symbol == '^'){
return(3);
}
else if(symbol == '*' || symbol == '/')
{
return(2);
}
else if(symbol == '+' || symbol == '-')
{
return(1);
}
else
{
return(0);
}
}
void InfixToPostfix(char infix_exp[], char postfix_exp[])
{
int i, j;
char item;
char x;
push('(');
strcat(infix_exp,")");
i=0;
j=0;
item=infix_exp[i];

while(item != '\0')
{
if(item == '(')
{
push(item);
}
else if( isdigit(item) || isalpha(item))
{
postfix_exp[j] = item;
j++;
}
else if(is_operator(item) == 1)
{
x=pop();
while(is_operator(x) == 1 &&
precedence(x)>= precedence(item))
{
postfix_exp[j] = x;
j++;
x = pop();
}
push(x);
push(item);
}
else if(item == ')')
{
x = pop();
while(x != '(')
{
postfix_exp[j] = x;
j++;
x = pop();
}
}
else
{
printf("\nInvalid infix Expression.\n");
}
i++;
item = infix_exp[i];
}
if(top>0)
{
printf("\nInvalid infix Expression.\n");
}
postfix_exp[j] = '\0';
}
void infixToPrefix(char infix[],char postfix[],char prefix[])
{
strrev(infix);
for(int i=0;i<strlen(infix);i++)
{
if(infix[i]=='(')
{
infix[i]=')';
}
else if(infix[i]==')')
{
infix[i]='(';
}
}
InfixToPostfix(infix,postfix);
strcpy(prefix,strrev(postfix));
}
};

int main()
{
char infix[100], postfix[100];
stack obj;
printf("\nEnter Infix expression : ");
gets(infix);
obj.InfixToPostfix(infix,postfix);
printf("Postfix Expression: ");
puts(postfix);
}

</strlen(infix);i++)

APPLICATION OF STACK - INFIX TO PREFIX


CONVERSION

#include
#include
#include
using namespace std;
class stack
{
char a[100];
int top,maxsize;
public:
stack()
{
maxsize=100;
top=-1;
}
void push(char item)
{
if(top==maxsize-1)
{
cout<<"Stack is in overflow condition";
}
else
{
top++;
a[top]=item;
}
}
char pop()
{
char item=a[top];
top--;
return item;
}
int is_operator(char symbol)
{
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+'
|| symbol =='-')
{
return 1;
}
else
{
return 0;
}
}
int precedence(char symbol)
{
if(symbol == '^')
{
return(3);
}
else if(symbol == '*' || symbol == '/')
{
return(2);
}
else if(symbol == '+' || symbol == '-')
{
return(1);
}
else
{
return(0);
}
}
void InfixToPostfix(char infix_exp[], char postfix_exp[])
{
int i, j;
char item;
char x;
push('(');
strcat(infix_exp,")");
i=0;
j=0;
item=infix_exp[i];
while(item != '\0')
{
if(item == '(')
{
push(item);
}
else if(isdigit(item) || isalpha(item))
{
postfix_exp[j] = item;
j++;
}
else if(is_operator(item) == 1)
{
x=pop();
while(is_operator(x) == 1 && precedence(x)>= precedence(item))
{
postfix_exp[j] = x;
j++;
x = pop();
}
push(x);
push(item);
}
else if(item == ')')
{
x = pop();
while(x != '(')
{
postfix_exp[j] = x;
j++;
x = pop();
}
}
else
{
printf("\nInvalid infix Expression.\n");
}
i++;
item = infix_exp[i];
}
if(top>0)
{
printf("\nInvalid infix Expression.\n");
}
postfix_exp[j] = '\0';
}
void infixToPrefix(char infix[],char postfix[],char prefix[])
{
strrev(infix);
for(int i=0;i<strlen(infix);i++)
{
if(infix[i]=='(')
{
infix[i]=')';
}
else if(infix[i]==')')
{
infix[i]='(';
}
}
InfixToPostfix(infix,postfix);
strcpy(prefix,strrev(postfix));
}
};

int main()
{
char infix[100], postfix[100],prefix[100];
stack obj;
printf("\nEnter Infix expression : ");
gets(infix);
obj.infixToPrefix(infix,postfix,prefix);
cout<<prefix;
}</prefix;
</strlen(infix);i++)

APPLICATION OF STACK - STRING


REVERSING

 #include
#include
using namespace std;
class stackOperation
{
char st[100];
int maxsize;
int top;
public:
stackOperation()
{
maxsize=100;
top=-1;
}
int isFull()
{
if(top==maxsize-1)
{
return 1;
}
else
{
return 0 ;
}
}
int isEmpty()
{
if(top==-1)
{
return 1;
}
else
{
return 0;
}
}
void push(char item)
{
if(isFull())
{
cout<<"Stack is in overflow condition";
}
else
{
st[++top]=item;
//cout<<"item push into the stack";
}
}
char pop()
{
if(isEmpty())
{
return NULL;
}
else
{
return st[top--];
}
}
};
int main()
{
stackOperation obj;
char data[100];
char a;
int i=0;
cout<<"Enter data:";
gets(data);//hello
while(data[i]!='\0')
{
obj.push(data[i]);
i++;
}
cout<<"The data In stack is:\n";
do
{
a=obj.pop();
cout<<a;
}
while(a!=NULL);
return 0;
}</a;
GRAPH - ADJACENCY MATRIX

#include <iostream>
using namespace std;
int main()
{
int i, j, v;
cout<<"Enter the number of vertexes: ";
cin>>v;
int mat[20][20];
cout<<"\n";
for(i = 0; i < v; i++)
{
for(j = i; j < v; j++)
{
if(i != j)
{
cout<<"Enter 1 if the vertex "<<i+1<<" is
adjacent to "<<j+1<<", otherwise 0: ";
cin>>mat[i][j];
mat[j][i] = mat[i][j];
}
else
mat[i][j] = 0;
}
}
cout<<"The matrix is:\n";
for(i=0;i<=v-1;i++)
{
for(j=0;j<=v-1;j++)
{
cout<<mat[i][j]<<"\t";
}
cout<<endl;
}
return 0;
}

GRAPH TRAVESING
#include<stdlib.h>
int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20],stack[20];
int delete();
void add(int item);
void bfs(int s,int n);
void dfs(int s,int n);
void push(int item);
int pop();
void main()
{
int n,i,s,ch,j;
char c,dummy;
printf("ENTER THE NUMBER VERTICES ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("THE ADJACENCY MATRIX IS\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}

do
{
for(i=1;i<=n;i++)
{
vis[i]=0;
}
printf("\nMENU");
printf("\n1.B.F.S");
printf("\n2.D.F.S");
printf("\nENTER YOUR CHOICE");
scanf("%d",&ch);
printf("ENTER THE SOURCE VERTEX :");
scanf("%d",&s);
switch(ch)
{
case 1:
bfs(s,n);
break;
case 2:
dfs(s,n);
break;
}
printf("DO U WANT TO CONTINUE(Y/N) ? ");
scanf("%c",&dummy);
scanf("%c",&c);
}
while((c=='y')||(c=='Y'));
}//main exit

void bfs(int s,int n)


{
int p,i;
add(s);
vis[s]=1;
p=delete();
if(p!=0)
{
printf(" %d",p);
}
while(p!=0)
{
for(i=1;i<=n;i++)
{
if((a[p][i]!=0)&&(vis[i]==0))
{
add(i);
vis[i]=1;
}
}
p=delete();
if(p!=0)
{
printf(" %d ",p);
}
}
for(i=1;i<=n;i++)
{
if(vis[i]==0)
{
bfs(i,n);
}
}
}

void add(int item)


{
if(rear==19)
{
printf("QUEUE FULL");
}
else
{
if(rear==-1)
{
q[++rear]=item;
front++;
}
else
{
q[++rear]=item;
}

}
}
int delete()
{
int k;
if((front>rear)||(front==-1))
{
return(0);
}
else
{
k=q[front++];
return(k);
}
}

void dfs(int s,int n)


{
int i,k;
push(s);
vis[s]=1;
k=pop();
if(k!=0)
{
printf(" %d ",k);
}
while(k!=0)
{
for(i=1;i<=n;i++)
{
if((a[k][i]!=0)&&(vis[i]==0))
{
push(i);
vis[i]=1;
}
}
k=pop();
if(k!=0)
{
printf(" %d ",k);
}
}
for(i=1;i<=n;i++)
{
if(vis[i]==0)
{
dfs(i,n);
}
}

void push(int item)


{
if(top==19)
printf("Stack overflow ");
else
stack[++top]=item;
}

int pop()
{
int k;
if(top==-1)
{
return(0);
}
else
{
k=stack[top--];
return(k);
}
}

HEAP
#include <iostream>
using namespace std;
class heap
{
int a[100];
int n,maxsize;
public:
heap()
{
maxsize=100;
n=-1;
}
void insertItem(int item)
{
if(n>maxsize)
{
cout<<"heap is in overflow condition";
}
else
{
n++;
a[n]=item;
reheapup();
cout<<"item inserted";
}
}
void deleteItem()
{
if(n==-1)
{
cout<<"underflow";
}
else
{
int item=a[0];
a[0]=a[n];
n--;
reheapDown();
}
}
void reheapDown()
{
int temp,parent,ptr,left,right;
parent=0;
left=2*parent+1;
right=2*parent+2;
while(left<=n)
{
if(a[left]>a[right])
{
ptr=left;
}
else
{
ptr=right;
}
if(a[parent]<a[ptr])
{
temp=a[ptr];
a[ptr]=a[parent];
a[parent]=temp;
}
else
{
break;
}
parent=ptr;
left=2*parent+1;
right=2*parent+2;
}
}
void reheapup()
{
int temp,ptr,parent;
ptr=n;
parent=(n-1)/2;
while(parent>=0 && a[ptr]>a[parent])
{
temp=a[ptr];
a[ptr]=a[parent];
a[parent]=temp;
ptr=parent;
parent=(ptr-1)/2;
}
}
void travesing()
{
for(int i=0;i<=n;i++)
{
cout<<a[i]<<endl;
}
}
};
int main()
{
int item,ch;
heap obj;
while(1)
{
cout<<"1 for insert\n";
cout<<"2 for travesing\n";
cout<<"3 for deletion\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter item:";
cin>>item;
obj.insertItem(item);
break;
case 2:
obj.travesing();
break;
case 3:
obj.deleteItem();
}
}
return 0;
}

QUICK SORT

/* C++ implementation of QuickSort */


#include
using namespace std;

// A utility function to swap two elements


void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}

/* This function takes last element as pivot, places


the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element

for (int j = low; j <= high - 1; j++)


{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}

/* The main function that implements QuickSort


arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);

// Separately sort elements before


// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

/* Function to print an array */


void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}

// Driver Code
int main()
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}

SORTING AND SEARCHING (LINEAR &


BINARY SEARCH, LINEAR & BUBBLE SORT)

 #include
void main()
{
int a[]={45,28,80,48,90};
int pos=-1,item,i;
printf("The elements in array:\n");
for(i=0;i<=4;i++)
{
printf("%d\n",a[i]);
}
printf("enter item to be search:");
scanf("%d",&item);
for(i=0;i<=4;i++)
{
if(a[i]==item)
{
pos=i+1;
break;
}
}
if(pos==-1)
{
printf("item not found");
}
else
{
printf("item found at position=%d",pos);
}
}
......................................................................
................................
#include
void main()
{
int a[]={10,20,30,40,50};
int pos=-1,item,i,beg,end,mid;
printf("The elements in array:\n");
for(i=0;i<=4;i++)
{
printf("%d\n",a[i]);
}
printf("enter item to be search:");
scanf("%d",&item);
beg=0;
end=4;
mid=(beg+end)/2;
while(beg<=end)
{
if(a[mid]==item)
{
pos=mid+1;
break;
}
else if(a[mid]>item)
{
end=mid-1;
}
else
{
beg=mid+1;
}
mid=(beg+end)/2;
}
if(pos==-1)
{
printf("item not found");
}
else
{
printf("item found at position=%d",pos);
}
}
......................................................................
........................
#include
void main()
{
int a[]={15,23,10,56,25};
int i,j,temp;
printf("The unsorted list of array:\n");
for(i=0;i<=4;i++)
{
printf("%d\n",a[i]);
}
for(i=0;i<=3;i++)
{
for(j=i+1;j<=4;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("The sorted list of array:\n");
for(i=0;i<=4;i++)
{
printf("%d\n",a[i]);
}
}
......................................................................
...................
#include
void main()
{
int a[]={15,23,10,56,25};
int i,j,temp;
printf("The unsorted list of array:\n");
for(i=0;i<=4;i++)
{
printf("%d\n",a[i]);
}
for(i=0;i<=4;i++)
{
for(j=4;j>i;j--)
{
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
printf("The sorted list of array:\n");
for(i=0;i<=4;i++)
{
printf("%d\n",a[i]);
}
}</a[j-1])

SORTING AND SERACH-QUICK SORT

void quicksort(int number[25],int first,int last)


{
int i, j, pivot, temp;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}

int main()
{
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
 </count;i++)
</count;i++)
</j){
</last)
</j)
</last)

SORTING(SELECTION SORT, INSERT SORT,


MERGE SORT)

 Selection Sort

#include
using namespace std;
void selectionSort(int arr[], int n)
{
int i, j, mindex,temp;
for (i = 0; i < n-1; i++)
{
mindex = i;
for (j = i+1; j < n; j++)
{
if (arr[j] < arr[mindex])
mindex = j;//4
}
temp=arr[i];
arr[i]=arr[mindex];
arr[mindex]=temp;
}
}
void printArray(int arr[], int size)
{
int i;
cout<<"Sorted list of array:\n";
for (i=0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

int main()
{
int a[100],i,n;
cout<<"Enter no of elements in array:";
cin>>n;
cout<<"Enter "<<n<<" style="box-sizing: border-box;" array:\n";
for(i=0;i<=n-1;i++)
{
cin>>a[i];
}
selectionSort(a,n);
printArray(a, n);
return 0;
}
......................................................................
.......................................
//insert Sort

#include
using namespace std;
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

int main()
{
int a[100],n,i;
cout<<"Enter no of elements in array:";
cin>>n;
cout<<"Enter "<<n<<" style="box-sizing: border-box;" in=""
elements="" array\n";
for(i=0;i<=n-1;i++)
{
cin>>a[i];
}
insertionSort(a, n);
printArray(a, n);
return 0;
}
......................................................................
........................
#include
void merge(int[], int, int, int);
void mergeSort(int a[], int p, int r)
{
int q;
if(p < r)
{
q = (p + r) / 2;//2
mergeSort(a, p, q);//0-2,3-4
mergeSort(a, q+1, r);
merge(a, p, q, r);
}
}
void merge(int a[], int p, int q, int r)
{
int b[5];
int i, j, k;
k = 0;
i = p;
j = q + 1;
while(i <= q && j <= r)
{
if(a[i] < a[j])
{
b[k] = a[i];
i++;
k++;
}
else
{
b[k] = a[j];
k++;
j++;
}
}
while(i <= q)
{
b[k++] = a[i++];
}
while(j <= r)
{
b[k++] = a[j++];
}
for(i=r; i >= p; i--)
{
a[i] = b[--k];
}
}

void printArray(int a[], int size)


{
int i;
for (i=0; i < size; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}

int main()
{
int arr[] = {32, 45, 67, 2, 7};
int len = sizeof(arr)/sizeof(arr[0]);//20/4=5
printf("Given array: \n");
printArray(arr, len);
mergeSort(arr, 0, len - 1);
printArray(arr, len);
return 0;
}</n<<"></n<<">

ALL OPERATION - ARRAY


#include
using namespace std;
class ArrayOP
{
int a[100];
int lb,ub;
public:
ArrayOP(int x[],int lb,int ub)
{
ArrayOP::lb=lb;
ArrayOP::ub=ub;
for(int i=lb-1;i<=ub-1;i++)
{
a[i]=x[i];
}
}
void Travesing()
{
for(int i=lb-1;i<=ub-1;i++)
{
cout<<a[i]<<endl;
}
}
void insertElement(int pos,int element)
{
for(int i=ub-1;i>=pos-1;i--)
{
a[i+1]=a[i];
}
a[pos-1]=element;
ub++;
cout<<"Item inserted\n\n";
Travesing();
}
void deleteElement(int pos)
{
if(pos>ub)
{
cout<<"Invalid position\n";
}
else
{
int item=a[pos-1];
for(int i=pos-1;i<=ub-1;i++)
{
a[i]=a[i+1];
}
ub--;
cout<<"Item delete :"<<item<<endl;
cout<<"The list after deletion\n";
Travesing();
}
}
int LinearSearch(int item)
{
int pos=-1;
for(int i=lb-1;i<=ub-1;i++)
{
if(item==a[i])
{
pos=i;
break;
}
}
return pos;
}
void LinearSort()
{
int i,j,temp;
for(i=lb-1;i<=ub-1;i++)
{
for(j=i+1;j<=ub-1;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
Travesing();
}
void bubbleSort()
{
int temp;
for(int i=lb-1;i<=ub-1;i++)
{
for(int j=ub-1;j>i;j--)
{
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
cout<<"The sorted list of array is:\n";
Travesing();
}
};
int main()
{
int a[100];
int i,n,ch,pos,item;
cout<<"Enter no of elements in array:";
cin>>n;
cout<<"Enter "<<n<<" style="box-sizing: border-box;" element";
for(i=0;i<=n-1;i++)
{
cin>>a[i];
}
ArrayOP obj(a,1,n);
while(1)
{
cout<<"Enter 1 for travesing:\n";
cout<<"Enter 2 for insert element\n";
cout<<"Enter 3 for delete element\n";
cout<<"Enter 4 for Linear Search\n";
cout<<"Enter 5 for Linear Sort\n";
cout<<"Enter 6 for Bubble Sort\n";
cout<<"Enter 7 for exit:\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Array elements:\n";
obj.Travesing();
break;
case 2:
cout<<"Enter item to be inserted:";
cin>>item;
cout<<"Enter position of item:";
cin>>pos;
obj.insertElement(pos,item);
break;
case 3:
cout<<"Enter position to be deleted:";
cin>>pos;
obj.deleteElement(pos);
break;
case 4:
cout<<"Enter item to be search:";
cin>>item;
pos=obj.LinearSearch(item);
if(pos==-1)
{
cout<<"Item not found\n";
}
else
{
cout<<"Item found at position "<<(pos+1)<<endl;
}
break;
case 5:
obj.LinearSort();
break;
case 6:
obj.bubbleSort();
break;
case 7:
return 0;
default:
cout<<"Wrong Choice";
}
}
return 0;
}</endl;
</n<<"></a[j-1])
</item<<endl;
</a[i]<<endl;

All Operation - BST

#include <iostream>
using namespace std;
struct node
{
int item;
struct node *left,*right;
};
class treeOperation
{
public:
struct node *root;
treeOperation()
{
root=NULL;
}
struct node* createNode(int item)
{
struct node *temp=new struct node;
if(temp!=NULL)
{
temp->item=item;
temp->left=temp->right=NULL;
}
return temp;
};
void insertNode(int item)
{
struct node *newnode=createNode(item);
if(newnode==NULL)
{
cout<<"Overflow";
}
else
{
if(root==NULL)
{
root=newnode;
}
else
{
struct node *temp=root,*parent=NULL;
while(temp!=NULL)
{
parent=temp;
if(item>temp->item)
{
temp=temp->right;
}
else
{
temp=temp->left;
}
}
if(parent->item>item)
{
parent->left=newnode;
}
else
{
parent->right=newnode;
}
}
}
}
void searchItem(int item)
{
struct node *temp=root;
int loc=0,t=0;
if(temp==NULL)
{
cout<<"The tree is empty";
}
else
{
while(temp!=NULL)
{
if(temp->item==item)
{
t=1;
break;
}
else if(temp->item<item)
{
loc=(loc*2)+2;
temp=temp->right;
}
else
{
loc=(loc*2)+1;
temp=temp->left;
}
}
if(t==1)
{
cout<<"item found at location:"<<(loc+1)<<endl;
}
else
{
cout<<"item not found";
}
}
}
void preOrderTravesing(struct node *temp)
{
if(temp==NULL)
{
return;
}
else
{
cout<<temp->item<<endl;
preOrderTravesing(temp->left);
preOrderTravesing(temp->right);
}
}
void inOrderTravesing(struct node *temp)
{
if(temp==NULL)
{
return;
}
else
{
inOrderTravesing(temp->left);
cout<<temp->item<<endl;
inOrderTravesing(temp->right);
}
}
void postOrderTravesing(struct node *temp)
{
if(temp==NULL)
{
return;
}
else
{
postOrderTravesing(temp->left);
postOrderTravesing(temp->right);
cout<<temp->item<<endl;
}
}
void serachForDeletion(struct node *&curr,struct node *&parent,int item)
{
while(curr!=NULL && curr->item!=item)
{
parent=curr;
if(item<curr->item)
{
curr=curr->left;
}
else
{
curr=curr->right;
}
}
}
struct node* findMin(struct node *temp)
{
while(temp->left!=NULL)
{
temp=temp->left;
}
return temp;
}
void deleteNode(struct node *tree,int item)
{
if(root==NULL)
{
cout<<"tree is empty";
}
else
{
struct node *curr,*parent;
curr=tree;
parent=NULL;
serachForDeletion(curr,parent,item);
if(curr==NULL)
{
cout<<"item not found";
}
else
{
if(curr->left==NULL && curr->right==NULL)
{
if(curr==root)
{
root=NULL;
}
else
{
if(parent->left==curr)
{
parent->left=NULL;
}
else
{
parent->right=NULL;
}
}
cout<<"item deleted";
}
else if(curr->left!=NULL && curr->right!=NULL)
{
struct node *min=findMin(curr->right);
curr->item=min->item;
deleteNode(curr->right,min->item);
}
else
{
struct node *child;
if(curr->left!=NULL)
{
child=curr->left;
}
else
{
child=curr->right;
}
if(curr==root)
{
root=child;
}
else
{
if(parent->left==curr)
{
parent->left=child;
}
else
{
parent->right=child;
}
}
cout<<"item deleted";
}
}
}
}
};
int main()
{
int item,ch;
treeOperation obj;
while(1)
{
cout<<"1 for insert item\n";
cout<<"2 for search item\n";
cout<<"3 for preorder travesing\n";
cout<<"4 for inorder travesing\n";
cout<<"5 for postorder travesing\n";
cout<<"6 for delete item\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter item:";
cin>>item;
obj.insertNode(item);
break;
case 2:
cout<<"Enter item to be search:";
cin>>item;
obj.searchItem(item);
break;
case 3:
cout<<"The preorder travesing is as:\n";
obj.preOrderTravesing(obj.root);
break;
case 4:
cout<<"The inorder travesing is as:\n";
obj.inOrderTravesing(obj.root);
break;
case 5:
cout<<"The postorder travesing is as:\n";
obj.postOrderTravesing(obj.root);
break;
case 6:
cout<<"Enter item to be deleted:";
cin>>item;
obj.deleteNode(obj.root,item);
break;
}
}
return 0;
}

ALL OPERATION - CIRCULAR LINKED LIST


 #include
using namespace std;
struct node
{
int info;
struct node *link;
};
class circularOperation
{
struct node *head;
public:
circularOperation()
{
head=NULL;
}
struct node* createNode(int item)
{
struct node *temp=new struct node;
if(temp!=NULL)
{
temp->info=item;
temp->link=NULL;
}
return temp;
}
void insertAtBeg(int item)
{
struct node *newnode=createNode(item);
if(newnode==NULL)
{
cout<<"Overflow condition";
}
else
{
if(head==NULL)
{
head=newnode;
head->link=head;
}
else
{
struct node *temp,*ptr;
ptr=temp=head;
head=newnode;
head->link=temp;
while(ptr->link!=temp)
{
ptr=ptr->link;
}
ptr->link=head;
}
travesing();
}
}
void insertAtEnd(int item)
{
struct node *newnode=createNode(item);
if(newnode==NULL)
{
cout<<"Overflow";
}
else
{
if(head==NULL)
{
head=newnode;
head->link=newnode;
}
else
{
struct node *temp=head;
newnode->link=head;
while(temp->link!=head)
{
temp=temp->link;
}
temp->link=newnode;
}
travesing();
}
}
void travesing()
{
if(head==NULL)
{
cout<<"List is in underflow condition";
}
else
{
struct node *temp=head;
while(temp->link!=head)
{
cout<info<<"->";
temp=temp->link;
}
cout<info<<"->";
temp=temp->link;
cout<info;
}
}
void deleteAtBeg()
{
if(head==NULL)
{
cout<<"List is in underflow condition";
return;
}
else if(head->link==head)//one node
{
cout<<"The deleted item is="<info<<endl;
head=NULL;
}
else
{
struct node *temp,*ptr;
cout<<"The deleted itemis="<info<<endl;
temp=ptr=head;
head=head->link;
while(temp->link!=ptr)
{
temp=temp->link;
}
temp->link=head;
}
travesing();
}
void deleteAtEnd()
{
if(head==NULL)
{
cout<<"The list is in underflow condition";
return;
}
else if(head==head->link)
{
cout<<"The deleted item is:"<info<<endl;
head=NULL;
}
else
{
struct node *ptr,*prev;
ptr=head;
prev=NULL;
while(ptr->link!=head)
{
prev=ptr;
ptr=ptr->link;
}
cout<<"The deleted item is="<info<<endl;
prev->link=head;
}
travesing();
}
void deleteByItem(int item)
{
//do urself
}
};
int main()
{
int ch,item;
circularOperation obj;
while(1)
{
cout<<"\n1 for insert at beginning\n";
cout<<"2 for insert at End\n";
cout<<"3 for travesing\n";
cout<<"4 for delation at the beginning\n";
cout<<"5 for deletion at end\n";
cout<<"6 for deletion by item\n";
cout<<"7 for exit\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter item to be insert:";
cin>>item;
obj.insertAtBeg(item);
break;
case 2:
cout<<"Enter item to be inserted:";
cin>>item;
obj.insertAtEnd(item);
break;
case 3:
cout<<"The list is:\n";
obj.travesing();
break;
case 4:
obj.deleteAtBeg();
break;
case 5:
obj.deleteAtEnd();
break;
case 6:
obj.deleteByItem();
break;
case 7:
return 0;
}
}
return 0;
}</endl;
</endl;
</endl;
</endl;

CIRCULAR LIST- ALL OPERATIONS


#include<iostream>
using namespace std;
struct node
{
int info;
struct node *next,*prev;
};
class operation
{
struct node *head;
public:
operation()
{
head=NULL;
}
struct node* createNode(int item)
{
struct node *temp=new struct node;
if(temp!=NULL)
{
temp->info=item;
temp->prev=temp->next=NULL;
}
return temp;
}
void insertAtBeg(int item)
{
struct node *newnode=createNode(item);
if(newnode==NULL)
{
cout<<"Overflow condition\n";
}
else
{
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next=head;
head->prev=newnode;
head=newnode;
}
cout<<"Item inserted\n";
travesing();
}
}
void travesing()
{
struct node *temp=head;
if(temp==NULL)
{
cout<<"underflow";
}
else
{
while(temp!=NULL)
{
if(temp->prev==NULL)
{
cout<<"\\0<-";
}
else
{
cout<<temp->prev->info<<"<-";
}
cout<<temp->info<<"->";
if(temp->next==NULL)
{
cout<<"\\0\n";
}
else
{
cout<<temp->next->info<<endl;
}
temp=temp->next;
}
}
}
void insertAtEnd(int item)
{
struct node *newnode=createNode(item);
if(newnode==NULL)
{
cout<<"overflow\n";
}
else
{
if(head==NULL)
{
head=newnode;
}
else
{
struct node *temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
newnode->prev=temp;
}
travesing();
}
}

void deletePos(int pos)


{
if(head==NULL)
{

cout<<"Linked list is in underflow condition";


}
else if(pos==1)
{
head=head->next;
}
else
{
int cnt=countNode();
if(pos>cnt)
{
cout<<"Invalid position";
}
else
{
struct node *temp=head,*pre=NULL,*post=head->next;
int i;
for(i=1;i<pos;i++)
{
pre=temp;
temp=temp->next;
post=temp;
}
pre->next=temp->next;

post->prev=temp->prev;
cout<<"Item deleted\n";

}
}
int countNode()
{
struct node *temp=head;
int cnt=0;
while(temp!=NULL)
{
cnt++;
temp=temp->next;
}
return cnt;
}
};
int main()
{
operation obj;
int item,ch,pos;
while(1)
{
cout<<"\n1 for insert at begning\n";
cout<<"2 for insert at ending\n";
cout<<"3 for Travesing\n";
cout<<"4 for Delaetion\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter item:";
cin>>item;
obj.insertAtBeg(item);
break;
case 2:
cout<<"Enter item:";
cin>>item;
obj.insertAtEnd(item);
break;
case 3:
obj.travesing();
break;
case 4:
cout<<"enter pos";
cin>>pos;
obj.deletePos(pos);
break;

}
}

return 0;
}
#include <iostream>
using namespace std;
struct node
{
int info;
struct node *link;
};
class circularOperation
{
struct node *head;
public:
circularOperation()
{
head=NULL;
}
struct node* createNode(int item)
{
struct node *temp=new struct node;
if(temp!=NULL)
{
temp->info=item;
temp->link=NULL;
}
return temp;
}
void insertAtBeg(int item)
{
struct node *newnode=createNode(item);
if(newnode==NULL)
{
cout<<"Overflow condition";
}
else
{
if(head==NULL)
{
head=newnode;
head->link=head;
}
else
{
struct node *temp,*ptr;
ptr=temp=head;
head=newnode;
head->link=temp;
while(ptr->link!=temp)
{
ptr=ptr->link;
}
ptr->link=head;
}
travesing();
}
}
void insertAtEnd(int item)
{
struct node *newnode=createNode(item);
if(newnode==NULL)
{
cout<<"Overflow";
}
else
{
if(head==NULL)
{
head=newnode;
head->link=newnode;
}
else
{
struct node *temp=head;
newnode->link=head;
while(temp->link!=head)
{
temp=temp->link;
}
temp->link=newnode;
}
travesing();
}
}
void travesing()
{
if(head==NULL)
{
cout<<"List is in underflow condition";
}
else
{
struct node *temp=head;
while(temp->link!=head)
{
cout<<temp->info<<"->";
temp=temp->link;
}
cout<<temp->info<<"->";
temp=temp->link;
cout<<temp->info;
}
}
void deleteAtBeg()
{
if(head==NULL)
{
cout<<"List is in underflow condition";
return;
}
else if(head->link==head)//one node
{
cout<<"The deleted item is="<<head->info;
head=NULL;
}
else
{
struct node *temp,*ptr;
cout<<"The deleted item is="<<head->info;
temp=head;
ptr=head;
head=head->link;
while(temp->link!=ptr)
{
temp=temp->link;
}
temp->link=head;
}
travesing();
}
void deleteAtEnd()
{
if(head==NULL)
{
cout<<"The list is in underflow condition";
return;
}
else if(head==head->link)
{
cout<<"The deleted item is:"<<head->info;
head=NULL;
}
else
{
struct node *ptr,*prev;
ptr=head;
prev=NULL;
while(ptr->link!=head)
{
prev=ptr;
ptr=ptr->link;
}
cout<<"The deleted item is="<<ptr->info;
prev->link=head;
}
travesing();
}
void deleteByItem(int item)
{
struct node *temp,*prev=NULL;
temp=head;
if(temp==NULL)
{
cout<<"The list is in underflow condition";
}
else
{
int flag=0;
while(temp->link!=head)
{
if(temp->info==item)
{
flag=1;
break;
}
prev=temp;
temp=temp->link;
}
if(temp->info==item && flag==0)
{
flag=1;
}
if(flag==0)
{
cout<<"item not found";
}
else
{
cout<<"item found";
if(prev==NULL)
{
deleteAtBeg();
}
else if(temp->link==head)
{
deleteAtEnd();
}
else
{
prev->link=temp->link;
}
}
}
}
};
int main()
{
int ch,item;
circularOperation obj;
while(1)
{
cout<<"\n1 for insert at beginning\n";
cout<<"2 for insert at End\n";
cout<<"3 for travesing\n";
cout<<"4 for delation at the beginning\n";
cout<<"5 for deletion at end\n";
cout<<"6 for deletion by item\n";
cout<<"7 for exit\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter item to be insert:";
cin>>item;
obj.insertAtBeg(item);
break;
case 2:
cout<<"Enter item to be inserted:";
cin>>item;
obj.insertAtEnd(item);
break;
case 3:
cout<<"The list is:\n";
obj.travesing();
break;
case 4:
obj.deleteAtBeg();
break;
case 5:
obj.deleteAtEnd();
break;
case 6:
cout<<"Enter item to be deleted:";
cin>>item;
obj.deleteByItem(item);
break;
case 7:
return 0;
}
}
return 0;
}

You might also like