Assignment 1
Assignment 1
AFREEN TAJ
5th Sem BCA
Topic: Stack & Queue
What Is Stack?
A stack is a linear data structure in which all the insertion and deletion of data or you
can say its values are done at one end only, rather than in the middle. Stacks can be
implemented by using arrays of type linear.
The stack is mostly used in converting and evaluating expressions in Polish notations,
i.e.:
Infix
Prefix
Postfix
In case of arrays and linked lists, these two allows programmers to insert and delete
elements from any place within the list, i.e., from the beginning or the end or even from
the middle also. But in computer programming and development, there may arise some
situations where insertion and deletion require only at one end wither at the beginning
or end of the list. The stack is a linear data structure, and all the insertion and deletion of
its values are done in the same end which is called the top of the stack. Let us suppose
take the real-life example of a stack of plates or a pile of books etc. As the item in this
form of data structure can be removed or added from the top only which means the last
item to be added to the stack is the first item to be removed. So you can say that the
stack follows the Last In First Out (LIFO) structure.
class stack {
int stk[5];
int top;
public:
stack()
{
top = -1;
}
void push(int x)
{
if (top >= 4) {
cout << "stack overflow";
return;
}
stk[++top] = x;
cout << "inserted " << x;
}
void pop()
{
if (top < 0) {
cout << "stack underflow";
return;
}
cout << "deleted " << stk[top--];
}
void display()
{
if (top < 0) {
cout << " stack empty"; return; } for (int i = top; i >= 0; i--)
cout << stk[i] << " ";
}
};
int main()
{
int ch;
stack st;
while (1) {
cout << "\n1.push 2.pop 3.display 4.exit\nEnter ur choice: "; cin >> ch;
switch (ch) {
case 1:
cout << "enter the element: "; cin >> ch;
st.push(ch);
break;
case 2:
st.pop();
break;
case 3:
st.display();
break;
case 4:
exit(0);
}
}
}
Output:
What Is Queue?
A queue is a linear list of elements in which deletion of an element can take place only
at one end called the front and insertion can take place on the other end which is
termed as the rear. The term front and rear are frequently used while describing queues
in a linked list. In this chapter, you will deal with the queue as arrays.
In the concept of a queue, the first element to be inserted in the queue will be the first
element to be deleted or removed from the list. So Queue is said to follow the FIFO
(First In First Out) structure. A real-life scenario in the form of example for queue will be
the queue of people waiting to accomplish a particular task where the first person in the
queue is the first person to be served first.
Other examples can also be noted within a computer system where the queue of tasks
arranged in the list to perform for the line printer, for accessing the disk storage, or even
in the time-sharing system for the use of CPU. So basically queue is used within a single
program where there are multiple programs kept in the queue or one task may create
other tasks which must have to be executed in turn by keeping them in the queue.
class queuearr {
int queue1[5];
int rear, front;
public:
queuearr()
{
rear = -1;
front = -1;
}
void insert(int x)
{
if (rear > 4) {
cout << "queue over flow";
front = rear = -1;
return;
}
queue1[++rear] = x;
cout << "inserted " << x;
}
void delet()
{
if (front == rear) {
cout << "queue under flow";
return;
}
cout << "deleted " << queue1[++front];
}
void display()
{
if (rear == front) {
cout << " queue empty";
return;
}
for (int i = front + 1; i <= rear; i++)
cout << queue1[i] << " ";
}
};
int main()
{
int ch;
queuearr qu;
while (1) {
cout << "\n1.insert 2.delet 3.display 4.exit\nEnter ur choice: "; cin >> ch;
switch (ch) {
case 1:
cout << "enter the element: "; cin >> ch;
qu.insert(ch);
break;
case 2:
qu.delet();
break;
case 3:
qu.display();
break;
case 4:
exit(0);
}
}
}
Output: