Vous êtes sur la page 1sur 8

DELHI PUBLIC SCHOOL

BULANDSHAHR UP
ASSIGNMNET
SHEKHAR TRIPATHI
PGT Comp Sc
E Mail : etwshekhar@gmail.com
CLASS : XII
TOPIC : STACK AND QUEUE
BOARD Exam Marks - 6

Q1. Evaluate the following postfix expression using a stack and show the Contents of stack after
execution of each operation:
(i) 50,40,+, 18,14,-,4,*,+
(ii) 100,40,8,+,20,10,-,+,*
(iii) 5,6,9,+,80,5,*,-,/
(iv) 120,45,20,+,25,15,-,+,*
(v) 20,45,+,20,10,-,15,+,*
(vi) TRUE,FALSE, TRUE FALSE, NOT, OR, TRUE , OR,OR,AND
Q2. Give postfix form of the following expression:
(i) A*(B+(C+D)*(E+F)/G)*H
(ii) A+[(B+C)*(D+E)*F]/G
(iii) A*(B+D)/E-F-(G+H/K)
(iv) ((A-B)*(D/E))/(F*G*H)
(v) (True && false) || !(false||true)

Q3. class stack


{ int data[10];
int top;
public:
Stack( ) { top=-1;}
void push ( ); // to push an element into the stack
void pop ( ) ; // to pop an element into the stack
void display( );// to display all the elements from the stack
};
complete the class with all function definition.
Q4. Write a function in C++ to perform insert operation in dynamically allocated Queue containing names
of students.
Q5. Write a function in C++ to perform push operation in a dynamically allocated stack containing
admission number of students. Also declare the relevant class/ structure and pointers.
Q6. Write a function in C++ to perform a DELETE operation in a dynamically allocated queue considering
the following description:
Struct Node
{ float U,V;
Node *Link;
};
class QUEUE
{ Node *Raer, *Front;
Public:
QUEUE( ) { Rear =NULL; Front= NULL;}
Void INSERT ( );
Void DELETE ( );
~QUEUE ( );
}
Q7. 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( );
};
Q8. Define function stackpush( ) to insert nodes and stackpop( ) to delete nodes, for a linklist
implemented stack having the following structure for each node:
Struct Node
{ char name[20];
int age;
Node *Link;
};
class STACK
{ Node * Top;
Public:
STACK( ) { TOP=NULL;}
Void stackpush( );
Void stackpop( );
~STACK( );
};
Q9. Consider the following portion of a program which implements passengers Queue for a bus.
Write the definition of function Insert , to insert a new node in the queue with required
information.
Struct Node
{ float U,V;
Node *Link;
};
class QUEUE
{ Node *Raer, *Front;
Public:
QUEUE( ) { Rear =NULL; Front= NULL;}
Void INSERT ( );
Void DELETE ( );
~QUEUE ( );
};
Q10. Give the necessary declaration of a linked list implemented queue containing float type values .
Also write a user defined functions in C++ to add and delete a float type number in the queue.
Q11. Define functions quesins() to insert nodes and quedel() to delete nodes, for a linked list implemented
class queue, where each node has following structure:
struct node
{
char name[20];
int age;
node *link;
};
class queue
{
node *rear,*front;
public:
queue()
{
rear=NULL;
front=NULL;
}
void queins();
void quedel();
};
Q12.Define functions stackpush() to insert nodes and stackpop() to delete nodes, for a linked list implemented
stack having the following structure for each node:

struct node
{
Char name[20];
int age;
node *link;
};
Class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void stackpush();
void stackpop();
};

Q13. Write a program to insert and delete a element from the queue (implemented with array) as per the user
choice.
Q14. Write a program to Push and Pop a element from the Stack (implemented with array) as per the user
choice.
Q15. Define functions stackpush( ) to insert nodes and stackpop( ) to delete nodes, for a
linked list implemented stack having the following structure for each node:
struct node
{
char name[20];
int age;
node *LINK;
};
class stack
{
node *top;
public:
stack( ) { top = NULL; };
void stackpush( );
void stackpop( );
};
Q16. Write a function in C++ to perform Insert and Delete operation in a dynamically
allocated Queue Containing names of the student.

Q19.Evaluate the following Postfix expression showing the stack contents.


16 , 2 , 6 , + , / , 2 , * , 1 , –
Q20.Write a C++ function which will insert a node in a dynamically allocated
stack defined as:
struct stack {
int u,v;
stack *next; };

Q21.Give the necessary declarations and define a function to push an element into the linked stack.
Assume the stack contain floating numbers.

Q22.Evaluate the following postfix expression using a stack and show the contents of the stack after
the execution of each operation.
TRUE, FALSE, TRUE, FALSE, NOT, OR, TRUE, OR, OR, AND
Q23. Consider the following program for linked QUEUE:
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.

Q25.Evaluate the following postfix expression using stack and show the contents after
execution of each operations
470,5,4,^,25,/,6,*,+,81,-

Q27 Evaluate the following postfix expression using stack and show the contents after
execution of each operations :
470,5,4,^,25,/,6,*,+,81,-

Q28 Evaluate the following postfix expression using a stack and show the Contents of stack after
execution of each operation:
TRUE,FALSE, TRUE,FALSE, NOT, OR, TRUE , OR,OR,AND

Q29. Define function stackpush( ) to insert nodes and stackpop( ) to delete nodes, for a linklist
implemented stack having the following structure for each node:(4)
Struct Node
{ char name[20];
int age;
Node *Link;
};
class STACK
{ Node * Top;
Public:
STACK( ) { TOP=NULL;}
void stackpush( );
void stackpop( );
~STACK( );
};

STACK AND QUEUE QUES WITH ANSWERS 4 MARKS

AS AN LINKED LIST
STACK
Q-5 Each node of a STACK containing the following information, in addition to required pointer field:
Roll no. of the student
Age of the student.
Gve the structure of node for the linked stack in question.
TOP is a pointer to the topmost node of the STACK. Write the following function:
PUSH() – TO push a node in to the stack which is allocated dynamically.
POP() – Te remove a node from the stack and to release the memory.
Ans- struct STACK
{
int rollno, age;
STACK*next;
} *top, *nptr, *ptr;

Void push()
{
nptr = new stack; //allocate memory
cout << “\n Enter roll number and age to be inserted : “ ;
cin >> nptr-> rollno >> nptr->age ;
nptr -> next = NULL;
if (!top)
top = nptr;
else
{
nptr -> next = top;
top = nptr
}
}
void pop()
{
if (!pop)
{
cout << ”\nUnderflow!!” ; exit(1);
}
else
{
ptr = top;
top = top -> next;
delete ptr;
}
}
Q. Define functions stackpush() to insert nodes and stackpop() to delete nodes, for a linked list
implemented stack having the following structure for each node:

struct node
{
Char name[20];
int age;
node *link;
};
Class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void stackpush();
void stackpop();
};

Ans:
void stack::stackpush()
{
node *nptr,*temp;
nptr = new node;
nptr->link=NULLl;
cout<<”enter the name for new node”;
gets(nptr ->name );
cout<<”enter the age for new node”;
cin>>nptr ->age ;
if(top==NULL)
top=nptr;
else
{
temp=top;
top=nptr;
nptr->link=temp;
}
}
void stack::stackpop()
{
Node *ptr;
if(top==NULL)
cout<<”underflow”;
else
{
ptr=top;
top=top->link;
delete ptr;
}
}
QUEUE
Q. Define functions quesins() to insert nodes and quedel() to delete nodes, for a linked list implemented
class queue, where each node has following structure:
struct node
{
char name[20];
int age;
node *link;
};
class queue
{
node *rear,*front;
public:
queue()
{
rear=NULL;
front=NULL;
}
void queins();
void quedel();
};
Ans: void queue::queins()
{
node *nptr;
nptr =new node;
nptr->link=NULL;
cout<<”enter the name for new node”;
gets(nptr ->name );
cout<<”enter the age for new node”;
cin>>nptr ->age ;
if(rear = =NULL)
{
front=rear=nptr;
}
else
{
rear->link=nptr;
rear=nptr;
}
}
void queue::quedel()
{
node *ptr;
if(front==NULL)
{
cout<<”underflow”;
}
else
{
ptr=front;
if(front ==rear)
front=rear=NULL;
else
front=front->link;
delete ptr;
}}
AS AN ARRAY
Q. Write a program to Push and Pop a element from the Stack (implemented with array) as per the user
choice.
Ans: STACK
Function to push an element in stack:
Void push(char ch)
{
if ( top != size-1)
{
top++;
Stack[top]=ch;
}
}
Function to pop an element in stack:
void pop()
{
char ch;
if(top = -1)
{
ch=Stack[top];
top--;
}
}
QUEUE
Q. Write a program to insert and delete a element from the queue (implemented with array) as per the
user choice.
Ans:
Function to INSERT an element in QUEUE:
int insert (int Queue[],int ele)
{
if(rear = = size -1)
return -1;
else if(rear = = -1)
{
front =rear =0 ;
Queue[rear]=ele;
}
else
{
rear++;
Queue[rear]=ele;
}
return 0;
}
Function to DELETE an element in QUEUE:
int delete(int Queue[])
{
int ret;
if(front = =-1)
return -1;
else
{
ret = Queue[front];
if(front == rear)
front = rear =-1;
else
front++;
}
return ret;}

Vous aimerez peut-être aussi