You are on page 1of 53

STACK QUEUE AND LINKED LIST

LINKED STACK

#include<iostream.h>

#include<conio.h>

#include<process.h>

struct node

int data;

node*link;

};

node *push(node*top,int val)

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";

cout<<"\n Enter your choice";

cin>>ch;

switch(ch)

case 1: cout<<"\n Enter value to be inserted";

cin>>value;

top=push(top,value);

break;

case 2: top=pop(top,val);

if(val!=-1)

cout<<"\n The deleted value is"<<val;

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;

cout<<"\n Enter size of first list:\n";

cin>>n;

first=new node;

cout<<"\n Enter value for first list:\n";

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;

cout<<"\n The entered linked list is:\n";

while(temp!=NULL)

{
cout<<temp->data<<endl;

temp=temp->link;

cout<<"\n Enter size of second list:\n";

cin>>n;

top=new node;

cout<<"\n Enter value for second list:\n";

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;

cout<<"\n The entered linked list is:\n";

while(temp!=NULL)

cout<<temp->data<<endl;
temp=temp->link;

first=conc(first,top);

cout<<"\n Concatenated list is:\n";

temp=first;

cout<<"\n The entered linked list is:\n";

while(temp!=NULL)

cout<<temp->data<<endl;

temp=temp->link;

getch();

OUTPUT

1st linked

2nd linked list

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;

cout<<"\n Enter size:\n";

cin>>n;

first=new node;

cout<<"\n Enter value\n";

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;

cout<<"\n The entered linked list is:\n";

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;

void insert_beg(node *first,int val);

void insert_last(node *first,node *last,int val1);

int n;

cout<<"\n Enter size:\n";

cin>>n;

first=new node;

cout<<"\n Enter value\n";

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;

cout<<"\n The entered linked list is:\n";

while(temp!=NULL)

cout<<temp->data<<endl;

temp=temp->link;

cout<<"\n Enter new value:\t";

int c;

cin>>c;

insert_beg(first,c);

cout<<"\n Enter new value:\t";

int c1;

cin>>c1;

insert_last(first,last,c1);
getch();

void insert_beg(node *first,int val)

node *temp,*newptr;

temp=first;

newptr=new node;

newptr->data=val;

if(first==NULL)

first=newptr;

else

newptr->link=first;

first=newptr;

temp=first;

cout<<"\n The new linked list is:\n";

while(temp!=NULL)

cout<<temp->data<<endl;

temp=temp->link;

}
temp=first;

void insert_last(node *first,node *last,int val1)

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;

cout<<"\n The new linked list is:\n";

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;

void push(int val);

int pop();

void disp();

};

void stack::push(int val)

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";

cout<<"\n Enter your choice";

cin>>ch;

switch(ch)

case 1:

cout<<"\n Enter value to be entered:\t";

cin>>a;

q.push(a);

break;

case 2:

b=q.pop();
cout<<b;

if(b!=-9999)

cout<<"\n Deleted value is:\n"<<b;

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 *push(node*rear,int val)

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";

cout<<"\n Enter your choice";

cin>>ch;

switch(ch)

case 1: cout<<"\n Enter value to be inserted";

cin>>value;

rear=push(rear,value);

if(top==NULL)

top=rear;

break;

case 2: top=pop(top,val);

if(val!=-1)

cout<<"\n The deleted value is"<<val;

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;

void insert(int val);

int del();

void disp();

void queue::insert(int val)

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";

cout<<"\n Enter your choice";

cin>>ch;

switch(ch)

case 1:cout<<"\n Enter value to be entered:\t";

cin>>a;

s.insert(a);

break;

case 2:b=s.del();

cout<<b;

if(b!=-9999)

cout<<"\n Deleted value is:\n"<<b;

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();

cout<<"\n Enter Employee Number";

cin>>emp.empno;
cout<<"\n Enter Name";

gets(emp.name);

cout<<"\n Enter Designation";

gets(emp.design);

cout<<"\n Enter Address:";

cout<<"\n Enter House number";

cin>>emp.address.houseno;

cout<<"\n Enter city:";

gets(emp.address.city);

cout<<"\n Enter State";

gets(emp.address.state);

cout<<"\n Enter Salary";

cin>>emp.salary;

cout<<"\n Employee Code"<<emp.empno;

cout<<"\n Employee Name"<<emp.name;

cout<<"\n Enployee Address";

cout<<"\n House Number"<<emp.address.houseno;

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:

 A constructor to assign initial values as 0.


 A function assignfare() which calculates and assigns the value of the data member totalfare as
follows :

FARE(RS) DISTANCE(KM)

500 >=1000

300 <1000 & >=500

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()

cout<<"\n Enter Code";

gets(tcode);

cout<<"\n Enter the No.of adults";

cin>>no_of_adults;

cout<<"\n Enter the No.of children";

cin>>no_of_children;

cout<<"\n Enter Distance";

cin>>distance;

assignfare();

void travel::showtravel()
{

cout<<"\n Code"<<tcode;

cout<<"\n Number of adults"<<no_of_adults;

cout<<"\n Number of Children"<<no_of_children;

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

Private members of the class book are:

bookno integer type

book_title 20 characters

price float(price per copy)

total_cost() a function to calculate the total cost of n number of copies where n is passed to the
function as argument.

Public members of the class book are:

enterdata() function to read bookno,book_title,price

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;

float total_cost(int n);

public:

void enterdata();

void purchase();

};

void book::enterdata()

cout<<"\n Enter Bookno";

cin>>bookno;

cout<<"\n Enter Book title";

gets(book_title);

cout<<"\n Enter Price";

cin>>price;

float book::total_cost(int n)

return price*n;

}
void book::purchase()

int num;

float cost;

cout<<"\n Enter the number of copies to be purchased:";

cin>>num;

cost=total_cost(num);

cout<<"\n Total cost"<<cost;

void main()

clrscr();

book bk;

int choice;

do

cout<<"\n 1. Enter book details";

cout<<"\n 2.Purchase";

cout<<"\n Enter choice:";

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>

const int len=80;

class employee

char name[len];

int code;

public:

void getdata()

cout<<"\n Enter Employee code";

cin>>code;

cout<<"\n Enter Name:";

gets(name);

void putdata()

cout<<"\n Employee code"<<code;

cout<<"\n Employee name"<<name;

};

class labour:public employee

{
};

class foreman:public labour

float jobs;

public:

void getsdata()

labour::getdata();

cout<<"\n Enter No.Of jobs";

cin>>jobs;

void putsdata()

labour::putdata();

cout<<"\n Jobs:"<<jobs;

};

void main()

clrscr();

labour ll;

foreman fl;

cout<<endl;

cout<<"\n Enter data for labours:";

fl.getdata();
cout<<"\n Enter data for foreman:";

fl.getsdata();

cout<<endl;

cout<<"\n Data on labour:";

fl.putdata();

cout<<"\n Data on foreman";

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()

cout<<"\n Enter Name";

gets(name);

cout<<"\n Enter age";

cin>>age;

void putemp()

cout<<"\n Name of employee:"<<name;

cout<<"\n Age:"<<age;

};

class student

char degree[20];
char university[20];

public:

void getstu()

cout<<"\n Enter highest qualification";

cin>>degree;

cout<<"\n Enter name of university ";

cin>>university;

void putstu()

cout<<"\n Degree"<<degree;

cout<<"\n University"<<university;

};

class manager:public employee, public student

char designation[20];

float salary;

public:

void getmanager()

cout<<"\n Enter Designation:";

cin>>designation;

cout<<"\n Enter Salary";


cin>>salary;

void putmanager()

cout<<"\n Designation"<<designation;

cout<<"\n Salary"<<salary;

};

void main()

clrscr();

manager m;

cout<<"\n Enter details:";

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)

cout<<"\n File cannot be opened";

afile<<"\n Computer converts data into information";

afile<<"\n It has 3 functional Components:";

afile<<"\n 1. Input Unit";

afile<<"\n 2.Processor";

afile<<"\n 3.Output Unit";

afile.close();

READING A FILE
#include<fstream.h>

void main()

ifstream afile("newfile.dat");

char line [100], ch;

for(int i=0;i>afile.eof();i++)
{

afile.getline(line,100);

cout<<line<<"\n";

afile.close();

OUTPUT

Computer converts data into information

It has 3 functional Components:

1. Input Unit

2.Processor

3.Output Unit

You might also like