Stack and Queue - Prince of Songkla University

Download Report

Transcript Stack and Queue - Prince of Songkla University

Chapter 7
Stack & Queue
Aree Teeraparbseree, Ph.D
1
Stack
Last In, First Out (LIFO) data structure
 Insertions and deletions are made at one
end, called the top


Everyday examples of such a structure
 Stack of dishes
 Batteries in the flashlight
 Clothes in the trunk
2
Stack operations



push()
push(): Add an element to the top of the stack
pop(): Remove the element at the top of the stack
stack_top(): Returns the last value added to the stack
pop()
push()
pop()
push()
top()
1
2
1
1
empty
3
Stack implementation



There are several data structures that could be
used to implement a stack, e.g. array or linked list
In this section, we implement it as a linked list
To implement the linked-list stack, we need two
different structures: - a head node - a data node
Original picture from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
4
Stack implementation (conf.)
STACK
count
top
node
data
link
typedef struct {
int count;
struct node *top;
}STACK;
struct node {
int data;
struct node *link;
}
5
Create stack
Algorithm createStack
1. allocate memory for stack head
2. set count to 0
3. set top to NULL
4. return stack head
end createStack
6
Create stack
struct node* create_stack() {
struct node *head;
head = (struct node*)malloc(struct node);
head->count = 0;
head->top = NULL;
return head;
}
7
Push stack
Algorithm pushStack(stack, data)
1. allocate new node
2. store data in new node
3. make current top node the second
node
4. make new node the top
5. increment stack count
end pushStack
8
Push stack
int pushStack(STACK* stk, int data)
{ struct node *newNodePtr;
newNodePtr = (struct node*)
malloc(sizeof(struct node));
if (newNodePtr!=NULL)
{ newNodePtr->data = data;
newNodePtr->link = stk->top;
stk->top = newNodePtr;
stk->count++;
return 1; //push successful
}
else return 0; // push failed
}
9
Pop stack
Allgorithm popStack(stack, dataOut)
1. if(stack empty) set succes to false
2. else
1. set dataOut to data in top node
2. make second node the top node
3. decrement stack count
4. set success to true
3. return success
10
Pop stack
int popStack(STACK* stk, int* n)
{ struct node* tmp;
if (stk->count == 0)
return 0; //pop failed, empty stack
else
{
*n = stk->top->data; // get data from stack
tmp = stk->top;
stk->top = stk->top->link;
(stk->count)--;
free(tmp); //destroy the deleted node
return 1; //pop successful
}
}
11
Stack top
Algorithm stackTop(stack, dataOut)
1. if(stack empty) set success to false
2. else
1. set dataOut to data in top node
2. set success to true
3. return success
end stackTop
12
Stack top
int topStack(STACK* stk, int* n)
{
if (stk->count == 0)
return 0; //stack top failed, empty stack
else {
*n = stk->top->data; // get data from stack
return 1;
}
}
13
Stack applications
Converting Decimal to Binary
 Converting Infix to Postfix
 Stack is implicitly used by system while
calling to sub routines function from main
program
 Recursion

14
Converting decimal to binary
Algorithm decimalToBinary
1 stack = createStack
2 prompt(Enter a decimal to convert to binary)
3 read (number)
4 loop (number > 0)
1 digit = number modulo 2
2 pushOK = push (stack, digit)
3 if (pushOK == false)
1 print (Stack overflow creating digit)
2 quit algorithm
4 number = number/2
5 loop (not emptyStack(stack))
1 popStack (stack, digit)
2 print(digit)
6 destroy(stack)
15
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Converting Infix to Postfix
Algorithm for Infix to Postfix
1. Examine the next element in the input.
2. If it is operand, output it.
3. If it is opening parenthesis, push it on stack.
4. If it is an operator, then
1. If stack is empty, push operator on stack.
2. If the top of stack is opening parenthesis, push operator on stack
3. If it has higher priority than the top of stack, push operator on stack.
4. Else pop the operator from the stack and output it, repeat step 4
5. If it is a closing parenthesis, pop operators from stack and output them
until an opening parenthesis is encountered. pop and discard the opening
parenthesis.
6. If there is more input go to step 1
7. If there is no more input, pop the remaining operators to output
16
Infix to Postfix in C
char* infix_to_postfix(char *expr)
{
static char out[], i;
while(*expr != NULL)
{
if(*expr == OPERAND) /*step-1*/
{ out[i] = *expr; i = i+1;} /*step-2*/
else
{ /*step-3*/
while(priority(*expr) <= priority(stack[top]))
{
out[i] = pop( );
i = i + 1;
}
push(*expr);
}
expr++;
}
while( top != -1) /*step-4*/
{ out[i] = pop( ); i++;}
return(out);
}
17
Queue
First In, First Out (FIFO) data structure
 Items are added at the rear of the queue,
and the only item that can be removed is
the one at the front of the queue.
 Everyday examples of such a structure

 line
of students in the food court
 waiting at doctors’ clinic
 line
at the grocery store
18
Queue operations
enqueue(): insert item at the rear of
queue
 dequeue(): remove the front item from
queue

dequeue
enqueue
front
rear
19
Queue implementation


Just as stacks, queues can be implemented
as arrays or linked lists.
To implement the linked-list queue, we need
two different structures:
- a head node - a data node
Original picture from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
20
Queue implementation (conf.)
QUEUE
front count rear
node
data
link
typedef struct {
int count;
struct node *front;
struct node *rear;
}QUEUE;
struct node {
int data;
strutct node *link;
}
21
Create queue
Algorithm createQueue
1. allocate memory for queue head
2. set count to 0
3. set front to NULL
4. set rear to NULL
5. return queue head
end createQueue
22
Create queue
struct node* create_queue() {
struct node *head;
head = (struct node*)malloc(struct node);
head->count = 0;
head->front = NULL;
head->rear = NULL;
return head;
}
23
enqueue()
Algorithm enqueue(queue, data)
1. allocate new node
2. store data in new node
3. link rear to the new node
4. make new node the rear
5. increment queue count
end enqueue
24
enqueue()
int enqueue(QUEUE* q, int data)
{ struct node *newNodePtr;
newNodePtr = (struct node*)
malloc(sizeof(struct node));
if (newNodePtr!=NULL)
{ newNodePtr->data = data;
newNodePtr->link = NULL;
q->rear->link = newNodePtr;
q->rear = newNodePtr
return 1; //enqueue successful
}
else return 0; // enqueue failed
}
25
dequeue()
Allgorithm dequeue(queue, dataOut)
1. if(queue empty) set succes to false
2. else
1. set dataOut to data in front node
2. make second node the front node
3. decrement queue count
4. set success to true
3. return success
26
dequeue()
int dequeue(QUEUE* q, int* n)
{ struct node* tmp;
if (q->count == 0)
return 0; //dequeue failed, empty queue
else
{
*n = q->front->data; // get data from queue
tmp = q->front;
if(q->front->link == null )
q->rear->link = null;
q->front = q->front->link;
(q->count)--;
free(tmp); //destroy the deleted node
return 1; //dequeue successful
}
}
27