0% found this document useful (0 votes)
12 views

DS C++ External Lab

Uploaded by

pranava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

DS C++ External Lab

Uploaded by

pranava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

1.

A SINGLE LINKED LIST

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

struct node
{
int info;
struct node *next;
}*start;

class single_llist
{
public:
node* create_node(int);
void insert_begin();
void insert_pos();
void insert_last();
void delete_pos();
void display();
int length_list();
single_llist()
{
start=NULL;
}
};
int main()
{
int choice,nodes,elements,position,i;
single_llist sl;
start=NULL;
while(1)
{
cout<<endl<<" "<<endl;
cout<<endl<<"Operations on single linked list"<<endl;
cout<<endl<<" "<<endl;
cout<<"1.Create node"<<endl;
cout<<"2.Insert Node at beginning"<<endl;
cout<<"3.Insert node at last"<<endl;
cout<<"4.Insert node at position"<<endl;
cout<<"5.Delete node at position"<<endl;
cout<<"6.Display Linked List"<<endl;
cout<<"7.Length of the Linked List"<<endl;
cout<<"8.Exit"<<endl;
cout<<"Enter your choice:";
cin>>choice;

switch(choice)
{
int val;
case 1:
cout<<"Creating a Node:"<<endl;
cout<<"Enter a value:"<<endl;
cin>>val;
sl.create_node(val);
cout<<endl;
break;
case 2:
cout<<"Inserting Node at Beginning:"<<endl;
sl.insert_begin();
cout<<endl;
break;
case 3:
cout<<"Inserting Node at Last:"<<endl;
sl.insert_last();
cout<<endl;
break;
case 4:
cout<<"Inserting Node at a given position:"<<endl;
sl.insert_pos();
cout<<endl;
break;
case 5:
cout<<"Delete a particular node:"<<endl;
sl.delete_pos();
cout<<endl;
break;
case 6:
cout<<"Display elements of linked list"<<endl;
sl.display();
cout<<endl;
break;
case 7:
cout<<"Length of the linked list:"<<sl.length_list()<<endl;
cout<<endl;
break;
case 8:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}
node *single_llist::create_node(int value)
{
struct node *temp,*s;
temp = new(struct node);
if(temp==NULL)
{
cout<<"Memory not allocated"<<endl;
return 0;
}
else
{
temp->info=value;
temp->next=NULL;
cout<<"A Node "<<temp->info<<" is created"<<endl;
return temp;
}
}
void single_llist::insert_begin()
{
int value;
cout<<"Enter the value to be inserted:";
cin>>value;
struct node *temp,*p;
temp=create_node(value);
if(start==NULL)
{
start=temp;
start->next=NULL;
}
else
{
p=start;
start=temp;
start->next=p;
}
cout<<"Element inserted at beginning"<<endl;
}
void single_llist::insert_last()
{
int value;
cout<<"Enter the value to be inserted:";
cin>>value;

struct node *temp,*s;


temp=create_node(value);
s=start;
while(s->next!=NULL)
{
s=s->next;
}
temp->next=NULL;
s->next=temp;
cout<<"Element Inserted at last"<<endl;
}
void single_llist::insert_pos()
{
int value,pos,counter=0;
cout<<"Enter the value to be inserted:";
cin>>value;
struct node *temp,*s,*ptr;
temp=create_node(value);
cout<<"Enter the position at which node to be inserted:";
cin>>pos;
int i;
s=start;
while(s!=NULL)
{
s=s->next;
counter++;
}
if(pos==1)
{
if(start==NULL)
{
start=temp;
start->next=NULL;
}
else
{
ptr=start;
start=temp;
start->next=ptr;
}
}
else if(pos>1 && pos<=counter)
{
s=start;
for(i=1;i<pos;i++)
{
ptr=s;
s=s->next;
}
ptr->next=temp;
temp->next=s;
}
else
{
cout<<"Position out of range"<<endl;
}
}
void single_llist::delete_pos()
{
int pos,i,counter=0;
if(start==NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the position of value to be deleted:";
cin>>pos;
struct node *s,*ptr;
s=start;
if(pos==1)
{
start=s->next;
}
else
{
while(s!=NULL)
{
s=s->next;
counter++;
}
if(pos>0 && pos<=counter)
{
s=start;
for(i=1;i<pos;i++)
{
ptr=s;
s=s->next;
}
ptr->next=s->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Delelted"<<endl;
}
}
int single_llist::length_list()
{
struct node *temp;
int count=0;
if(start==NULL)
{
cout<<"The List is Empty"<<endl;
return 0;
}
temp=start;
while(temp!=NULL)
{
count++;
temp=temp->next;
}
return count;
}
void single_llist::display()
{
struct node *temp;
if(start==NULL)
{
cout<<"The List is empty"<<endl;
return;
}
temp=start;
cout<<"Elements of list are:"<<endl;
while(temp!=NULL)
{
cout<<temp->info<<"->";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
2.A DOUBLE LINKED LIST

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

struct node
{
int info;
struct node *next;
struct node *prev;
}*start;

class double_llist
{
public:
void create_list(int value);
void add_begin(int value);
void add_after(int value,int position);
void delete_element(int value);
void display_dlist();
void reverse();
int length_list();
double_llist()
{
start=NULL;
}
};
int main()
{
int choice,element,position;
double_llist dl;
while(1)
{
cout<<endl<<" "<<endl;
cout<<endl<<"Operations on Double linked list"<<endl;
cout<<endl<<" "<<endl;
cout<<"1.Create Node"<<endl;
cout<<"2.Add at beginning"<<endl;
cout<<"3.Add after position"<<endl;
cout<<"4.Delete"<<endl;
cout<<"5.Traverse"<<endl;
cout<<"6.Reverse"<<endl;
cout<<"7.Quit"<<endl;
cout<<"8.Length of list"<<endl;
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the element:";
cin>>element;
dl.create_list(element);
cout<<endl;
break;
case 2:
cout<<"Enter the element:";
cin>>element;
dl.add_begin(element);
cout<<endl;
break;
case 3:
cout<<"Enter the element:";
cin>>element;
cout<<"Insert element after position:";
cin>>position;
dl.add_after(element,position);
cout<<endl;
break;
case 4:
if(start==NULL)
{
cout<<"List empty nothing to delete"<<endl;
break;
}
cout<<"Enter the element for deletion:";
cin>>element;
dl.delete_element(element);
cout<<endl;
break;
case 5:
dl.display_dlist();
cout<<endl;
break;
case 6:
if(start==NULL)
{
cout<<"List empty nothing to reverse"<<endl;
break;
}
dl.reverse();
cout<<endl;
break;
case 7:
exit(1);
case 8:
cout<<"Length of the linked list:"<<dl.length_list()<<endl;
cout<<endl;
break;
default:
cout<<"Wrong choice"<<endl;
}
}
return 0;
}
void double_llist::create_list(int value)
{
struct node *s,*temp;
temp=new(struct node);
temp->info=value;
temp->next=NULL;
if(start==NULL)
{
temp->prev=NULL;
start=temp;
}
else
{
s=start;
while(s->next!=NULL)
{
s=s->next;
}
s->next=temp;
temp->prev=s;
}
}
void double_llist::add_begin(int value)
{
if(start==NULL)
{
cout<<"First create the list"<<endl;
return;
}
struct node *temp;
temp=new(struct node);
temp->prev=NULL;
temp->info=value;
temp->next=start;
temp->prev=temp;
start=temp;
cout<<"Element inserted"<<endl;
}
void double_llist::add_after(int value,int pos)
{
if(start==NULL)
{
cout<<"First create the list"<<endl;
return;
}
struct node *tmp,*q;
int i;
q=start;
for(i=0;i<pos-1;i++)
{
q=q->next;
if(q==NULL)
{
cout<<"There are less than";
cout<<pos<<" elements"<<endl;
return;
}
}
tmp=new(struct node);
tmp->info=value;
if(q->next==NULL)
{
q->next=tmp;
tmp->next=NULL;
tmp->prev=q;
}
else
{
tmp->next=q->next;
tmp->next->prev=tmp;//temp is assigned to previous field of q
q->next=tmp;
tmp->prev=q;
}
cout<<"Element inserted"<<endl;
}
void double_llist::delete_element(int value)
{
struct node *tmp,*q;
if(start->info==value)
{
tmp=start;
start=start->next;
start->prev=NULL;
cout<<"Element Deleted"<<endl;
free(tmp);
return;
}
q=start;
while(q->next->next!=NULL)
{
if(q->next->info==value)
{
tmp=q->next;
q->next=tmp->next;
tmp->next->prev=q;
cout<<"Element Deleted"<<endl;
free(tmp);
return;
}
q=q->next;
}
if(q->next->info==value)
{
tmp=q->next;
free(tmp);
q->next=NULL;
cout<<"Element Deleted"<<endl;
return;
}
cout<<"Element"<<value<<"not found"<<endl;
}
void double_llist::reverse()
{
struct node *p1,*p2;
p1=start;
p2=p1->next;
p1->next=NULL;
p1->prev=p2;
while(p2!=NULL)
{
p2->prev=p2->next;
p2->next=p1;
p1=p2;
p2=p2->prev;
}
start=p1;
cout<<"List Reversed"<<endl;
}
void double_llist::display_dlist()
{
struct node *temp;
if(start==NULL)
{
cout<<"The List is empty"<<endl;
return;
}
temp=start;
cout<<"Elements of list are:"<<endl;
while(temp!=NULL)
{
cout<<temp->info<<"->";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
int double_llist::length_list()
{
struct node *temp;
int count=0;
if(start==NULL)
{
cout<<"The List is Empty"<<endl;
return 0;
}
temp=start;
while(temp!=NULL)
{
count++;
temp=temp->next;
}
return count;
}
3.A CIRCULAR LINKED LIST

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

struct node
{
int info;
struct node *next;
}*last;

class circular_llist
{
public:
void create_node(int value);
void add_begin(int value);
void add_after(int value,int position);
void delete_element(int value);
void display_list();
int count();
circular_llist()
{
last=NULL;
}
};
int main()
{
int choice,element,position;
circular_llist cl;
while(1)
{
cout<<endl<<" "<<endl;
cout<<endl<<"Circular single linked list"<<endl;
cout<<endl<<" "<<endl;
cout<<endl<<"1.Create Node"<<endl;
cout<<endl<<"2.Add at beginning"<<endl;
cout<<endl<<"3.Add after"<<endl;
cout<<endl<<"4.Delete"<<endl;
cout<<endl<<"5.Display"<<endl;
cout<<endl<<"6.count"<<endl;
cout<<endl<<"7.Quit"<<endl;
cout<<"Enter your choice:";
cin>>choice;

switch(choice)
{
case 1:
cout<<"Enter the element:";
cin>>element;
cl.create_node(element);
cout<<endl;
break;
case 2:
cout<<"Enter the element:";
cin>>element;
cl.add_begin(element);
cout<<endl;
break;
case 3:
cout<<"Enter the element:";
cin>>element;
cout<<"Insert element after position:";
cin>>position;
cl.add_after(element,position);
cout<<endl;
break;
case 4:
if(last==NULL)
{
cout<<"List is empty, nothing to delete"<<endl;
break;
}
cout<<"Enter the element for deletion:";
cin>>element;
cl.delete_element(element);
cout<<endl;
break;
case 5:
cl.display_list();
break;
case 6:
cout<<"Count of Nodes:"<<cl.count()<<endl;
break;
case 7:
exit(1);
break;
defalut:
cout<<"Wrong choice"<<endl;
}
}
return 0;
}
void circular_llist::create_node(int value)
{
struct node *temp;
temp=new(struct node);
temp->info=value;
if(last==NULL)
{
last=temp;
temp->next=last;
}
else
{
temp->next=last->next;
last->next=temp;
last=temp;
}
}
void circular_llist::add_begin(int value)
{
if(last==NULL)
{
cout<<"First Create the list"<<endl;
return;
}
struct node *temp;
temp=new(struct node);
temp->info=value;
temp->next=last->next;
last->next=temp;
}
void circular_llist::add_after(int value,int pos)
{
if(last==NULL)
{
cout<<"First Create the list"<<endl;
return;
}
struct node *temp,*s;
s=last->next;
for(int i=0;i<pos-1;i++)
{
s=s->next;
if(s==last->next)
{
cout<<"There are less than";
cout<<pos<<"in the list"<<endl;
return;
}
}
temp=new(struct node);
temp->next=s->next;
temp->info=value;
s->next=temp;

if(s==last)
{
last=temp;
}
}
void circular_llist::delete_element(int value)
{
struct node *temp,*s;
s=last->next;

if(last->next==last&&last->info==value)
{
temp=last;
last=NULL;
free(temp);
return;
}
if(s->info==value)
{
temp=s;
last->next=s->next;
free(temp);
return;
}
while(s->next!=last)
{
if(s->next->info==value)
{
temp=s->next;
s->next=temp->next;
free(temp);
cout<<"Element"<<value;
cout<<"deleted from the list"<<endl;
return;
}
s=s->next;
}
if(s->info==value)
{
temp=s->next;
s->next=last->next;
free(temp);
last=s;
return;
}
cout<<"Element"<<value<<" not found in the list"<<endl;
}
void circular_llist::display_list()
{
struct node *s;
if(last==NULL)
{
cout<<"List is empty, nothing to display"<<endl;
return;
}
s=last->next;
cout<<"Circular link list"<<endl;
do
{
cout<<s->info<<"->";
s=s->next;
}while(s!=last->next);
if(s==last->next)
{
cout<<s->info<<endl;
}
}
int circular_llist::count()
{
int count=0;
struct node *s;
if(last==NULL)
{
cout<<"List is empty, nothing to display"<<endl;
return 0;
}
s=last->next;
while(s!=last)
{
count++;
s=s->next;
}
count++;
return count;
}
4.A STACK USING ARRAY

#include<iostream>
using namespace std;

int stack[100],n=100,top=-1;

void push(int val)


{
if(top>=n-1)
{
cout<<"Stack Overflow"<<endl;
}
else
{
top++;
stack[top]=val;
}
}
void pop()
{
if(top<=-1)
{
cout<<"Stack Underflow"<<endl;
}
else
{
cout<<"The popped element is:"<<stack[top]<<endl;
top--;
}
}
void display()
{
if(top>=0)
{
cout<<"Stack elements are:";
for(int i=top;i>=0;i--)
{
cout<<stack[i]<<" ";
}
cout<<endl;
}
else
{
cout<<"Stack is empty";
}
}
int main()
{
int ch,val;
cout<<"1.Push in stack"<<endl;
cout<<"2.Pop from stack"<<endl;
cout<<"3.Display stack"<<endl;
cout<<"4.Exit"<<endl;
do
{
cout<<"Enter choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
cout<<"Exit"<<endl;
break;
default:
cout<<"Invalid Choice"<<endl;
}
}while(ch!=4);
return 0;
}

4.B LINEAR SEARCH

#include<iostream>
using namespace std;
int Lsearch(int list[],int n,int key);
int main()
{
int n,i,key,list[25],pos;
cout<<"Enter no of elements\n";
cin>>n;
cout<<"Enter"<<n<<"elements";
for(i=0;i<n;i++)
cin>>list[i];
cout<<"Enter key to search";
cin>>key;
pos=Lsearch(list,n,key);
if(pos==-1)
cout<<"\n element not found";
else
cout<<"\n element found at index"<<pos;
}
int Lsearch(int list[],int n,int key)
{
int i,pos=-1;
for(i=0;i<n;i++)
if(key==list[i])
{
pos=i;
break;
}
return pos;
}
5.A QUEUE USING ARRAY

#include<stdlib.h>
#include<iostream>
using namespace std;

#define MAX 5
template<class T>
class queue
{
private:
T q[MAX],item;
int front,rear;
public:
queue();
void insert_q();
void delete_q();
void display_q();
};
template<class T>
queue<T>::queue()
{
front=rear=-1;
}
template<class T>
void queue<T>::insert_q()
{
if(rear>=MAX-1)
{
cout<<"Queue Overflow\n";
}
else
{
if(front>rear)
{
front=rear=-1;
}
else
{
if(front==-1)
{
front=0;
}
rear++;
cout<<"Enter an item to be inserted:";
cin>>item;
q[rear]=item;
cout<<"Inserted Successfully into Queue\n";
}
}
}
template<class T>
void queue<T>::delete_q()
{
if(front==-1||front>rear)
{
front=rear=-1;
cout<<"Queue is empty\n";
}
else
{
item=q[front];
front++;
cout<<item<<" is deleted Successfully\n";
}
}
template<class T>
void queue<T>::display_q()
{
if(front==-1||front>rear)
{
front=rear=-1;
cout<<"Queue is empty\n";
}
else
{
for(int i=front;i<=rear;i++)
{
cout<<"|"<<q[i]<<"|<--";
}
}
}
int main()
{
int choice;
queue<int> q;
while(1)
{
cout<<"\n\n******Menu for QUEUE Operations******\n\n";
cout<<"1.INSERT\n2.DELETE\n3.DISPLAY\n4.EXIT\n";
cout<<"Enter Choice:";
cin>>choice;
switch(choice)
{
case 1:
q.insert_q();
break;
case 2:
q.delete_q();
break;
case 3:
cout<<"Elements in the queue are:";
q.display_q();
break;
case 4:
exit(0);
default:
cout<<"Invalid choice";
}
}
return 0;
}

5.B BINARY SEARCH

#include<iostream>
using namespace std;
int rbinary_search(int list[],int key,int low,int high);
int main()
{
int n,i,key,list[25],pos;
cout<<"Enter no.of elements:\n";
cin>>n;
cout<<"Enter "<<n<<" elements in ascending order:\n";
for(i=0;i<n;i++)
{
cin>>list[i];
}
cout<<"Enter key to search:";
cin>>key;
pos = rbinary_search(list,key,0,n-1);
if(pos==-1){
cout<<"\nElement not found\n";
}
else{
cout<<"\nElement found at index"<<pos;
}
}
int rbinary_search(int list[],int key,int low,int high)
{
int mid,pos=-1;
if(low<=high)
{
mid=(low+high)/2;
if(key==list[mid])
{
pos=mid;
return pos;
}
else if(key<list[mid])
{
return rbinary_search(list,key,low,mid-1);
}
else
{
return rbinary_search(list,key,mid+1,high);
}
}
return pos;
}
6. STACK USING LINKED LIST

#include<iostream>
#include<stdlib.h>
using namespace std;

struct Node
{
int data;
struct Node *next;
};

struct Node *top=NULL;


void push(int val)
{
struct Node *newnode=(struct Node*)malloc(sizeof(struct Node));

newnode->data=val;
newnode->next=top;
top=newnode;
}
void pop()
{
if(top==NULL)
{
cout<<"Stack Underflow"<<endl;
}
else
{
cout<<"The popped element is:"<<top->data<<endl;
top=top->next;
}
}
void display()
{
struct Node *ptr;
if(top==NULL)
{
cout<<"Stack is empty";
}
else
{
ptr=top;
cout<<"Stack elements are:";
while(ptr!=NULL)
{
cout<<ptr->data<<" ";
ptr=ptr->next;
}
}
cout<<endl;
}
int main()
{
int ch,val;
cout<<"1.Push in stack"<<endl;
cout<<"2.Pop from stack"<<endl;
cout<<"3.Display stack"<<endl;
cout<<"4.Exit"<<endl;
do
{
cout<<"Enter choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
cout<<"Exit"<<endl;
break;
default:
cout<<"Invalid Choice"<<endl;
}
}while(ch!=4);
return 0;
}
7. QUEUE USING LINKED LIST

#include<stdlib.h>
#include<iostream>
using namespace std;

template<class T>
class node
{
public:
T data;
node<T>*next;
};
template<class T>
class queue
{
private:
T item;
friend class node<T>;
node<T>*front,*rear;
public:
queue();
void insert_q();
void delete_q();
void display_q();
};
template<class T>
queue<T>::queue()
{
front=rear=NULL;
}
template<class T>
void queue<T>::insert_q()
{
node<T>*p;
cout<<"Enter an element to be inserted:";
cin>>item;
p=new node<T>;
p->data=item;
p->next=NULL;
if(front==NULL)
{
rear=front=p;
}
else
{
rear->next=p;
rear=p;
}
cout<<"\nInserted into Queue Successfully\n";
}
template<class T>
void queue<T>::delete_q()
{
node<T>*t;
if(front==NULL)
{
cout<<"\nQueue is Underflow";
}
else
{
item=front->data;
t=front;
front=front->next;
cout<<"\n"<<item<<" is deleted Successfully from Queue \n";
}
delete(t);
}
template<class T>
void queue<T>::display_q()
{
node<T>*t;
if(front==NULL)
{
cout<<"\n Queue Underflow";
}
else
{
cout<<"\n Elements in the queue are:";
t=front;
while(t!=NULL)
{
cout<<"|"<<t->data<<"|<--";
t=t->next;
}
}
}
int main()
{
int choice;
queue<int> q;
while(1)
{
cout<<"\n\n******Menu for QUEUE Operations******\n\n";
cout<<"1.INSERT\n2.DELETE\n3.DISPLAY\n4.EXIT\n";
cout<<"Enter Choice:";
cin>>choice;
switch(choice)
{
case 1:
q.insert_q();
break;
case 2:
q.delete_q();
break;
case 3:
cout<<"Elements in the queue are:";
q.display_q();
break;
case 4:
exit(0);
default:
cout<<"Invalid choice";
}
}
return 0;
}
8.A IMPLEMENT GRAPH USING ARRAY

#include <iostream>
using namespace std;
int vertArr[20][20];
int count=0;
void displayMatrix(int v){
for(int i=0;i<v;i++){
for(int j=0;j<v;j++){
cout<<vertArr[i][j]<<" ";
}
cout<<endl;
}
}
void add_edge(int u,int v){
vertArr[u][v]=1;
vertArr[v][u]=1;
}
main(int argc,char* argv[]){
int v=6;
add_edge(0,4);
add_edge(0,3);
add_edge(1,2);
add_edge(1,4);
add_edge(1,5);
add_edge(2,3);
add_edge(2,5);
add_edge(5,3);
add_edge(5,4);
displayMatrix(v);
}

8.B MERGESORT

#include<iostream>
using namespace std;
#define max 15
template<class T>
void merge(T a[],int l,int m,int u)
{
T b[max];
int i,j,k;
i=l;
j=m+1;
k=l;
while((i<=m)&&(j<=u))
{
if(a[i]<a[j])
{
b[k]=a[i];
++i;
}
else
{
b[k]=a[j];
++j;
}
++k;
}
if(i>m)
{
while(j<=u)
{
b[k]=a[j];
++j;
++k;
}
}
else
{
while(i<=m)
{
b[k]=a[i];
++i;
++k;
}
}
for(int r=l;r<=u;r++)
{
a[r]=b[r];
}
}
template<class T>
void mergesort(T a[],int p,int q)
{
int mid;
if(p<q)
{
mid=(p+q)/2;
mergesort(a,p,mid);
mergesort(a,mid+1,q);
merge(a,p,mid,q);
}
}
int main()
{
int n,i;
int list[30];
cout<<"Enter no of elements:";
cin>>n;
cout<<"Enter "<<n<<" numbers:\n";
for(i=0;i<n;i++)
cin>>list[i];
mergesort(list,0,n-1);
cout<<"After sorting\n";
for(i=0;i<n;i++)
cout<<list[i]<<endl;
return 0;
}
9.A INFIX TO POSTFIX CONVERSION

#include<iostream>
#include<string.h>
#include<ctype.h>
using namespace std;
const int MAX=50;
class infix
{
private:
char target[MAX],stack[MAX];
char *s,*t;
int top;
public:
infix();
void setexpr(char *str);
void push(char c);
char pop();
void convert();
int priority(char c);
void show();
};
infix::infix()
{
top=-1;
strcpy(target,"");
strcpy(stack,"");
t=target;
s="";
}
void infix::setexpr(char *str)
{
s=str;
}
void infix::push(char c)
{
if(top==MAX)
{
cout<<"\nStack is full\n";
}
else
{
top++;
stack[top]=c;
}
}
char infix::pop()
{
if(top==-1)
{
cout<<"\nStack is empty\n";
return -1;
}
else
{
char item=stack[top];
top--;
return item;
}
}
void infix::convert()
{
while(*s)
{
if(*s==' '||*s=='\t')
{
s++;
continue;
}
if(isdigit(*s)||isalpha(*s))
{
while(isdigit(*s)||isalpha(*s))
{
*t=*s;
s++;
t++;
}
}
if(*s=='(')
{
push(*s);
s++;
}
char opr;
if(*s=='*'||*s=='+'||*s=='/'||*s=='%'||*s=='-'||*s=='$')
{
if(top!=-1)
{
opr=pop();
while(priority(opr)>=priority(*s))
{
*t=opr;
t++;
opr=pop();
}
push(opr);
push(*s);
}
else
{
push(*s);
s++;
}
}
if(*s==')')
{
opr=pop();
while((opr)!='(')
{
*t=opr;
t++;
opr=pop();
}
s++;
}
}
while(top!=-1)
{
char opr=pop();
*t=opr;
t++;
}
*t='\0';
}
int infix::priority(char c)
{
if(c=='$')
{
return 3;
}
if(c=='*'||c=='/'||c=='%')
{
return 2;
}
else
{
if(c=='+'||c=='-')
{
return 1;
}
else
{
return 0;
}
}
}
void infix::show()
{
cout<<target;
}
int main()
{
char expr[MAX];
infix q;
cout<<"\nEnter an expression in infix form:";
cin.getline(expr,MAX);
q.convert();
cout<<"\nThe postfix expression is:";
q.show();
return 0;
}

9.B QUICKSORT

#include<iostream>
using namespace std;
int part(int low,int high,int *a)
{
int i,h=high,l=low,p,t;
p=a[low];
while(low<high)
{
while(a[l]<p)
{
l++;
}
while(a[h]>p)
{
h--;
}
if(l<h)
{
t=a[l];
a[l]=a[h];
a[h]=t;
}
else
{
t=p;
p=a[l];
a[l]=t;
break;
}
}
return h;
}
void quick(int l,int h,int *a)
{
int index,i;
if(l<h)
{
index=part(l,h,a);
quick(l,index-1,a);
quick(index+1,h,a);
}
}
int main()
{
int a[100],n,l,h,i;
cout<<"Enter number of elements:";
cin>>n;
cout<<"Enter the elements(Use Space As a Seperator):";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nIntial Array:\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
h=n-1;
l=0;
quick(l,h,a);
cout<<"\nAfter Sorting:\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
return 0;
}
10.A RADIXSORT

#include<iostream>
using namespace std;
int getMax(int arr[],int n)
{
int max=arr[0];
for(int i=1;i<n;i++)
if(arr[i]>max)
max=arr[i];
return max;
}
void countSort(int arr[],int n,int exp)
{
int output[n],i,count[10]={0};
for(i=0;i<n;i++)
count[arr[i]/exp%10]++;
for(i=1;i<10;i++)
count[i]+=count[i-1];
for(i=n-1;i>=0;i--)
{
output[count[(arr[i]/exp)%10]-1]=arr[i];
count[(arr[i]/exp)%10]--;
}
for(i=0;i<n;i++)
arr[i]=output[i];
}
void radixsort(int arr[],int n)
{
int exp,m;
m=getMax(arr,n);
for(exp=1;m/exp>0;exp*=10)
countSort(arr,n,exp);
}
int main()
{
int n,i;
cout<<"\nEnter the number of data element to be sorted:";
cin>>n;
int arr[n];
for(i=0;i<n;i++)
{
cout<<"Enter element"<<i+1<<":";
cin>>arr[i];
}
radixsort(arr,n);
cout<<"\nSorted Data";
for(i=0;i<n;i++)
cout<<"->"<<arr[i];
return 0;
}

10.B LINEAR SEARCH

#include<iostream>
using namespace std;
int Lsearch(int list[],int n,int key);
int main()
{
int n,i,key,list[25],pos;
cout<<"Enter no of elements\n";
cin>>n;
cout<<"Enter"<<n<<"elements";
for(i=0;i<n;i++)
cin>>list[i];
cout<<"Enter key to search";
cin>>key;
pos=Lsearch(list,n,key);
if(pos==-1)
cout<<"\n element not found";
else
cout<<"\n element found at index"<<pos;
}
int Lsearch(int list[],int n,int key)
{
int i,pos=-1;
for(i=0;i<n;i++)
if(key==list[i])
{
pos=i;
break;
}
return pos;
}
11.A IMPLEMENTATION OF BINARY TREE USING ARRAY

#include <bits/stdc++.h>
using namespace std;
char tree[10];
int root(char key){
if(tree[0]!='\0')
cout<<"Tree already had root";
else
tree[0]=key;
return 0;
}
int set_left(char key,int parent){
if(tree[parent]=='\0')
cout<<"Can't set child at "<<(parent*2)+1<<",no parent found\n";
else
tree[(parent*2)+1]=key;
return 0;
}
int set_right(char key,int parent){
if(tree[parent]=='\0')
cout<<"Can't set child at "<<(parent*2)+2<<",no parent found\n";
else
tree[(parent*2)+2]=key;
return 0;
}
int print_tree(){
for(int i=0;i<10;i++){
if(tree[i]!='\0')
cout<<tree[i];
else
cout<<"-";
}
return 0;
}
int main(){
root('A');
set_left('B',0);
set_right('C',0);
set_left('D',1);
set_right('E',1);
set_right('F',2);
print_tree();
return 0;
}

11.B DFS

#include <bits/stdc++.h>
using namespace std;
class Graph{
public:
map<int,bool>visited;
map<int,list<int>>adj;

void addEdge(int v,int w);


void DFS(int v);
};
void Graph::addEdge(int v,int w){
adj[v].push_back(w);
}
void Graph::DFS(int v){
visited[v]=true;
cout<<v<<" ";

list<int>::iterator i;
for( i=adj[v].begin();i!=adj[v].end();++i){
if(!visited[*i])
DFS(*i);
}
}
int main(){
Graph g;
int e,a,b,src;
cin>>e;

for(int i=0;i<e;i++){
cin>>a>>b;
g.addEdge(a,b);
}
cin>>src;

cout<<"Depth First Traversal is: ";


g.DFS(src);
return 0;
}
12. BINARY TREE TRAVERSAL

#include<iostream>
using namespace std;
struct tree
{
tree *l,*r;
int data;
}*root=NULL,*p=NULL,*np=NULL,*q;
void create()
{
int value,c=0;
while(c<7)
{
if(root==NULL)
{
root=new tree;
cout<<"enter value of root node\n";
cin>>root->data;
root->r=NULL;
root->l=NULL;
}
else
{
p=root;
cout<<"enter value of node\n";
cin>>value;
while(true)
{
if(value<p->data)
{
if(p->l==NULL)
{
p->l=new tree;
p=p->l;
p->data=value;
p->l=NULL;
p->r=NULL;
cout<<"value entered in left\n";
break;
}
else if(p->l!=NULL)
{
p=p->l;
}
}
else if(value>p->data)
{
if(p->r=NULL)
{
p->r=new tree;
p=p->r;
p->data=value;
p->l=NULL;
p->r=NULL;
cout<<"value entered in right\n";
break;
}
else if(p->r!=NULL)
{
p=p->r;
}
}
}
}
}
c++;
}
void inorder(tree *p)
{
if(p!=NULL)
{
inorder(p->l);
cout<<p->data<<endl;
inorder(p->r);
}
}
void preorder(tree *p)
{
if(p!=NULL)
{
cout<<p->data<<endl;
preorder(p->l);
preorder(p->r);
}
}
void postorder(tree *p)
{
if(p!=NULL)
{
postorder(p->l);
postorder(p->r);
cout<<p->data<<endl;
}
}
int main()
{
create();
cout<<"printing traversal in inorder\n";
inorder(root);
cout<<"printing traversal in preorder\n";
preorder(root);
cout<<"printing traversal in postorder\n";
postorder(root);
return 0;
}

You might also like