CBSE Computer Science - Revision Tour(Solved)

CBSE Guess > eBooks > Class XII > CBSE Computer Science Link Lists Stacks And Queues Solved Revision Tour By Mr. Ravi Kiran

COMPUTER SCIENCE LINKED LISTS , STACKS AND QUEUES

Previous Index Next

3.d) Write a function in C++ to perform Insert operation in dynamically allocated Queue containing names of students.

struct NODE
{ char Name[20];
NODE *Link;
};

Solution:

class Queue
{
NODE *front,*rear;
public:
Queue( )
{ front = rear = NULL; }
void Insert( );
void Delete( );
void Display( );
};
void Queue::Insert( )
{
NODE *ptr;
ptr=new NODE;
if(ptr= = NULL)
{
cout<<”\nNo memory to create a new node….”;
exit(1);
}
cout<<”\nEnter the name….”;
gets(ptr → Name);
ptr→ Link=NULL;
if(rear= = NULL)
front=rear=ptr;
else
{
rear→Link=ptr;
rear=ptr;
}
}

3.e) Write the equivalent infix expression for 10, 3, *, 7, 1, --,*, 23, +

Solution: 10, 3, *, 7, 1, - , *, 23, + This is in Postfix form( ie Operator will come after the operand(s));. Infix form means Operator must come in between the operands. 10, 3, *, 7, 1, - , *, 23, +.

Prefix: 10 * 3, 7 – 1,*,23,+
(10 * 3) * (7 – 1),23,+
(10 * 3) * (7 – 1) + 23
(Infix form)

DELHI : 2005

3.c) Write a function in C++ to perform a PUSH operation in a dynamically allocated stack considering the following :

struct Node
{
int X,Y ;
Node *Link ;
} ;
class STACK
{
Node *Top ;
public :
STACK( ) {Top = Null ;}
void PUSH( ) ;
void POP( ) ;
~STACK( ) ;
} ;

Solution:

struct Node
{
int X,Y ;
Node *Link ;
} ;
class STACK
{
Node *Top ;
public :
STACK( ) {Top = NULL ;}
void PUSH( ) ;
void POP( ) ;
~STACK( ) ;
} ;
void STACK::PUSH( )
{
Node *Temp;
Temp=new Node;
if(Temp= =NULL)
{
cout<<”\nNo memory to create the node…”;
exit(1);
}
cout<<”Enter the value of X and Y”;
cin>>Temp → X>>Temp → Y;
Temp → Link=Top;
Top=Temp;}
}

 

Previous Index Next

CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )