Вы находитесь на странице: 1из 12

Question no 1: delete_();

#include <iostream> break;

using namespace std; case 3:

#define MAX 50 search();

int queue_array[MAX]; break;

int rear = - 1; case 4:

int front = - 1; display();

void insert(); break;

void delete_(); case 5:

void search(); exit(1);

void display(); default:

void exit(); cout<<"Wrong choice \n";

main(){ } /*End of switch*/

int choice; } /*End of while*/

while (1){ } /*End of main()*/

cout<<"1.Insert element to queue \n"; void insert(){

cout<<"2.Delete element from queue \n"; int add_item;

cout<<"3.Display all elements of queue \n"; if (rear == MAX - 1)

cout<<"4.Quit \n"; cout<<"Queue Overflow \n"; else{

cout<<"Enter your choice :"; if (front == - 1)

cin>>choice; /*If queue is initially empty */

switch (choice) { front = 0;

case 1: cout<<"Inset the element in queue : ";

insert(); cin>>add_item;

break;case 2: rear = rear + 1;


queue_array[rear] = add_item;}

} /*End of insert()*/

void delete_(){

if (front == - 1 || front > rear){

cout<<"Queue Underflow \n";

return ;}else{

cout<<"Element deleted from queue is :\n",


queue_array[front];

front = front + 1;}

} /*End of delete() */

void display()

{int i;

if (front == - 1)

cout<<"Queue is empty \n";

else {

cout<<"Queue is : \n";

for (i = front; i <= rear; i++)

cout<< queue_array[i];

cout<<"\n";}

} /*End of display() */
Question no 2: start = NULL;

#include<iostream> while (1){

using namespace std; cout<<endl<<"------------------------"<<endl;

struct node{ cout<<endl<<" singly linked list"<<endl;

int data; cout<<endl<<"-----------------------"<<endl;

node *next; cout<<"1.create Node"<<endl;

}*start; cout<<"2.display node"<<endl;

class all_in_one { cout<<"3.add node at begin"<<endl;

public: cout<<"4.add end node"<<endl;

node* create_node() cout<<"5.add any position"<<endl;

void display(); cout<<"6.delete begin"<<endl;

void add_begin(); cout<<"7.delete end"<<endl;

void add_end(); cout<<"8.delete any position"<<endl;

void add_pos(); cout<<"9.treverse max and mini "<<endl;

void delete_begin(); cout<<"10.total vale"<<endl;

void delete_end(); cout<<"11.sum "<<endl;

void delete_anypos(); cout<<"12.average"<<endl;

void teriversal(); cout<<"Enter your choice : ";

void sum(); cin>>ch;

void average(); switch(ch){

all_in_one() { case 1:

start = NULL;}}; cout<<" *****ceate a node:**** "<<endl;

main(){ obj.create_node();

int ch,n, ele, pos,i; cout<<endl;

all_in_one obj; break;


case 2: cout<<"****Delete end:**** "<<endl;

cout<<"****Display:****: "<<endl; obj.delete_end();

obj.display(); cout<<endl; break;

cout<<endl; case 8:

break; cout<<"****Delete any


position****"<<endl;
case 3:
obj.delete_anypos();
cout<<"****add begin:****:"<<endl;
cout<<endl;
obj.add_begin();
break;
cout<<endl;
case 9:
break;
cout<<"****treverse max and
case 4:
mini:****"<<endl;
cout<<" ****add end:****: "<<endl; obj.teriversal();
obj.add_end(); cout<<endl;
cout<<endl;
break;
break; case 10:
case 5: cout<<"****sum:****"<<endl;
cout<<"****add position:****: "<<endl;
obj.sum();
obj.add_pos(); break;
break; case 11:
case 6:

cout<<"****delete begin:****"<<endl; cout<<"****average:****"<<endl;

obj.delete_begin(); obj.average();

cout<<endl; break;

break; default:

case 7: cout<<"Wrong choice"<<endl;}}}

void create_node(int value){


node *temp=new node; else{

temp->data=value; p = start;

temp->next=NULL; start = temp;

if(start==NULL){ start->next = p;}

start=temp; cout<<"ElementInserted at beginning"<<endl;

start=temp; }void add_end(){

temp=NULL;} int value;

else{ cout<<"Enter value: ";

start->next=temp; cin>>value;

start=temp;}} struct node *temp, *s;

void display(){ create_node(value);

node *temp=new node; s = start;

temp=start; while (s->next != NULL){

while(temp!=NULL){ s = s->next;}

cout<<temp->data<<"\t"; temp->next = NULL;

temp=temp->next;}} s->next = temp;

int add_begin(){ cout<<"Element Inserted at end"<<endl; }

int value; void insert_position(int p, int n){

cout<<"Enter value: "; node *pre=new node;

cin>>value; node *cur=new node;

struct node *temp, *p; node *temp=new node;

create_node(value); cur=start;

if (start == NULL){ for(int i=1;i<p;i++){

start = temp; pre=cur;

start->next = NULL; } cur=cur->next;


} current=current->next;}

temp->data=n; previous->next=current->next;}

pre->next=temp; void terivarsal(){

temp->next=cur;} int v, p, i;

void delete_begin(){ if (start == NULL){

node *temp=new node; cout<<"empty"<<endl;

temp=start; return;}

start=start->next; cout<<"Enter the node postion to be updated:


";cin>>p;
delete temp;}
cout<<"new value: ";
void delete_end(){
cin>>v;
node *current=new node;
struct node *s, *ptr;
node *previous=new node;
s = start;
current=start;
if (p == 1){
while(current->next!=NULL){
start->data = v; }
previous=current;
else{
current=current->next;}
for (i = 0;i < p - 1;i++){
start=previous;
if (s == NULL){
previous->next=NULL;
cout<<"less than "<<p<<" elements";
delete current;}
return;}
void delete_anypos(int p){
s = s->next;}
node *current=new node;
s->data = v; }
node *previous=new node;
cout<<"Node Updated"<<endl}
current=start;
void sum(){
for(int i=1;i<p;i++){
int v, p = 0;
previous=current;
bool flag = false;
if (start == NULL){

cout<<"empty"<<endl; value = ptr->data;

return;} ptr->data = s->data;

cout<<"Enter the value: "; s->data = value;}}

cin>>v; ptr = ptr->next;}}

struct node *s; int count(node *temp){

s = start; if(temp == NULL)

while (s != NULL){ return(0);

p++; return(1 + count(temp->next));}

if (s->data == v){

flag = true;

cout<<"Element "<<v<<" is found at position


"<<p<<endl;}

s = s->next;}

if (!flag)

cout<<"Element "<<v<<" not found"<<endl; }

void average(){

struct node *ptr, *s;

int value;

if (start == NULL){

cout<<"empty"<<endl;

return;}

ptr = start;

while (ptr != NULL){

for (s = ptr->next;s !=NULL;s = s->next){

if (ptr->data > s->data){


Question no 3: cout<<"\n 8 - Destroy stack";

#include <iostream> create();

using namespace std; while (1){

struct node{ cout<<"\n Enter choice : ";

int info; cin>>ch;

struct node *ptr; switch (ch){

}*top,*top1,*temp; case 1:

int topelement(); cout<<"Enter data : ";

void push(int data); cin>>no;

void pop(); push(no);

void empty(); break;

void display(); case 2:

void destroy(); pop();

void stack_count(); break;

void create(); case 3:

int count = 0; if (top == NULL)

int main(){ cout<<"No elements in stack";

int no, ch, e; else{

cout<<"\n 1 - Push"; e = topelement();

cout<<"\n 2 - Pop"; cout<<"\n Top element : "<<endl;

cout<<"\n 3 - Top"; cin>>e;}

cout<<"\n 4 - Empty"; break;

cout<<"\n 5 - Exit"; case 4:

cout<<"\n 6 - Dipslay"; empty();

cout<<"\n 7 - Stack Count"; break;


case 5: top =(struct node *)(1*sizeof(struct node));

exit(0); top->ptr = NULL;

case 6: top->info = data;

display(); }

break; else

case 7: {

stack_count(); temp =(struct node *)(1*sizeof(struct


node));
break;
temp->ptr = top;
case 8:
temp->info = data;
destroy();
top = temp;
break;
}
default
count++;
cout<<" Wrong choice, Please enter correct
choice "; }

break;}}}

/* Create empty stack */ /* Display stack elements */

void create(){top = NULL;} void display()

/* Count stack elements */ {

void stack_count(){ top1 = top;

cout<<"\n No. of elements in stack :"<<count;

} if (top1 == NULL){

cout<<"Stack is empty";

/* Push data into stack */ return;}

void push(int data) while (top1 != NULL){

{ cout<< top1->info;

if (top == NULL){ top1 = top1->ptr;}}


/* Pop Operation on stack */

void pop() top1 = top;

{top1 = top;

if (top1 == NULL){ while (top1 != NULL)

cout<<"\n Error : Trying to pop from empty {


stack";return;}
top1 = top->ptr;
else
(top);
top1 = top1->ptr;
top = top1;
cout<<"\n Popped value : %d", top->info;
top1 = top1->ptr;
top;
}
top = top1;
(top1);
count--;}
top = NULL;
/* Return top element */

int topelement(){
cout<<"\n All stack elements destroyed";
return(top->info);}
count = 0;
/* Check if stack is empty or not */
}
void empty(){

if (top == NULL)

cout<<"\n Stack is empty";

else

cout<<"\n Stack is not empty with %d


elements"<<count;

/* Destroy entire stack */

void destroy(){
Question no 4: switch(ch){

#include<iostream> case 1:{

using namespace std; cout<<"Enter The Element Value\n";

cin>>no;

struct link insert_q(no);

{ cout<<"\n queue after insertion";

int info; display();

struct link *next; break;}

}*front,*rear; case 2:{

void insert_q(int no); no=delete_q();

int delete_q(); cout<<"\n The deleted element =";

void display(); cin>>no;

int main() cout<<"\n queue after deletion";

{ display();

int ch,no; break;}

rear=NULL; case 3:{

front=NULL; cout<<"\n Queue is as follow";

while(1){ display();

cout<<"\n Dynemic Queue Menu"; break;}

cout<<"\n 1->Insert "; case 4:{

cout<<"\n 2->Delete "; cout<<"Exit....!!!!";

cout<<"\n 3->Display"; exit(0);break;}

cout<<"\n 4->Exit "; default :

cout<<"\n enter your choice : "; cout<<"\n Wrong choice...!!!! " ;} } }

cin>>ch; void insert_q(int no)


t=t->next;}}

{struct link *new1;

new1->info=no;

new1->next=NULL;

if(rear==NULL||front==NULL){

front=new1;}else{

rear->next=new1;}

rear=new1;}

int delete_q(){

struct link *t;

int no;

if(front==NULL||rear==NULL){

cout<<"\n queue is Under Flow";

}else{ t=front;

no=t->info;

front=front->next;}

return(no);}

void display(){

struct link *t;

t=front;

if(front==NULL||rear==NULL){

cout<<"\nQueue is empty";

exit(0);}

while(t!=NULL){

cout<<"\n %d",t->info;

Вам также может понравиться