Chapter 5 Data Structure
Chapter 5 Data Structure
The only difference is that here you can only delete from
the end of the list and only one at a time.
main()
{
int delval;
..
delval = pop(); /*POP returns the deleted value from the
stack*/
}
…
int pop( )
{
int pop_val = 0;
struct node *target = stack;
if(stack == NULL) /*step-1*/
cout<<"Stack Underflow";
else
{
if(top == bottom) /*step-2*/
{
pop_val = top -> nodeval; /*step-3*/
delete top;
stack = NULL;
top = bottom = stack;
}
else /*step-4*/
{
while(target->next != top)
target = target ->next;
pop_val = top->nodeval;
delete top;
top = target;
target ->next = NULL;
}
}
return(pop_val);
}
APPLICATIONS OF STACKS
Evaluation of postfix expression
programs
A Stack is useful for designing the compiler in
Reverse a List
Prefix notation
Postfix notation
Infix notation : The infix notation is what we
come across in our general mathematics, where
the operator is written in-between the operands.
For example :
The expression to add two numbers A and B is
written in infix notation as:
A + B
Note that the operator ‘+’ is written in between
the operands A and B.
Prefix notation : The prefix notation is a
notation in which the operator(s) is written
before the operands,
it is also called polish notation in the honor of
the polish mathematician Jan Lukasiewicz who
developed this notation.
The same expression when written in prefix
notation looks like:
+ A B
As the operator ‘+’ is written before the
operands A and B, this notation is called prefix
(pre means before).
Postfix notation :In the postfix notation the
Algorithm
Suppose Q is an arithmetic expression
written in infix notation. This algorithm finds
the equivalent postfix expression p
Exercise
Write the complete code of stack
implementation using single linked list.
Queue
A data structure that has access to its data at
the front and rear.
Operates on FIFO (First In First Out) basis.
Uses two pointers(indices) to keep track of
information(data).
It has two basic operations:
enqueue - inserting data at the rear of the
queue
dequeue – removing data at the front of the
queue
dequeue enqueue
Front Rear
Example
Operation Content of queue
Enqueue(B) B
Enqueue(C) B, C
Dequeue() C
Enqueue(G) C, G
Enqueue (F) C, G, F
Dequeue() G, F
Enqueue(A) G, F, A
Dequeue() F, A
dequeue enqueue
Front Rear
Enqueue(B) B
dequeue B enqueue
Front Rear
Enqueue(C) B, C
dequeue B C enqueue
Front Rear
Dequeue() C
dequeue C enqueue
Front Rear
Enqueue(G) C, G
dequeue C G enqueue
Front Rear
Enqueue (F) C, G, F
dequeue C G F enqueue
Front Rear
Dequeue() G, F
dequeue G F enqueue
Front Rear
Enqueue(A) G, F, A
dequeue G F A enqueue
Front Rear
Dequeue() F, A
dequeue F A enqueue
Front Rear
Simple array implementation of enqueue
and dequeue operations
Analysis- Consider the following structure
int Num[MAX_SIZE];
- Increment FRONT
- Decrement QUEUESIZE
FRONT = = -1?
No: - Queue Underflow
Implementation
const int MAX_SIZE=100;
int FRONT =-1, REAR =-1;
int QUEUESIZE = 0;
void enqueue(int x)
{
if(Rear<MAX_SIZE-1)
{
REAR++;
Num[REAR]=x;
QUEUESIZE++;
if(FRONT = = -1)
FRONT++;
}
else
cout<<"Queue Overflow";
}
…
int dequeue()
{
int x;
if(QUEUESIZE>0)
{
x=Num[FRONT];
FRONT++;
QUEUESIZE--;
}
else
cout<<"Queue Underflow";
return(x);
}
Exercise
Write a simple array implementation of
queue data structure ( including the
operations enqueue, dequeue, isempty, isfull,
an so on)
There is A problem with simple arrays
implementation of Queue and that is we run
out of space even if the queue never reaches
the size of the array
5 7 6 9 6
Front Rear
5 7 6 9 6
Front Rear
Assume this is our queue
5 7 6 9 6
Front Rear
7 6 9 6
Front Rear
Assume this is our queue
5 7 6 9 6
Front Rear
6 9 6
Front Rear
Assume this is our queue
5 7 6 9 6
Front Rear
9 6
Front Rear
Assume this is our queue
5 7 6 9 6
Front Rear
Front Rear
Assume this is our queue
5 7 6 9 6
Front Rear
Front Rear
Assume this is our queue
5 7 6 9 6
Front Rear
Front Rear
Thus, we can overcoming the above problem
by implementing simulated circular Queue
(circular arrays in which freed spaces are re-
used to store data)
MAX_SIZE - 1 8
0 7
1 6
2 5
3 4
Circular Queue
Example: Consider a queue with MAX_SIZE
=4
Circular array implementation of enqueue
and dequeue operations
Analysis- Consider the following structure
int Num[MAX_SIZE];
- Increment FRONT
- FRONT = = MAX_SIZE ?
Yes: FRONT = 0
- Decrement QUEUESIZE
No: - Queue Underflow
Implementation
const int MAX_SIZE=100;
int FRONT =-1, REAR =-1;
int QUEUESIZE = 0;
void enqueue(int x)
{
if(QUEUESIZE<MAX_SIZE)
{
REAR++;
if(REAR = = MAX_SIZE)
REAR=0;
Num[REAR]=x;
QUEUESIZE++;
if(FRONT = = -1)
FRONT++;
}
else
cout<<"Queue Overflow";
}
…
int dequeue()
{
int x;
if(QUEUESIZE>0)
{
x=Num[FRONT];
FRONT++;
if(FRONT = = MAX_SIZE)
FRONT = 0;
QUEUESIZE--;
}
else
cout<<"Queue Underflow";
return(x);
}
Exercise
Write the array implementation of circular
queue data structure
Linked list implementation of enqueue and
dequeue
Enqueue – inserting node at the end of the
list.
Dequeue – deleting the first node in the list.
Implementation
exercise, just see the linked list adding at the
end and deleting at the start example from the
previous chapter
Deque (pronounced as Deck)
is a Double Ended Queue
insertion and deletion can occur at either end
has the following basic operations
EnqueueFront – inserts data at the front of the list
DequeueFront – deletes data at the front of the list
EnqueueRear – inserts data at the end of the list
DequeueRear – deletes data at the end of the list
implementation is similar to that of queue
is best implemented using doubly linked list
Front Rear
Now the queue has data having equal priority and dequeue operation
deletes the front element like in the case of ordinary queues.