Professional Documents
Culture Documents
LINKED STACK
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
int data;
node*link;
};
node *newptr;
newptr=new node;
newptr->data=val;
newptr->link=top;
top=newptr;
return (top);
node *pop(node*top,int&val)
node*ptr;
if (top==NULL)
{
cout<<"\n List empty";
else
ptr=top;
val=ptr->data;
top=top->link;
ptr->link=NULL;
delete (ptr);
return ( top);
void display(node*top)
node *temp;
temp=top;
while (temp!=NULL)
cout<<temp->data<<"\t";
temp=temp->link;
void main()
{
clrscr();
node*top;
top=NULL;int ch,val,value;
do
cout<<"\n 1.Push";
cout<<"\n 2.Pop";
cout<<"\n 3.Display";
cout<<"\n 4.Exit";
cin>>ch;
switch(ch)
cin>>value;
top=push(top,value);
break;
case 2: top=pop(top,val);
if(val!=-1)
break;
case 3: display(top);
break;
case 4: exit(0);
}
} while(ch<4);
getch();
}
OUTPUT
Push
DISPLAY
STACK
POP
Original Stack
Deleted Value
Stack
LINKED LIST
CONCATENATION
#include<iostream.h>
#include<conio.h>
struct node
int data;
node *link;
};
node *conc(node*first,node*top)
node *temp,*back;
temp=first;
while(temp!=NULL)
back=temp;
temp=temp->link;
back->link=top;
return first;
}
void main()
clrscr();
node *top,*first,*last,*temp;
int n,m;
cin>>n;
first=new node;
cin>>first->data;
first->link=NULL;
temp=first;
for(int i=1;i<n;i++)
last=new node;
cin>>last->data;
last->link=NULL;
temp->link=last;
temp=last;
temp=first;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->link;
cin>>n;
top=new node;
cin>>top->data;
top->link=NULL;
temp=top;
for(i=1;i<n;i++)
last=new node;
cin>>last->data;
last->link=NULL;
temp->link=last;
temp=last;
temp=top;
while(temp!=NULL)
cout<<temp->data<<endl;
temp=temp->link;
first=conc(first,top);
temp=first;
while(temp!=NULL)
cout<<temp->data<<endl;
temp=temp->link;
getch();
OUTPUT
1st linked
Concatenated
list
CREATION OF LINKED LIST
#include<iostream.h>
#include<conio.h>
struct node
int data;
node *link;
};
void main()
clrscr();
node *first,*last,*temp;
int n;
cin>>n;
first=new node;
cin>>first->data;
first->link=NULL;
temp=first;
for(int i=1;i<n;i++)
{
last=new node;
cin>>last->data;
last->link=NULL;
temp->link=last;
temp=last;
temp=first;
while(temp!=NULL)
cout<<temp->data<<endl;
temp=temp->link;
getch();
OUTPUT
INSERTION IN LINKED LIST
#include<iostream.h>
#include<conio.h>
struct node
int data;
node *link;
};
void main()
clrscr();
node *first,*last,*temp;
int n;
cin>>n;
first=new node;
cin>>first->data;
first->link=NULL;
temp=first;
for(int i=1;i<n;i++)
last=new node;
cin>>last->data;
last->link=NULL;
temp->link=last;
temp=last;
temp=first;
while(temp!=NULL)
cout<<temp->data<<endl;
temp=temp->link;
int c;
cin>>c;
insert_beg(first,c);
int c1;
cin>>c1;
insert_last(first,last,c1);
getch();
node *temp,*newptr;
temp=first;
newptr=new node;
newptr->data=val;
if(first==NULL)
first=newptr;
else
newptr->link=first;
first=newptr;
temp=first;
while(temp!=NULL)
cout<<temp->data<<endl;
temp=temp->link;
}
temp=first;
node *temp,*newptr;
newptr=new node;
newptr->data=val1;
newptr->link=NULL;
if(first==NULL)
first=last=newptr;
else
last->link=newptr;
last=newptr;
temp=first;
while(temp!=NULL)
cout<<temp->data<<endl;
temp=temp->link;
OUTPUT
Original
Linked list
Beginning
End
ARRAY STACK
#include<iostream.h>
#include<process.h>
#include<conio.h>
class stack
int s[20],n,top;
public:
stack()
top=-1;
n=5;
int pop();
void disp();
};
if(top<n)
top++;
s[top]=val;
}
else
cout<<"\nSTACK OVERFLOW!!!!!!!!!";
int stack::pop()
int v;
if(top>=0)
v=s[top];
top--;
return v;
else
return -9999;
void stack::disp()
for(int i=top;i>=0;i--)
cout<<s[i]<<endl;
}
void main()
clrscr();
stack q;
int a,b,ch;
do
cout<<"\n 1. Push";
cout<<"\n 2. Pop";
cout<<"\n 3. Display";
cout<<"\n 4. Quit";
cin>>ch;
switch(ch)
case 1:
cin>>a;
q.push(a);
break;
case 2:
b=q.pop();
cout<<b;
if(b!=-9999)
break;
case 3:
q.disp();
break;
case 4 :
exit(0);
}while(ch!=4);
getch();
OUTPUT
PUSH
Push
Stack
POP
Original Stack
Pop
Stack
LINKED QUEUE
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
int data;
node*link;
};
node *newptr;
newptr=new node;
newptr->data=val;
newptr->link=NULL;
rear->link=newptr;
rear=newptr;
return (rear);
node *pop(node*top,int&val)
node*ptr;
if (top==NULL)
{
cout<<"\n List empty";
val=-1;
else
ptr=top;
val=ptr->data;
top=top->link;
ptr->link=NULL;
delete (ptr);
return (top);
void display(node*top)
node *temp;
temp=top;
while (temp!=NULL)
cout<<temp->data<<"\t";
temp=temp->link;
void main()
{
clrscr();
node*rear,*top;
top=NULL;int ch,val,value;
rear=NULL;
do
cout<<"\n 1.Enter";
cout<<"\n 2.Delete";
cout<<"\n 3.Display";
cout<<"\n 4.Exit";
cin>>ch;
switch(ch)
cin>>value;
rear=push(rear,value);
if(top==NULL)
top=rear;
break;
case 2: top=pop(top,val);
if(val!=-1)
break;
case 3: display(top);
break;
case 4: exit(0);
} while(ch<4);
getch();
OUTPUT
INSERTION
Insertion
Queue
DELETION
Original Queue
Deletion
Queue
ARRAY QUEUE
#include<iostream.h>
#include<process.h>
#include<conio.h>
class queue
int q[20],n,rear,front;
public:
queue()
rear=front=-1;
n=5;
int del();
void disp();
if(rear<n)
rear++;
q[rear]=val;
}
else
cout<<"\nQUEUE OVERFLOW!!!!!!!!!";
int queue::del()
int v;
if(front!=rear)
v=q[front+1];
front++;
return v;
else
return -9999;
void queue::disp()
for(int i=front+1;i<=rear;i++)
cout<<q[i]<<endl;
void main()
{
clrscr();
queue s;
int a,b,ch;
do
cout<<"\n 1. Enter";
cout<<"\n 2. Delete";
cout<<"\n 3. Display";
cout<<"\n 4.Quit";
cin>>ch;
switch(ch)
cin>>a;
s.insert(a);
break;
case 2:b=s.del();
cout<<b;
if(b!=-9999)
break;
case 3:s.disp();
break;
case 4 :exit(0);
}while(ch!=4);
getch();
INSERTION
Insertion
Queue
DELETION
Deletion
Queue
NESTED STRUCTURE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct addr
int houseno;
char city[15];
char state[15];
};
struct employee
int empno;
char name[20];
char design[20];
addr address;
float salary;
}emp;
void main()
clrscr();
cin>>emp.empno;
cout<<"\n Enter Name";
gets(emp.name);
gets(emp.design);
cin>>emp.address.houseno;
gets(emp.address.city);
gets(emp.address.state);
cin>>emp.salary;
cout<<"\n City"<<emp.address.city;
cout<<"\n State"<<emp.address.state;
cout<<"\n Designation"<<emp.design;
cout<<"\n Salary"<<emp.salary;
getch();
OUTPUT
CLASSES AND OBJECTS
Define a class travel with the description given below:
PRIVATE MEMBERS:
tcode string
no_of_adults integer
no_of_children integer
distance integer
totalfare float
PUBLIC MEMBERS:
FARE(RS) DISTANCE(KM)
500 >=1000
200 <500
For each child the above fare will be 50% of the fare mentioned in the above table
A function entertravel() to input the values of the data members tcode, no_of_adults,
no_of_children, distance; and invoke the assignfare() function.
A function showtravel() which displays the content of all the data members for a travel
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class travel
char tcode[5];
int no_of_adults;
int no_of_children;
int distance;
float totalfare;
public:
travel();
void assignfare();
void entertravel();
void showtravel();
};
travel::travel()
strcpy(tcode,"NULL");
no_of_adults=0;
no_of_children=0;
distance=0;
totalfare=0;
void travel::assignfare()
if(distance>=1000)
{
totalfare=500*no_of_adults+250*no_of_children;
else if(distance>=500)
totalfare=300*no_of_adults+150*no_of_children;
else
totalfare=200*no_of_adults+100*no_of_children;
void travel::entertravel()
gets(tcode);
cin>>no_of_adults;
cin>>no_of_children;
cin>>distance;
assignfare();
void travel::showtravel()
{
cout<<"\n Code"<<tcode;
cout<<"\n Distance"<<distance;
cout<<"\n Totalfare"<<totalfare;
void main()
clrscr();
travel trav;
trav.entertravel();
trav.showtravel();
getch();
OUTPUT
DISTANCE>=1000
DISTANCE>=500
ELSE
CLASS AND OBJECT
Write a program using the following specifications
Class-book
book_title 20 characters
total_cost() a function to calculate the total cost of n number of copies where n is passed to the
function as argument.
purchase() function to ask the user to input the number of copies to be purchased.it invokes total cost
paid by user
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class book
int bookno;
char book_title[20];
float price;
public:
void enterdata();
void purchase();
};
void book::enterdata()
cin>>bookno;
gets(book_title);
cin>>price;
float book::total_cost(int n)
return price*n;
}
void book::purchase()
int num;
float cost;
cin>>num;
cost=total_cost(num);
void main()
clrscr();
book bk;
int choice;
do
cout<<"\n 2.Purchase";
cin>>choice;
switch(choice)
case 1:bk.enterdata();
break;
case 2: bk.purchase();
break;
while(choice<=3);
getch();
OUTPUT
MULTILEVEL INHERITANCE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class employee
char name[len];
int code;
public:
void getdata()
cin>>code;
gets(name);
void putdata()
};
{
};
float jobs;
public:
void getsdata()
labour::getdata();
cin>>jobs;
void putsdata()
labour::putdata();
cout<<"\n Jobs:"<<jobs;
};
void main()
clrscr();
labour ll;
foreman fl;
cout<<endl;
fl.getdata();
cout<<"\n Enter data for foreman:";
fl.getsdata();
cout<<endl;
fl.putdata();
fl.putsdata();
getch();
OUTPUT
MULTIPLE INHERITANCE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class employee
char name[20];
int age;
public:
void getemp()
gets(name);
cin>>age;
void putemp()
cout<<"\n Age:"<<age;
};
class student
char degree[20];
char university[20];
public:
void getstu()
cin>>degree;
cin>>university;
void putstu()
cout<<"\n Degree"<<degree;
cout<<"\n University"<<university;
};
char designation[20];
float salary;
public:
void getmanager()
cin>>designation;
void putmanager()
cout<<"\n Designation"<<designation;
cout<<"\n Salary"<<salary;
};
void main()
clrscr();
manager m;
m.getemp();
m.getmanager();
m.getstu();
m.putemp();
m.putstu();
m.putmanager();
getch();
}
OUTPUT
FILE HANDLING-WRITING A FILE
#include<fstream.h>
void main()
ofstream afile("newfile.txt");
if(!afile)
afile<<"\n 2.Processor";
afile.close();
READING A FILE
#include<fstream.h>
void main()
ifstream afile("newfile.dat");
for(int i=0;i>afile.eof();i++)
{
afile.getline(line,100);
cout<<line<<"\n";
afile.close();
OUTPUT
1. Input Unit
2.Processor
3.Output Unit