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.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Application of linked list Geletaw S.. Linked list can be used in various environment. Some of the common examples are Creation of a polynomial Polynomial.
Stacks, Queues, and Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
CS201: Data Structures and Discrete Mathematics I Linked Lists, Stacks and Queues.
CSCI2100B Linked List Jeffrey
§1 Abstract Data Type (ADT)
Stack & Queues COP 3502.
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)
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
CSCE 3110 Data Structures & Algorithm Analysis Queues Reading: Chap. 3 Weiss.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
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
Topic 15 Implementing and Using Stacks
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks, Queues & Deques CSC212.
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
Topic 15 Implementing and Using Stacks
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
The Stack Alexander G. Chefranov CMPE-231 Spring 2012.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Data Structures and Algorithm Analysis Lecturer: Jing Liu Homepage:
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
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.
CSC 211 Data Structures Lecture 23
Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types (ADTs) Lists Stacks Queues.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Stacks And Queues Chapter 18.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
 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.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
CS 201 Data Structures and Algorithms
Data Structures and Algorithms
Stacks and Queues.
Cinda Heeren / Geoffrey Tien
Stacks.
Stacks Chapter 4.
CMSC 341 Lecture 5 Stacks, Queues
Stack and Queues Stack implementation using Array
Data Structures and Algorithms
Stacks and Queues 1.
Stacks and Queues CSE 373 Data Structures.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
LINEAR DATA STRUCTURES
Presentation transcript:

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 ADT and its applications

3.1 Abstract Data Type Modular program Advantages: (1) debugging (2)team work (3) easy to modify

3.1 Abstract Data Type Abstract data type a set of operations mathematical abstractions just as integer, boolean Basic idea write once, use many Any changes are transparent to the other modules

3.2 List ADT Review on linked list Common errors memory allocation segmentation violation check for NULL pointer free memory

3.2 List ADT Doubly linked list struct node {int Number; struct node *left; struct node *right; }

3.2.1 List ADT: Example1 Single-variable Polynomials typedef struct {int CoeffArray[MaxDegree+1]; int HighPower } *polynomial

3.2.1 List ADT: Example 1 Initialize a polynomial void ZeroPolynomial(Polynomial Poly) {int j; for (j = 0; j < MaxDegree; j++) poly->CoeffArray[j] = 0; Poly->HighPower = 0; }

3.2.1 List ADT: Example 1 Add two polynomials void AddPolynomial(const Polynomial Poly1, const Polynomial Poly2, Polynomial PolySum) {int j; ZeroPolynomial(PolySum); PolySum->HighPower = Max(Poly1-> HighPower, Poly2 -> HighPower); for (j=PolySum->HighPower; j>=0; j--) PolySum->CoeffArray[j] = Poly1-> CoeffArray [j] + Poly2-> CoeffArray[j]; }

3.2.1 List ADT: Example 1 Multiply two polynomials void MultPolynomial(const Polynomial Poly1, const Polynomial Poly2, Polynomial PolyProd) {int j,k; ZeroPolynomial(PolyProd); PolySum->HighPower = Poly1-> HighPower+ Poly2 -> HighPower; for (j=0; j=Poly1->HighPower; j++) for (k=0; k=Poly2->HighPower; k++) PolyProd->CoeffArray[j+k] += Poly1-> CoeffArray [j]* Poly2-> CoeffArray[k]; }

3.2.1 List ADT: Example 1 Multiply two polynomials: limitation Consider the following situation P 1 (X) = 10 X X P 2 (X) = 3X X X +5 Most of the time is spent multiplying zeros

3.2.1 List ADT: Example 1 Multiply two polynomials: better structure struct node { int Coefficient; int Exponent; struct node *Next; }

3.2.1 List ADT: Example 1 Linked list representation of the previous structure

3.2.2 List ADT: Example 2 Multilists A university with 40,000 students and 2,500 subjects needs to generate 2 reports 1. Lists of registration for each class 2. Classes that each student registered. Implementation: construct 2D array (40Kx2K5) = 100M entries if each student takes 3 subjects => only 120K entries (~0.1% of 100M) => waste of resources.

3.2.2 List ADT: Example 2 Multilists

3.3 Stack ADT Stack model

3.3 Stack ADT LIFO structure Access from the top Basic Operations –empty (s) –pop (s) –push (s, i) –top (s)

3.3.1Implementation of stack using Linked List struct Node; typedef struct Node *PtrToNode; typedef PtrToNode Stack; struct Node {ElementType Element; PtrToNode Next; }

3.3.1Implementation of stack using Linked List int IsEmpty(Stack S); Stack CreatStack(void); void DisposeStack(Stack S); void MakeEmpty(Stack S); void Push(ElementType X, Stack S); void Pop(Stack S); ElementType Top(Stack S);

3.3.1Implementation of stack using Linked List Test for an empty stack int Isempty(Stack S) { return (S Next == NULL) }

3.3.1Implementation of stack using Linked List Create an empty stack stack CreatStack(void) {Stack S; S=malloc(sizeof(struct Node)); if (S==NULL) FatalError(Out of space!!!); MakeEmpty(S) return(S); } void MakeEmpty(Stack S) { if (S==NULL) printf(Error message!); else while (!IsEmpty(S)) pop(S) }

3.3.1Implementation of stack using Linked List Push onto a stack void Push(ElementType X, Stack S) {PtrtoNode TmpCell; TmpCell = malloc(sizeof(struct Node)); if (TmpCell == NULL) fatalError(message); else {TmpCell Element = X; TmpCell Next = S Next; S Next = TmpCell; } }

3.3.1Implementation of stack using Linked List Return top element in a stack ElementType Top(Stack S) {if (!IsEmpty(S)) return (S Next Element); Error(Empty stack); return 0; }

3.3.1Implementation of stack using Linked List Pop from a stack void Pop(Stack S) {PtrToNode FirstCell; if (ISEmpty(S); Error (Empty stack); else {FirstCell = S Next; S Next =S Next Next; free(FirstCell);} }

3.3.2 Implementation of stack using Array Struct StackRecord { int Capacity; int TopOfStack; ElementType *Array; } #define STACKSIZE 100 Struct StackRecord { int top; ElementType Array[STACKSIZE]; }

3.3.2 Implementation of stack using Array Test for empty stack empty (stack *ps) { return (ps top == -1) }

3.3.2 Implementation using Array Pop top element from stack int pop (stack *ps) { int x if (empty (ps)) printf (%s\n, stack underflow!); else {x = ps array [ps top]; (ps top)--; return (x); } }

3.3.2 Implementation using Array Push an element onto the stack push (stack *ps, int x) { if (ps top == STACKSIZE – 1) printf (%s\n, stack overflow!); else { (ps top)++; ps items [ps top] = x; } }

3.3.3 Variable types of stack elements #define STACKSIZE 100 #define INTGR1 #define FLT2 #define STRING3 struct stackelement { int etype;/* element type */ union { int ival; float fval; char *pval; } element;/* end union */ }

3.3.3 Variable Types of Stack Elements To access the top element struct stack s; struct stackelement se; se = s.items [s.top]; switch (se.etype) { case INTGR: printf (%d\n, se.ival); break; case FLT: printf (%d\n, se.fval); break; case STRING: printf (%s\n, se.pval); }

3.3.4 Applications Balancing Symbols Make an empty stack. Read characters until end of file. If the character is an opening symbol, push it onto stack. If it is a closing symbol, then if the stack is empty, report an error. Otherwise, pop the stack. If the symbol popped is not the corresponding opening symbol, then report an error. At the end of file, if the stack is not empty, report an error.

3.3.4 Applications Postfix Expressions *1.06 = or postfix notation of (4.99* *1.06) is * * + Why do we need postfix notation?

3.3.4 Application: Postfix How does postfix work? e.g * *

3.3.4 Application : Postfix

3.3.4 Applications Converting infix expressions into postfix InfixA * B + C * D PostfixA B * C D * +

3.3.4 Application: infix postfix Stack -> Input Postfix StringA **A *BA B ++A B * +CA B * C + **A B * C + *DA B * C D pop stackA B * C D * +

3.3.4 Application: infix postfix opstk = the empty stack; while (not end of input) {symb = next input character; if (symb is an operand) add symb in the postfix string; else { while (!empty (opstk) && prcd (stack (opstk), symb)) { topsymb = pop (opstk); add topsymb to postfix string; } push (opstk, symb); }

3.3.4 Application: infix postfix /* output any remaining operators */ while (!empty (opstk)) { topsymb = pop (opstk); add topsymb to the postfix string; }

3.3.4 Application: infix postfix With parentheses Stack -> InputPostfix String( (AA ( ++A ( +BA B )A B + **A B + *CA B + C pop stackA B + C *

3.3.4 Application: infix postfix Please refer to infix2postfix.doc for implementation.

3.4 Queue ADT Ordered collections of data items –Delete item at front of the queue –Insert item at rear of the queue –A FIFO structure A B CD E front rear

3.4 Queue ADT Basic operations (1) insert (q, x) (2) remove (q, x) (3) empty (q)

3.4 Queue ADT Array implementation #define MAXQUEUE 100 struct queue { int items [MAXQUEUE]; int front, rear, length; } q;

3.4 Queue ADT void insert (q, x) { q.length++; q.rear++; q.items [q.rear] = x; }

3.4 Queue ADT void remove (q, x) { x = q.items [q.front]; q.front++; q.length--; }

3.4 Queue ADT Problem: May run out of rooms (1) Keep front always at 0 by shifting the contents up the queue, but the computer solution is inefficient (2) Use a circular queue (wrap around & use a length variable to keep track of the queue length)

3.4 Queue ADT insert (queue *pq, int x) {if ((pq length) == MAXQUEUE) /* check for overflow */ printf (Queue overflow); else { (pq length)++;/* make room for a new element */ if ((pq rear) == MAXQUEUE - 1) pq rear = 0; else (pq rear)++; pq items [pq rear] = x; /* check if queue is originally empty */ if ((pq length) == 1) pq front = pq rear; } }

3.4 Queue ADT void empty (queue *pq) { if ((pq length) == 0) return (TRUE); else return (FALSE); }

3.4 Queue ADT void remove (queue *pq) { if (empty (pq)) printf (Queue underflow); else if (pq front == MAXQUEUE - 1) { pq front = 0; return (pq items [MAXQUEUE - 1]); } else { (pq front)++; return (pq items [(pq front) - 1]); } }

3.4 Applications of Queues Print jobs Computer networks OS Real-life waiting lines