DSC 4th Unit Notes
DSC 4th Unit Notes
STACKS
Stacks:
Stack is a collection of homogenous data items where insertion and deletion operations take place at
one end in a LIFO fission and that end is termed as top of the stack.
Stack as ADT:
We have already seen lists &arrays as ADT and another ADT is a stack.
The minimal set of operations that we would need for abstract stack is
1. CREATE STACK () - create a stack of elements.
2. PUSH STACK () - top of the stack and if stack is full push is not possible.
3. POP STACK () - pop an element from the stack (top element) if the stack is empty pop is not
possible.
4. IS EMPTY () - returns true if the stack is empty otherwise false.
5. TRAVERSE () - accessing all elements in the stack.
6. COUNT () - count the numbers of elements in the stack.
Note:
stack can be implemented in two ways.
1. Using Arrays.
2. Using Linked list.
Operations on stack:
1. push- operation
2. pop-operation
3. display- operation
4. is empty-operation
5. is full – operation
6. peak- operation
1. push-operation:
Push operation is used for inserting elements in to a stack is called push operation.
Dept. of Computer Science 1 of 15 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Algorithm
Step 1: if(top==size-1)
Then write “stack is full”
Else
Step 2: read data (or)item
Step 3: top=top+1
Step 4: stack[top]=item
Step 5: stop.
Stack overflow: When the stack is full then the insertion operation takes place, it is known as stack overflow.
POP- operation: Pop operation is used for removing an element (or)deleting an element from the stack is
called pop operation.
Algorithm:
Step 1: if (top== -1)
Then write (“stack is empty”)
(“stack underflow”)
else
Step 2: item =start(top)
Step 3: Top =Top -1
Step 4: stop.
Dept. of Computer Science 2 of 15 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
C - implementation of pop operation:
pop ()
{
if (top==-1)
printf (“stack underflow in pop\n”);
else
{
printf (“popped element is:%d\n’’s[top]);
top= top -1;
}
}
Display – operation:
• Display operation is used to display the contents of the stacks. If the stacks contain 5 elements and the
top is pointing to top element s (4) the following code is used to display all the 5 elements in the stack.
• Generally, the contents of the stack are displayed from the top of the stage to the bottom of the stack
is finished.
is empty operation:
• This operation is used to find whether the stack contains any elements (or) not.
• If stack is empty then it returns true else if stack is not empty it returns false.
• This function is mainly used to test underflow operation.
Dept. of Computer Science 3 of 15 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
is – full operation: In the elements in the stack is full it returns true else stack is false.
Application of stack:
1. recursion.
2. reversal of a string.
3. checking the parantasis matching.
4. Infix notation. (conversion)
5. postfix notation. (conversion)
6. pre – fix notation. (conversion)
Infix notation:
The conventional way of writing an expression is called Infix i.e. if the operator is placed between its
two operands then we called the notation as Infix notation.
Syntax:
<operand> operator <operand >
a+b
(a=operand, b=operand,” +” = operator)
Dept. of Computer Science 4 of 15 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Precedence & Associativity Notations are:
1. () →R-L
2. ^ →R-L
3. */ →L-R
4. + - →L-R
Prefix notation:
If the operator symbol is placed before its two operands then we call that notation as pre-fix notation.
Syntax:
<operator><operand><operand>
++a
*-ab/cd
Postfix notation: If the operator symbol is placed after its two operands then we call that notation as postfix
notation.
Syntax:
<operand><operand><operator>
b++
abc*
Dept. of Computer Science 5 of 15 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Example: -
(A+B/C*(D+E)-F) using stack.
Operand →postfix.
Operator →stack.
) (-) ABC/DE+*+F-
Dept. of Computer Science 6 of 15 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Evaluation of postfix expression:
1. while reading expression from left to right PUSH the element in the stack. If it is an operand.
2. if the operator is encountered pop two elements A→top element B→ next to top element and evaluates
B operator A and push the result on to the stack.
3. After the expression is finished then return element of stack top.
Algorithm:
Step 1: Add) to postfix expression.
Step 2: Read postfix expression left to right until) encountered
Step 3: if operand is encountered, push in onto stack [END IF]
Step 4: if operator is encountered, pop two elements.
A→ Top element
B→ Next to Top element
Push B operator A on to stack.
Step 5: set result =pop
Step 6: END
Example:
Dept. of Computer Science 7 of 15 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
QUEUES
Queues:
Queue is defined as an ordered collection of items from which the item will be deleted at one end
called FRONT END, and in which items may be inserted at another end is called REAR END.
Array representation of queues:
Array representation of queues
Example: int Q [4];
Q (0) Q (1) Q (2) Q (3)
• And the last element is 40, we insert last element in Q (3) =40
Q (0) Q (1) Q (2) Q (3)
10 20 30 40
Dept. of Computer Science 8 of 15 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Operations on queues:
Based on the actions performed in queues there are 5 operations.
1. queue insert(or)enqueue
2. Qdelete(or)dequeue
3. Qempty
4. Qfull
5. Display
1. Qinsert Or enqueue: -Inserting the element into queue through the rear end is called as Qinsert or
Enqueue
Ex: -int Q [3];
20
Q (0) Q (1) Q (3) Enqueue 20 at Q [0]
Rear = 0
Front = 0
20 30
10 20 30 40
Dept. of Computer Science 9 of 15 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
C Implementation of Insertion OR Enqueue
void Qinsert ()
{
if (REAR==N-1)
printf (“\nQueue overflow”);
else
{
printf (“\nEnter the Item:”);
scanf(“%d”, &ITEM);
REAR++;
Queue [REAR]=ITEM;
}
}
• QDELETE OR dequeue: It is defined as the process of deleting an element in queue through its
front end is called dequeue OR Qdelete
Ex: int Q [4];
Q (0) Q (1) Q (2) Q (3)
10 20 30 40 Rear=3
Front=0
dequeue (10) = front + 1
Q (0) Q (1) Q (2) Q (3)
20 30 40 Rear=3
Front=1
dequeue (20) =front+1
Q (0) Q (1) Q (2) Q (3)
30 40 Rear=3
Front=2
Dept. of Computer Science 10 of 15 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Algorithm for dequeue operations:
Qdelete (Q, Front, Rear, N, Item)
Step1: [Underflow check]
if REAR=FRONT-1
then write(“underflow”)
return (0)
Step2: [Delete the item]
ITEM=Q[FRONT]
Step3: if FRONT=REAR [When there is only one item]
FRONT=0, REAR=-1
Else
FRONT=FRONT+1
Step4: return
Dept. of Computer Science 11 of 15 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Algorithm for Queue Empty:
Step1: if (FRONT==REAR==-1)
then “QUEUE IS EMPTY”
Step2: Stop
Dept. of Computer Science 12 of 15 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
C - Implementation of Qfull:
int Qfull ()
{
if (REAR==N-1)
return 1;
else
return 0;
}
• Queue Display (): It is the operation which displays the items present in the Queue
C Implementation of queue display ()
void Qdisplay ()
{
int I;
if (REAR==FRONT -1)
printf (“\n\t Number of Elements in Queue”);
else
{
printf (“\n QUEUE:”);
for (i=FRONT; i<= REAR; i++)
printf (“%d\t”, QUEUE[i]);
printf (“\FRONT Element of the Queue id %d”, QUEUE[FRONT]);
printf (“\n REAR element of the Queue is %d”, QUEUE[REAR]);
}
}
Dept. of Computer Science 13 of 15 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Types Of Queues:
Queues are broadly classified into 4 types, they are: -
QUEUES
1. Linear /Single Queue: It is defined as an ordered linear collection of items from which the items
from the items deleted at one end called frontend and in which item may be Quested at another end
called Rear end.
Example:
Q (0) Q (1) Q (2) Q (3)
10 20 30 40 10=front
40= rear
Circular Queue (Ring Buffer):
A circular queue is similar to a linear queue as it is also based on the FIFO principle except that the last
position is connected to the first position in a circular queue that form a circle.
Circular queue is also known as ring buffer.
Priority Queue:
A priority is a Queue such that each element of the Queue has been assigned with a priority and such that the
order in the element are inserted and deleted using the following rules
1. An element of higher priority is processed before any element of lower priority.
2. if two elements have same priority then the element which comes first will be processed first.
There are two types of priority Queues.
1. Ascending priority queue: it is a collection of elements in to which items can be inserted in an
order but deletion is possible but deletion is possible only for the smallest element.
2. Descending priority order: it is a collection of elements in to which items can be inserted in order
but deletion is possible but deletion is possible only for the largest element.
Dept. of Computer Science 14 of 15 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Double Ended Queue:
A double ended queue is a list in which the element are inserted (or) deleted at either ends.
It is a queue data structure in which the insertion and deletion operations are performed at both ends called
front and rear.
deletion
deletion
1. Input Restricted dequeue: Input restricted dequeue is the one which allows the insertion only one
end. but allows at both ends deletion.
deletion
Deletion
insertion
front rear
2. Output Restricted dequeue: Output restricted dequeue is the one which allows deletions at one end
and insertion at both ends and those deletions are possible only at front end.
deletion
insertion
Dept. of Computer Science 15 of 15 From the desk of Mr. Chaitanya Reddy Mtech