0% found this document useful (0 votes)
169 views

Assignment 1

The document discusses stacks and queues as linear data structures. It defines a stack as a structure where insertion and deletion occurs at one end, following a LIFO structure. A queue is defined as a structure where insertion occurs at the rear and deletion at the front, following a FIFO structure. Examples of stack and queue implementations using arrays are provided to demonstrate abstract data type operations like push, pop, insert, delete.

Uploaded by

Sheikh Afreen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
169 views

Assignment 1

The document discusses stacks and queues as linear data structures. It defines a stack as a structure where insertion and deletion occurs at one end, following a LIFO structure. A queue is defined as a structure where insertion occurs at the rear and deletion at the front, following a FIFO structure. Examples of stack and queue implementations using arrays are provided to demonstrate abstract data type operations like push, pop, insert, delete.

Uploaded by

Sheikh Afreen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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.

Stack as Abstract Data Type


He stacks of elements of any particular type is a finite sequence of elements of that type
together with the following operations:
 Initialize the stack to be empty
 Determine whether the stack is empty or not
 Check whether the stack is full or not
 If the stack is not full, add or insert a new node at the top of the stack. This
operation is termed as Push Operation
 If the stack is not empty, then retrieve the node at its top
 If the stack is not empty, the delete the node at its top. This operation is called as
Pop operation
Example:
#include <iostream>
#include<stdlib.h>
using namespace std;

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.

Queue as an ADT (Abstract Data Type)


The meaning of an abstract data type clearly says that for a data structure to be
abstract, it should have the below-mentioned characteristics:
 First, there should be a particular way in which components are related to each
other
 Second, a statement for the operation that can be performed on elements of
abstract data type must have to be specified
Thus for defining a Queue as an abstract data type, these are the following criteria:
 Initialize a queue to be empty
 Check whether a queue is empty or not
 Check whether a queue is full or not
 Insert a new element after the last element in a queue, if the queue is not full
 Retrieve the first element of the queue, if it is not empty
 Delete the first element in a queue, if it is not empty
Example:
#include <iostream>
#include<stdlib.h>
using namespace std;

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:

You might also like