Top Banner
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea http://www.cs.unt.edu/~rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.
24

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

Mar 30, 2015

Download

Documents

Silas Layng
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

CSCE 3110Data Structures & Algorithm Analysis

Rada Mihalceahttp://www.cs.unt.edu/~rada/CSCE3110

More on lists. Circular lists. Doubly linked lists.

Page 2: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

Applications of Linked Lists

Stacks and Queues Implemented with Linked ListsPolynomials Implemented with Linked Lists

Remember the array based implementation?

• Hint: two strategies, one efficient in terms of space, one in terms of running time

Page 3: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

Operations on Linked Lists

Running time?insert, removetraverse, swap

How to reverse the elements of a list?

Page 4: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

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

A x a x a x a xm

e

m

e em m( ) ...

1 2 01 2 0

coef expon link

Representation

Polynomials

Page 5: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

3 14 2 8 1 0a

8 14 -3 10 10 6b

a x x 3 2 114 8

b x x x 8 3 1014 10 6

null

null

Example

Page 6: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

3 14 2 8 1 0a

8 14 -3 10 10 6b

11 14d

a->expon == b->expon

3 14 2 8 1 0a

8 14 -3 10 10 6b

11 14d

a->expon < b->expon-3 10

Adding Polynomials

Page 7: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

3 14 2 8 1 0a

8 14 -3 10 10 6b

11 14

a->expon > b->expon

-3 10d

2 8

Adding Polynomials (cont’d)

Page 8: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

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)

Page 9: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

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

Page 10: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

(1) coefficient additions0 additions min(m, n)where m (n) denotes the number of terms in A (B).

(2) exponent comparisonsextreme caseem-1 > fm-1 > em-2 > fm-2 > … > e0 > f0

m+n-1 comparisons(3) creation of new nodes

extreme casem + n new nodessummary O(m+n)

Analysis

Page 11: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

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

Page 12: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

Other types of lists:

Circular listsDoubly linked lists

Page 13: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

3 14 2 8 1 0ptr

ptr

avail ...

avail

temp

circular list vs. chain

Circularly linked lists

Page 14: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

X1 X2 X3 a

What happens when we insert a node to the front of a circularlinked list?

Problem: move down the whole list.

Operations in a circular list

X1 X2 X3 a

Keep a pointer points to the last node.

A possible solution:

Page 15: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

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

(1)(2) ptr

Insertion

Page 16: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

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

Page 17: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

Doubly Linked List

Keep a pointer to the next and the previous element in the listtypedef struct node *pnode;

typedef struct node { char data [4]; pnode next;

pnode prev; }

Page 18: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

Doubly Linked List

Keep a header and trailer pointers (sentinels) with no content

header.prev = null; header.next = first elementtrailer.next = null; trailer.prev = last element

Update pointers for every operation performed on the listHow to remove an element from the tail of the list ?

Page 19: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

Doubly Linked List – removeLast()

Running time?How does this compare to simply linked lists?

Page 20: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

Doubly Linked List

insertFirstswapElements

Page 21: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

15000

0040

00012

01100

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

Page 22: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

down right

value

row col

aij

i j

entry node

aij

Nodes in the Sparse Matrix

Page 23: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

4 4

1 012

2 1-4

0 211

3 3-15

1 15

Circular linked list

Linked Representation

Page 24: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 More on lists. Circular lists. Doubly linked lists.

#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