[go: up one dir, main page]

0% found this document useful (0 votes)
36 views8 pages

II PUC C++ Progs (13-16)

The document contains C++ programs to perform operations on stacks, queues and linked lists. It includes programs to push and pop elements from stacks, enqueue and dequeue elements from queues, and add and print nodes of a linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views8 pages

II PUC C++ Progs (13-16)

The document contains C++ programs to perform operations on stacks, queues and linked lists. It includes programs to push and pop elements from stacks, enqueue and dequeue elements from queues, and add and print nodes of a linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C++ program-13

Write a C++ program to perform push items into the stack.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define n 3
class stack
{
int a[n],top,i;
public :
stack()
{
top=-1;
}
void push(int ele)
{
if(top==n-1)
{
cout<<"stack is full\n";
display();
getch();
exit(0);
}
a[++top]=ele;
}
void display()
{
if(top==-1)
cout<<"stack is empty \n";
else
cout<<"stack contents are \n";
for(i=top;i>=0;i--)
cout<<a[i]<<"\t";
}
};

void main()
{
char ch;
int ele;
stack s;
clrscr();
cout<<"enter your choice to push an elements(y/n)\n";
cin>>ch;

while(ch=='y')
{
cout<<"enter an element to push \n";
cin>>ele;
s.push(ele);
cout<<"enter your choice to push another element(y/n)\n";
cin>>ch;
}
s.display();
getch();
}
C++ Program-14

Write a C++ program to pop elements from the stack.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define n 3
class stack
{
int a[n],i,top;
public :
stack()
{
top=-1;
}
void push(int ele)
{
if(top==n-1)
{
cout<<"stack is full";
display();
}
else
a[++top]=ele;
}
void pop()
{
if(top==-1)
cout<<"stack is empty";
else
{
cout<<a[top]<<" is popped";
top--;
}
}
void display()
{
if(top==-1)
cout<<"stack is empty";
else
{
cout<<"stack contents are \n";
for(i=top; i>=0 ;i--)
cout<<a[i]<<"\t";
}
}
};
void main()
{
stack s;
int ele,ch;
clrscr();
while(1)
{
cout<<"\n enter the choice 1.Push\t2.Pop\t3.Display\t4.Exit\n";
cin>>ch;
switch(ch)
{
case 1: cout<<"enter the element to insert:\n";
cin>>ele;
s.push(ele);
break;
case 2: s.pop();
break;
case 3: s.display();
break;
default: cout<<"End of stack operation";
getch();
exit(0);
}
}
}
C++ Program-15

Write a C++ program to perform enqueue and dequeue.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define n 3
class que
{
int a[n],i,r,f;
public:
que()
{
f=0;
r=-1;
}
void insert(int ele)
{
if(r==n-1)
{
cout<<"queue is full";
display();
}
else
a[++r]=ele;
}
void del()
{
if(f>r)
cout<<"queue is empty";
else
{
cout<<a[f]<<" is deleted";
f++;
}
}
void display( )
{
if(f>r)
cout<<"queue is empty";
else
{
cout<<"queue contents are \n";
for(i=f;i<=r; i++)
cout<<a[i]<<"\t";
}
}
};
void main()
{
que q;
int ele,ch;
clrscr();
while(1)
{
cout<<"\n enter the choice:1.Insert\t 2.Delete\t 3.Display\t
4. Exit\n";
cin>>ch;
switch(ch)
{
case 1:cout<<"enter the element to insert:\n";
cin>>ele;
q.insert(ele);
break;
case 2:q.del( );
break;
case 3:q.display( );
break;
default:cout<<"end of queue operation";
getch( );
exit(0);
}
}
}
C++ Program 16

Write a c++ program to create a linked list and appending


nodes.

#include<iostream.h>
#include<conio.h>
class list
{
struct node
{
int data;
node *link;
}*start;
public:
list()
{
start=NULL;
}
void add(int);
void print();
};

void list::add(int ele)


{
node *ptr=new node;
ptr->data=ele;
ptr->link=NULL;
if(start==NULL)
start=ptr;
else
{
node *temp=start;
while(temp->link!= NULL)
{
temp=temp->link;
}
temp->link=ptr;
}
}
void list::print()
{
node *temp=start;
if(start==NULL)
cout<<"empty linked list\n";
else
{
cout<<"linked list contents are\n";
while(temp != NULL)
{
cout<<temp -> data<<"\t";
temp=temp -> link;
}
}
}

void main()
{
list *obj=new list;
char ch;
int ele;
clrscr();
cout<<"enter your choice to push an element(y/n)\n";
cin>>ch;
while(ch=='y')
{
cout<<"enter an element to push\n";
cin>>ele;
obj->add(ele);
cout<<"enter your choice push another element(y/n) \n";
cin>>ch;
}
obj->print();
getch();
}

You might also like