0% found this document useful (0 votes)
25 views11 pages

Stack and Queue: Programming & Data Structures

This document discusses stacks and queues as data structures. It defines a stack as a first-in, last-out (LIFO) data structure and provides code to implement push and pop operations on a stack. It also defines a queue as a first-in, first-out (FIFO) data structure and provides circular queue implementation code for addq and deleteq operations. As an example application, it describes how a stack can be used to check for matching parentheses in an expression.

Uploaded by

Paras Dorle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views11 pages

Stack and Queue: Programming & Data Structures

This document discusses stacks and queues as data structures. It defines a stack as a first-in, last-out (LIFO) data structure and provides code to implement push and pop operations on a stack. It also defines a queue as a first-in, first-out (FIFO) data structure and provides circular queue implementation code for addq and deleteq operations. As an example application, it describes how a stack can be used to check for matching parentheses in an expression.

Uploaded by

Paras Dorle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Stack and Queue

CS10001: Programming & Data Structures

Pallab Dasgupta
Professor, Dept. of Computer
Sc. & Engg.,
Indian Institute of
Technology Kharagpur
Dept. of CSE, IIT KGP

Stack
In

Dept. of CSE, IIT KGP

Out

Stack: Definition
#define MAX_STACK_SIZE 100
typedef struct {
int key;
/* other fields */
} element;
typedef struct {
element list[MAX_STACK_SIZE];
int top;
} stack;
stack z;
z.top = -1;

/* Declaration */
/* Initialization */

Dept. of CSE, IIT KGP

Stack: Operations
void push( stack *s, element item )
{
if (s-> top >= MAX_STACK_SIZE -1) { stack_full( ); return; }
(s->top)++;
s->list[s->top] = item;
}
element pop( stack *s )
{
element item;
if (s->top = -1) return stack_empty( );
item = s->list[s->top];
(s->top)--; return item;
}

Dept. of CSE, IIT KGP

Application: Parenthesis Matching

Given a parenthesized expression, test whether the expression is properly


parenthesized.

Examples:

()({}[({}{}())])

is proper

(){[]

is not proper

({)}

is not proper

)([]

is not proper

([]))

is not proper

Approach:

Whenever a left parenthesis is encountered, it is pushed in the stack.

Whenever a right parenthesis is encountered, pop from stack and check if the
parentheses match.

Works for multiple types of parentheses ( ), { }, [ ]

Dept. of CSE, IIT KGP

Parenthesis matching
while (not end of string) do
{
a = get_next_token();
if (a is ( or { or [) push (a);
if (a is ) or } or ])
{
if (is_stack_empty( )) { print (Not well formed); exit(); }
x = pop();
if (a and x do not match) { print (Not well formed); exit(); }
}
}
if (not is_stack_empty( )) print (Not well formed);

Dept. of CSE, IIT KGP

Queue

Out

In

Dept. of CSE, IIT KGP

Queue: Definition
#define MAX_QUEUE_SIZE 100
typedef struct {
int key;
/* other fields */
} element;
typedef struct {
element list[MAX_QUEUE_SIZE];
int front;
int rear;
} queue;
queue z;

/*Declaration */

z.front = z.rear = 0; /* Initialization */

Dept. of CSE, IIT KGP

Queue: Circular Implementation


[3]

[4]
[5]

[2]
[1]
[0]
front=0
rear=0

[3]
[2] B
[1]

[6]

[6]

front=0[0]

[7]

After insertion
of A, B, C, D

[7]

Queue Empty

front=2
[2]

[3]
C

[4] rear = 4
D
[5]

[1]

[6]
[0]

Dept. of CSE, IIT KGP

[4] rear = 4
D
[5]

[7]

After deletion of
of A, B

Queue: Circular Implementation


[3]

[4]

[2]

[5]

[1]

[6]

[0]
front=0
rear=0

[7]
Queue Empty

rear = 3
[3]

front=4
[4]

front: index of queue-head (always empty why?)


[2]
rear: index of last element, unless rear = front

[5]

[1]

[6]

[0]
Queue Empty Condition: front == rear
Queue Full Condition: front == (rear + 1) % MAX_QUEUE_SIZE
Dept. of CSE, IIT KGP

[7]
Queue Full

Queue: Operations
void addq( queue *q, element item )
{
q->rear = (q->rear + 1)% MAX_QUEUE_SIZE;
if (q->front == q->rear) { queue_full( ); return; }
q->list[q->rear] = item;
}
element deleteq( queue *q )
{
element item;
if (q->front == q->rear) return empty_queue( );
q-> front = (q-> front + 1)% MAX_QUEUE_SIZE;
return q->list[q->front] ;
}

Dept. of CSE, IIT KGP

You might also like