CS235102 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.

Slides:



Advertisements
Similar presentations
Chapter 3: Linked List Tutor: Angie Hui
Advertisements

COMP171 Fall 2005 Lists.
Stacks, Queues, and Linked Lists
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
1/27 COP 3540 Data Structures with OOP Chapter 5 Linked Lists.
Linear Lists – Linked List Representation
Chapter 4 Lists Pointers Singly Linked Lists
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
CSCE 3110 Data Structures & Algorithm Analysis
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
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
§1 Abstract Data Type (ADT)
CSE Lecture 12 – Linked Lists …
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
M180: Data Structures & Algorithms in Java
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Chapter 4.
CS Data Structures Chapter 4 Lists. Dynamically Linked Stacks and Queues (1/8)  When several stacks and queues coexisted, there was no efficient.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
CS Data Structures Chapter 4 Lists.
Chapter 4 1. Why “linked lists” 2 IndexWord A[0]BAT A[1]CAT A[2]EAT … A[n]WAT Insert “FAT” ? Or delete something.
Reference: Vinu V Das, Principles of Data Structures using C and C++
Introduction to Data Structure
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
Linked List (Part II). Introduction  Definition of equivalence relation: A relation ≡ over a set S, is said to be an equivalence relation over S iff.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
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 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Copyright Networking Laboratory Chapter 4. LISTS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
Linked List Containers. Concatenate Example Concatenate(LinkedList listB): Add all of the elements of a second list to the end of the first list Concatenate(LinkedList.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More Linking.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Linked List Containers. Useful Linked List Add-Ons Are there new variables/changes to the lists as they have been defined that could make our jobs as.
Data Structure & Algorithms
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
Data Structures Using C, 2e
Lectures linked lists Chapter 6 of textbook
Prepared by, Jesmin Akhter, Lecturer, IIT, JU
More Linking Up with Linked Lists
Lecture - 6 On Data Structures
Data Structure Dr. Mohamed Khafagy.
Abstract Data Types Polynomials CSCI 240
UNIT-3 LINKED LIST.
Data Structures 7th Week
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stack and Queue APURBO DATTA.
EEE2108: Programming for Engineers Chapter 4. Linked Lists
Chapter 16-2 Linked Structures
DATA STRUCTURE QUEUE.
CS212D: Data Structures Week 5-6 Linked List.
Data Structures and Programming Techniques
Chapter 16 Linked Structures
CSCS-200 Data Structure and Algorithms
Data Structures Chapter 4: Linked Lists.
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Chapter 9 Linked Lists.
Presentation transcript:

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 which the last node has a null link Operations for chains Operations for chains Inverting a chain Inverting a chain For a list of length 1 nodes, the while loop is executed length times and so the computing time is linear or O(length). For a list of length 1 nodes, the while loop is executed length times and so the computing time is linear or O(length).... NULL × lead invert

Chain (2/3)... NULL trialmiddle lead Two extra pointers middle lead trial NULL

Chain (3/3) Concatenates two chains Concatenates two chains Concatenates two chains, ptr1 and ptr2. Concatenates two chains, ptr1 and ptr2. Assign the list ptr1 followed by the list ptr2. Assign the list ptr1 followed by the list ptr2. O(length of list ptr1) NULL ptr1 NULL ptr2 temp

Circularly Linked Lists (1/10) Circular Linked list Circular Linked list The link field of the last node points to the first node in the list. The link field of the last node points to the first node in the list. Example Example Represent a polynomial ptr = 3x 14 +2x 8 +1 as a circularly linked list. Represent a polynomial ptr = 3x 14 +2x 8 +1 as a circularly linked list. ptr

Circularly Linked Lists (2/10) Maintain an Available List Maintain an Available List We free nodes that are no longer in use so that we may reuse these nodes later We free nodes that are no longer in use so that we may reuse these nodes later We can obtain an efficient erase algorithm for circular lists, by maintaining our own list (as a chain) of nodes that have been freed. We can obtain an efficient erase algorithm for circular lists, by maintaining our own list (as a chain) of nodes that have been freed. Instead of using malloc and free, we now use get_node (program 4.13) and ret_node (program 4.14). Instead of using malloc and free, we now use get_node (program 4.13) and ret_node (program 4.14). avail... NULL List of freed nodes

Circularly Linked Lists (3/10) Maintain an Available List (contd) Maintain an Available List (contd) When we need a new node, we examine this list. When we need a new node, we examine this list. If the list is not empty, then we may use one of its nodes. If the list is not empty, then we may use one of its nodes. Only when the list is empty we do need to use malloc to create a new node. Only when the list is empty we do need to use malloc to create a new node.

Circularly Linked Lists (4/10) Maintain an Available List (contd) Maintain an Available List (contd) Insert ptr to the front of this list Let avail be a variable of type poly_pointer that points to the first node in our list of freed nodes. Let avail be a variable of type poly_pointer that points to the first node in our list of freed nodes. Henceforth, we call this list the available space list or avail list. Henceforth, we call this list the available space list or avail list. Initially, we set avail to NULL Initially, we set avail to NULL

Circularly Linked Lists (5/10) Maintain an Available List Maintain an Available List Erase a circular list in a fixed amount (constant) of time independent of the number of nodes in the list using cerase Erase a circular list in a fixed amount (constant) of time O(1) independent of the number of nodes in the list using cerase temp ptr NULL avail link chain

Circularly Linked Lists (6/10) We must handle the zero polynomial as a special case. To avoid it, we introduce a head node into each polynomial We must handle the zero polynomial as a special case. To avoid it, we introduce a head node into each polynomial each polynomial, zero or nonzero, contains one additional node. each polynomial, zero or nonzero, contains one additional node. The expon and coef fields of this node are irrelevant. The expon and coef fields of this node are irrelevant. Why ? So !

Circularly Linked Lists (7/10) For fit the circular list with head node representation For fit the circular list with head node representation We may remove the test for (*ptr) from cerase We may remove the test for (*ptr) from cerase Changes the original padd to cpadd Changes the original padd to cpadd /* head node */ /*a->expon=-1, so b->expont > -1 */ /* link to the first node */

Circularly Linked Lists (8/10) Operations for circularly linked lists Operations for circularly linked lists Question: What happens when we want to insert a new node at the front of the circular linked list ptr? Answer: move down the entire length of ptr. Possible Solution: ptr x1x1 x2x2 x3x3 x1x1 x2x2 x3x3

Circularly Linked Lists (9/10) x1x1 x2x2 x3x3 ptr node Insert a new node at the front of a circular list Insert a new node at the front of a circular list To insert node at the rear, we only need to add the additional statement *ptr = node to the else clause of insert_front To insert node at the rear, we only need to add the additional statement *ptr = node to the else clause of insert_front

Circularly Linked Lists (10/10) Finding the length of a circular list Finding the length of a circular list

Equivalence Relations (1/6) Reflexive Relation Reflexive Relation for any polygon x, x x (e.g., x is electrically equivalent to itself) for any polygon x, x x (e.g., x is electrically equivalent to itself) Symmetric Relation Symmetric Relation for any two polygons x and y, if x y, then y x. for any two polygons x and y, if x y, then y x. Transitive Relation Transitive Relation for any three polygons x, y, and z, if x y and y z, then x z. for any three polygons x, y, and z, if x y and y z, then x z. Definition: Definition: A relation over a set, S, is said to be an over S iff it is symmertric, reflexive, and transitive over S. A relation over a set, S, is said to be an equivalence relation over S iff it is symmertric, reflexive, and transitive over S. Example: Example: equal to relationship is an equivalence relation equal to relationship is an equivalence relation

Equivalence Relations (2/6) Example: Example: if we have 12 polygons numbered 0 through 11 if we have 12 polygons numbered 0 through , 3 1, 6 10, 8 9, 7 4, 6 8, 3 5, 2 11, 11 0 we can partition the twelve polygons into the following equivalence classes: we can partition the twelve polygons into the following equivalence classes: {0, 2, 4, 7, 11};{1, 3, 5};{6, 8, 9,10} Two phases to determine equivalence Two phases to determine equivalence First phase: the equivalence pairs (i, j) are read in and stored. First phase: the equivalence pairs (i, j) are read in and stored. Second phase: Second phase: we begin at 0 and find all pairs of the form (0, j). Continue until the entire equivalence class containing 0 has been found, marked, and printed. we begin at 0 and find all pairs of the form (0, j). Continue until the entire equivalence class containing 0 has been found, marked, and printed. Next find another object not yet output, and repeat the above process. Next find another object not yet output, and repeat the above process.

Equivalence Relation (3/6) Program to find equivalence classes Program to find equivalence classes void main(void){ short int out[MAX_SIZE]; node_pointer seq[MAX_SIZE]; node_pointer x, y, top; int i, j, n; printf(Enter the size (<=%d), MAX_SIZE); scanf(%d, &n); for(i=0; i<n; i++){ /*initialize seq and out */ out[i] = TRUE;seq[i] = NULL; } /* Phase 1 */ /* Phase 2 */ } typedef struct node *node_pointer; typedef struct node { int data; node_pointer link; }; #include #define MAX_SIZE24 #define IS_FULL(ptr)(!(ptr)) #define FALSE0 #define TRUE1

Equivalence Relations (4/6) Phase 1: Phase 1: read in and store the equivalence pairs Insert x to the top of lists seq[i] Insert x to the top of lists seq[j] (1)(2) [11] [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 4, 3 1, 6 10, 8 9, 7 4, 6 8, 3 5, 2 11, NULL NULL NULL

Equivalence Relations (5/6) Phase 2: begin at 0 and find all pairs of the form, where 0 and j are in the same equivalence class begin at 0 and find all pairs of the form, where 0 and j are in the same equivalence class by transitivity, all pairs of the form imply that k in the same equivalence class as 0 by transitivity, all pairs of the form imply that k in the same equivalence class as 0 continue this way until we have found, marked, and printed the entire equivalent class containing 0 continue this way until we have found, marked, and printed the entire equivalent class containing 0

Equivalence Relations (6/6) Phase 2 Phase 2 [11] [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0]4 NULL NULL NULL New class: 0 i= [0][1][2][3][4][5][6][7][8][9] [10][11] out: x top j=11 y

Doubly Linked Lists (1/4) Singly linked lists pose problems because we can move easily only in the direction of the links Singly linked lists pose problems because we can move easily only in the direction of the links Doubly linked list has at least three fields Doubly linked list has at least three fields left link field(llink), data field(item), right link field(rlink). left link field(llink), data field(item), right link field(rlink). The necessary declarations: The necessary declarations: typedef struct node *node_pointer; typedef struct node{ node_pointer llink; element item; node_pointer rlink; };... NULL ptr ?

Doubly Linked Lists (2/4) Sample Sample doubly linked circular with head node: (Figure 4.23) doubly linked circular with head node: (Figure 4.23) empty double linked circular list with head node (Figure 4.24) empty double linked circular list with head node (Figure 4.24) suppose that ptr points to any node in a doubly linked list, then: suppose that ptr points to any node in a doubly linked list, then: ptr = ptr -> llink -> rlink = ptr -> rlink -> llink ptr = ptr -> llink -> rlink = ptr -> rlink -> llink

Doubly Linked Lists (3/4) Insert node Insert node Head node llink item rlink New node node

Doubly Linked Lists (4/4) Delete node Delete node Head node llink item rlink deleted node