210 likes | 741 Views
Doubly linked list. 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.
E N D
Doubly linked list • 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. • An item of the doubly linked list is defined as: struct item{ int val; // could be any type struct item *next, *previous; } NODEPTR *item; Data Structures: Doubly Linked List
Get node & free node operations NODEPTR getnode(void) { NODEPTR p; P = (NODEPTR) malloc(sizeof(struct item)); return(p); } void freenode(NODEPTR p) { free(p); } Data Structures: Doubly Linked List
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 */ Data Structures: Doubly Linked List
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 */ Data Structures: Doubly Linked List
Application: Sparse matrix • What is a sparse matrix? A matrix of most of its entries are zeros. • What is the size of memory for a matrix of size 100x100? • If there are many matrices? • Is there a way of representing the Sparse matrix? Data Structures: Doubly Linked List
Sparse Matrix Representation • Each node must contain 5 fields: • Value • Row • Column • Next row • Next column • struct item { int val; int col, row; struct item *nextcol, *nextrow; }; • typedef item *NODEPTR; Data Structures: Doubly Linked List
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; } Data Structures: Doubly Linked List
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; } } Data Structures: Doubly Linked List
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;} Data Structures: Doubly Linked List
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 < q2->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; } } } Data Structures: Doubly Linked List
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"); } } Data Structures: Doubly Linked List