Professional Documents
Culture Documents
13 Stacks and Queues
13 Stacks and Queues
Debasis Samanta
Computer Science & Engineering
Indian Institute of Technology Kharagpur
Spring-2017
Lecture #13
Stack & Queue
*Stack *Queue
* Basic principles * Basic principles
* Operation of stack * Operation of queue
* Stack using Array * Queue using Array
* Stack using Linked List * Queue using Linked List
* Applications of stack * Applications of queue
pop
create
STACK
isempty
isfull
PUSH
top
top
POP
top
top
PUSH OPERATION
top
POP OPERATION
top
#define
#define MAXSIZE
MAXSIZE 100
100 struct
struct lifo
lifo
{{
struct
struct lifo
lifo int
int value;
value;
{{ struct
struct lifo
lifo *next;
*next;
int
int st[MAXSIZE];
st[MAXSIZE]; };
};
int
int top;
top; typedef
typedef struct
struct lifo
lifo
};
}; stack;
stack;
typedef
typedef struct
struct lifo
lifo
stack;
stack; stack
stack *top;
*top;
stack s;
stack s;
void
void create
create (stack
(stack *s)
*s) void
{{ void create
create (stack
(stack **top)
**top)
s->top {{
s->top == -1;
-1; *top
*top == NULL;
NULL;
/*
/* s->top
s->top points
points to
to /*
last
last element
element /* top
top points
points to
to NULL,
NULL,
pushed indicating
indicating empty
empty
pushed in;
in; stack
stack */
*/
initially
initially -1
-1 */
*/ }}
}}
void void
void push
push (stack
(stack **top,
**top, int
int element)
element)
void push
push (stack
(stack *s,
*s, int
int element)
element)
{{ {{
stack
stack *new;
*new;
if
if (s->top
(s->top ==
== (MAXSIZE-1))
(MAXSIZE-1))
{{ new
new == (stack
(stack *)malloc
*)malloc (sizeof(stack));
(sizeof(stack));
printf
printf (“\n
(“\n Stack
Stack overflow”);
overflow”); if (new == NULL)
if (new == NULL)
exit(-1);
exit(-1); {{
}} printf
printf (“\n
(“\n Stack
Stack is
is full”);
full”);
exit(-1);
exit(-1);
else
else }}
{{
s->top++;
s->top++; new->value
new->value == element;
element;
s->st[s->top]
s->st[s->top] == element;
element; new->next = *top;
new->next = *top;
}} *top
*top == new;
new;
}}
}}
int
int pop
pop (stack
(stack **top)
**top)
{{
int
int pop
pop (stack
(stack *s)
*s) int
int t;t;
{{ stack
stack *p;*p;
if
if (s->top
(s->top ==
== -1)
-1) if
if (*top
(*top ==== NULL)
NULL)
{{ {{
printf printf
printf (“\n
(“\n Stack
Stack is
is empty”);
empty”);
printf (“\n
(“\n Stack
Stack underflow”);
underflow”); exit(-1);
exit(-1);
exit(-1);
exit(-1); }}
}} else
else
else
else {{
{{ tt == (*top)->value;
(*top)->value;
pp == *top;
*top;
return
return (s->st[s->top--]);
(s->st[s->top--]); *top
*top == (*top)->next;
(*top)->next;
}} free (p);
free (p);
}} return
return t;t;
}}
}}
int
int isempty
isempty (stack
(stack *s)
*s) int
int isempty
isempty (stack
(stack *top)
*top)
{{ {{
if
if (s->top
(s->top ==
== -1)
-1) if
if (top
(top ==
== NULL)
NULL)
return
return 1;
1; return
return (1);
(1);
else
else else
else
return
return (0);
(0); return
return (0);
(0);
}} }}
int
int isempty
isempty (stack
(stack *s)
*s) int
int isempty
isempty (stack
(stack *top)
*top)
{{ {{
if
if (s->top
(s->top ==
== -1)
-1) if
if (top
(top ==
== NULL)
NULL)
return
return 1;
1; return
return (1);
(1);
else
else else
else
return
return (0);
(0); return
return (0);
(0);
}} }}
• Indirect applications:
• Auxiliary data structure for algorithms
• Component of other data structures
A+B* C (A + (B * C)) (A + (B C *) ) A B C * +
Operands:
Add to postfix expression.
Close parenthesis:
pop stack symbols until an open parenthesis appears.
Operators:
Pop all stack symbols until a symbol of lower precedence appears. Then push
the operator.
End of input:
Pop all remaining stack symbols and add to the expression.
Autumn 2016 30
Queue
dequeue
create
QUEUE
isempty
size
ENQUEUE
front rear
DEQUEUE
front rear
struct
struct queue
queue
{{
struct
struct qnode
qnode *qfront,
*qfront, *qrear;
*qrear;
};
};
typedef
typedef struct
struct queue
queue QUEUE;
QUEUE;
void
void enqueue
enqueue (QUEUE
(QUEUE *q,int
*q,int element)
element)
{{
struct
struct qnode
qnode *q1;
*q1;
q1=(struct
q1=(struct qnode *)malloc(sizeof(struct
qnode *)malloc(sizeof(struct qnode));
qnode));
q1->val=
q1->val= element;
element;
q1->next=q->qfront;
q1->next=q->qfront;
q->qfront=q1;
q->qfront=q1;
}}
front
front rearrear
• Indirect applications:-
• Auxiliary data structure for algorithms
• Component of other data structures