Ex No: 2 Array Implementation of List Date
Ex No: 2 Array Implementation of List Date
Date :
AIM:
To develop a C++ program for implementing list abstract data type using array.
ALGORITHM:
STEP 1: Start the program and include the necessary header files for the program
STEP 2: Declare the necessary variables.
STEP 3: Create the main function and declare the necessary operations to be performed on the list.
STEP 4: Define create() function such that it is called whenever the array has be created.
STEP 5: Define insert() function such that it is called whenever the array elements to are to be inserted into
the list
STEP 6: Define the display() function such that it is called whenever the array elements has to be displayed
STEP 7: Define the search() function such that it is called whenever an array element has to be searched and
its position will be returned.
STEP 8: Define the delete() function such that it is called whenever an array element has to be deleted from
the list.
STEP 9: Define the reverse() function such that it is called whenever the array elements has to be displayed
in reverse form.
STEP 10: Get the choice from the user and call the necessary functions wherever needed.
STEP 11: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 20
int list[MAX];
class arr_list
{
public:
int create();
void display(int);
void reverse(int);
int search(int);
void delet(int);
};
int arr_list::create()
{
int i,n;
clrscr();
cout<<"\n\t\t CREATE LIST";
cout<<"\n\t\t -----------";
cout<<"\n\t\t -----------";
cout<<"\n\n How many elements u want in list?:";
cin>>n;
cout<<"\n";
if(n>MAX)
{
cout<<"\n ERROR: No of elements exceed the limit !";
}
for(i=0;i<n;i++)
{
cout<<"\n\n Enter the element number"<<i+1<<":";
cin>>list[i];
}
cout<<"\n List is succesfully created!";
cout<<"\n Press any key to continue...";
getch();
return(n);
}
void arr_list::display(int n)
{
int i;
clrscr();
cout<<"\n\t\t DISPLAY LIST";
cout<<"\n\t\t -------------";
cout<<"\n\t\t -------------";
cout<<"\n\n The list is:";
cout<<"\n\n\t";
for(i=0;i<n;i++)
{
cout<<list[i]<<"\n\n\t";
}
cout<<"\n\n Press any key to continue.";
getch();
}
void arr_list::reverse(int n)
{
int i;
clrscr();
cout<<"\n\t\t REVERSE LIST";
cout<<"\n\t\t ------------";
cout<<"\n\t\t ------------";
cout<<"\n\n The reversed list is:\n\n";
cout<<"\n\t";
for(i=n-1;i>=0;i--)
{
cout<<list[i]<<"\n\n\t";
}
cout<<"\n\n press any key to cntinue...";
getch();
}
int arr_list::search(int n)
{
int i,key;
clrscr();
cout<<"\n\t\t SEARCH LIST";
cout<<"\n\t\t ------------";
cout<<"\n\t\t ------------";
cout<<"\n\nEnter the number u want to search?:";
cin>>key;
for(i=0;i<n;i++)
{
if(list[i]==key)
{
cout<<"\n\n The given number is at position"<<i;
cout<<"\n\n press any key to continue...";
getch();
return i;
}
}
cout<<"\n The given number is not in the list...";
cout<<"\n\n press any key to continue...";
getch();
}
void arr_list::delet(int n)
{
cout<<"\n\t\t DELETE LIST ";
cout<<"\n\t\t -----------";
cout<<"\n\t\t -----------";
int i;
i=search(n);
list[i]=-1;
cout<<"\n\n The given element is now deleted...";
cout<<"\n\n WE put -1 to indicate empty location...";
getch();
}
void main()
{
int choice,len,position;
clrscr();
arr_list a;
do
{
cout<<"\n\n PROGRAM FOR ARRAY IMPLEMENTATION FOR ADT!!!";
cout<<"\n\n 1.Create";
cout<<"\n 2.Display";
cout<<"\n 3.Search for numbers";
cout<<"\n 4.Reverse";
cout<<"\n 5.Delete";
cout<<"\n 6.Quit";
cout<<"\n\nEnter the choice:";
cin>>choice;
switch(choice)
{
case 1: len=a.create();
break;
case 2: a.display(len);
break;
case 3: a.search(len);
break;
case 4: a.reverse(len);
break;
case 5: a.delet(len);
break;
case 6: exit(0);
}
}while(choice!=6);
getch();
}
OUTPUT:
MENU:
1. CREATE
2. DISPLAY
3. SEARCH FOR NUMBERS
4. REVERSE
5. DELETE
6. QUIT
ENTER THE CHOICE: 1
CREATE LIST
------------------
------------------
How many elements you want in the list? 3
Enter the element number1: 10
Enter the element number2: 20
Enter the element number3: 30
List is successfully created!!!
Press any key to continue….
ENTER THE CHOICE: 2
DISPLAY LIST
------------------
------------------
The list is 10 20 30
Press any key to continue….
ENTER THE CHOICE: 3
SEARCH LIST
------------------
------------------
Enter the number you want to search: 20
The given number is at position 1
Press any key to continue….
ENTER THE CHOICE: 4
REVERSE LIST
------------------
------------------
The reverse list is 30 20 10
Press any key to continue…..
ENTER THE CHOICE: 5
DELETE LIST
------------------
------------------
Enter the number you want to search : 20
The given number is at position 1
Press any key to continue….
The given element is now deleted…..
We put -1 to indicate the empty location…
ENTER THE CHOICE: 2
DISPLAY LIST
------------------
------------------
The list is 10 30
ENTER THE CHOICE: 6
RESULT:
Thus the program for implementing list abstract data type using array has been developed, executed and the
output was verified.
Ex no: 3 LINKED LIST IMPLEMENTATION OF LIST ADT
Date :
AIM:
To develop a C++ program for implementing list abstract data type using linked list implementation.
ALGORITHM:
STEP 1: Start the program and include the necessary header files for the program
STEP 2: Declare the necessary variables.
STEP 3: Create the main function and declare the necessary operations to be performed on the list.
STEP 4: Define create() function such that it is called whenever the array has be created.
STEP 5: Define insert() function such that it is called whenever the array elements to are to be inserted into
the list.
STEP 6: Define the printlist() function such that it is called whenever the array elements has to be displayed
STEP 7: Define the find() function such that it is called whenever an array element has to be searched and
its position will be returned.
STEP 8: Define the remove() function such that it is called whenever an array element has to be deleted
from the list.
STEP 9: Define the reverse() function such that it is called whenever the array elements has to be displayed
in reverse form.
STEP 10: Define the findprev() function such that it is called whenever an array element has to be searched
and its previous position will be returned.
STEP 11: Get the choice from the user and call the necessary functions wherever needed.
STEP 12: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
template<class t>
class list
{
struct node
{
t element;
node*next;
node(t E=0,node*n=NULL)
{
element=E;
next=n;
}
};
t x;
node *head;
node *current;
public:
list()
{
head=new node;
current=head;
};
void create();
void insert();
void remove();
void find();
void printlist();
int findprev(t);
};
template<class t>
void list<t>::create()
{
int n,i;
cout<<"\n Enter how many nodes to create:";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"\n Enter node "<<i<<":";
cin>>x;
node*p=new node(x,current->next);
current->next=p;
current=current->next;
}
}
template<class t>
void list<t>::printlist()
{
node*p;
p=head->next;
cout<<"\n\tELEMENTS IN THE LIST\n"<<endl;
while(p!=NULL)
{
cout<<"-> "<<p->element<<" ";
p=p->next;
}
}
template<class t>
int list<t>::findprev(t x)
{
node*q;
for(q=head;q->next!=NULL;q=q->next)
if(q->next->element==x)
{
current=q;
return 1;
}
return 0;
}
template<class t>
void list<t>::remove()
{
node*p;
cout<<"\n ENTER ELEMENT TO BE DELETED:";
cin>>x;
if(findprev(x))
{
p=current->next;
current->next=p->next;
delete p;
}
else
{
cout<<"\n ELEMENT NOT IN THE LIST:";
}
}
template<class t>
void list<t>::insert()
{
int ch,i=1,pos;
node*q,*p;
cout<<"\n\t"<<"INSERT AT\n 1.FIRST\n 2.ANY WHERE\n 3.LAST";
cout<<"\n \n ENTER THE CHOICE:";
cin>>ch;
cout<<"\n ENTER ELEMENT TO INSERT:";
cin>>x;
switch(ch)
{
case 1:
p=new node(x,head->next);
head->next=p;
break;
case 2:
cout<<"\n ENTER THE POSITION:";
cin>>pos;
for(q=head;i<pos&&q!=NULL;q=q->next,i++);
if(q)
{
p=new node(x,q->next);
q->next=p;
}
else
{
cout<<"\n ENTER CORRECT POSITION:";
}
break;
.
case 3:
for(q=head;q->next!=NULL;q=q->next);
p=new node(x,q->next);
q->next=p;
}
}
template<class t>
void list<t>::find()
{
cout<<"\n ENTER ELEMENT TO BE SEARCHED:";
cin>>x;
node*q;
q=head->next;
while(q!=NULL&&q->element!=x)
q=q->next;
if(q)
cout<<"\n ELEMENT FOUND!";
else
cout<<"\n ELEMENT NOT FOUND!";
}
void main()
{
clrscr();
list <int>l;
int ch1;
while(1)
{
cout<<"\n\nOPERATIONS OF SINGLY LINKED LIST\n"<<endl;
cout<<"\n 1.CREATE\n 2.INSERT\n 3.PRINTLIST\n 4.DELETE\n
5.FIND\n 6.EXIT\n";
cout<<"\nEnter the operation to be performed:";
cin>>ch1;
switch(ch1)
{
case 1:
l.create();
break;
case 2:
l.insert();
break;
case 3:
l.printlist();
break;
case 4:
l.remove();
break;
case 5:
l.find();
break;
case 6:
exit(0);
}
}
getch();
}
OUTPUT:
MENU:
1. CREATE
2. DISPLAY
3. PRINTLIST
4. DELETE
5. FIND
6. EXIT
ENTER THE OPERATION TO BE PERFORMED: 1
ENTER HOW MANY NODES TO CREATE? 2
ENTER THE NODE1: 10
ENTER THE NODE2: 20
RESULT:
Thus the program for implementing list abstract data type using linked list has been developed, executed
and the output was verified.
Ex no: 4 CURSOR IMPLEMENTATION OF LIST ADT
Date :
AIM:
To develop a C++ program for implementing list abstract data type using cursor implementation.
ALGORITHM:
STEP 1: Start the program and include the necessary header files for the program
STEP 2: Declare the necessary variables.
STEP 3: Create the main function and declare the necessary operations to be performed on the list.
STEP 4: Define create() function such that it is called whenever the array has be created.
STEP 5: Define the display() function such that it is called whenever the array elements has to be displayed
STEP 6: Define the search() function such that it is called whenever an array element has to be searched and
its position will be returned.
STEP 7: Define the delete() function such that it is called whenever an array element has to be deleted from
the list.
STEP 8: Get the choice from the user and call the necessary functions wherever needed.
STEP 9: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 20
int List[MAX];
void main()
{
int choice,len,position;
int create();
char ans;
clrscr();
void display(int);
int search(int);
void delet(int);
do
{
cout<<"\n\n\tProgram to implement cursor using list...";
cout<<"\n\n 1.CREATE";
cout<<"\n 2.DISPLAY";
cout<<"\n 3.SEARCH FOR A NUMBER";
cout<<"\n 4.DELETE";
cout<<"\n 5.QUIT";
cout<<"\n\n ENTER YOUR CHOICE(1-6):";
cin>>choice;
switch(choice)
{
case 1: len=create();
break;
case 2: display(len);
break;
case 3: position=search(len);
break;
case 4: delet(len);
break;
case 5: cout<<"\n Do u want to exit(y/n)???:";
ans=getche();
if(ans=='y')
exit(0);
else
break;
default: clrscr();
cout<<"\n Invalid choice....Try again";
getch();
}
}while(choice!=5);
}
int create()
{
int n,i;
clrscr();
cout<<"\n How many elements you want in the list:";
cin>>n;
if(n>MAX)
cout<<"\n ERROR: Number of elements exceed the limit...";
for(i=0;i<n;i++)
{
cout<<"\n Enter the element number"<<i+1<<":";
cin>>List[i];
}
cout<<"\n\n The list is successfully created...\n";
getch();
return(n);
}
void display(int n)
{
int i;
clrscr();
cout<<"\n The list is....\n\n";
for(i=0;i<n;i++)
{
if(List[i]!=-1)
cout<<"\t"<<List[i]<<" ";
}
cout<<"\n\n press any key to continue...\n";
getch();
}
int search(int n)
{
int i,key;
clrscr();
cout<<"\n Enter the number u want to search?:";
cin>>key;
for(i=0;i<n;i++)
{
if(List[i]==key)
{
cout<<"\n The given number is at position"<<i+1;
getch();
return i;
}
}
cout<<"\n\n The given number is not in the list...\n";
getch();
}
void delet(int n)
{
int i;
i=search(n);
List[i]=-1;
cout<<"\n\n The element is now deleted...\n\n";
getch();
}
OUTPUT:
MENU:
1. CREATE
2. DISPLAY
3. SEARCH FOR NUMBERS
4. DELETE
5. QUIT
ENTER THE CHOICE: 1
How many elements you want in the list? 3
Enter the element number1: 10
Enter the element number2: 20
Enter the element number3: 30
List is successfully created!!!
ENTER THE CHOICE: 2
The list is 10 20 30
ENTER THE CHOICE: 3
Enter the number you want to search: 20
The given number is at position 1
ENTER THE CHOICE: 4
Enter the number you want to search : 30
The given number is at position 2
The element is now deleted…..
ENTER THE CHOICE: 2
The list is 10 20
ENTER THE CHOICE: 5
RESULT:
Thus the program for implementing list abstract data type using cursor implementation has been
developed, executed and the output was verified
Ex no: 5 ARRAY IMPLEMENTATION OF STACK ADT
Date :
AIM:
To develop a C++ program for implementing stack abstract data type using array implementation.
ALGORITHM:
STEP 1: Start the program and include the necessary header files for the program
STEP 2: Declare the necessary variables and the structure for the stack.
STEP 3: Create the main function and declare the necessary operations to be performed on the stack.
STEP 4: Define the isfull() function such that it is used to check whether the stack is full or not.
STEP 5: Define the isempty() function such that it is used to check whether the stack is empty.
STEP 6: Define the display() function such that it is called whenever the elements in a stack has to be
displayed
STEP 7: Define the push() function such that it is called whenever an array element has to be inserted into
the stack.
STEP 8: Define the pop() function such that it is called whenever an array element has to be deleted from
the stack.
STEP 9: Get the choice from the user and call the necessary functions wherever needed.
STEP 10: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
struct stack
{
int s[size];
int top;
}st;
class arr_stack
{
public:
int stfull();
int stempty();
void push(int);
int pop();
void display();
};
int arr_stack::stfull()
{
if(st.top>=size-1)
{
return 1;
}
else
{
return 0;
}
}
void arr_stack::push(int item)
{
st.top++;
st.s[st.top]=item;
}
int arr_stack::stempty()
{
if (st.top==-1)
{
return 1;
}
else
{
return 0;
}
}
int arr_stack::pop()
{
int item;
item=st.s[st.top];
st.top--;
return(item);
}
void arr_stack::display()
{
int i;
if(stempty())
{
cout<<"\nStack is empty!!!";
}
else
{
for(i=st.top;i>=0;i--)
{
cout<<"\n\n\t";
cout<<st.s[i];
}
}
}
void main()
{
int item,choice;
int ans;
st.top=-1;
arr_stack a;
clrscr();
cout<<"\n PROGRAM FOR ARRAY IMPLEMENTATION OF STACK...";
do
{
cout<<"\n\n 1.Push";
cout<<"\n 2.Pop";
cout<<"\n 3.Display";
cout<<"\n 4.Quit";
cout<<"\n\n Enter your choice(1-4):";
cin>>choice;
switch(choice)
{
case 1: cout<<"\n Enter the item to be pushed:";
cin>>item;
if(a.stfull())
{
cout<<"\n stack is full!";
}
else
{
a.push(item);
}
break;
case 2: if(a.stempty())
{
cout<<"\n Stack is empty!underflow!!";
}
else
{
item=a.pop();
cout<<"\n The popped plement is:"<<item;
}
break;
case 3: a.display();
break;
case 4: exit(0);
}
cout<<"\n\n Do you want to continue?(1-yes,2-no):";
cin>>ans;
}while(ans==1);
getch();
}
OUTPUT:
MENU:
1. PUSH
2. POP
3. DISPLAY
4. QUIT
ENTER THE CHOICE: 1
Enter the item to be pushed : 10
ENTER THE CHOICE: 1
Enter the item to be pushed : 15
ENTER THE CHOICE: 1
Enter the item to be pushed : 25
ENTER THE CHOICE: 3
The stack is 10 15 25
ENTER THE CHOICE: 2
The popped element is 25
ENTER THE CHOICE: 3
The stack is 10 15
ENTER THE CHOICE: 4
RESULT:
Thus the program for implementing stack abstract data type using array implementation has been
developed, executed and the output was verified
Ex no: 6 LINKED LIST IMPLEMENTATION OF STACK ADT
Date :
AIM:
To develop a C++ program for implementing stack abstract data type using linked list implementation.
ALGORITHM:
STEP 1: Start the program and include the necessary header files for the program
STEP 2: Declare the necessary variables and the structure for the stack.
STEP 3: Create the main function and declare the necessary operations to be performed on the stack.
STEP 4: Define the isfull() function such that it is used to check whether the stack is full or not.
STEP 5: Define the isempty() function such that it is used to check whether the stack is empty.
STEP 6: Define the display() function such that it is called whenever the elements in a stack has to be
displayed
STEP 7: Define the push() function such that it is called whenever an array element has to be inserted into
the stack.
STEP 8: Define the pop() function such that it is called whenever an array element has to be deleted from
the stack.
STEP 9: Get the choice from the user and call the necessary functions wherever needed.
STEP 10: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int info;
struct node*link;
}*top=NULL;
class st_list
{
public:
void push();
void pop();
void display();
};
void st_list::push()
{
struct node*tmp;
int pushed_item;
tmp=(struct node*)malloc(sizeof(struct node));
cout<<"\n\n Input the new value to be pushed on the stack:";
cin>>pushed_item;
tmp->info=pushed_item;
tmp->link=top;
top=tmp;
}
void st_list::pop()
{
struct node*tmp;
if(top==NULL)
cout<<"\n\nStack is empty\n";
else
{
tmp=top;
cout<<"\n\n popped item is:"<<tmp->info;
top=top->link;
free(tmp);
}
}
void st_list::display()
{
struct node*ptr;
ptr=top;
if(top==NULL)
cout<<"\n\n stack is empty\n";
else
{
cout<<"\n\n stack elements:\n";
while(ptr!=NULL)
{
cout<<"\n\t"<<ptr->info<<"\n";
ptr=ptr->link;
}
}
}
void main()
{
st_list s;
int choice;
clrscr();
while(1)
{
cout<<"\n\n PROGRAM FOR LIST IMPLEMENTATION OF STACK...";
cout<<"\n\n 1.PUSH";
cout<<"\n 2.POP";
cout<<"\n 3.DISPLAY";
cout<<"\n 4.QUIT";
cout<<"\n\n ENTER YOUR CHOICE:";
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
exit(0);
default:
cout<<"\n\n Wrong decision\n";
}
}
getch();
}
OUTPUT:
1.PUSH
2.POP
3.DISPLAY
4.QUIT
ENTER YOUR CHOICE:3
stack elements:
6
3
PROGRAM FOR LIST IMPLEMENTATION OF STACK...
1.PUSH
2.POP
3.DISPLAY
4.QUIT
ENTER YOUR CHOICE:2
popped item is:6
PROGRAM FOR LIST IMPLEMENTATION OF STACK...
1.PUSH
2.POP
3.DISPLAY
4.QUIT
ENTER YOUR CHOICE:3
stack elements:
3
PROGRAM FOR LIST IMPLEMENTATION OF STACK...
1.PUSH
2.POP
3.DISPLAY
4.QUIT
ENTER YOUR CHOICE:4
RESULT:
Thus the program for implementing stack abstract data type using linked list implementation has been
developed, executed and the output was verified.
Ex no: 7 ARRAY IMPLEMENTATION OF STACK ADT - APPLICATION
Date :
AIM:
To develop a C++ program for implementing application of stack abstract data type using array
implementation.
ALGORITHM:
STEP 1: Start the program and include the necessary header files for the program
STEP 2: Declare the necessary variables and the structure for the stack.
STEP 3: Create the main function and declare the necessary operations to be performed on the stack.
STEP 4: Define the infix() function such that it is used to display the expression in infix format.
STEP 5: Define the prefix() function such that it is used to display the expression in prefix format.
STEP 6: Define the postfix() function such that it is called whenever the elements in a stack has to be
displayed in the postfix format.
STEP 7: Define the push() function such that it is called whenever an array element has to be inserted into
the stack.
STEP 8: Define the pop() function such that it is called whenever an array element has to be deleted from
the stack.
STEP 9: Get the choice from the user and call the necessary functions wherever needed.
STEP 10: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
char inf[40],post[40];
int top=0,st[20];
class infix
{
public:
void postfix();
void push(int);
char pop();
};
void infix::postfix()
{
int i,j=0;
for(i=0;inf[i]!='\0';i++)
{
switch(inf[i])
{
case '+': while(st[top]>=1)
post[j++]=pop();
push(1);
break;
case '-': while(st[top]>=1)
post[j++]=pop();
push(2);
break;
case '*': while(st[top]>=3)
post[j++]=pop();
push(3);
break;
case '/': while(st[top]>=3)
post[j++]=pop();
push(4);
break;
case '^': while(st[top]>=4)
post[j++]=pop();
push(5);
break;
case '(': push(0);
break;
case ')': while(st[top]!=0)
post[j++]=pop();
top--;
break;
default: post[j++]=inf[i];
}
}
while(top>0)
post[j++]=pop();
cout<<"\n\n postfix expression is:"<<post;
}
void infix::push(int ele)
{
top++;
st[top]=ele;
}
char infix::pop()
{
int e1;
char e;
e1=st[top];
top--;
switch(e1)
{
case 1: e='+';
break;
case 2: e='-';
break;
case 3: e='*';
break;
case 4: e='/';
break;
case 5: e='^';
break;
}
return(e);
}
void main()
{
clrscr();
infix i;
cout<<"\n\n ENTER THE INFIX EXPRESSION:";
cin>>inf;
i.postfix();
getch();
}
OUTPUT:
ENTER THE INFIX EXPRESSION:a*b/c+(d*e)-f
postfix expression is:ab*c/de*+f-
RESULT:
Thus the program for implementing the application of stack abstract datatype using array implementation
has been developed, executed and the output was verified.
Ex no: 8 ARRAY IMPLEMENTATION OF QUEUE ADT
Date :
AIM:
To develop a C++ program for implementing queue abstract data type using array implementation.
ALGORITHM:
STEP 1: Start the program and include the necessary header files for the program
STEP 2: Declare the necessary variables and the structure for the queue.
STEP 3: Create the main function and declare the necessary operations to be performed on the queue.
STEP 4: Define the display() function such that it is called whenever the elements in a queue has to be
displayed.
STEP 5: Define the insert() function such that it is called whenever an array element has to be inserted into
the queue.
STEP 6: Define the delete() function such that it is called whenever an array element has to be deleted from
the queue.
STEP 7: Get the choice from the user and call the necessary functions wherever needed
STEP 8: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 5
int queue_arr[MAX];
int rear=-1;
int front=-1;
class arr_q
{
public:
void insert();
void del();
void display();
};
void arr_q::insert()
{
int item;
if(rear==MAX-1)
{
cout<<"\n Queue overflow\n";
}
else
{
if(front==-1)
{
front=0;
}
cout<<"\n Enter the element for adding in queue:";
cin>>item;
rear=rear+1;
queue_arr[rear]=item;
}
}
void arr_q::del()
{
if(front==-1|| front>rear)
{
cout<<"\n Queue underflow\n";
return;
}
else
{
cout<<"\n Element deleted from queue is:"<<queue_arr[front];
front=front+1;
}
cout<<"\n";
}
void arr_q::display()
{
int i;
if(front==-1)
{
cout<<"\n Queue is empty\n";
}
else
{
cout<<"\n Queue is:\n\n";
for(i=front;i<=rear;i++)
{
cout<<" "<<queue_arr[i];
}
cout<<"\n";
}
}
void main()
{
int choice;
arr_q q;
clrscr();
while(1)
{
cout<<"\n PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE...";
cout<<"\n\n 1.Insert\n";
cout<<" 2.Delete\n";
cout<<" 3.Display\n";
cout<<" 4.Quit\n";
cout<<"\n Enter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1: q.insert();
break;
case 2: q.del();
break;
case 3: q.display();
break;
case 4: exit(0);
default: cout<<"\n Wrong choice\n";
}
}
getch();
}
OUTPUT:
PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE...
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice(1-4): 2
Queue underflow
1.Insert
2.Delete
3.Display
4.Quit
1.Insert
2.Delete
3.Display
4.Quit
1.Insert
2.Delete
3.Display
4.Quit
23 34
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice(1-4): 2
1.Insert
2.Delete
3.Display
4.Quit
Queue is:
34
1.Insert
2.Delete
3.Display
4.Quit
RESULT:
Thus the program for implementing the application of queue abstract datatype using array implementation
has been developed, executed and the output was verified.
Ex no: 9 LINKED LIST IMPLEMENTATION OF QUEUE ADT
Date :
AIM:
To develop a C++ program for implementing queue abstract data type using linked list implementation.
ALGORITHM:
STEP 1: Start the program and include the necessary header files for the program
STEP 2: Declare the necessary variables and the structure for the queue.
STEP 3: Create the main function and declare the necessary operations to be performed on the queue.
STEP 4: Define the display() function such that it is called whenever the elements in a queue has to be
displayed.
STEP 5: Define the insert() function such that it is called whenever element has to be inserted into the
queue.
STEP 6: Define the delete() function such that it is called whenever element has to be deleted from the
queue.
STEP 7: Get the choice from the user and call the necessary functions wherever needed
STEP 8: Stop the program.
PROGRAM :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int info;
struct node * link;
}*front=NULL,*rear=NULL;
class list_q
{
public:
void insert();
void del();
void display();
};
void list_q::insert()
{
struct node*tmp;
int added_item;
tmp=(struct node*)malloc(sizeof(struct node));
cout<<"\n\nInput the element for adding in queue:";
cin>>added_item;
tmp->info=added_item;
tmp->link=NULL;
if(front==NULL)
front=tmp;
else
rear->link=tmp;
rear=tmp;
}
void list_q::del()
{
struct node*tmp;
if(front==NULL)
{
cout<<"\n\nQUEUE UNDERFLOW\n";;
}
else
{
tmp=front;
cout<<"\n\nDELETED ELEMENT IS:"<<tmp->info;
front=front->link;
free(tmp);
}
}
void list_q::display()
{
struct node*ptr;
ptr=front;
if(front==NULL)
{
cout<<"\n\nQUEUE IS EMPTY\n";
}
else
{
cout<<"\n\nQUEUE ELEMENTS :\n\n";
while(ptr!=NULL)
{
cout<<"\n\t"<<ptr->info<<"\n";
ptr=ptr->link;
}
cout<<"\n";
}
}
void main()
{
clrscr();
list_q l;
int choice;
while(1)
{
cout<<"\n\n PROGRAM FOR LINKED LIST IMPLEMENTATION OF QUEUE...";
cout<<"\n\n\n 1.INSERT\n";
cout<<"\n 2.DELETE\n";
cout<<"\n 3.DISPLAY\n";
cout<<"\n 4.QUIT\n";
cout<<"\n ENTER YOUR CHOICE(1-4):";
cin>>choice;
switch(choice)
{
case 1: l.insert();
break;
case 2: l.del();
break;
case 3: l.display();
break;
case 4: exit(0);
default: cout<<"\n\n WRONG CHOICE(1-4):";
}
}
getch();
}
OUTPUT:
1.INSERT
2.DELETE
3.DISPLAY
4.QUIT
1.INSERT
2.DELETE
3.DISPLAY
4.QUIT
QUEUE ELEMENTS :
1.INSERT
2.DELETE
3.DISPLAY
4.QUIT
1.INSERT
2.DELETE
3.DISPLAY
4.QUIT
QUEUE ELEMENTS :
1.INSERT
2.DELETE
3.DISPLAY
4.QUIT
RESULT:
Thus the program for implementing the application of queue abstract datatype using linked list
implementation has been developed, executed and the output was verified.