§1 Abstract Data Type (ADT)

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
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.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 3: Linked List Tutor: Angie Hui
Pointers.
Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Linked Lists CS-212 Dick Steflik. Linked Lists A sequential collection of information Can be unordered; i.e. in no specific order Can be ordered; may.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Stacks, Queues, and Linked Lists
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Linked Lists: deleting...
Linked Lists Chapter 4.
Linear Lists – Array Representation
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.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Linear Lists – Linked List Representation
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
Chapter 4 Lists Pointers Singly Linked Lists
C and Data Structures Baojian Hua
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.
Abstract Data Types and Algorithms
LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
Data Structures Using C++
CS Data Structures Chapter 4 Lists. Chain (1/3) Chain: Chain: A singly linked list in which the last node has a null link A singly linked list in.
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
LIST PROCESSING.
CHP-5 LinkedList.
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Data structure is concerned with the various ways that data files can be organized and assembled. The structures of data files will strongly influence.
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
CSCI2100B Linked List Jeffrey
CSE Lecture 12 – Linked Lists …
1 Symbol Tables Chapter Sedgewick. 2 Symbol Tables Searching Searching is a fundamental element of many computational tasks looking up a name.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
§2 Binary Trees Note: In a tree, the order of children does not matter. But in a binary tree, left child and right child are different. A B A B andare.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
CS Data Structures Chapter 4 Lists. Dynamically Linked Stacks and Queues (1/8)  When several stacks and queues coexisted, there was no efficient.
Linked List C and Data Structures Baojian Hua
CS Data Structures Chapter 4 Lists.
Introduction to Data Structure
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.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
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.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
Data Structure & Algorithms
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
LINKED LISTS.
Linked List :: Basic Concepts
UNIT-3 LINKED LIST.
EEE2108: Programming for Engineers Chapter 4. Linked Lists
Programmazione I a.a. 2017/2018.
Chapter 16-2 Linked Structures
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Further Data Structures
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.
Figure 4.1 a) A linked list of integers; b) insertion; c) deletion.
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Presentation transcript:

§1 Abstract Data Type (ADT) 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/14

 Finding the length, N, of a list. §2 The List ADT  ADT: Objects: ( item0, item1,  , itemN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. Why after? 2/14

 MaxSize has to be estimated.  Find_Kth takes O(1) time. §2 The List ADT 1. Simple Array implementation of Lists array[ i ] = itemi Address Content array+i itemi array+i+1 itemi+1 …… Sequential mapping  MaxSize has to be estimated.  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/14

Locations of the nodes may change on different runs. §2 The List ADT 2. Linked Lists ptr Address Data Pointer 0010 0011 0110 1011 SUN QIAN ZHAO LI NULL Head pointer ptr = 0110 ZHAO QIAN SUN LI NULL 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 ; Locations of the nodes may change on different runs. Initialization: typedef struct list_node *list_ptr; typedef struct list_node { char data [ 4 ] ; list_ptr next ; } ; list_ptr ptr ; ZHAO QIAN ptr NULL 4/14

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

 takes O(1) time. Deletion a1 ai ai+1 an ...  pre->next = §2 The List ADT Deletion pre a1 ptr NULL ai ai+1 an ... b b node  pre->next = node->next  free ( node ) node Question: How can we delete the first node from a list? Answer: We can add a dummy head node to a list. Read programs in Figures 3.6-3.15 for detailed implementations of operations. 6/14

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

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

int CoeffArray [ MaxDegree + 1 ] ; int HighPower; } *Polynomial ; §2 The List ADT 【Representation 1】 typedef struct { int CoeffArray [ MaxDegree + 1 ] ; int HighPower; } *Polynomial ; Try to apply MultPolynomial (p.47) On P1(x) = 10x1000+5x14+1 and P2(x) = 3x19902x1492+11x+5 -- now do you see my point? 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 N1 and N2? O( N1*N2 ) What’s wrong with that? 9/14

Home work: p.79 3.6 Add two polynomials §2 The List ADT 【Representation 2】 Given: We represent each term as a node Exponent Coefficient Next  Home work: p.79 3.6 Add two polynomials 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1 em1  a0 e0 NULL …… a 10/14

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

Self-study the sparse matrix §2 The List ADT 【Representation 2】 S1 S2 S3 S4 S5 Home work: Self-study the sparse matrix representation on p.50 C1 C2 C3 C4 12/14

3. Cursor Implementation of Linked Lists (no pointer) §2 The List ADT 3. Cursor Implementation of Linked Lists (no pointer)  Features that a linked list must have: The data are stored in a collection of structures. Each structure contains data and a pointer to the next structure. 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 1 2 S-1 … … 1 2 3 S-1 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/14

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