Sie sind auf Seite 1von 31

PROGRAM-4:

#include<fstream.h>
#include<conio.h>
#include<process.h>
class student
{
int rno;
char nm[30];
public:
void accept()
{
cout<<"\n Enter the roll number and name of the student ";
cin>>rno>>nm;
}
void display()
{
cout<<"\n Roll Number: "<<rno<<"\n Name : "<<nm;
}
};
void main()
{
fstream fob;
int ch;
char rep;
student s;
clrscr();
do
{
cout<<"\n 1. write into file";
cout<<"\n 2. read file ";
cout<<"\n enter choice ";
cin>>ch;
switch(ch)
{
case 1:
{
fob.open("stu.dat",ios::app | ios::binary);
s.accept();
fob.write((char *) &s, sizeof(s));
fob.close();
break;
}
case 2:
{
fob.open("stu.dat",ios::in | ios::binary);
clrscr();
while(!fob.eof())
{
fob.read((char *) &s, sizeof(s));
s.display();
}
fob.close();
break;
}
default: exit(0);
}
cout<<"\t Want to continue ? (y/n)";
cin>>rep;
}while(rep=='y');
getch();
}
PROGRAM-5:
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
struct book
{
int id;
char des[50];
};
class stack
{
int top;
book b[50];
public:
stack()
{
top=0;
}
void push()
{
if(top==5)
cout<<"\n Stack is full";
else
{
cout<<"\n Enter the ID and Description of the book";
cin>>b[top].id;
gets(b[top].des);
top++;
cout<<"\n Data stored";
}
}
void pop()
{
if(top==0)
cout<<"\n Stack empty";
else
{
top--;
cout<<"Book deleted"<<b[top].id<<" "<<b[top].des;
}
}
void display()
{
if(top==0)
cout<<"\n Stack empty";
else
for(int i=top-1; i>=0; i--)
cout<<endl<<b[i].id<<" "<<b[i].des;
}
};
void main()
{
clrscr();
stack s;
int ch;
while(1)
{
cout<<"\n 1. PUSH";
cout<<"\n 2. POP";
cout<<"\n 3. DISPLAY";
cout<<"\n 4. EXIT";
cout<<"\n Enter choice(1/2/3/4)";
cin>>ch;
switch(ch)
{
case 1 : s.push();
break;
case 2 : s.pop();
break;
case 3 : s.display();
break;
case 4 : exit(0);
break;
default : cout<<"INVALID CHOICE";
}
}
getch();
}
PROGRAM-6:
#include <iostream.h>
#include<conio.h>
int queue[5], n = 5, front = - 1, rear = - 1;int ch;
void Insert()
{
int val;
if (rear == n - 1)
{
cout<<"Queue Overflow"<<endl;
getch();
}
else
{
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}
}
void Delete()
{
if (front == - 1 || front > rear)
{
cout<<"Queue Underflow ";
return ;
}
else
{
cout<<"Element deleted from queue is : "<< queue[front]
<<endl;
front++;
}
}
void Display()
{
if (front == - 1)
{
cout<<"Queue is empty"<<endl;
getch();
}
else
{
cout<<"Queue elements are : ";
for (int i =front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
int main() {
int ch;
do {
clrscr();
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;
cout<<"4) Exit"<<endl;
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch)
{
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
getch();
break;
case 4: cout<<"Exit"<<end;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}
PROGRAM-7:
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void push();
void pop();
void display();
};
void stack::display()
{
node *ptr;
ptr=top;
cout<<"The stack is :<<endl;
while(ptr!=NULL)
{
cout<<ptr->data<<endl;
ptr=ptr->next;
}
}
void stack::push()
{
node *ptr;
ptr=new node;
cout<<"Enter the element to be pushed "<<endl;
cin>>ptr->data;
if(top==NULL)
ptr->next=NULL;
else
ptr->next=top;
top=ptr;
}
void stack::pop()
{
node *ptr;
ptr=top;
cout<<"The popped element is "<<ptr->data;
top=top->next;
delete ptr;
}
void main()
{
clrscr();
char ans;
stack s1;
do
{
s1.push();
cout<<"Wish to continue ?"<<endl;
cin>>ans;
}while(ans=='y');
s1.display();
cout<<"Press any key to pop an element"<<endl;
getch();
s1.pop();
s1.display();
getch();
}
PROGRAM-8:
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
class queue()
{
node *front,*rear;
public:
queue()
{
rear=front=NULL;
}
void ins();
void del();
void dis();
};
void queue::dis()
{
node *ptr;
ptr=front;
cout<<"The queue is "<<endl;
while(ptr!=NULL)
{
cout<<ptr->data<<endl;
ptr=ptr->next;
}
}
void queue::ins()
{
node *ptr;
ptr=new node;
cout<<"Enter the element to be inserted"<<endl;
cin>>ptr->data;
ptr->next=NULL;
if(rear==NULL)
front=rear=ptr;
else
{
rear->next=ptr;
rear=ptr;
}
}
{
void queue::del()
{
node *ptr;
ptr=front;
cout<<"The deleted element is"<<ptr->data;
if(front==rear)
front=rear=NULL;
else
front=front->next;
delete ptr;
}
void main()
{
clrscr();
char ans;
queue q1;
do
{
q1.ins();
cout<<"Wish to continue ? "<<endl;
cin>>ans;
}while(ans=='y'||ans=='Y')
q1.ins();
cout<<"Press any key to delete an element"<<endl;
getch();
q1.del();
q1.dis();
getch();
}

Das könnte Ihnen auch gefallen