info = x; q -> next = p -> next; q -> previous = p; p->next=q; q->next->previous = q; } /* end Insert after */"> info = x; q -> next = p -> next; q -> previous = p; p->next=q; q->next->previous = q; } /* end Insert after */">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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<n ;c++) { q = getnode(); q->col= c; q->row =-1; q->val = 0; q->nextrow = q; p->nextcol= q; p = q; } p->nextcol = mp; p = mp; for(r=0; r < n; r++) { q = getnode(); q->row= r; q->col =-1; q->val = 0; q->nextcol = q; p->nextrow= q; p = q; } p->nextrow = mp; return mp; }

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<n; r++){ for(c=0;c<n;c++){ printf("Enter Number %d %d\n", r, c); scanf("%d", &v); p = findabove(m, c); if(v) cp = insertafter(cp, p, v); } rp = rp->nextrow; cp=rp->nextcol; } }

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<n; r++){ for(c=0;c<n;c++){ if (cp->col == c) {printf("%d ", cp->val); cp=cp->nextcol;} else printf("%d ",0); } p = p->nextrow; cp = p->nextcol; printf("\n"); } }


Download ppt "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."

Similar presentations


Ads by Google