Download presentation

Presentation is loading. Please wait.

Published bySilas Layng Modified over 2 years ago

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

2
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

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

4
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

5
a b null Example

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

7
a b a->expon > b->expon d 2 8 Adding Polynomials (contd)

8
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 (contd)

9
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->next; 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; }

10
(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

11
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; } Attach a Term

12
Other types of lists: Circular lists Doubly linked lists

13
ptr avail... avail temp circular list vs. chain Circularly linked lists

14
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:

15
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

16
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

17
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; }

18
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 ?

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

20
Doubly Linked List insertFirst swapElements

21
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

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

23
Circular linked list Linked Representation

24
#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 Sparse Matrix Implementation

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google