Download presentation

Presentation is loading. Please wait.

Published byPriscilla Ketch Modified over 2 years ago

1
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 an item. To minimize the amount of time needed to access the list items we use doubly linked list. So that the list can be accessed in both directions. l An item of the doubly linked list is defined as: struct item{ int val; // could be any type struct item *next, *previous; } NODEPTR *item;

2
Data Structures: Doubly Linked List2 Get node & free node operations NODEPTR getnode(void) { NODEPTR p; P = (NODEPTR) malloc(sizeof(struct item)); return(p); } void freenode(NODEPTR p) { free(p); }

3
Data Structures: Doubly Linked List3 Linked List – Insert, cont. void insafter(NODEPTR p, int x) { NODEPTR q; if (p == NULL) { printf("void insertion\n"); exit(1); } q = getnode(); q -> info = x; q -> next = p -> next; q -> previous = p; p->next=q; q->next->previous = q; } /* end Insert after */

4
Data Structures: Doubly Linked List4 Linked List – Delete, cont. DeleteAfter void deleteafter(NODEPTR p, int *px) { NODEPTR q; if (p == NULL) || (p -> next == NULL)) { printf("void deletion\n"); exit(1); } q = p -> next; *px = q -> info; p -> next = q -> next; q->next->previous = p; freenode(q); } /* end delete after */

5
Data Structures: Doubly Linked List5 Application: Sparse matrix l What is a sparse matrix? A matrix of most of its entries are zeros. l What is the size of memory for a matrix of size 100x100? l If there are many matrices? l Is there a way of representing the Sparse matrix?

6
Data Structures: Doubly Linked List6 Sparse Matrix Representation l Each node must contain 5 fields: n Value n Row n Column n Next row n Next column l struct item { int val; int col, row; struct item *nextcol, *nextrow; }; l typedef item *NODEPTR;

7
Data Structures: Doubly Linked List7 Create a new matrix Its size is: nxn NODEPTR create(int n) { int c, r; NODEPTR mp, cp, rp, p, q; mp = getnode(); mp->col = -1; mp->row = -1; mp->val = 0; mp->nextcol = mp; mp->nextrow = mp; p = mp; for(c=0; c

8
Data Structures: Doubly Linked List8 Initialize a matrix void initiliaze(NODEPTR m, int n) { int v, c, r; NODEPTR cp, rp, p; rp = m->nextrow; cp = rp; for(r=0; r

9
Data Structures: Doubly Linked List9 Find above & insert after NODEPTR findabove(NODEPTR m, int c){ NODEPTR rp, cp; for (cp = m->nextcol; cp->col != c; cp=cp->nextcol); for(rp = cp; rp->nextrow != cp; rp = rp->nextrow); return rp;} NODEPTR insertafter(NODEPTR cp, NODEPTR rp, int v){ NODEPTR q, p; q = getnode(); q->val = v; q->col = rp->col; q->row = cp->row; q->nextcol = cp->nextcol; q->nextrow = rp->nextrow; rp->nextrow = q;cp->nextcol = q; return q;}

10
Data Structures: Doubly Linked List10 Add function void add(NODEPTR m1, NODEPTR m2, NODEPTR m) { NODEPTR q1, p1, q2, p2, q, p; int v; for(p1=m1->nextrow,p2=m2->nextrow,p=m->nextrow; p1!= m1 && p2 != m2; p1=p1->nextrow,p2=p2->nextrow, p=p->nextrow) {for (q1=p1->nextcol,q2=p2->nextcol, q=p->nextcol; p1!=q1 && p2!=q2; ) if (q1->col == q2->col){q=insertafter(q, findabove(m, q1->col),q1->val+q2->val); q1=q1->nextcol; q2=q2->nextcol;} else if (q1->col col) {q=insertafter(q, findabove(m, q1->col),q1->val); q1=q1->nextcol;} else if (q1->col > q2->col) {q=insertafter(q, findabove(m, q2->col),q2->val); q2=q2->nextcol;} while (p1!=q1) { q=insertafter(q, findabove(m, q1->col),q1->val); q1=q1->nextcol;} while(p2!=q2) { q=insertafter(q, findabove(m, q2->col),q2->val); q2=q2->nextcol; } }

11
Data Structures: Doubly Linked List11 The print function void print(NODEPTR m, int n) { int v, c, r; NODEPTR cp, rp, p; p = m->nextrow; cp = p->nextcol; for(r=0; r

Similar presentations

Presentation is loading. Please wait....

OK

Data Structure Lecture-5

Data Structure Lecture-5

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on media and networking Ppt on relations and functions for class 11th english Ppt on electrical power in india Ppt on media revolution entertainment Ppt on case study method Ppt on power system stability using facts devices Ppt on marie curie radioactivity Ppt on intelligent transportation systems Download ppt on cultural heritage of india Ppt on combination of resistances barb