Pds Prgs CSE

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

//balancing paranthesis

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Node
{
private:
char data;
Node *next;
public:
Node(){}
Node(char newdata)
{data=newdata;
next=NULL;}
void push(char);
void pop(void);
};
Node *top=NULL;
void Node :: push(char ele)
{
Node *newNode=new Node(ele);
if(top== NULL)
{top=newNode;}
else
{newNode->next=top;
top=newNode;}
}
void Node::pop(void)
{
if (top==NULL)
{ cout<<"\n STACK IS EMPTY\n"; }
else
{ Node *temp=top;
top=top->next;
delete temp; }
}
const size=25;
void main()
{
int i=0;
Node Nd;
char expr[size];
clrscr();
cout<<"\nENTER THE EXPRESSION";
cin>>expr;
while(expr[i]!='\0')
{
if(expr[i]=='(')
Nd.push(expr[i]);
if(expr[i]==')')
{
if(top==NULL)
{ cout<<"\nPARANTHESIS IS NOT BALANCED\n";
getch();
exit(0); }
else
Nd.pop();
}
i++;
}
if(top==NULL)
cout<<"\nPARANTHESIS IS BALANCED\n";
else
cout<<"\nPARANTHESIS IS NOT BALANCED\n";
getch();
}





Output:
ENTER THE EXPRESSION ((a+d)
PARANTHESIS IS NOT BALANCED
ENTER THE EXPRESSION ((a+e)-(a*d))
PARANTHESIS IS BALANCED


//application of queue
#include <iostream.h>
#include<conio.h>
#include <stdlib.h>
#define MAX 5
void insert_by_priority(int);
void delete_by_priority(int);
void create();
void check(int);
void display_pqueue();
int pri_que[MAX];
int front, rear;
void main()
{
clrscr();
int n, ch;
cout<<"\nTRAIN RESERVATION SYSTEM";
cout<<"\n1 - TO ISSUE A TICKET";
cout<<"\n2 - TO CANCEL A TICKET";
cout<<"\n3 - DISPLAY ALL BOOKED TICKETS";
cout<<"\n4 - EXIT";
create();
while (1)
{
cout<<"\nEnter your choice : ";
cin>>ch;
switch (ch)
{
case 1:
cout<<"\nEnter ticket number to be issued : ";
cin>>n;
insert_by_priority(n);
break;
case 2:
cout<<"\nEnter ticket number to be cancelled : ";
cin>>n;
delete_by_priority(n);
break;
case 3:
display_pqueue();
break;
case 4:
exit(0);
default:
cout<<"\nChoice is incorrect";
}
}
}
void create()
{
front = rear = -1;
}
void insert_by_priority(int data)
{
if (rear >= MAX - 1)
{ cout<<"\nall tickets booked";
return; }
if ((front == -1) && (rear == -1))
{
front++;
rear++;
pri_que[rear] = data;
return;
}
else
check(data);
rear++;
}
void check(int data)
{
int i,j;
for (i = 0; i <= rear; i++)
{
if (data >= pri_que[i])
{
for (j = rear + 1; j > i; j--)
{ pri_que[j] = pri_que[j - 1]; }
pri_que[i] = data;
return;
}
}
pri_que[i] = data;
}
void delete_by_priority(int data)
{
int i;
if ((front==-1) && (rear==-1))
{
cout<<"\nno ticket has been issued ";
return;
}
for (i = 0; i <= rear; i++)
{
if (data == pri_que[i])
{
for (; i < rear; i++)
{ pri_que[i] = pri_que[i + 1]; }
pri_que[i] = -99;
rear--;
if (rear == -1)
front = -1;
return;
}
}
cout<<"\nticket number %d has not been issued"<< data;
}
void display_pqueue()
{
if ((front == -1) && (rear == -1))
{ cout<<"\nall tickets available for reservation";
return; }
for (; front <= rear; front++)
{ cout<<"\n"<< pri_que[front]; }
front = 0;
}


Output:
TRAIN RESERVATION SYSTEM
1 - TO ISSUE A TICKET
2 -TO CANCEL A TICKET
3 - DISPLAY ALL BOOKED TICKETS
4 - EXIT
Enter your choice: 1
Enter ticket number to be issued : 1729
Enter your choice: 1
Enter ticket number to be issued : 823
Enter your choice: 1
Enter ticket number to be issued : 936
Enter your choice: 3
1729
936
823
Enter your choice: 2
Enter ticket number to be cancelled: 936
Enter your choice: 3
1729
823
Enter your choice: 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy