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

Linked Lists/ Stacks/ Queues

1) Consider the following program for linked QUEUE :


4
struct NODE
{ int x;
float y;
NODE *next; };
class QUEUE
{ NODE *R,*F;
public :
QUEUE( )
{ R=NULL;
F=NULL;
}
void INSERT( );
void DELETE( );
void Show( );
~QUEUE( ); };
Define INSERT( ) & DELETE( ) functions outside the class.
SOLUTION:#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>

struct NODE
{ int x;
float y;
NODE *next; };

class QUEUE
{ NODE *R,*F;
public :
QUEUE( )
{ R=NULL;
F=NULL;
}
void INSERT( );

void DELETE( );
void Show( );
~QUEUE( ); };

void QUEUE::INSERT()
{ NODE *PTR;
PTR=new NODE;
PTR->next=NULL;
cout<<" ENTER X AND Y";
cin>>PTR->x;
cin>>PTR->y;
if(R==NULL)
F=R=PTR;
else
{ R->next=PTR;
R=PTR;
}
}
void QUEUE::DELETE()
{ NODE* NPTR;
if(F==NULL)
{ cout<<"underflow";
}
else
{ NPTR=F;
if(F==R)
F=R=NULL;
else
F=F->next;
delete NPTR;
}
}
void QUEUE::Show()
{ NODE*n=F;

while(n!=NULL)

{ cout<<n->x;
cout<<"\n"<<n->y;
n=n->next;}}

(2)
WAP using function to insert and delete a set of integer values in a
circular queue and display them.
SOLUTION:#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class Queue
{
int s[100];
int rear,front,n;
public:
Queue()
{
cout<<"\n\tEnter the Queue Size : ";
cin>>n;
rear=front=-1;
}
void insert(int elt)
{
if(front==rear+1||(front==0&&rear==n-1))

{ cout<<"overflow";
exit(0);}
else if(rear==-1)
{
rear=front=0;}
else if(rear=n-1)
rear=0;
else rear++;
s[rear]=elt;
cout<<s[rear];
}

void remove()
{
if(front==-1) {
cout<<"\n\tQueue is empty.\n";
exit(1);

}
else
{ if(front==rear)
front=rear=-1;
else if(front==n-1)
front=0;
else front++;

}
}
void display();
};

void Queue::display()
{ if(front==-1)
cout<<" empty";
else
{ for(int i=front;i<rear;i++)

cout<<s[i];}}

void main(){
Queue q;
int choice;
char elt;
cout<<"\n\n\t1.Insert\t2.Remove\tAny Key To Exit\n\tChoice : ";
cin>>choice;
switch(choice)
{
case 1 :
cout<<"\n\tEnter the Element to insert : ";
cin>>elt;
q.insert(elt);

break;
case 2 :
q.remove();
q.display();
break;
default: exit(0);

}
q.display();
}

3) WAP using function to perform PUSH operation in a dynamically allocated stack containing name
and registration number of students. Also declare the relevant structure/class and pointers.
SOLUTION:#include<iostream.h>
struct node{
char name[20];
int rno;
node*link;
}*top,*ptr;
void push()
{ ptr=new node;
cout<<" enter name reg no;";
cin>>ptr->rno>>ptr->name;
ptr->link=NULL;
if(!top)
top=ptr;
else{ ptr->link=top;
top=ptr;}}

4) WAP using function Quedel( ) in C++ to display and delete an element , from a dynamically
allocated Queue containing nodes of the following structure :
struct student
{
int rollno;
char name[20];
student *link;
};
#include<iostream.h>
#include<process.h>
#include<string.h>
struct student
{
int rollno;
char name[20];
student *link;
}*fr,*rr,*save,*ptr,*nptr;
student*crnew(int,char[]);
void in(student*);
void display(student*);
void delnode_q();
void main()
{ fr=rr=NULL;
int rollno;
char ch='y',name[20];
while(ch=='y'||ch=='Y')
{ cout<<"enter information for new node";
cin>>rollno;
cin>>name;
nptr=crnew(rollno,name);
if(nptr==NULL)
{ cout<<"cannot create new node";
exit(10);}
in(nptr);
cout<<" \n press y to enter more nodes ";
cin>>ch;
}
do{ cout<<"the linked queue is";
display(fr);
cout<<"want to delete first node";
cin>>ch;
if(ch=='y'||ch=='Y')
delnode_q();
}while(ch=='y'||ch=='Y');
}
student*crnew(int n,char a[20])
{ ptr=new student;
ptr->rollno=n;
strcpy(ptr->name,a);

ptr->link=NULL;
return ptr;}
void in(student*np)
{ if(fr==NULL)
fr=rr=np;
else
{ rr->link=np;
rr=np;
}}
void delnode_q()
{ if(fr==NULL)
cout<<"underflow";
else{ ptr=fr;
fr=fr->link;
delete ptr;}}
void display(student*np)
{ while(np!=NULL)
{ cout<<np->rollno<<np->name;
np=np->link;
}
cout<<"\n";}

5) Consider the following portion of a program, which implements a linked stack for Library .
WAP using functions PUSH() & POP(),to insert & delete a node in the stack with required
information
struct Library
{
int id;
char names[20];
};
class stack
{
Library *top;
public :

stack()
{
top=NULL;
}
void PUSH();
void POP();
};
#include<iostream.h>
#include<process.h>
#include<string.h>
#include<stdio.h>
struct Library
{
int id;
char names[20];
Library *link;
};
class stack
{
Library *top;
public :
stack()
{
top=NULL;
}
void PUSH();
void POP();
};
void stack::PUSH()
{ Library*nptr;
nptr=new Library;
nptr->link=NULL;
cout<<"enter name ";
gets(nptr->names);
cout<<"enter id";
cin>>nptr->id;
if(top==NULL)
top=nptr;
else
{ nptr->link=top;
top=nptr;
}
}
void stack::POP(){
if(top==NULL)
cout<<"underflow";
else
{ cout<<"element beieng poopped";
cout<<top->names<<top->id<<endl;
Library*ptr;

ptr=top;
top=top->link;
delete ptr;}}

6) Give the necessary declarations for a queue containing float type numbers; write a
user defined function in C++ to insert a float type number in the queue. Use linked
representation of queue.

#include<iostream.h>
#include<stdio.h>
#include<string.h>
struct node{
float x;
node*link;}
class queue{
node*rear,*front;
public:
queue()
{ rear=NULL;
front=NULL;}
void in();
}
void queue::in()
{ node*nptr;
nptr=new node;
nptr->link=NULL;
cout<<"enter information";
cin>>nptr->x;
if(rear==NULL){ front=rear=nptr;}
else
{ rear->link=nptr;
rear=nptr;}
}

7)
Consider the following portion of a program , which implements names queue for Books .
Write the definition of function Insert(), to insert a new node in the queue with required
information .
4
struct Book
{
char names[4][20];
};
class QueueofBooks
{
Book Q[10];
public :
int front ,rear;
QueueofBooks()
{
front=rear=-1;
}
void Insert();
void Delete();

};
void QueueofBooks::Insert()
{ node*nptr;
nptr=new node;
nptr->link=NULL;
cout<<"enter information";
for(int i=0;i<4;i++)
{ for(int j=0;j<80;j++)
cin>>nptr->names[i][j];
}
if(rear==NULL){ front=rear=nptr;}
else
{ rear->link=nptr;
rear=nptr;}

7) WAP using a function in C++ to perform Insert operation in dynamically allocated Queue
containing names of students.
#include<stdio.h>
#include<string.h>
struct node{
char name[2][20];
node*link;}

class queue
{
node *rear, *front;
public:
queue()
{ rear=NULL;
front=NULL;}
void in();
}
void queue::in()
{ node*nptr;
nptr=new node;
nptr->link=NULL;
cout<<"enter information";

for(int i=0;i<4;i++)
{ for(int j=0;j<80;j++)
cin>>nptr->names[i][j];
}
if(rear==NULL){ front=rear=nptr;}
else
{ rear->link=nptr;
rear=nptr;}

9)

Write a function in C++ which accepts a character array and its size as arguments and reverse that
array without using second array and library function.
Example : if the array is having: Computer Science
Then after reversal it should rearranged as: ecneicS retupmoC

#include<stdio.h>
#include<string.h>
char rev(char s[50],int ,int );
void main()
{
int i,l;
char a[50],b[50];
cout<<"Enter the string\n";
gets(a);
l=strlen(a);
rev(a,0,l-1);
cout<<"Reverse string is:";
puts(a);
}
char rev(char *s,int beg,int end)
{
char p;
if(beg>=end)
{
return 0;
}
else
{

p=*(s+beg);
*(s+beg)=*(s+end);
*(s+end)=p;
return rev(s,++beg,--end);
}
}

10) Write a function in C++ to perform insert operation on a dynamically allocated Queue.
4
struct Node
{
Int Code;
char Description[10];
Node * link;
}
void Insert()
{ node*nptr;
nptr=new node;
nptr->link=NULL;
cout<<"enter information";
cin>>Code;
cin>>Description;
if(rear==NULL){ front=rear=nptr;}
else
{ rear->link=nptr;
rear=nptr;}

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