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

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
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.
Pointers.
Lists CS 3358.
COMP171 Fall 2005 Lists.
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
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.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Linked Lists CENG 213 Data Structures.
Data Structures: A Pseudocode Approach with C
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
EE Data Structures and Algorithms N Radhakrishnan Assistant Professor Anna University, Chennai.
Lists, Stacks, and Queues Abstract Data Types (ADTs) The List ADT The Stack ADT The Queue ADT.
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.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
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.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
LINKED LISTS.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
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.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
CSE 1342 Programming Concepts
Lecture 6 of Computer Science II
Unit – I Lists.
Cpt S 122 – Data Structures Abstract Data Types
Linked List :: Basic Concepts
Introduction to Linked Lists
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 5 (continued)
Data Structure Interview Question and Answers
Data structures and algorithms
CE 221 Data Structures and Algorithms
Data Structure and Algorithms
More Linking Up with Linked Lists
Lists CS 3358.
Data Structure Dr. Mohamed Khafagy.
UNIT-3 LINKED LIST.
Data Structures and Algorithms Introduction
Chapter 4 Linked Lists.
Data Structures Interview / VIVA Questions and Answers
CSCI 3333 Data Structures Linked Lists.
LINKED LISTS CSCD Linked Lists.
Introduction to Linked Lists
Linked Lists.
Stacks, Queues, and Deques
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Further Data Structures
Linked Lists.
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.
Lists CSE 373 Data Structures.
A Robust Data Structure
Linked Lists Chapter 4.
Linked Lists Chapter 5 (continued)
Data Structures & Algorithms
Lists 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.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

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 (ADTs) One of the basic rules concerning programming is to break the program down into modules. Each module is a logical unit and does a specific job. Its size is kept small by calling other modules. Modularity has several advantages. (1) It is much easier to debug small routines than large routines; (2) It is easier for several people to work on a modular program simultaneously; (3) A well-written modular program places certain dependencies in only one routing, making changes easier.

Abstract Data Types (ADTs) An abstract data type (ADT) is a set of operations. Abstract data types are mathematical abstractions; nowhere in an ADT’s definition is there any mention of how the set of operations is implemented. Objects such as lists, sets, and graphs, along with their operations, can be viewed as abstract data types, just as integers, reals, and booleans are data types. Integers, reals, and booleans have operations associated with them, and so do ADTs.

Abstract Data Types (ADTs) The basic idea is that the implementation of the operations related to ADTs is written once in the program, and any other part of the program that needs to perform an operation on the ADT can do so by calling the appropriate function. If for some reason implementation details need to be changed, it should be easy to do so by merely changing the routings that perform the ADT operations. There is no rule telling us which operations must be supported for each ADT; this is a design decision.

The List ADT The form of a general list: A1, A2, A3, …, AN; The size of this list is N; An empty list is a special list of size 0; For any list except the empty list, we say that Ai+1 follows (or succeeds) Ai (i<N) and that Ai-1 precedes Ai (i>1); The first element of the list is A1, and the last element is AN. We will not define the predecessor of A1 or the successor of AN. The position of element Ai in a list is i.

The List ADT There is a set of operations that we would like to perform on the list ADT: PrintList MakeEmpty Find: return the position of the first occurrence of a key Insert and Delete: insert and delete some key from some position in the list FindKth: return the element in some position Next and Previous: take a position as argument and return the position of the successor and predecessor

The List ADT Example: The list is 34, 12, 52, 16, 13 Find(52) Insert(X, 3) Delete(52) The interpretation of what is appropriate for a function is entirely up to the programmer.

Simple Array Implementation of Lists All these functions about lists can be implemented by using an array. PrintList MakeEmpty Find Insert Delete Next Previous

Simple Array Implementation of Lists Disadvantages: An estimate of the maximum size of the list is required, even if the array is dynamically allocated. Usually this requires a high overestimate, which wastes considerable space. Insertion and deletion are expensive. For example, inserting at position 0 requires first pushing the entire array down one spot to make room. Because the running time for insertions and deletions is so slow and the list size must be known in advance, simple arrays are generally not used to implement lists.

Linked Lists In order to avoid the linear cost of insertion and deletion, we need to ensure that the list is not stored contiguously, since otherwise entire parts of the list will need to be moved. A1 A2 A3 A4 A5 A linked list The linked list consists of a series of structures, which are not necessarily adjacent in memory. Each structure contains the element and a pointer to a structure containing its successor. We call this the Next pointer. The last cell’s Next pointer points to NULL;

Linked list with actual pointer values Linked Lists If P is declared to be a pointer to a structure, then the value stored in P is interpreted as the location, in main memory, where a structure can be found. A field of that structure can be accessed by P->FieldName, where FieldName is the name of the field we wish to examine. A1 800 A2 712 A3 992 A4 692 A5 1000 800 712 992 692 Linked list with actual pointer values In order to access this list, we need to know where the first cell can be found. A pointer variable can be used for this purpose.

Linked Lists To execute PrintList(L) or Find(L, Key), we merely pass a pointer to the first element in the list and then traverse the list by following the Next pointers. The Delete command can be executed in one pointer change. A1 A2 A3 A4 A5 The Insert command requires obtaining a new cell from the system by using a malloc call and then executing two pointer maneuvers. A1 A2 A3 A4 A5 X

Linked Lists There are several places where you are likely to go wrong: (1) There is no really obvious way to insert at the front of the list from the definitions given; (2) Deleting from the front of the list is a special case, because it changes the start of the list; careless coding will lose the list; (3) A third problem concerns deletion in general. Although the pointer moves above are simple, the deletion algorithm requires us to keep track of the cell before the one that we want to delete.

Linked list with a header Linked Lists One simple change solves all three problems. We will keep a sentinel node, referred to an a header or dummy node. Header A1 A2 A3 A4 A5 Linked list with a header L To avoid the problems associated with deletions, we need to write a routing FindPrevious, which will return the position of the predecessor of the cell we wish to delete. If we use a header, then if we wish to delete the first element in the list, FindPrevious will return the position of the header.

Doubly Linked Lists Sometimes it is convenient to traverse lists backwards. The solution is simple. Merely add an extra field to the data structure, containing a pointer to the previous cell. The cost of this is an extra link, which adds to the space requirement and also doubles the cost of insertions and deletions because there are more pointers to fix. A1 A2 A3 A4 A5 A doubly linked list How to implement doubly linked lists?

Circularly Linked Lists A popular convention is to have the last cell keep a pointer back to the first. This can be done with or without a header. If the header is present, the last cell points to it. It can also be done with doubly linked lists, the first cell’s previous pointer points to the last cell. A1 A2 A3 A4 A5 A double circularly linked list

Example – The Polynomial ADT If most of the coefficients Ai are nonzero, we can use a simple array to store the coefficients. Write codes to calculate F(X) based on array.

Example – The Polynomial ADT If most of the coefficients Ai are zero, the implementation based on array is not efficient, since most of the time is spent in multiplying zeros. 10 1000 5 14 1 P1 An alternative is to use a singly linked list. Each term in the polynomial is contained in one cell, and the cells are sorted in decreasing order of exponents.

Type declarations for array implementation of the polynomial ADT typedef struct { int coeff_array[ MAX_DEGREE+1 ]; unsigned int high_power; } *POLYNOMIAL; © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Procedure to initialize a polynomial to zero void zero_polynomial( POLYNOMIAL poly ) { unsigned int i; for( i=0; i<=MAX_DEGREE; i++ ) poly->coeff_array[i] = 0; poly->high_power = 0; } © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Procedure to add two polynomials void add_polynomial( POLYNOMIAL poly1, POLYNOMIAL poly2, POLYNOMIAL poly_sum ) { int i; zero_polynomial( poly_sum ); poly_sum->high_power = max( poly1->high_power, poly2->high_power); for( i=poly_sum->high_power; i>=0; i-- ) poly_sum->coeff_array[i] = poly1->coeff_array[i] + poly2->coeff_array[i]; } © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Procedure to multiply two polynomials void mult_polynomial( POLYNOMIAL poly1, POLYNOMIAL poly2, POLYNOMIAL poly_prod ) { unsigned int i, j; zero_polynomial( poly_prod ); poly_prod->high_power = poly1->high_power + poly2->high_power; if( poly_prod->high_power > MAX_DEGREE ) © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Cont… error("Exceeded array size"); else for( i=0; i<=poly->high_power; i++ ) for( j=0; j<=poly2->high_power; j++ ) poly_prod->coeff_array[i+j] += poly1->coeff_array[i] * poly2->coeff_array[j]; } © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Linked list representations of two polynomials © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

Type declaration for linked list implementation of the Polynomial ADT typedef struct node *node_ptr; struct node { int coefficient; int exponent; node_ptr next; } ; typedef node_ptr POLYNOMIAL; /* keep nodes sorted by exponent */ © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED