SlideShare a Scribd company logo
1 of 57
Linear Data Structure
Stack
 A Stack is a linear data structure that follows the LIFO
(Last-In-First-Out) principle.
 Stack has one end, whereas the Queue has two ends
(front and rear).
 It contains only one pointer top pointer pointing to
the topmost element of the stack.
 Whenever an element is added in the stack, it is added
on the top of the stack, and the element can be deleted
only from the top of stack.
 It is called as stack because it behaves like a real-world
stack, piles of books, piles of plates, etc.
 A Stack is an abstract data type with a pre-defined
capacity, which means that it can store the elements of
a limited size.
 In stack terminology, insertion operation is
called PUSH operation and removal operation is
called POP operation.
Example
Working of Stack
 Since our stack is full as the size of the stack is 5.
 In the above cases, we can observe that it goes from the
top to the bottom when we were entering the new
element in the stack.
 The stack gets filled up from the bottom to the top.
Standard Stack Operations
 push(): When we insert an element in a stack then the
operation is known as a push.
 If the stack is full then the overflow condition occurs.
 pop(): When we delete an element from the stack, the
operation is known as a pop.
 If the stack is empty means that no element exists in
the stack, this state is known as an underflow state.
 isEmpty(): It determines whether the stack is empty
or not.
 isFull(): It determines whether the stack is full or not.
 peek(): It returns the element at the given position.
 count(): It returns the total number of elements
available in a stack.
 change(): It changes the element at the given
position.
 display(): It prints all the elements available in the
stack.
PUSH operation
 Before inserting an element in a stack, we check
whether the stack is full.
 If we try to insert the element in a stack, and the stack
is full, then the overflow condition occurs.
 When we initialize a stack, we set the value of top as -1
to check that the stack is empty
 When the new element is pushed in a stack, first, the
value of the top gets incremented, i.e., top=top+1, and
the element will be placed at the new position of
the top.
 The elements will be inserted until we reach
the max size of the stack.
POP operation
 Before deleting the element from the stack, we check
whether the stack is empty.
 If we try to delete the element from the empty stack,
then the underflow condition occurs.
 If the stack is not empty, we first access the element
which is pointed by the top
 Once the pop operation is performed, the top is
decremented by 1, i.e., top=top-1.
Applications of Stack
 String reversal:
 First, we push all the characters of the string in a stack
until we reach the null character.
 After pushing all the characters, we start taking out the
character one by one until we reach the bottom of the
stack.
 UNDO/REDO
 Recursion
 Expression conversion
 Memory management
Array implementation of Stack
 In array implementation, the stack is formed by using
the array.
 All the operations regarding the stack are performed
using arrays.
push operation
 Algorithm:
begin
if top = max-1 then stack full return
else
top = top + 1
stack (top) : = item;
end
Program
void push (int val,int max) //max is size of the stack
{
if (top == max-1 )
printf("n Overflow");
else
{
top = top +1;
stack[top] = val;
}
}
Pop operation
 Algorithm :
begin
if top = -1 then stack empty return
else
item := stack(top);
top = top - 1;
end;
Program
int pop ()
{
if(top == -1)
{
printf("Underflow");
return 0;
}
else
{
return stack[top - - ];
}
}
Queue
 A queue can be defined as an ordered list which
enables insert operations to be performed at one end
called REAR and delete operations to be performed at
another end called FRONT.
Applications of Queue
 Queues are widely used as waiting lists for a single
shared resource like printer, disk, CPU.
 Queue are used to maintain the play list in media
players in order to add and remove the songs from the
play-list.
 Queues are used in operating systems for handling
interrupts.
Types of Queue
 Simple Queue or Linear Queue
 Circular Queue
 Priority Queue
 Double Ended Queue (or Deque)
Simple Queue or Linear Queue
 In Linear Queue, an insertion takes place from one
end while the deletion occurs from another end.
 The major drawback of using a linear Queue is that
insertion is done only from the rear end.
 If the first three elements are deleted from the Queue,
we cannot insert more elements even though the space
is available in a Linear Queue.
 In this case, the linear Queue shows the overflow
condition as the rear is pointing to the last element of
the Queue.
Circular Queue
 It is similar to the linear Queue except that the last
element of the queue is connected to the first
element.
 The drawback that occurs in a linear queue is
overcome by using the circular queue.
 If the empty space is available in a circular queue, the
new element can be added in an empty space by
simply incrementing the value of rear.
 The main advantage of using the circular queue is
better memory utilization.
Priority Queue
 It is a special type of queue in which the elements are
arranged based on the priority.
 Suppose some elements occur with the same priority,
they will be arranged according to the FIFO principle.
 Insertion in priority queue takes place based on the
arrival, while deletion in the priority queue occurs
based on the priority.
 Priority queue is mainly used to implement the CPU
scheduling algorithms.
Deque (or, Double Ended
Queue)
 In Deque or Double Ended Queue, insertion and
deletion can be done from both ends of the queue
either from the front or rear.
 Deque can be used as a palindrome checker means
that if we read the string from both ends, then the
string would be the same.
 Deque can be used both as stack and queue as it
allows the insertion and deletion operations on both
ends.
Types of DEQUE
 There are two types of deque that are discussed as
follows –
 Input restricted deque -
 As the name implies, in input restricted queue,
insertion operation can be performed at only one
end, while deletion can be performed from both
ends.
 Output restricted deque -
 As the name implies, in output restricted queue,
deletion operation can be performed at only one end,
while insertion can be performed from both ends.
Array representation of
Queue
 We can easily represent queue by using linear
arrays.
 Front and rear variables point to the position from
where insertions and deletions are performed in a
queue.
 Initially, the value of front and queue is -1 which
represents an empty queue.
 The above figure shows the queue of characters
forming the English word "HELLO".
 Since, No deletion is performed in the queue till now,
therefore the value of front remains 0 .
 However, the value of rear increases by one every
time an insertion is performed in the queue.
Insertion In Queue
 Algorithm
 Step 1: IF Front = 0 & REAR = MAX - 1
Write OVERFLOW
 Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
 Step 3: Set QUEUE[REAR] = NUM

Example
void insert (int queue[], int max, int front, int rear, int item)
{
if ( front==0 && rear == max-1)
{
printf("overflow");
}
else
{
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear + 1;
} }
queue[rear]=item;
}
Deletion in Queue
Algorithm
Step 1: IF FRONT = -1
Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT]
Step2: IF (front == Rear)
Set front=rear=-1
else
SET FRONT = FRONT + 1
[END OF IF]
Exit
Example
int delete (int queue[], int max, int front, int rear)
{
int y;
if (front == -1 )
{
printf("underflow");
}
else
{
y = queue[front];
if(front == rear)
{
front = rear = -1;
else
front = front + 1;
}
return y;
}
}
Drawback of array
implementation
 Memory wastage :
 The space of the array, which is used to store queue
elements, can never be reused to store the elements
of that queue because the elements can only be
inserted at front end and the value of front might be
so high so that, all the space before that, can never
be filled.
• Deciding the array size:
• On of the most common problem with array
implementation is the size of the array which
requires to be declared in advance.
Circular Queue
 There was one limitation in the array implementation
of Queue.
 If the rear reaches to the end position of the Queue
then there might be possibility that some vacant
spaces are left in the beginning which cannot be
utilized.
 So, to overcome such limitations, the concept of the
circular queue was introduced.
 A circular queue is similar to a linear queue as it is
also based on the FIFO (First In First Out) principle
except that the last position is connected to the first
position in a circular queue that forms a circle.
 It is also known as a Ring Buffer.
Enqueue operation
• First, we will check whether the Queue is full or not.
• Initially the front and rear are set to -1. When we
insert the first element in a Queue, front and rear
both are set to 0.
• When we insert a new element, the rear gets
incremented, i.e., rear=rear+1.
 There are two scenarios in which queue is not
full:
• If rear != max - 1, then rear will be incremented
to mod(maxsize) and the new value will be inserted
at the rear end of the queue.
• If front != 0 and rear = max - 1, it means that queue
is not full, then set the value of rear to 0 and insert
the new element there.
 There are two cases in which the element cannot
be inserted:
• When front ==0 && rear == max-1, which means
that front is at the first position of the Queue and rear
is at the last position of the Queue.
• front== rear + 1;
Insertion In Circular Queue
 Algorithm
 Step 1: IF (Front = 0 & REAR = MAX – 1)
or(front=rear+1)
Write OVERFLOW
 Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE IF REAR = MAX - 1 and FRONT ! = 0
SET REAR = 0
 ELSE
SET REAR = REAR + 1
[END OF IF]
 Step 3: Set QUEUE[REAR] = NUM
Deletion In Circular Queue
 Step 1: IF FRONT = -1
Write " UNDERFLOW "
Goto Step 4
[END of IF]
 Step 2: SET VAL = QUEUE[FRONT]
 Step 3: IF FRONT = REAR
SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END of IF]
[END OF IF]
 Step 4: Exit
Example
Module 2 ppt.pptx
Module 2 ppt.pptx
Module 2 ppt.pptx
Module 2 ppt.pptx

More Related Content

Similar to Module 2 ppt.pptx

Similar to Module 2 ppt.pptx (20)

Stacks
StacksStacks
Stacks
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
Stacks-and-Queues.pdf
Stacks-and-Queues.pdfStacks-and-Queues.pdf
Stacks-and-Queues.pdf
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Priorty queue
Priorty queuePriorty queue
Priorty queue
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdf
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
Queue
QueueQueue
Queue
 
chapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdfchapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdf
 
Stack and its operations
Stack and its operationsStack and its operations
Stack and its operations
 
Lect 15-16 Zaheer Abbas
Lect 15-16 Zaheer  AbbasLect 15-16 Zaheer  Abbas
Lect 15-16 Zaheer Abbas
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 

Recently uploaded

Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Cantervoginip
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Book
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Bookvip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Book
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Bookmanojkuma9823
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...limedy534
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort servicejennyeacort
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 

Recently uploaded (20)

Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Canter
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Book
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Bookvip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Book
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Book
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 

Module 2 ppt.pptx

  • 2. Stack  A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle.  Stack has one end, whereas the Queue has two ends (front and rear).  It contains only one pointer top pointer pointing to the topmost element of the stack.  Whenever an element is added in the stack, it is added on the top of the stack, and the element can be deleted only from the top of stack.
  • 3.  It is called as stack because it behaves like a real-world stack, piles of books, piles of plates, etc.  A Stack is an abstract data type with a pre-defined capacity, which means that it can store the elements of a limited size.  In stack terminology, insertion operation is called PUSH operation and removal operation is called POP operation.
  • 6.  Since our stack is full as the size of the stack is 5.  In the above cases, we can observe that it goes from the top to the bottom when we were entering the new element in the stack.  The stack gets filled up from the bottom to the top.
  • 7. Standard Stack Operations  push(): When we insert an element in a stack then the operation is known as a push.  If the stack is full then the overflow condition occurs.  pop(): When we delete an element from the stack, the operation is known as a pop.  If the stack is empty means that no element exists in the stack, this state is known as an underflow state.  isEmpty(): It determines whether the stack is empty or not.
  • 8.  isFull(): It determines whether the stack is full or not.  peek(): It returns the element at the given position.  count(): It returns the total number of elements available in a stack.  change(): It changes the element at the given position.  display(): It prints all the elements available in the stack.
  • 9. PUSH operation  Before inserting an element in a stack, we check whether the stack is full.  If we try to insert the element in a stack, and the stack is full, then the overflow condition occurs.  When we initialize a stack, we set the value of top as -1 to check that the stack is empty
  • 10.  When the new element is pushed in a stack, first, the value of the top gets incremented, i.e., top=top+1, and the element will be placed at the new position of the top.  The elements will be inserted until we reach the max size of the stack.
  • 11.
  • 12. POP operation  Before deleting the element from the stack, we check whether the stack is empty.  If we try to delete the element from the empty stack, then the underflow condition occurs.  If the stack is not empty, we first access the element which is pointed by the top  Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1.
  • 13.
  • 14. Applications of Stack  String reversal:  First, we push all the characters of the string in a stack until we reach the null character.  After pushing all the characters, we start taking out the character one by one until we reach the bottom of the stack.  UNDO/REDO  Recursion  Expression conversion  Memory management
  • 15. Array implementation of Stack  In array implementation, the stack is formed by using the array.  All the operations regarding the stack are performed using arrays.
  • 16. push operation  Algorithm: begin if top = max-1 then stack full return else top = top + 1 stack (top) : = item; end
  • 17. Program void push (int val,int max) //max is size of the stack { if (top == max-1 ) printf("n Overflow"); else { top = top +1; stack[top] = val; } }
  • 18. Pop operation  Algorithm : begin if top = -1 then stack empty return else item := stack(top); top = top - 1; end;
  • 19. Program int pop () { if(top == -1) { printf("Underflow"); return 0; } else { return stack[top - - ]; } }
  • 20. Queue  A queue can be defined as an ordered list which enables insert operations to be performed at one end called REAR and delete operations to be performed at another end called FRONT.
  • 21. Applications of Queue  Queues are widely used as waiting lists for a single shared resource like printer, disk, CPU.  Queue are used to maintain the play list in media players in order to add and remove the songs from the play-list.  Queues are used in operating systems for handling interrupts.
  • 22. Types of Queue  Simple Queue or Linear Queue  Circular Queue  Priority Queue  Double Ended Queue (or Deque)
  • 23. Simple Queue or Linear Queue  In Linear Queue, an insertion takes place from one end while the deletion occurs from another end.  The major drawback of using a linear Queue is that insertion is done only from the rear end.  If the first three elements are deleted from the Queue, we cannot insert more elements even though the space is available in a Linear Queue.  In this case, the linear Queue shows the overflow condition as the rear is pointing to the last element of the Queue.
  • 24. Circular Queue  It is similar to the linear Queue except that the last element of the queue is connected to the first element.  The drawback that occurs in a linear queue is overcome by using the circular queue.  If the empty space is available in a circular queue, the new element can be added in an empty space by simply incrementing the value of rear.  The main advantage of using the circular queue is better memory utilization.
  • 25.
  • 26. Priority Queue  It is a special type of queue in which the elements are arranged based on the priority.  Suppose some elements occur with the same priority, they will be arranged according to the FIFO principle.
  • 27.  Insertion in priority queue takes place based on the arrival, while deletion in the priority queue occurs based on the priority.  Priority queue is mainly used to implement the CPU scheduling algorithms.
  • 28. Deque (or, Double Ended Queue)  In Deque or Double Ended Queue, insertion and deletion can be done from both ends of the queue either from the front or rear.  Deque can be used as a palindrome checker means that if we read the string from both ends, then the string would be the same.  Deque can be used both as stack and queue as it allows the insertion and deletion operations on both ends.
  • 29.
  • 30. Types of DEQUE  There are two types of deque that are discussed as follows –  Input restricted deque -  As the name implies, in input restricted queue, insertion operation can be performed at only one end, while deletion can be performed from both ends.  Output restricted deque -  As the name implies, in output restricted queue, deletion operation can be performed at only one end, while insertion can be performed from both ends.
  • 31.
  • 32. Array representation of Queue  We can easily represent queue by using linear arrays.  Front and rear variables point to the position from where insertions and deletions are performed in a queue.  Initially, the value of front and queue is -1 which represents an empty queue.
  • 33.
  • 34.  The above figure shows the queue of characters forming the English word "HELLO".  Since, No deletion is performed in the queue till now, therefore the value of front remains 0 .  However, the value of rear increases by one every time an insertion is performed in the queue.
  • 35.
  • 36.
  • 37. Insertion In Queue  Algorithm  Step 1: IF Front = 0 & REAR = MAX - 1 Write OVERFLOW  Step 2: IF FRONT = -1 and REAR = -1 SET FRONT = REAR = 0 ELSE SET REAR = REAR + 1 [END OF IF]  Step 3: Set QUEUE[REAR] = NUM 
  • 38. Example void insert (int queue[], int max, int front, int rear, int item) { if ( front==0 && rear == max-1) { printf("overflow"); } else { if(front == -1 && rear == -1) { front = 0; rear = 0; } else { rear = rear + 1; } } queue[rear]=item; }
  • 39. Deletion in Queue Algorithm Step 1: IF FRONT = -1 Write UNDERFLOW ELSE SET VAL = QUEUE[FRONT] Step2: IF (front == Rear) Set front=rear=-1 else SET FRONT = FRONT + 1 [END OF IF] Exit
  • 40. Example int delete (int queue[], int max, int front, int rear) { int y; if (front == -1 ) { printf("underflow"); } else { y = queue[front]; if(front == rear) { front = rear = -1; else front = front + 1; } return y; } }
  • 41. Drawback of array implementation  Memory wastage :  The space of the array, which is used to store queue elements, can never be reused to store the elements of that queue because the elements can only be inserted at front end and the value of front might be so high so that, all the space before that, can never be filled.
  • 42.
  • 43. • Deciding the array size: • On of the most common problem with array implementation is the size of the array which requires to be declared in advance.
  • 44. Circular Queue  There was one limitation in the array implementation of Queue.  If the rear reaches to the end position of the Queue then there might be possibility that some vacant spaces are left in the beginning which cannot be utilized.  So, to overcome such limitations, the concept of the circular queue was introduced.
  • 45.
  • 46.  A circular queue is similar to a linear queue as it is also based on the FIFO (First In First Out) principle except that the last position is connected to the first position in a circular queue that forms a circle.  It is also known as a Ring Buffer.
  • 47. Enqueue operation • First, we will check whether the Queue is full or not. • Initially the front and rear are set to -1. When we insert the first element in a Queue, front and rear both are set to 0. • When we insert a new element, the rear gets incremented, i.e., rear=rear+1.
  • 48.  There are two scenarios in which queue is not full: • If rear != max - 1, then rear will be incremented to mod(maxsize) and the new value will be inserted at the rear end of the queue. • If front != 0 and rear = max - 1, it means that queue is not full, then set the value of rear to 0 and insert the new element there.
  • 49.  There are two cases in which the element cannot be inserted: • When front ==0 && rear == max-1, which means that front is at the first position of the Queue and rear is at the last position of the Queue. • front== rear + 1;
  • 50. Insertion In Circular Queue  Algorithm  Step 1: IF (Front = 0 & REAR = MAX – 1) or(front=rear+1) Write OVERFLOW  Step 2: IF FRONT = -1 and REAR = -1 SET FRONT = REAR = 0 ELSE IF REAR = MAX - 1 and FRONT ! = 0 SET REAR = 0  ELSE SET REAR = REAR + 1 [END OF IF]  Step 3: Set QUEUE[REAR] = NUM
  • 51. Deletion In Circular Queue  Step 1: IF FRONT = -1 Write " UNDERFLOW " Goto Step 4 [END of IF]  Step 2: SET VAL = QUEUE[FRONT]
  • 52.  Step 3: IF FRONT = REAR SET FRONT = REAR = -1 ELSE IF FRONT = MAX -1 SET FRONT = 0 ELSE SET FRONT = FRONT + 1 [END of IF] [END OF IF]  Step 4: Exit