CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Stacks, Queues, and Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
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.
Chapter 4 Lists Pointers Singly Linked Lists
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
CSCI2100B Linked List Jeffrey
§1 Abstract Data Type (ADT)
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
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.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
 Balancing Symbols 3. Applications
CS Data Structures II Review COSC 2006 April 14, 2017
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
Stacks, Queues, and Deques
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.
DS.L.1 Lists, Stacks, and Queues (Review) Chapter 3 Overview Abstract Data Types Linked Lists, Headers, Circular Links Cursor (Array) Implementation Stacks.
Data Structures and Algorithm Analysis Lecturer: Jing Liu Homepage:
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types (ADTs) Lists Stacks Queues.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
Copyright Networking Laboratory Chapter 4. LISTS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
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.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
Data Structure & Algorithms
Click to edit Master text styles Stacks Data Structure.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
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.
CSE 1342 Programming Concepts
Linked List :: Basic Concepts
Stacks and Queues Chapter 4.
CE 221 Data Structures and Algorithms
CS 201 Data Structures and Algorithms
Data Structures and Algorithms
Stack and Queue APURBO DATTA.
EEE2108: Programming for Engineers Chapter 4. Linked Lists
Chapter 16-2 Linked Structures
Stacks, Queues, and Deques
Further Data Structures
CSC 143 Queues [Chapter 7].
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Data Structures and Algorithms
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Stacks, Queues, and Deques
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Abstract Data Types Stacks CSCI 240
Presentation transcript:

CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1,  2,   , INT_MAX, INT_MIN }  { , , , , ,    } 【 Definition 】 An Abstract Data Type (ADT) is a data type that is organized in such a way that the specification on the objects and specification of the operations on the objects are separated from the representation of the objects and the implementation on the operations. 1/18

§2 The List ADT Objects: ( item 0, item 1, , item N  1 ) Operations:  Finding the length, N, of a list.  Printing all the items in a list.  Making an empty list.  Finding the k- th item from a list, 0  k < N.  Inserting a new item after the k -th item of a list, 0  k < N.  Deleting an item from a list.  Finding next of the current item from a list.  Finding previous of the current item from a list.  ADT: Why after? 2/18

1. Simple Array implementation of Lists §2 The List ADT array[ i ] = item i  MaxSize has to be estimated. AddressContent array+iitem i array+i+1item i+1 …… Sequential mapping  Find_Kth takes O(1) time.  Insertion and Deletion not only take O(N) time, but also involve a lot of data movements which takes time. 3/18

§2 The List ADT 2. Linked Lists AddressDataPointer SUN QIAN ZHAO LI NULL Head pointer ptr = 0110 ZHAOQIANSUNLI ptr NULL Initialization: typedef struct list_node *list_ptr; typedef struct list_node { char data [ 4 ] ; list_ptr next ; } ; list_ptr ptr ; To link ‘ZHAO’ and ‘QIAN’: list_ptr N1, N2 ; N1 = (list_ptr)malloc(sizeof(struct list_node)); N2 = (list_ptr)malloc(sizeof(struct list_node)); N1->data = ‘ZHAO’ ; N2->data = ‘QIAN’ ; N1->next = N2 ; N2->next = NULL ; ptr = N1 ; ZHAOQIAN ptr NULL Locations of the nodes may change on different runs. 4/18

§2 The List ADT a1a1 ptr NULL aiai a i+1 anan... Insertion node b temp  temp->next = node->next  node->next = temp Question: What will happen if the order of the two steps is reversed? Question: How can we insert a new first item?  takes O(1) time. 5/18

§2 The List ADTDeletion a1a1 ptr NULL aiai a i+1 anan... b pre node  pre->next = node->next  free ( node ) b node Question: How can we delete the first node from a list? Answer: We can add a dummy head node to a list.  takes O(1) time. Read programs in Figures for detailed implementations of operations. 6/18

§2 The List ADT Doubly Linked Circular Lists Don’t we have enough headache already? Why do we need the doubly linked lists? Suppose you have a list 1->2->3->…->m. Now how would you get the m-th node? I’ll go from the 1st node to the m-th node. Then you are asked to find its previous node m  1? Uhhh... Then I’ll have to go from the 1st node again. But hey, why do I wantta find the previous node? Why do you ask me? :-) Maybe you wantta delete the m-th node? typedef struct node *node_ptr ; typedef struct node { node_ptr llink; element item; node_ptr rlink; } ; item  llinkrlink ptr = ptr->llink->rlink = ptr->rlink->llink A doubly linked circular list with head node: item1  item2  item3  H An empty list :  H 7/18

§2 The List ADT Two Applications  The Polynomial ADT Objects : P ( x ) = a 1 x e1 +  + a n x en ; a set of ordered pairs of where a i is the coefficient and e i is the exponent. e i are nonnegative integers. Operations:  Finding degree, max { e i }, of a polynomial.  Addition of two polynomials.  Subtraction between two polynomials.  Multiplication of two polynomials.  Differentiation of a polynomial. 8/18

§2 The List ADT 【 Representation 1 】 typedef struct { int CoeffArray [ MaxDegree + 1 ] ; int HighPower; } *Polynomial ; I like it! It’s easy to implement most of the operations, such as Add and Multiplication. Really? What is the time complexity for finding the product of two polynomials of degree N 1 and N 2 ? O( N 1 *N 2 ) What’s wrong with that? Try to apply MultPolynomial (p.53) On P 1 (x) = 10x x and P 2 (x) = 3x 1990  2x x+5 -- now do you see my point? 9/18

§2 The List ADT Given: We represent each term as a node ExponentCoefficient Next  Declaration: typedef struct poly_node *poly_ptr; struct poly_node { int Coefficient ; /* assume coefficients are integers */ int Exponent; poly_ptr Next ; } ; typedef poly_ptr a ; /* nodes sorted by exponent */ am1am1 em1em1  a0a0 e0e0 NULL …… a 【 Representation 2 】 Home work: p Add two polynomials 10/18

§2 The List ADT  Multilists 〖 Example 〗 Suppose that we have 40,000 students and 2,500 courses. Print the students’ name list for each courses, and print the registered classes’ list for each student. 【 Representation 1 】 int Array[40000][2500]; 11/18

§2 The List ADT 【 Representation 2 】 S1S2S3S4S5C1 C2 C3 C4 Home work: Self-study the sparse matrix representation on p.50 12/18

§2 The List ADT 3. Cursor Implementation of Linked Lists ( no pointer )  Features that a linked list must have: a)The data are stored in a collection of structures. Each structure contains data and a pointer to the next structure. b)A new structure can be obtained from the system’s global memory by a call to malloc and released by a call to free. Cursor Space Element Next 012S-1 … 123S-10 Note: The interface for the cursor implementation (given in Figure 3.27 on p. 52) is identical to the pointer implementation (given in Figure 3.6 on p. 40). 13/18

§2 The List ADT Element Next 25S S-1 … malloc: p p = CursorSpace[ 0 ].Next ; CursorSpace[ 0 ].Next = CursorSpace[ p ].Next ; x Element Next 25S S-1 … p free(p): 2 CursorSpace[ p ].Next = CursorSpace[ 0 ].Next ; p CursorSpace[ 0 ].Next = p ; Note: The cursor implementation is usually significantly faster because of the lack of memory management routines. Read operation implementations given in Figures Home work: p Reverse a singly linked list 14/18

§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 made at the top only. Objects: A finite ordered list with zero or more elements. Operations:  Int IsEmpty( Stack S );  Stack CreateStack( );  DisposeStack( Stack S );  MakeEmpty( Stack S );  Push( ElementType X, Stack S );  ElementType Top( Stack S );  Pop( Stack S ); Note: A Pop (or Top ) on an empty stack is an error in the stack ADT. Push on a full stack is an implementation error but not an ADT error. Note: A Pop (or Top ) on an empty stack is an error in the stack ADT. Push on a full stack is an implementation error but not an ADT error. 15/18

§3 The Stack ADT 2. Implementations  Linked List Implementation (with a header node) NULL Element    Push:  TmpCell->Next = S->Next  S->Next = TmpCell  Top:  FirstCell = S->Next  S->Next = S->Next->Next  free ( FirstCell ) return S->Next->Element  S  Element TmpCell  S  Pop: Element FirstCell  S But, the calls to malloc and free are expensive. Easy! Simply keep another stack as a recycle bin. 16/18

§3 The Stack ADT  Array Implementation struct StackRecord { int Capacity ; /* size of stack */ int TopOfStack; /* the top pointer */ /* ++ for push, -- for pop, -1 for empty stack */ ElementType *Array; /* array for stack elements */ } ; Note:  The stack model must be well encapsulated. That is, no part of your code, except for the stack routines, can attempt to access the Array or TopOfStack variable.  Error check must be done before Push or Pop ( Top ). Note:  The stack model must be well encapsulated. That is, no part of your code, except for the stack routines, can attempt to access the Array or TopOfStack variable.  Error check must be done before Push or Pop ( Top ). Read Figures for detailed implementations of stack operations. 17/18

§3 The Stack ADT 3. Applications  Balancing Symbols Check if parenthesis ( ), brackets [ ], and braces { } are balanced. Algorithm { Make an empty stack S; while (read in a character c) { if (c is an opening symbol) Push(c, S); else if (c is a closing symbol) { if (S is empty) { ERROR; exit; } else { /* stack is okay */ if (Top(S) doesn’t match c) { ERROR, exit; } else Pop(S); } /* end else-stack is okay */ } /* end else-if-closing symbol */ } /* end while-loop */ if (S is not empty) ERROR; } T( N ) = O ( N ) where N is the length of the expression. This is an on-line algorithm. 18/18