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

Stack

Stacks and queues are commonly used data structures. Stacks are useful for arithmetic expression evaluation, backtracking, and function call processing by using LIFO operations. Queues are useful for printer spooling, round robin scheduling, and customer service by using FIFO operations. Both can be implemented using arrays or linked lists, with arrays allowing faster access but linked lists more flexible memory usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Stack

Stacks and queues are commonly used data structures. Stacks are useful for arithmetic expression evaluation, backtracking, and function call processing by using LIFO operations. Queues are useful for printer spooling, round robin scheduling, and customer service by using FIFO operations. Both can be implemented using arrays or linked lists, with arrays allowing faster access but linked lists more flexible memory usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

YIASCM

UNIT 3

STACK AND QUEUE

Following is the various Applications of Stack in Data Structure:

o Evaluation of Arithmetic Expressions


o Backtracking
o Delimiter Checking
o Reverse a Data
o Processing Function Call

Evaluation of Arithmetic Expressions


A stack is a very effective data structure for evaluating arithmetic expressions in
programming languages. An arithmetic expression consists of operands and
operators.

In addition to operands and operators, the arithmetic expression may also


include parenthesis like "left parenthesis" and "right parenthesis".

Example: A + (B - C)

To evaluate the expressions, one needs to be aware of the standard precedence


rules for arithmetic expression. The precedence rules for the five basic
arithmetic operators are:

Operators Associativity Precedence

^ exponentiation Right to left Highest followed by *Multiplication


and /division

*Multiplication, Left to right Highest followed by + addition and -


/division subtraction

+ addition, - Left to right lowest


subtraction

Evaluation of Arithmetic Expression requires two steps:

Mrs. Deepthi Vikram (Department of BCA)


YIASCM

o First, convert the given expression into special notation.


o Evaluate the expression in this new notation.

Notations for Arithmetic Expression

There are three notations to represent an arithmetic expression:

o Infix Notation
o Prefix Notation
o Postfix Notation

Infix Notation

The infix notation is a convenient way of writing an expression in which each


operator is placed between the operands. Infix expressions can be parenthesized
or parenthesized depending upon the problem requirement.

Example: A + B, (C - D) etc.

All these expressions are in infix notation because the operator comes between
the operands.

Prefix Notation

The prefix notation places the operator before the operands. This notation was
introduced by the Polish mathematician and hence often referred to as polish
notation.

Example: + A B, -CD etc.

All these expressions are in prefix notation because the operator comes before
the operands.

Postfix Notation

The postfix notation places the operator after the operands. This notation is just
the reverse of Polish notation and also known as Reverse Polish notation.

Example: AB +, CD+, etc.

All these expressions are in postfix notation because the operator comes after
the operands.

Conversion of Arithmetic Expression into various Notations:


Mrs. Deepthi Vikram (Department of BCA)
YIASCM

Infix Notation Prefix Notation Postfix Notation

A*B *AB AB*

(A+B)/C /+ ABC AB+C/

(A*B) + (D-C) +*AB - DC AB*DC-+

Application of queue:
1. Implementation of a queue is also an important application of data
structure. Nowadays computer handles multiuser, multiprogramming
environment and time-sharing environment. In this environment a
system(computer) handles several jobs at a time, to handle these jobs the
concept of a queue is used.
2. To implement printer spooler so that jobs can be printed in the order of
their arrival.
3. Round robin scheduling technique is implemented using queue
4. All types of customer service(like railway reservation) centers are
designed using the concept of queues.

Array implementation of queue

In queue, insertion and deletion happen at the opposite ends, so


implementation is not as simple as stack.
To implement a queue using array, create an array arr of size n and take two
variables front and rear both of which will be initialized to 0 which means the
queue is currently empty. Element rear is the index upto which the elements
are stored in the array and front is the index of the first element of the array.
Now, some of the implementation of queue operations are as follows:
1. Enqueue: Addition of an element to the queue. Adding an element
will be performed after checking whether the queue is full or not.
If rear < n which indicates that the array is not full then store the
element at arr[rear] and increment rear by 1 but if rear == n then it
is said to be an Overflow condition as the array is full.

Mrs. Deepthi Vikram (Department of BCA)


YIASCM

2. Dequeue: Removal of an element from the queue. An element can


only be deleted when there is at least an element to delete i.e. rear >
0. Now, element at arr[front] can be deleted but all the remaining
elements have to shifted to the left by one position in order for the
dequeue operation to delete the second element from the left on
another dequeue operation.
3. Front: Get the front element from the queue i.e. arr[front] if queue
is not empty.
4. Display: Print all element of the queue. If the queue is non-empty,
traverse and print all the elements from index front to rear.

Linked List implementation of Queue


Due to the drawbacks discussed in the previous section of this tutorial, the array
implementation can not be used for the large scale applications where the
queues are implemented. One of the alternative of array implementation is
linked list implementation of queue.

The storage requirement of linked representation of a queue with n elements is


o(n) while the time requirement for operations is o(1).

In a linked queue, each node of the queue consists of two parts i.e. data part and
the link part. Each element of the queue points to its immediate next element in
the memory.

Mrs. Deepthi Vikram (Department of BCA)


YIASCM

In the linked queue, there are two pointers maintained in the memory i.e. front
pointer and rear pointer. The front pointer contains the address of the starting
element of the queue while the rear pointer contains the address of the last
element of the queue.

Insertion and deletions are performed at rear and front end respectively. If front
and rear both are NULL, it indicates that the queue is empty.

he linked representation of queue is shown in the following figure.

Operation on Linked Queue

There are two basic operations which can be implemented on the linked queues.
The operations are Insertion and Deletion.

Insert operation

The insert operation append the queue by adding an element to the end of the
queue. The new element will be the last element of the queue.

Firstly, allocate the memory for the new node ptr by using the following
statement.

1. Ptr = (struct node *) malloc (sizeof(struct node));

There can be the two scenario of inserting this new node ptr into the linked
queue.

In the first scenario, we insert element into an empty queue. In this case, the
condition front = NULL becomes true. Now, the new element will be added as
the only element of the queue and the next pointer of front and rear pointer both,
will point to NULL.

ptr -> data = item;

Mrs. Deepthi Vikram (Department of BCA)


YIASCM

if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}

In the second case, the queue contains more than one element. The condition
front = NULL becomes false. In this scenario, we need to update the end pointer
rear so that the next pointer of rear will point to the new node ptr. Since, this is a
linked queue, hence we also need to make the rear pointer point to the newly
added node ptr. We also need to make the next pointer of rear point to NULL.

rear -> next = ptr;


rear = ptr;
rear->next = NULL;

In this way, the element is inserted into the queue. The algorithm and the C
implementation is given as follows.

Algorithm

C Function
Void insert(struct node *ptr, int item; )
{

ptr = (struct node *) malloc (sizeof(struct node));


if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
ptr -> data = item;
if(front == NULL)
{

Mrs. Deepthi Vikram (Department of BCA)


YIASCM

front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
Deletion
Deletion operation removes the element that is first inserted among all the
queue elements. Firstly, we need to check either the list is empty or not. The
condition front == NULL becomes true if the list is empty, in this case , we
simply write underflow on the console and make exit.

Otherwise, we will delete the element that is pointed by the pointer front. For
this purpose, copy the node pointed by the front pointer into the pointer ptr.
Now, shift the front pointer, point to its next node and free the node pointed by
the node ptr. This is done by using the following statements.

ptr = front;
front = front -> next;
free(ptr);

The algorithm and C function is given as follows.

Algorithm
o Step1: IFFRONT=NULL
Writ"Underflow"
GotoStep5
[END OF IF]
o Step 2: SET PTR = FRONT
o Step 3: SET FRONT = FRONT -> NEXT
o Step 4: FREE PTR

Mrs. Deepthi Vikram (Department of BCA)


YIASCM

o Step 5: END

C Function
void delete (struct node *ptr)
{
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
Menu-Driven Program implementing all the operations on Linked Queue
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu****************
*************\n");

Mrs. Deepthi Vikram (Department of BCA)


YIASCM

printf("\
n======================================================
===========\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue
\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *) malloc (sizeof(struct node));


if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
Mrs. Deepthi Vikram (Department of BCA)
YIASCM

else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
Mrs. Deepthi Vikram (Department of BCA)
YIASCM

struct node *ptr;


ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}

Mrs. Deepthi Vikram (Department of BCA)

You might also like