CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Stacks, Queues, and Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
Linked Lists: deleting...
Queues and Linked Lists
Data Structures ADT List
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.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
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.
CSCE 3110 Data Structures & Algorithm Analysis
§1 Abstract Data Type (ADT)
Data Structure Lecture-5
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.
Data Structures: Doubly Linked List1 Doubly linked list l The linear linked list is accessed from the first node each time we want to insert or delete.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
1 Linked Lists Gordon College Prof. Brinton. 2 Linked List Basics Why use? 1.Efficient insertion or deletion into middle of list. (Arrays are not efficient.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
Chapter 4.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
CS Data Structures Chapter 4 Lists. Dynamically Linked Stacks and Queues (1/8)  When several stacks and queues coexisted, there was no efficient.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
CS Data Structures Chapter 4 Lists.
Chapter 4 Lists Fundamentals of Data Structures in C Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer
Reference: Vinu V Das, Principles of Data Structures using C and C++
CHAPTER 41 LISTS All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures.
Introduction to Data Structure
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
CSCE 3110 Data Structures & Algorithm Analysis Arrays and Lists.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
Polynomial Addition using Linked lists
Data Structure in C Transparency No. 4-1 Copyright(c) 1997, Sungkyunkwan University Chapter #4: LISTS Fundamentals of Data Structure in C Horowitz, Sahni.
 Array ◦ sequential representation ◦ some operation can be very time-consuming (data movement) ◦ size of data must be predefined ◦ static storage allocation.
Chapter 4 (cont.) Additional Lists Operations Circular Lists The link field of the last node points to the first node in the list... BATCATFATWAT.
Copyright Networking Laboratory Chapter 4. LISTS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More Linking.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
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.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Software Learning Resource Service Platform CHAPTER 4 鏈結串列 (Linked List) 1.
Queues Manolis Koubarakis Data Structures and Programming Techniques 1.
Programming Application in Civil Engineering
Dynamic Allocation Review Structure and list processing
Lectures linked lists Chapter 6 of textbook
Linked List Stacks, Linked List Queues, Dequeues
Data Structure Dr. Mohamed Khafagy.
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures 7th Week
EEE2108: Programming for Engineers Chapter 4. Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
CSCE 3110 Data Structures & Algorithm Analysis
Linked List (Part I) Data structure.
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Data Structures Chapter 4: Linked Lists.
Presentation transcript:

CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.

Applications of Linked Lists Stacks and Queues Implemented with Linked Lists Polynomials Implemented with Linked Lists Remember the array based implementation? Hint: two strategies, one efficient in terms of space, one in terms of running time

Operations on Linked Lists Running time? insert, remove traverse, swap How to reverse the elements of a list?

typedef struct poly_node *poly_pointer; typedef struct poly_node { int coef; int expon; poly_pointer next; }; poly_pointer a, b, c; coef expon link Representation Polynomials

a b null Example

a b d a->expon == b->expon a b d a->expon expon Adding Polynomials

a b a->expon > b->expon d 2 8 Adding Polynomials (cont’d)

poly_pointer padd(poly_pointer a, poly_pointer b) { poly_pointer front, rear, temp; int sum; rear =(poly_pointer)malloc(sizeof(poly_node)); if (IS_FULL(rear)) { fprintf(stderr, “The memory is full\n”); exit(1); } front = rear; while (a && b) { switch (COMPARE(a->expon, b->expon)) { Adding Polynomials (cont’d)

case -1: /* a->expon expon */ attach(b->coef, b->expon, &rear); b= b->next; break; case 0: /* a->expon == b->expon */ sum = a->coef + b->coef; if (sum) attach(sum,a->expon,&rear); a = a->next; b = b->link; break; case 1: /* a->expon > b->expon */ attach(a->coef, a->expon, &rear); a = a->next; } for (; a; a = a->next) attach(a->coef, a->expon, &rear); for (; b; b=b->next) attach(b->coef, b->expon, &rear); rear->next = NULL; temp = front; front = front->next; free(temp); return front; }

(1)coefficient additions 0  additions  min(m, n) where m (n) denotes the number of terms in A (B). (2)exponent comparisons extreme case e m-1 > f m-1 > e m-2 > f m-2 > … > e 0 > f 0 m+n-1 comparisons (3)creation of new nodes extreme case m + n new nodes summaryO(m+n) Analysis

Other types of lists: Circular lists Doubly linked lists

ptr circular Circularly linked lists

X 1  X 2  X 3  a What happens when we insert a node to the front of a circular linked list? Problem: move down the whole list. Operations in a circular list X 1  X 2  X 3  a Keep a pointer points to the last node. A possible solution:

void insertFront (pnode* ptr, pnode node) { /* insert a node in the list with head (*ptr)->next */ if (IS_EMPTY(*ptr)) { *ptr= node; node->next = node; /* circular link */ } else { node->next = (*ptr)->next; (1) (*ptr)->next = node; (2) } X 1  X 2  X 3  (1) (2) ptr Insertion

int length(pnode ptr) { pnode temp; int count = 0; if (ptr) { temp = ptr; do { count++; temp = temp->next; } while (temp!=ptr); } return count; } List length

Doubly Linked List Keep a pointer to the next and the previous element in the list typedef struct node *pnode; typedef struct node { char data [4]; pnode next; pnode prev; }

Doubly Linked List Keep a header and trailer pointers (sentinels) with no content header.prev = null; header.next = first element trailer.next = null; trailer.prev = last element Update pointers for every operation performed on the list How to remove an element from the tail of the list ?

Doubly Linked List – removeLast() Running time? How does this compare to simply linked lists?

Doubly Linked List insertFirst swapElements

Previous scheme: represent each non-NULL element as a tuple (row, column, value) New scheme: each column (row): a circular linked list with a head node Revisit Sparse Matrices

down right value row col a ij ij entry node a ij Nodes in the Sparse Matrix

Circular linked list Linked Representation

#define MAX_SIZE 50 /* size of largest matrix */ typedef struct mnode *pmnode; typedef struct mnode { int row; int col; int value; pmnode next, down; }; Sparse Matrix Implementation

Operations on Sparse Matrices Transpose Addition Multiplication