Anda di halaman 1dari 12

Program: Singly Linked List

#include<iostream>
#include<process.h>
using namespace std;
class slink
{
private:
struct node
{
int data;
node *next;
}*head;
public:
slink()
{
head=NULL;
}
void create()
{
head=new node;
cout<<"Enter data for the node:";
cin>>head->data;
head->next=NULL;
}
void insert()
{
int n;
char p;
node*list,*prev,*temp;
list=head;
cout<<"Insert node at front of last(F or L)?";
cin>>p;
if(p=='f'||'F')
{
temp=new node;
cout<<"Enter the data for the node:";
cin>>temp->data;
temp->next=head;
head=temp;
}
else
{
temp=new node;
cout<<"Enter the data for the node:";
cin>>temp->data;
temp->next=NULL;
while(list->next!=NULL)
{
list=list->next;
list->next=temp;
}
}
}
void delnode()
{
int n,p=0;
node *list,*prev,*temp;
list=head;
cout<<"Enter the data for the node to be deleted:";
cin>>n;
if(head->data==n)
{
head=head->next;
p=1;
}
else
{
while((list->next!=NULL)&&(list->data!=n))
{
prev=list;
list=list->next;
}
if((list->next!=NULL)&&(list->data==n))
{
temp=list;
prev->next=list->next;
p=1;
}
else if(list->data==n)
{
temp=list;
prev->next=list->next;
p=1;
}
delete(temp);
}
if(p==0)
{
cout<<"Node is not present";
}
else
{
cout<<"Node is deleted";
}
}
void display()
{
node*list;
list=head;
if(list==NULL)
{
cout<<"List is empty";
}
else
{
cout<<"The list is :";
while(list!=NULL)
{
cout<<list->data<<"-->";
list=list->next;
}
cout<<"NULL";
}
}
};
int main()
{
int option;
slink s;
while(option!=5)
{
cout<<"1.Create"<<endl<<"2.Insert"<<endl<<"3.Delete"<<
endl<<"4.Display"<<endl<<"5.Exit"<<endl<<"Enter your
choice:";
cin>>option;
switch(option)
{
case 1:s.create();
break;
case 2:s.insert();
break;
case 3:s.delnode();
break;
case 4:s.display();
break;
case 5:break;
}
}
}
Program: Doubly Linked List

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
struct node
{
int data;
node *next;
node *prev;
}*new1,*temp,*start,*dummy;

class linkedlist
{
public:
void add();
void display();
void del();
};
void linkedlist::add()
{
int item;
cout<<"Enter the element:";
cin>>item;
dummy=start;
if(dummy==NULL)/*create the list if it is empty*/
{
new1=new node;
new1->data=item;
new1->next=NULL;
new1->prev=NULL;
start=new1;
}
else/* insert at the end*/
{
while(dummy->next!=NULL)
{
dummy=dummy->next;
}
new1=new node;
new1->data=item;
new1->next=NULL;
dummy->next=new1;
new1->prev=dummy;
}
}
void linkedlist::display()
{
temp=start;
if(temp==NULL)
{
cout<<"Doubly linked list is empty"<<endl;
}
else
{
while(temp!=NULL)
{
cout<<temp->data<<"<-->";
temp=temp->next;
}
cout<<"NULL";
}
}
void linkedlist::del()
{
int num;
node *temp, *temp1;
temp=start;
if(temp==NULL)
{
cout<<"\n DLL is not present"<<endl;
}
else
{
cout<<"Enter the number to be deleted:";
cin>>num;
while(temp!=NULL)
{
if(temp->data == num)
{
break;
}
else
{
temp1=temp;
temp=temp->next;
}

}
if(temp==NULL)
{
cout<<"number not found"<<endl;
}
else
{
if(temp==start) /*Delete the first Node*/
{
start=start->next;
temp->next=NULL;
start->prev=NULL;
delete(temp);
cout<<"The starting node is deleted"<<endl;
}
else /*Delete at middle or at the end*/
{
(temp1->next)=temp->next;
if(temp->next!=NULL)
(temp->next)->prev=temp1;
delete(temp);
cout<<"The number is deleted"<<endl;
}
}

}
}
int main()
{
int choice=0;
linkedlist llist;
start=NULL;
do
{
cout<<"\n1.Insertion\n2.Deletion\n3.Display\n4.Exit\n"
<<endl;
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
llist.add();
break;
case 2:
llist.del();
break;
case 3:
llist.display();
break;
case 4:
exit(0);
break;
}
}while(choice<5);
}
Program: Infix to Postfix Conversion

#include<iostream>
#include<string.h>
using namespace std;
#define MAX 25
class infix2postfix
{
public:
void intopost();
};
int s[MAX],top=-1;
char post[MAX],b[MAX];
int pre(char a)
{
int b;
switch(a)
{
case '(':b=0;
break;
case '+':case '-':b=1;
break;
case '*':case '/':b=2;
break;
case '^':b=3;
break;
}
return b;
}
int alphanum(char a)
{
return((a>='a'&&a<='z')||(a>='A'&&a<='Z'));
}
int opr(char a)
{
int b=0;
switch(a)
{
case '+':case '-':case '*':case '/':case '^':b=1;
break;
}
return b;
}
void infix2postfix::intopost()
{
char a,in[25];
int i,ti=-1,tp=-1;
s[++top]='(';
cout<<"\nEnter the infix Expression:\n";
cin>>in;
while(in[++ti]!='\0')
{
if(alphanum(in[ti]))
{
post[++tp]=in[ti];
}
else if(opr(in[ti]))
{
while(pre(in[ti])<=pre(s[top]))
{
post[++tp]=s[top--];
}
s[++top]=in[ti];
}
else if(in[ti]=='(')
{
s[++top]='(';
}
else if(in[ti]==')')
{
while(s[top]!='(')
{
post[++tp]=s[top--];
}
top--;
}
else
{
cout<<"\nInvalid expression"<<endl;
}
}
while(s[top]!='(')
{
post[++tp]=s[top--];
}
post[++tp]='\0';
cout<<"\nPost Fix Expression is:"<<post;
for(i=0;i<strlen(post);i++)
{
b[i]=post[i];
}
}
int main()
{
char a;
char post[25];
infix2postfix i2p;
i2p.intopost();
return 0;
}
Program: Double Ended Queue

#include<iostream>
#include<stdlib.h>
#define size 5
using namespace std;
struct queue
{
int que[size];
int front,rear;
}q;
class de_queue
{
public:
void insert();
void delet();
void display();
};
int qfull()
{
if(q.rear>=size-1)
return 1;
else
return 0;
}
int qempty()
{
if((q.front>q.rear)||(q.rear==-1&&q.front==-1))
return 1;
else
return 0;
}
int insert_rear(int item)
{
if(q.front==-1&&q.rear==-1)
q.front++;
q.que[++q.rear]=item;
return q.rear;
}
int delet_front()
{
int item;
if(q.front==-1)
q.front++;
item=q.que[q.front];
q.que[q.front]=-1;
q.front++;
return item;
}
void delet_rear()
{
int item;
item=q.que[q.rear];
q.que[q.rear]=-1;
q.rear--;
cout<<"\nThe deleted item from the rear end is:"<<item;
}
int insert_front(int item)
{
int i,j;
if(q.front==-1)
q.front++;
else
i=q.front-1;
while(i>=0)
{
q.que[i+1]=q.que[i];
i--;
}
j=q.rear;
while(j>=q.front)
{
q.que[j+1]=q.que[j];
j--;
}
q.rear++;
q.que[q.front]=item;
return q.front;
}
void de_queue::insert()
{
int choice,item;
cout<<"\n1.Insert at front end\n2.Insert at rear end";
cout<<"\nEnter the choice:";
cin>>choice;
switch(choice)
{
case 1:
if(qfull())
cout<<"\nQueue is Full";
else
cout<<"\nEnter the element:";
cin>>item;
q.front=insert_front(item);
break;
case 2:
if(qfull())
cout<<"\nQueue is Full";
else
cout<<"\nEnter the element:";
cin>>item;
q.rear=insert_rear(item);
break;
}
}
void de_queue::delet()
{
int choice,item;
cout<<"\n1.Delete the element at front end";
cout<<"\n2.Delete the element at rear end";
cout<<"\nEnter the choice:";
cin>>choice;
switch(choice)
{
case 1:
if(qempty())
cout<<"\nQueue is empty";
else
item=delet_front();
cout<<"\nThe deleted item from front is:"<<item;
break;
case 2:
if(qempty())
cout<<"\nQueue is empty";
else
delet_rear();
break;
}
}
void de_queue::display()
{
int i;
cout<<"\nQueue is...";
for(i=q.front;i<=q.rear;i++)
cout<<"\t"<<q.que[i];
}
int main()
{
de_queue dq;
int choice,i,item;
char ans='y';
q.front=-1;
q.rear=-1;
do
{
cout<<"\n\nDouble Ended Queue";
cout<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit";
cout<<"\nEnter your choice:";
cin>>choice;
switch(choice)
{
case 1:
dq.insert();
break;
case 2:
dq.delet();
break;
case 3:
if(qempty())
cout<<"\nThe Queue is Empty";
else
dq.display();
break;
case 4:exit(0);
break;
}
cout<<"\nDo you want to continue?(y/n):";
cin>>ans;
}while(ans=='y'||ans=='Y');
return 0;
}

Anda mungkin juga menyukai