140 likes | 244 Views
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 -
E N D
Today’s Agenda Lists Random Access vs. Sequential Access Sundar B. BITS, Pilani.
Course Agenda Modules • Approach – Personal Programming Process • Data-Driven Programming • Dynamic Data Storage Sundar B. BITS, Pilani.
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.
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.
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.
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.
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.
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.
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.
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[]) { }
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.
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.
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.
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.