Académique Documents
Professionnel Documents
Culture Documents
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)
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.
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( );
};
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;}