CSCE 3110 Data Structures & Algorithm Analysis

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

EENG212 Algorithms and Data Structures
Linked Lists.
Linked Lists: deleting...
Queues and 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.
Linear Lists – Linked List Representation
Data Structures ADT List
Chapter 4 Lists Pointers Singly Linked Lists
C and Data Structures Baojian Hua
LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
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.
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
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Linked List Variations
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 …
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.
§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.
Doubly-linked list library.
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 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.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
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.
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++
Introduction to Data Structure
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, 
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
 Array ◦ sequential representation ◦ some operation can be very time-consuming (data movement) ◦ size of data must be predefined ◦ static storage allocation.
Copyright Networking Laboratory Chapter 4. LISTS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
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.
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
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)
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
Linked List Stacks, Linked List Queues, Dequeues
More Linking Up with Linked Lists
Data Structure Dr. Mohamed Khafagy.
CSCE 3110 Data Structures & Algorithm Analysis
Abstract Data Types Polynomials CSCI 240
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures 7th Week
EEE2108: Programming for Engineers Chapter 4. Linked Lists
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures Chapter 4: Linked Lists.
Presentation transcript:

CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea http://www.cs.unt.edu/~rada/CSCE3110 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?

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

Example a 3 14 2 8 1 0 null b 8 14 -3 10 10 6 null

Adding Polynomials 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 a->expon == b->expon d 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 -3 10 a->expon < b->expon d

Adding Polynomials (cont’d) 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 -3 10 2 8 d a->expon > b->expon

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)) {

case -1: /* a->expon < b->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->next; case 1: /* a->expon > b->expon */ attach(a->coef, a->expon, &rear); a = a->next; } for (; a; a = a->next) for (; b; b=b->next) rear->next = NULL; temp = front; front = front->next; free(temp); return front;

Analysis (1) coefficient additions 0  additions  min(m, n) where m (n) denotes the number of terms in A (B). (2) exponent comparisons extreme case em-1 > fm-1 > em-2 > fm-2 > … > e0 > f0 m+n-1 comparisons (3) creation of new nodes m + n new nodes summary O(m+n)

Attach a Term void attach(float coefficient, int exponent, poly_pointer *ptr) { /* create a new node attaching to the node pointed to by ptr. ptr is updated to point to this new node. */ poly_pointer temp; temp = (poly_pointer) malloc(sizeof(poly_node)); if (IS_FULL(temp)) { fprintf(stderr, “The memory is full\n”); exit(1); } temp->coef = coefficient; temp->expon = exponent; (*ptr)->next = temp; *ptr = temp;

Other types of lists: Circular lists Doubly linked lists

Circularly linked lists circular list vs. chain ptr 3 14 2 8 1 0 avail ptr temp avail ...

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

Insertion ptr (2) (1) 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) X1  X2  X3  ptr (2) (1)

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

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

Revisit Sparse Matrices 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

Nodes in the Sparse Matrix down col row right entry node value i j aij aij

Linked Representation 4 4 2 11 1 1 1 12 5 2 1 -4 3 3 -15 Circular linked list

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