SlideShare a Scribd company logo
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top of the stack Nodes are removed from the front (top) of the list Straight-forward linked list implementation push  and  pop  can be implemented fairly easily, e.g. assuming that  head  is a reference to the node at the front of the list public void  push( int  x){ // Make a new node whose next reference is // the existing list Node newNode = new Node(x, top); top = newNode;  // top points to new node }
List Stack Example Java Code Stack st =  new  Stack(); st.push(6);  top 6
List Stack Example Java Code Stack st =  new  Stack(); st.push(6);  st.push(1);  top 6 1
List Stack Example Java Code Stack st =  new  Stack(); st.push(6);  st.push(1);  st.push(7); top 6 1 7
List Stack Example Java Code Stack st =  new  Stack(); st.push(6);  st.push(1);  st.push(7);  st.push(8);  top 6 1 7 8
List Stack Example Java Code Stack st =  new  Stack(); st.push(6);  st.push(1);  st.push(7);  st.push(8);  st.pop();  top 6 1 7 8
List Stack Example Java Code Stack st =  new  Stack(); st.push(6);  st.push(1);  st.push(7);  st.push(8);  st.pop();  top 6 1 7
Stack: ADT List Implementation Push() and pop() either at the beginning or at the end of ADT List at the beginning:   public   void  push(Object newItem) { list.add(1, newItem); }  // end push public  Object pop() { Object temp = list.get(1); list.remove(1); return  temp; }  // end pop
Stack: ADT List Implementation Push() and pop() either at the beginning or at the end of ADT List at the end:   public   void  push(Object newItem) { list.add(list.size()+1, newItem); }  // end push public  Object pop() { Object temp = list.get(list.size()); list.remove(list.size()); return  temp; }  // end pop
Stack: ADT List Implementation Push() and pop() either at the beginning or at the end of ADT List Efficiency depends on implementation of ADT List (not guaranteed) On other hand: it was very fast to implement (code is easy, unlikely that errors were introduced when coding).
Applications of Stacks Call stack (recursion). Searching networks, traversing trees (keeping a track where we are). Examples: Checking balanced expressions Recognizing palindromes Evaluating algebraic expressions
Simple Applications of the ADT Stack: Checking for Balanced Braces A stack can be used to verify whether a program contains balanced braces An example of balanced braces abc{defg{ijk}{l{mn}}op}qr An example of unbalanced braces abc{def}}{ghij{kl}m abc{def}{ghij{kl}m
Checking for Balanced Braces Requirements for balanced braces Each time you encounter a “}”, it matches an already encountered “{” When you reach the end of the string, you have matched each “{”
Checking for Balanced Braces Figure 7-3 Traces of the algorithm that checks for balanced braces
Evaluating Postfix Expressions A postfix (reverse Polish logic) calculator Requires you to enter postfix expressions Example: 2 3 4 + * When an operand is entered, the calculator Pushes it onto a stack When an operator is entered, the calculator Applies it to the top two operands of the stack Pops the operands from the stack Pushes the result of the operation on the stack
Evaluating Postfix Expressions Figure 7-8 The action of a postfix calculator when evaluating the expression 2 * (3 + 4)
Evaluating Postfix Expressions Pseudo code: int  evaluate(String expression) { Stack stack= new  Stack();  // creaty empty stack while  ( true ) { String c=expression.getNextItem(); if  (c==ENDOFLINE) return  stack.pop(); if  (c is operand) stack.push(c); else  {  // operation int  operand2=stack.pop(); int  operand1=stack.pop(); stack.push(execute(c,operand1,operand2)); } } }
Queues A queue is a data structure that only allows items to be inserted at the end and removed from the front “ Queue” is the British word for a line (or line-up) Queues are  FIFO  (First In First Out) data structures – “fair” data structures
Using a Queue
What Can You Use a Queue For? Processing inputs and outputs to screen (console) Server requests Instant messaging servers queue up incoming messages Database requests Print queues One printer for dozens of computers Operating systems use queues to schedule CPU jobs Simulations
Queue Operations A queue should implement (at least) these operations: enqueue  – insert an item at the back of the queue dequeue  – remove an item from the front  peek  – return the item at the front of the queue without removing it Like stacks it is assumed that these operations will be implemented efficiently That is, in constant time
Queue: Array Implementation First consider using an array as the underlying structure for a queue, one plan would be to Make the back of the queue the current size of the queue (i.e., the number of elements stored) Make the front of the queue index 0 Inserting an item can be performed in constant time But removing an item would require shifting all elements in the queue to the left which is  too slow ! Therefore we need to find another way
An Array-Based Implementation Figure 8-8 a) A naive array-based implementation of a queue; b) rightward drift can cause the queue to appear full
Ad

More Related Content

What's hot (20)

Stack
StackStack
Stack
Seema Sharma
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
ForwardBlog Enewzletter
 
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
Praveen Vishwakarma
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
vaibhav2910
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
tameemyousaf
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queue
Amit Vats
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
Sheikh Monirul Hasan
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
data structure, stack, stack data structure
data structure, stack, stack data structuredata structure, stack, stack data structure
data structure, stack, stack data structure
pcnmtutorials
 
Stacks
StacksStacks
Stacks
sardorbek mamazhanov
 
Stacks
StacksStacks
Stacks
sweta dargad
 
Stacks
StacksStacks
Stacks
Sadaf Ismail
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
Soumen Santra
 
Stack data structure
Stack data structureStack data structure
Stack data structure
rogineojerio020496
 
Stack
StackStack
Stack
Zaid Shabbir
 
Stack application
Stack applicationStack application
Stack application
Student
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
Yaksh Jethva
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
Archie Jamwal
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
Afaq Mansoor Khan
 
Stack project
Stack projectStack project
Stack project
Amr Aboelgood
 

Viewers also liked (12)

Stack and queue
Stack and queueStack and queue
Stack and queue
Katang Isip
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
SANTOSH RATH
 
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
Rai University
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
stack & queue
stack & queuestack & queue
stack & queue
manju rani
 
Stack & queue
Stack & queueStack & queue
Stack & queue
Siddique Ibrahim
 
Queue and stacks
Queue and stacksQueue and stacks
Queue and stacks
grahamwell
 
Linked stacks and queues
Linked stacks and queuesLinked stacks and queues
Linked stacks and queues
Ramzi Alqrainy
 
Stack Applications
Stack ApplicationsStack Applications
Stack Applications
Kulachi Hansraj Model School Ashok Vihar
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
anooppjoseph
 
Linked list
Linked listLinked list
Linked list
akshat360
 
Linked to ArrayList: the full story
Linked to ArrayList: the full storyLinked to ArrayList: the full story
Linked to ArrayList: the full story
José Paumard
 
Ad

Similar to Stack linked list (20)

stack.ppt
stack.pptstack.ppt
stack.ppt
ssuserec1395
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.pptlecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
partho5958
 
introduction of the Stacks and Queues.pptx
introduction of the  Stacks and Queues.pptxintroduction of the  Stacks and Queues.pptx
introduction of the Stacks and Queues.pptx
kavitashingi123
 
Stack & Queue in Data Structure and Algorithms
Stack & Queue in Data Structure and AlgorithmsStack & Queue in Data Structure and Algorithms
Stack & Queue in Data Structure and Algorithms
Virgo Lay
 
Stack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.ppt
Stack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.pptStack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.ppt
Stack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.ppt
aamirali1061a
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
SajalFayyaz
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
MouDhara1
 
DSA_Unit3_ Stacks and Queues using array (1).pptx
DSA_Unit3_ Stacks and Queues  using array  (1).pptxDSA_Unit3_ Stacks and Queues  using array  (1).pptx
DSA_Unit3_ Stacks and Queues using array (1).pptx
nandinigujarathi9
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
Abscddnddmdkwkkstack implementation.pptx
Abscddnddmdkwkkstack implementation.pptxAbscddnddmdkwkkstack implementation.pptx
Abscddnddmdkwkkstack implementation.pptx
zainshahid3040
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
haaamin01
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
Ssankett Negi
 
7 stacksqueues
7 stacksqueues7 stacksqueues
7 stacksqueues
ashish bansal
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
AliRaza899305
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 
04 stacks
04 stacks04 stacks
04 stacks
Rajan Gautam
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.pptlecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
partho5958
 
introduction of the Stacks and Queues.pptx
introduction of the  Stacks and Queues.pptxintroduction of the  Stacks and Queues.pptx
introduction of the Stacks and Queues.pptx
kavitashingi123
 
Stack & Queue in Data Structure and Algorithms
Stack & Queue in Data Structure and AlgorithmsStack & Queue in Data Structure and Algorithms
Stack & Queue in Data Structure and Algorithms
Virgo Lay
 
Stack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.ppt
Stack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.pptStack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.ppt
Stack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.ppt
aamirali1061a
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
SajalFayyaz
 
DSA_Unit3_ Stacks and Queues using array (1).pptx
DSA_Unit3_ Stacks and Queues  using array  (1).pptxDSA_Unit3_ Stacks and Queues  using array  (1).pptx
DSA_Unit3_ Stacks and Queues using array (1).pptx
nandinigujarathi9
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
Abscddnddmdkwkkstack implementation.pptx
Abscddnddmdkwkkstack implementation.pptxAbscddnddmdkwkkstack implementation.pptx
Abscddnddmdkwkkstack implementation.pptx
zainshahid3040
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
haaamin01
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 
Ad

Recently uploaded (20)

antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 

Stack linked list

  • 1. Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top of the stack Nodes are removed from the front (top) of the list Straight-forward linked list implementation push and pop can be implemented fairly easily, e.g. assuming that head is a reference to the node at the front of the list public void push( int x){ // Make a new node whose next reference is // the existing list Node newNode = new Node(x, top); top = newNode; // top points to new node }
  • 2. List Stack Example Java Code Stack st = new Stack(); st.push(6); top 6
  • 3. List Stack Example Java Code Stack st = new Stack(); st.push(6); st.push(1); top 6 1
  • 4. List Stack Example Java Code Stack st = new Stack(); st.push(6); st.push(1); st.push(7); top 6 1 7
  • 5. List Stack Example Java Code Stack st = new Stack(); st.push(6); st.push(1); st.push(7); st.push(8); top 6 1 7 8
  • 6. List Stack Example Java Code Stack st = new Stack(); st.push(6); st.push(1); st.push(7); st.push(8); st.pop(); top 6 1 7 8
  • 7. List Stack Example Java Code Stack st = new Stack(); st.push(6); st.push(1); st.push(7); st.push(8); st.pop(); top 6 1 7
  • 8. Stack: ADT List Implementation Push() and pop() either at the beginning or at the end of ADT List at the beginning: public void push(Object newItem) { list.add(1, newItem); } // end push public Object pop() { Object temp = list.get(1); list.remove(1); return temp; } // end pop
  • 9. Stack: ADT List Implementation Push() and pop() either at the beginning or at the end of ADT List at the end: public void push(Object newItem) { list.add(list.size()+1, newItem); } // end push public Object pop() { Object temp = list.get(list.size()); list.remove(list.size()); return temp; } // end pop
  • 10. Stack: ADT List Implementation Push() and pop() either at the beginning or at the end of ADT List Efficiency depends on implementation of ADT List (not guaranteed) On other hand: it was very fast to implement (code is easy, unlikely that errors were introduced when coding).
  • 11. Applications of Stacks Call stack (recursion). Searching networks, traversing trees (keeping a track where we are). Examples: Checking balanced expressions Recognizing palindromes Evaluating algebraic expressions
  • 12. Simple Applications of the ADT Stack: Checking for Balanced Braces A stack can be used to verify whether a program contains balanced braces An example of balanced braces abc{defg{ijk}{l{mn}}op}qr An example of unbalanced braces abc{def}}{ghij{kl}m abc{def}{ghij{kl}m
  • 13. Checking for Balanced Braces Requirements for balanced braces Each time you encounter a “}”, it matches an already encountered “{” When you reach the end of the string, you have matched each “{”
  • 14. Checking for Balanced Braces Figure 7-3 Traces of the algorithm that checks for balanced braces
  • 15. Evaluating Postfix Expressions A postfix (reverse Polish logic) calculator Requires you to enter postfix expressions Example: 2 3 4 + * When an operand is entered, the calculator Pushes it onto a stack When an operator is entered, the calculator Applies it to the top two operands of the stack Pops the operands from the stack Pushes the result of the operation on the stack
  • 16. Evaluating Postfix Expressions Figure 7-8 The action of a postfix calculator when evaluating the expression 2 * (3 + 4)
  • 17. Evaluating Postfix Expressions Pseudo code: int evaluate(String expression) { Stack stack= new Stack(); // creaty empty stack while ( true ) { String c=expression.getNextItem(); if (c==ENDOFLINE) return stack.pop(); if (c is operand) stack.push(c); else { // operation int operand2=stack.pop(); int operand1=stack.pop(); stack.push(execute(c,operand1,operand2)); } } }
  • 18. Queues A queue is a data structure that only allows items to be inserted at the end and removed from the front “ Queue” is the British word for a line (or line-up) Queues are FIFO (First In First Out) data structures – “fair” data structures
  • 20. What Can You Use a Queue For? Processing inputs and outputs to screen (console) Server requests Instant messaging servers queue up incoming messages Database requests Print queues One printer for dozens of computers Operating systems use queues to schedule CPU jobs Simulations
  • 21. Queue Operations A queue should implement (at least) these operations: enqueue – insert an item at the back of the queue dequeue – remove an item from the front peek – return the item at the front of the queue without removing it Like stacks it is assumed that these operations will be implemented efficiently That is, in constant time
  • 22. Queue: Array Implementation First consider using an array as the underlying structure for a queue, one plan would be to Make the back of the queue the current size of the queue (i.e., the number of elements stored) Make the front of the queue index 0 Inserting an item can be performed in constant time But removing an item would require shifting all elements in the queue to the left which is too slow ! Therefore we need to find another way
  • 23. An Array-Based Implementation Figure 8-8 a) A naive array-based implementation of a queue; b) rightward drift can cause the queue to appear full
  翻译: