1 / 21

Data Structures More on lists. Circular lists. Doubly linked lists. Multi list

Data Structures More on lists. Circular lists. Doubly linked lists. Multi list. Souhad Daraghma. Applications of Linked Lists. Stacks and Queues Implemented with Linked Lists Polynomials Implemented with Linked Lists Remember the array based implementation?

lea
Download Presentation

Data Structures More on lists. Circular lists. Doubly linked lists. Multi list

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures More on lists. Circular lists. Doubly linked lists. Multi list Souhad Daraghma

  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. coef expon link Polynomials Representation typedef struct poly_node *poly_pointer; typedef struct poly_node { int coef; int expon; poly_pointer next; }; poly_pointer a, b, c;

  5. Example a null 1 0 3 14 2 8 b null 8 14 -3 10 10 6

  6. Adding Polynomials 2 8 1 0 3 14 a -3 10 10 6 8 14 b 11 14 a->expon == b->expon d 2 8 1 0 3 14 a -3 10 10 6 8 14 b a->expon < b->expon -3 10 11 14 d

  7. Adding Polynomials (cont’d) 2 8 1 0 3 14 a -3 10 10 6 8 14 b -3 10 11 14 2 8 d a->expon > b->expon

  8. Adding Polynomials (cont’d) 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)) {

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

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

  11. Other types of lists: • Circular lists • Doubly linked lists

  12. Circularly linked lists circular list vs. chain ptr 2 8 1 0 3 14 avail ptr temp avail ...

  13. X3 X2 X2 X1 X1 Operations in a circular list What happens when we insert a node to the front of a circular linked list? a Problem: move down the whole list. A possible solution: X3 a Keep a pointer points to the last node.

  14. X2 X1 Insertion 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) } } X3 ptr (2) (1)

  15. List length int length(pnode ptr) { pnode temp; int count = 0; if (ptr) { temp = ptr; do { count++; temp = temp->next; } while (temp!=ptr); } return count; }

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

  17. 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 ?

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

  19. Doubly Linked List • insertFirst • swapElements

  20.  Multilists 〖Example〗 Suppose that we have 40,000 students and 2,500 courses. Print the students’ name list for each courses, and print the registered classes’ list for each student. 【Representation 1】 int Array[40000][2500];

  21. S1 S2 S3 S4 S5 C1 C2 C3 C4 【Representation 2】

More Related