1 / 14

Today’s Agenda

Today’s Agenda. Lists Random Access vs. Sequential Access. Course Agenda. Modules Approach – Personal Programming Process  Data-Driven Programming  Dynamic Data Storage. Dynamic Data Storage. Lists – Random Access vs. Sequential Access Storage -

trygg
Download Presentation

Today’s Agenda

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. Today’s Agenda Lists Random Access vs. Sequential Access Sundar B. BITS, Pilani.

  2. Course Agenda Modules • Approach – Personal Programming Process  • Data-Driven Programming  • Dynamic Data Storage Sundar B. BITS, Pilani.

  3. Dynamic Data Storage • Lists – • Random Access vs. Sequential Access • Storage - • Static Allocation vs Dynamic Allocation • Applications – Access Restricted Lists: • Stacks and Queues • Files Sundar B. BITS, Pilani.

  4. Example Consider Registration Day: Registration is just over. Dean asks you to produce 3 lists: Students who were not allowed to register Students who registered successfully. Students who had (time table) conflicts Sundar B. BITS, Pilani.

  5. Example - Types Assume the following types: typedef struct { ID i; Year y; char name[NAME_LEN]; } Student; typedef enum { DENY=1; SUCC=2; CON=3; } RegStatus; typedef enum { LEC=1; COMPRE=2; DTC=3; } Conflict; typedef unsigned int Code; Sundar B. BITS, Pilani.

  6. union { Code courses[NUM_COURSES]; String regRest; Conflict con; } rd; Example - Types typedef struct { Student st; RegStatus rs; } RegInfo; typedef RegInfo RegList[MAX]; Sundar B. BITS, Pilani.

  7. unsigned int ks =0, kd=0; kc=0; unsigned int j; for (j = 0; j < size; j++) { } if (rl[j].rs == DEN) den[kd++] = rl[j]; else if (rl[j].rs == SUC) suc[ks++] = rl[j]; else conf[kc++] = rl[j]; Example - Implementation void regCats(RegList rl, unsigned int size, RegList suc, RegList den, RegList con) { } Sundar B. BITS, Pilani.

  8. Example – Issues? • What should the size of suc, den, and con be? • Each must be of size len : total 3 * len • But the total size cannot be more than len Sundar B. BITS, Pilani.

  9. Example – Second Solution • Use cursors: • Each element specifies position of next element in the list. • Declare new type: typedef struct { RegInfo ri; int next; } Element; Sundar B. BITS, Pilani.

  10. unsigned int ks =0, kd=1; kc=2, j; for (j = 0; j < size; j++) { } res[ks].next = res[kd].next = res[kc].next = -1; if (rl[j].rs == DEN) { res[kd].next = j+3; kd = j+3; res[kd].ri = rl[j]; } else if (rl[j].rs == SUC) { res[ks].next = j+3; ks = j+3; res[ks].ri = rl[j]; } else { res[kc].next = j+3; kc = j+3; res[kc].ri = rl[j]; } Example – Implementation 2 void regCats(RegList rl[], unsigned int size, Element res[]) { }

  11. Example – Analysis of Solution 2 • Array size passed: (len + 3)*sizeof(Element) which is same as (len+3) * (sizeof(RegInfo) + sizeof(int)) Compare this with (3*len) * (sizeof(RegInfo)) in the earlier implementation. Sundar B. BITS, Pilani.

  12. But “what is the solution?” • Elements of a list are not “randomly accessible” any more. • We have a created a “sequentially accessible” list • If you need element at position k then you need to step through elements in positions 0 to k-1 first. Sundar B. BITS, Pilani.

  13. ri next ri next ri next Structure of the (sequential access) list This is the logical structure. Physically each ri and next are just stored in adjacent positions in the array. Sundar B. BITS, Pilani.

  14. Is this a good representation? • (As usual) judge by the operations • Accessing the kth element – O(k) time (bad) • Inserting an element (ordered list): • Find the right element: create space for one element but no shifting required. • Still O(k) time (as opposed to O(n) time) - not much improvement. (Why not?) • Deleting an element: • Similar to insertion: no shifting! • What is the primary use? • Space Savings Sundar B. BITS, Pilani.

More Related