0% found this document useful (0 votes)
60 views15 pages

DSC 4th Unit Notes

Data structure

Uploaded by

Rishitha MB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views15 pages

DSC 4th Unit Notes

Data structure

Uploaded by

Rishitha MB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Data Structure Using C Shree Medha Degree College, Ballari

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.

C - implementation of push operation:


push ()
{
if (top==(maxstk-1))
printf (“stack overflow\n”)
else
printf (“enter the item to be pushed in stack:”);
scanf (“%d”, &item);
top=top+1;(or) s[++top] =item
s[top]=item;
}
}

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.

C – implementation of display operation:


display ()
{
int i;
if (top == -1)
printf (“the stack is empty\n”);
else
{
printf (“the stack contents from top to bottom are \n”);
for (i=top; i>=0; i--)
printf (“the element %d is %d\n”, i+1, s[i]);
}
}

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.

C – implementation of is empty operation:


int is empty ()
{
if (top == -1)
return (true);
else
return (false);
}

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.

C - implementation of is -full operation:


int is full ()
{
if (top == size -1)
return (true);
else
return (false);
}

Peak operation: It is used to get the top element of the stack.

C – implementation of peak operation:


int peak ()
{
if (top == -1)
printf (“stack underflow”);
else
return (stack [top])
}

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)

Polish notation of Arthematic expression:


Expressions involved operators on operands.
There are three notations of Arthematic expressions using stack they are
1. Infix – notation.
2. Postfix – notation.
3. Prefix – notation.

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*

Conversion of infix to postfix using stack:


Rule 1: priorities of operators.
Rule 2: No two operators of same priority can stay together in stack column.
Rule 3: Lowest priority cannot be placed before the highest priority
Rule 4: Any operator placed between open & closed parantasis then just immediately pop that operator.

Algorithm for transforming infix expression in to postfix expression:


Step 1: PUSH the left parenthesis (“on to STACK and add right paraptosis”) at the end of Q.
Step 2: Scan Q from left to right and repeat step 3 to 6 for each element of Q until the stack is empty.
Step 3: if an operand is encountered, add it to p.
Step 4: if” (“is encountered, push it onto STACK and this “(“can be removed popped only when”)” is
encountered.
Step 5: if an operator is encountered.
a. Repeatedly pop from stack and add to p each operator which has the same precedence.
b. push to stack if”)” is encountered.
Step 6: If “)” is encountered.
a. Repeatedly pop from the stack, add top each operator until the left parenthesis is encountered.
b. Remove the left paraptosis “(“
[Do not add left parenthesis to p]
Step 7: EXIT

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.

Symbol Stack Postfix


(
( A
A
( A
+ (+
AB
B
(+ AB
/
(+/ ABC
(
(+/ ABC
*
(+/ ABC/
(
(** ABC/
D (+*(
ABC/D
(+*(
+
(+*(+ ABC/D
E
(+*(+ ABC/DE
)
(+ ABC/DE+
-
(- ABC/DE+*+
F
(- ABC/DE+*+F

) (-) 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)

• We have 4 elements 10,20,30,40 now first we insert element in Q (0)


Q (0) Q (1) Q (2) Q (3)
10

• Second element is 20, we insert 20 in Q (1) =20


Q (0) Q (1) Q (2) Q (3)
10 20

• Third element is 30, we insert 30 in Q (2) =30


Q (0) Q (1) Q (2) Q (3)
10 20 30

• 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

• The queue is full, that queue is displayed


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

Q (0) Q (1) Q (2) Qinsert 30 at Q [1]


Rear =1
Front =0

Algorithm for Enqueue operation: -


Step1: [Overflow check]
if REAR=N-1
then write(“OVERFLOW”)
return
Step2: [Increment rear pointer]
REAR=REAR+1
Step3: [Insert the item]
Q[REAR]=ITEM
Step4: Return

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

C - Implementation of Qdelete is:


void Qdelete ()
{
if (REAR==FRONT-1)
printf (“\nQueue underflow”);
else if (REAR==FRONT)
{
printf (“\n Deleted item is %d”, QUEUE[FRONT]);
FRONT++;
}
}
• Queue Empty: Qempty is defined as when the rear and front are at -1 position then we state that as
Qempty
Ex: int Q [5]; Q (0) Q (1) Q (2) Q (3) Q (4)
Hence Front= -1
Rear= -1

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

C Implementation of Queue Empty


int Empty ()
{
if (REAR==FRONT-1)
return 1;
else
return 0;
}
• Queue Full: Qfull is defined as when REAR and FRONT are at n-1 position then we state that as
Qfull
Ex: int Q [4];
Q (0) Q (1) Q (2) Q (3)
10 20 30 40

In this case REAR=MAX


i.e., REAR=SIZE-1

Algorithm for Queue full operation:


Step1: if (REAR==SIZE-1)
then “QUEUE IS FULL”
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

Linear/single circular priority double ended Queue


Queue Queue Queue

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

insertion front rear insertion

There are two types of double ended queue


1. input restricted dequeue
2. output restricted dequeue

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

insertion front rear

Dept. of Computer Science 15 of 15 From the desk of Mr. Chaitanya Reddy Mtech

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy