17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Chapter 3: Lists, Stacks and Queues Concept of Abstract Data Type (ADTS) How to efficiently perform operations on list Stack ADT and its applications Queue.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
 Balancing Symbols 3. Applications
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Data Structures and Algorithm Analysis Lecturer: Jing Liu Homepage:
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
Data Structures – Week #4 Queues. January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List.
CE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
CS505 Data Structures and Algorithms
CS 201 Data Structures and Algorithms
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
CC 215 Data Structures Queue ADT
CE 221 Data Structures and Algorithms
Chapter 15 Lists Objectives
CS 201 Data Structures and Algorithms
Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Queue data structure.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Principles of Computing – UFCFA3-30-1
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Queues.
Data Structures and Database Applications Stacks in C#
Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
ADT list.
Stacks and Queues CSE 373 Data Structures.
CE 221 Data Structures and Algorithms
Data Structures and Algorithms
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Stacks and Queues 1.
Abstract Data Type Abstract Data Type as a design tool
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
CSE 373 Data Structures Lecture 6
CE 221 Data Structures and Algorithms
Stacks and Queues CSE 373 Data Structures.
CS210- Lecture 6 Jun 13, 2005 Announcements
CSE 373 Data Structures Lecture 6
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Data Structures – Week #4
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Presentation transcript:

17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Stack ADT

Stack ADT A stack is a list with the restriction that insertions and deletions can be performed in only one position, namely, the end of the list, called the top. The fundamental operations on a stack are Push, which is equivalent to an insert, and Pop, which deletes the most recently inserted element. The most recently inserted element can be examined prior to performing a Pop by use of the Top routine.

Stack ADT A Pop or Top on an empty stack is generally considered an error in the stack ADT. Running out of space when performing a Push is an implementation error but not an ADT error. Stacks are sometimes known as LIFO (last in, first out) lists.

Stack model: input to a stack is by push, output is by pop © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Stack ADT Stack model: only the top element is accessible Top 7 9 4 2 3 6 Stack model: only the top element is accessible

Implementation of Stacks Since a stack is a list, any list implementation will do. We will give two popular implementations. One uses pointers and the other uses an array. No matter in which case, if we use good programming principles, the calling routines do not need to know which method is being used.

Linked List Implementation of Stacks We perform a Push by inserting at the front of the list We perform a Pop by deleting the element at the front of the list A Top operation merely examines the element at the front of the list, returning its value.

Type declaration for linked list implementation of the stack ADT typedef struct node *node_ptr; struct node { element_type element; node_ptr next; }; typedef node_ptr STACK; /* Stack implementation will use a header. */ © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Routine to test whether a stack is empty-linked list implementation int is_empty( STACK S ) { return( S->next == NULL ); } © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Routine to create an empty stack-linked list implementation STACK create_stack( void ) { STACK S; S = (STACK) malloc( sizeof( struct node ) ); if( S == NULL ) fatal_error("Out of space!!!"); return S; } © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Cont… void make_null( STACK S ) { if( S != NULL ) S->next = NULL; else error("Must use create_stack first"); } © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Routine to push onto a stack-linked list implementation void push( element_type x, STACK S ) { node_ptr tmp_cell; tmp_cell = (node_ptr) malloc( sizeof ( struct node ) ); if( tmp_cell == NULL ) fatal_error("Out of space!!!"); else © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Cont… { tmp_cell->element = x; tmp_cell->next = S->next; S->next = tmp_cell; } © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Routine to return top element in a stack--linked list implementation element_type top( STACK S ) { if( is_empty( S ) ) error("Empty stack"); else return S->next->element; } © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Routine to give top element and pop a stack--array implementation void pop( STACK S ) { node_ptr first_cell; if( is_empty( S ) ) error("Empty stack"); else first_cell = S->next; S->next = S->next->next; free( first_cell ); } © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

The Queue ADT Like stacks, queues are lists. With a queue, however, insertion is done at one end, whereas deletion is performed at the other end. The basic operations on a queue are Enqueue, which inserts an element at the end of the list (called the rear), and Dequeue, which deletes (and returns) the element at the start of the list (known as the front).

Model of a queue © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Linked List Implementation of Queues Front Header …… Rear /

Linked List Implementation of Queues Front Empty Queue / Reart Front Enqueue x x / Reart Front Enqueue y x y / Reart Front Dequeue x x y / Reart

Example Applications When jobs are submitted to a printer, they are arranged in order of arrival. Every real-life line is a queue. For instance, lines at ticket counters are queues, because service is first-come first-served. A whole branch of mathematics, known as queueing theory, deals with computing, probabilistically, how long users expect to wait on a line, how long the line gets, and other such questions.

Homework of Chapter 3 Exercises: 3.2 (Don’t need to analyze the running time.) 3.3 3.4 3.5 3.21 3.25

Exercises 1, 6, 8, 10, 12