Professional Documents
Culture Documents
Chapter 4
Chapter 4
4.1. Stack
Stack is a data structure provides temporary storage in such a way that the element stored last will be
retried first (Last In First Out or LIFO method). Items can be inserted (“pushed”) and deleted
(“popped”). But only the most recently inserted elements can be operated on. This element is called
“top” of the stack. It can be implemented using.
Array
Linked list representation
Stack is useful for temporary storage, especially for dealing with nested structure or processes,
expressions with tin expressions, functions calling other functions, directors within directors.
Pop Operation
Pop operation is a way to remove values from the stack and return its value.
Algorithm:
1. Determine if any value is on the stack
2. If the stack is empty return “stack under flow”
3. Else go to the next step
4. Assign the value at the stack to variable which referenced by the stack pointer () top
Then
Assign null value for the stack slot pointed by the top and determine the stack pointer (top)
3. Return the value bold by the variable
Implementation
Data-type pop (data-type-stack [])
{
Int var=0;
If (top==-1)
cout<<”stack underflow\n”;
Else
{
Var=stack [top];
Stack [top] =NULL;
Top--;
}
Return var;
}
Peek Operation
The peek operation return the top element in the sack without modifying stacks contents.
Implementation exercise:
Laboratory: Write a complete code about array implementation of stack.
To implementation pop operation we need a list pointer “target” which will be pointing to the node
before the top node. Each time we pop, the top most nodes will be deleted and “target” will be made as
the top most nodes.
Algorithm for pop operation at the tail:
1. If the stack is empty then, give an alert”stack underflow” and exit the program
2. else if only one node if left free that node and make the “ stack” top and bottom pointers
point to NULL and quit.
3. Else make “target” pointer to just a node before the top, free top most nodes, the target as
your top most elements.
Implementation:
Data-type pop (node *tail, data-type pop-val)
{
Pop-val=0
Node *target=head;
If (head==NULL)
{
cout<<”stack underflow”;
Exit ();
}
Else
{
If (tail==head)
{
Pop-val=tail->data;
Delete tail;
Head=NULL;
Tail=head;
3 Data Structure and Algorithm Analysis ----- Stack and Queue
}
Else
{
While (target->next! =tail)
Target=target->next;
Pop-val=tail->data;
Delete tail;
Tail=target;
Target->next=NULL;
}
}
Return pop-val;
}
Implementation for Pop operation when pop from the head left as exercise for you.
Laboratory: Write a complete code for linked list implementation of stack
Application of stack
In computer science stacks are useful for temporary storage, especially for dealing with nested
structures pr processes for example:
1. To pass parameters between functions (function calling other functions)
2. Expressions with in expression (expression evaluation)
3. Directories with in directories.
F()
G() H()
Main function call F and F call G and H that must be first completed before F then F before Main
function. Hence, G and H contain calling function address (return address) of f.
Expression with in expression
Expressions can be evaluated by machine by placing all operations before or after their operands this
method is called polish notation. An arithmetic notation is said to postfix (suffix)notation also called as
reverse polish notation (RNP) if each operator is placed after its operands
E.g. ABC-* is A*(B-C)
48
6
Next is operator *, so 48 and 6 are popped, and 6*48 =288 is pushed
288
Now there are no more items and there is a single value on the stack, representing the final answer
288.
Queues
A queue is a data structure that stores a set of elements in particular order and permits addition of
elements from end and removal of elements from front. Elements are removed in the same order in
which they are stored (FIFO). E.g. job scheduling
Operations in Queue
enqueue - inserting data at the rear of the queue
dequeue – removing data at the front of the queue
Dequeue Enqueue
Example:
Front Rear
}
Else
Cout<<"Queue Underflow";
Return(x);
}
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;
}
else
cout<<"Queue Underflow";
return (x);
}
In linked list data is stored in a node of a linked list. a more natural queue implementation is a double
linked list in which both enqueue and dequeue can be executed in a constant time 0(1). In single list
implementation dequeue requires 0(n) operations primarily to scan the head and stop at the next to
the last node. Initially the list is empty so both front and rear pointers are NULL. The enqueue function
creates a new node, puts the new node data value in it, appends it to an existing list, and makes the
rear pointer point to it. a dequeue function checks whether the queue is empty, and if not, retrieves the
data value of the node pointed to by the front, advances the front and frees the storage of the node
whose data value has been retrieved.
Struct node {
Int data;
Node prev, next;
}
Front=NULL;
Rear=NULL:
Void enqueue (int x)
{
Node *Temp=new Node;
Temp->data=x;
If (! rear)
Head=Temp;
Rear=Temp;
Temp->Next=NULL;
Temp->Prev=NULL;
}
Else